blob: 894ee07222b22e66bf2831b736d6e3ff6552701a [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()) {
3753 case Primitive::kPrimLong: {
3754 locations->SetInAt(0, Location::RequiresRegister());
3755 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003756 // Output overlaps because it is written before doing the low comparison.
3757 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00003758 break;
3759 }
3760 case Primitive::kPrimFloat:
3761 case Primitive::kPrimDouble: {
3762 locations->SetInAt(0, Location::RequiresFpuRegister());
3763 locations->SetInAt(1, Location::RequiresFpuRegister());
3764 locations->SetOut(Location::RequiresRegister());
3765 break;
3766 }
3767 default:
3768 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3769 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003770}
3771
3772void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003773 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003774 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003775 Location left = locations->InAt(0);
3776 Location right = locations->InAt(1);
3777
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003778 Label less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00003779 Primitive::Type type = compare->InputAt(0)->GetType();
Vladimir Markod6e069b2016-01-18 11:11:01 +00003780 Condition less_cond;
Calin Juravleddb7df22014-11-25 20:56:51 +00003781 switch (type) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003782 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003783 __ cmp(left.AsRegisterPairHigh<Register>(),
3784 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003785 __ b(&less, LT);
3786 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003787 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00003788 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003789 __ cmp(left.AsRegisterPairLow<Register>(),
3790 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Vladimir Markod6e069b2016-01-18 11:11:01 +00003791 less_cond = LO;
Calin Juravleddb7df22014-11-25 20:56:51 +00003792 break;
3793 }
3794 case Primitive::kPrimFloat:
3795 case Primitive::kPrimDouble: {
3796 __ LoadImmediate(out, 0);
3797 if (type == Primitive::kPrimFloat) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003798 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003799 } else {
3800 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
3801 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
3802 }
3803 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00003804 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003805 break;
3806 }
3807 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003808 LOG(FATAL) << "Unexpected compare type " << type;
Vladimir Markod6e069b2016-01-18 11:11:01 +00003809 UNREACHABLE();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003810 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003811 __ b(&done, EQ);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003812 __ b(&less, less_cond);
Calin Juravleddb7df22014-11-25 20:56:51 +00003813
3814 __ Bind(&greater);
3815 __ LoadImmediate(out, 1);
3816 __ b(&done);
3817
3818 __ Bind(&less);
3819 __ LoadImmediate(out, -1);
3820
3821 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003822}
3823
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003824void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003825 LocationSummary* locations =
3826 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003827 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3828 locations->SetInAt(i, Location::Any());
3829 }
3830 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003831}
3832
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003833void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003834 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003835}
3836
Roland Levillainc9285912015-12-18 10:38:42 +00003837void CodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
3838 // TODO (ported from quick): revisit ARM barrier kinds.
3839 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
Calin Juravle52c48962014-12-16 17:02:57 +00003840 switch (kind) {
3841 case MemBarrierKind::kAnyStore:
3842 case MemBarrierKind::kLoadAny:
3843 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003844 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00003845 break;
3846 }
3847 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003848 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00003849 break;
3850 }
3851 default:
3852 LOG(FATAL) << "Unexpected memory barrier " << kind;
3853 }
Kenny Root1d8199d2015-06-02 11:01:10 -07003854 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00003855}
3856
3857void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
3858 uint32_t offset,
3859 Register out_lo,
3860 Register out_hi) {
3861 if (offset != 0) {
Roland Levillain3b359c72015-11-17 19:35:12 +00003862 // Ensure `out_lo` is different from `addr`, so that loading
3863 // `offset` into `out_lo` does not clutter `addr`.
3864 DCHECK_NE(out_lo, addr);
Calin Juravle52c48962014-12-16 17:02:57 +00003865 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003866 __ add(IP, addr, ShifterOperand(out_lo));
3867 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003868 }
3869 __ ldrexd(out_lo, out_hi, addr);
3870}
3871
3872void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
3873 uint32_t offset,
3874 Register value_lo,
3875 Register value_hi,
3876 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00003877 Register temp2,
3878 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003879 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00003880 if (offset != 0) {
3881 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003882 __ add(IP, addr, ShifterOperand(temp1));
3883 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003884 }
3885 __ Bind(&fail);
3886 // We need a load followed by store. (The address used in a STREX instruction must
3887 // be the same as the address in the most recently executed LDREX instruction.)
3888 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00003889 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003890 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003891 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00003892}
3893
3894void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3895 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3896
Nicolas Geoffray39468442014-09-02 15:17:15 +01003897 LocationSummary* locations =
3898 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003899 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003900
Calin Juravle52c48962014-12-16 17:02:57 +00003901 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003902 if (Primitive::IsFloatingPointType(field_type)) {
3903 locations->SetInAt(1, Location::RequiresFpuRegister());
3904 } else {
3905 locations->SetInAt(1, Location::RequiresRegister());
3906 }
3907
Calin Juravle52c48962014-12-16 17:02:57 +00003908 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00003909 bool generate_volatile = field_info.IsVolatile()
3910 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003911 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01003912 bool needs_write_barrier =
3913 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003914 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00003915 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01003916 if (needs_write_barrier) {
3917 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003918 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003919 } else if (generate_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00003920 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00003921 // - registers need to be consecutive
3922 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00003923 // We don't test for ARM yet, and the assertion makes sure that we
3924 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00003925 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3926
3927 locations->AddTemp(Location::RequiresRegister());
3928 locations->AddTemp(Location::RequiresRegister());
3929 if (field_type == Primitive::kPrimDouble) {
3930 // For doubles we need two more registers to copy the value.
3931 locations->AddTemp(Location::RegisterLocation(R2));
3932 locations->AddTemp(Location::RegisterLocation(R3));
3933 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003934 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003935}
3936
Calin Juravle52c48962014-12-16 17:02:57 +00003937void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003938 const FieldInfo& field_info,
3939 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003940 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3941
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003942 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003943 Register base = locations->InAt(0).AsRegister<Register>();
3944 Location value = locations->InAt(1);
3945
3946 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003947 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00003948 Primitive::Type field_type = field_info.GetFieldType();
3949 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01003950 bool needs_write_barrier =
3951 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003952
3953 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00003954 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00003955 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003956
3957 switch (field_type) {
3958 case Primitive::kPrimBoolean:
3959 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003960 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003961 break;
3962 }
3963
3964 case Primitive::kPrimShort:
3965 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003966 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003967 break;
3968 }
3969
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003970 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003971 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01003972 if (kPoisonHeapReferences && needs_write_barrier) {
3973 // Note that in the case where `value` is a null reference,
3974 // we do not enter this block, as a null reference does not
3975 // need poisoning.
3976 DCHECK_EQ(field_type, Primitive::kPrimNot);
3977 Register temp = locations->GetTemp(0).AsRegister<Register>();
3978 __ Mov(temp, value.AsRegister<Register>());
3979 __ PoisonHeapReference(temp);
3980 __ StoreToOffset(kStoreWord, temp, base, offset);
3981 } else {
3982 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
3983 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003984 break;
3985 }
3986
3987 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00003988 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003989 GenerateWideAtomicStore(base, offset,
3990 value.AsRegisterPairLow<Register>(),
3991 value.AsRegisterPairHigh<Register>(),
3992 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003993 locations->GetTemp(1).AsRegister<Register>(),
3994 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003995 } else {
3996 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003997 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003998 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003999 break;
4000 }
4001
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004002 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004003 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004004 break;
4005 }
4006
4007 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004008 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004009 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004010 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
4011 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
4012
4013 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
4014
4015 GenerateWideAtomicStore(base, offset,
4016 value_reg_lo,
4017 value_reg_hi,
4018 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00004019 locations->GetTemp(3).AsRegister<Register>(),
4020 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004021 } else {
4022 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004023 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004024 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004025 break;
4026 }
4027
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004028 case Primitive::kPrimVoid:
4029 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004030 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004031 }
Calin Juravle52c48962014-12-16 17:02:57 +00004032
Calin Juravle77520bc2015-01-12 18:45:46 +00004033 // Longs and doubles are handled in the switch.
4034 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
4035 codegen_->MaybeRecordImplicitNullCheck(instruction);
4036 }
4037
4038 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4039 Register temp = locations->GetTemp(0).AsRegister<Register>();
4040 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004041 codegen_->MarkGCCard(
4042 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004043 }
4044
Calin Juravle52c48962014-12-16 17:02:57 +00004045 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004046 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004047 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004048}
4049
Calin Juravle52c48962014-12-16 17:02:57 +00004050void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4051 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain3b359c72015-11-17 19:35:12 +00004052
4053 bool object_field_get_with_read_barrier =
4054 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004055 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004056 new (GetGraph()->GetArena()) LocationSummary(instruction,
4057 object_field_get_with_read_barrier ?
4058 LocationSummary::kCallOnSlowPath :
4059 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004060 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004061
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004062 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00004063 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004064 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain3b359c72015-11-17 19:35:12 +00004065 // The output overlaps in case of volatile long: we don't want the
4066 // code generated by GenerateWideAtomicLoad to overwrite the
4067 // object's location. Likewise, in the case of an object field get
4068 // with read barriers enabled, we do not want the load to overwrite
4069 // the object's location, as we need it to emit the read barrier.
4070 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
4071 object_field_get_with_read_barrier;
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01004072
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004073 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4074 locations->SetOut(Location::RequiresFpuRegister());
4075 } else {
4076 locations->SetOut(Location::RequiresRegister(),
4077 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
4078 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004079 if (volatile_for_double) {
Roland Levillainc9285912015-12-18 10:38:42 +00004080 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00004081 // - registers need to be consecutive
4082 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00004083 // We don't test for ARM yet, and the assertion makes sure that we
4084 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00004085 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4086 locations->AddTemp(Location::RequiresRegister());
4087 locations->AddTemp(Location::RequiresRegister());
Roland Levillainc9285912015-12-18 10:38:42 +00004088 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4089 // We need a temporary register for the read barrier marking slow
4090 // path in CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier.
4091 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004092 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004093}
4094
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004095Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
4096 Opcode opcode) {
4097 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
4098 if (constant->IsConstant() &&
4099 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
4100 return Location::ConstantLocation(constant->AsConstant());
4101 }
4102 return Location::RequiresRegister();
4103}
4104
4105bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
4106 Opcode opcode) {
4107 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
4108 if (Primitive::Is64BitType(input_cst->GetType())) {
4109 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode) &&
4110 CanEncodeConstantAsImmediate(High32Bits(value), opcode);
4111 } else {
4112 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
4113 }
4114}
4115
4116bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value, Opcode opcode) {
4117 ShifterOperand so;
4118 ArmAssembler* assembler = codegen_->GetAssembler();
4119 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, &so)) {
4120 return true;
4121 }
4122 Opcode neg_opcode = kNoOperand;
4123 switch (opcode) {
4124 case AND:
4125 neg_opcode = BIC;
4126 break;
4127 case ORR:
4128 neg_opcode = ORN;
4129 break;
4130 default:
4131 return false;
4132 }
4133 return assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, neg_opcode, ~value, &so);
4134}
4135
Calin Juravle52c48962014-12-16 17:02:57 +00004136void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
4137 const FieldInfo& field_info) {
4138 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004139
Calin Juravle52c48962014-12-16 17:02:57 +00004140 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004141 Location base_loc = locations->InAt(0);
4142 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004143 Location out = locations->Out();
4144 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004145 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004146 Primitive::Type field_type = field_info.GetFieldType();
4147 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4148
4149 switch (field_type) {
Roland Levillainc9285912015-12-18 10:38:42 +00004150 case Primitive::kPrimBoolean:
Calin Juravle52c48962014-12-16 17:02:57 +00004151 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004152 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004153
Roland Levillainc9285912015-12-18 10:38:42 +00004154 case Primitive::kPrimByte:
Calin Juravle52c48962014-12-16 17:02:57 +00004155 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004156 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004157
Roland Levillainc9285912015-12-18 10:38:42 +00004158 case Primitive::kPrimShort:
Calin Juravle52c48962014-12-16 17:02:57 +00004159 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004160 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004161
Roland Levillainc9285912015-12-18 10:38:42 +00004162 case Primitive::kPrimChar:
Calin Juravle52c48962014-12-16 17:02:57 +00004163 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004164 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004165
4166 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004167 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004168 break;
Roland Levillainc9285912015-12-18 10:38:42 +00004169
4170 case Primitive::kPrimNot: {
4171 // /* HeapReference<Object> */ out = *(base + offset)
4172 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4173 Location temp_loc = locations->GetTemp(0);
4174 // Note that a potential implicit null check is handled in this
4175 // CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier call.
4176 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4177 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4178 if (is_volatile) {
4179 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4180 }
4181 } else {
4182 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
4183 codegen_->MaybeRecordImplicitNullCheck(instruction);
4184 if (is_volatile) {
4185 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4186 }
4187 // If read barriers are enabled, emit read barriers other than
4188 // Baker's using a slow path (and also unpoison the loaded
4189 // reference, if heap poisoning is enabled).
4190 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4191 }
4192 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004193 }
4194
Roland Levillainc9285912015-12-18 10:38:42 +00004195 case Primitive::kPrimLong:
Calin Juravle34166012014-12-19 17:22:29 +00004196 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004197 GenerateWideAtomicLoad(base, offset,
4198 out.AsRegisterPairLow<Register>(),
4199 out.AsRegisterPairHigh<Register>());
4200 } else {
4201 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
4202 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004203 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004204
Roland Levillainc9285912015-12-18 10:38:42 +00004205 case Primitive::kPrimFloat:
Calin Juravle52c48962014-12-16 17:02:57 +00004206 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004207 break;
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004208
4209 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004210 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004211 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004212 Register lo = locations->GetTemp(0).AsRegister<Register>();
4213 Register hi = locations->GetTemp(1).AsRegister<Register>();
4214 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00004215 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004216 __ vmovdrr(out_reg, lo, hi);
4217 } else {
4218 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004219 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004220 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004221 break;
4222 }
4223
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004224 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004225 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004226 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004227 }
Calin Juravle52c48962014-12-16 17:02:57 +00004228
Roland Levillainc9285912015-12-18 10:38:42 +00004229 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
4230 // Potential implicit null checks, in the case of reference or
4231 // double fields, are handled in the previous switch statement.
4232 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004233 codegen_->MaybeRecordImplicitNullCheck(instruction);
4234 }
4235
Calin Juravle52c48962014-12-16 17:02:57 +00004236 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004237 if (field_type == Primitive::kPrimNot) {
4238 // Memory barriers, in the case of references, are also handled
4239 // in the previous switch statement.
4240 } else {
4241 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4242 }
Roland Levillain4d027112015-07-01 15:41:14 +01004243 }
Calin Juravle52c48962014-12-16 17:02:57 +00004244}
4245
4246void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4247 HandleFieldSet(instruction, instruction->GetFieldInfo());
4248}
4249
4250void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004251 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004252}
4253
4254void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4255 HandleFieldGet(instruction, instruction->GetFieldInfo());
4256}
4257
4258void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4259 HandleFieldGet(instruction, instruction->GetFieldInfo());
4260}
4261
4262void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4263 HandleFieldGet(instruction, instruction->GetFieldInfo());
4264}
4265
4266void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4267 HandleFieldGet(instruction, instruction->GetFieldInfo());
4268}
4269
4270void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4271 HandleFieldSet(instruction, instruction->GetFieldInfo());
4272}
4273
4274void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004275 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004276}
4277
Calin Juravlee460d1d2015-09-29 04:52:17 +01004278void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
4279 HUnresolvedInstanceFieldGet* instruction) {
4280 FieldAccessCallingConventionARM calling_convention;
4281 codegen_->CreateUnresolvedFieldLocationSummary(
4282 instruction, instruction->GetFieldType(), calling_convention);
4283}
4284
4285void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
4286 HUnresolvedInstanceFieldGet* instruction) {
4287 FieldAccessCallingConventionARM calling_convention;
4288 codegen_->GenerateUnresolvedFieldAccess(instruction,
4289 instruction->GetFieldType(),
4290 instruction->GetFieldIndex(),
4291 instruction->GetDexPc(),
4292 calling_convention);
4293}
4294
4295void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
4296 HUnresolvedInstanceFieldSet* instruction) {
4297 FieldAccessCallingConventionARM calling_convention;
4298 codegen_->CreateUnresolvedFieldLocationSummary(
4299 instruction, instruction->GetFieldType(), calling_convention);
4300}
4301
4302void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
4303 HUnresolvedInstanceFieldSet* instruction) {
4304 FieldAccessCallingConventionARM calling_convention;
4305 codegen_->GenerateUnresolvedFieldAccess(instruction,
4306 instruction->GetFieldType(),
4307 instruction->GetFieldIndex(),
4308 instruction->GetDexPc(),
4309 calling_convention);
4310}
4311
4312void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
4313 HUnresolvedStaticFieldGet* instruction) {
4314 FieldAccessCallingConventionARM calling_convention;
4315 codegen_->CreateUnresolvedFieldLocationSummary(
4316 instruction, instruction->GetFieldType(), calling_convention);
4317}
4318
4319void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
4320 HUnresolvedStaticFieldGet* instruction) {
4321 FieldAccessCallingConventionARM calling_convention;
4322 codegen_->GenerateUnresolvedFieldAccess(instruction,
4323 instruction->GetFieldType(),
4324 instruction->GetFieldIndex(),
4325 instruction->GetDexPc(),
4326 calling_convention);
4327}
4328
4329void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
4330 HUnresolvedStaticFieldSet* instruction) {
4331 FieldAccessCallingConventionARM calling_convention;
4332 codegen_->CreateUnresolvedFieldLocationSummary(
4333 instruction, instruction->GetFieldType(), calling_convention);
4334}
4335
4336void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
4337 HUnresolvedStaticFieldSet* instruction) {
4338 FieldAccessCallingConventionARM calling_convention;
4339 codegen_->GenerateUnresolvedFieldAccess(instruction,
4340 instruction->GetFieldType(),
4341 instruction->GetFieldIndex(),
4342 instruction->GetDexPc(),
4343 calling_convention);
4344}
4345
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004346void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004347 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4348 ? LocationSummary::kCallOnSlowPath
4349 : LocationSummary::kNoCall;
4350 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravle77520bc2015-01-12 18:45:46 +00004351 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004352 if (instruction->HasUses()) {
4353 locations->SetOut(Location::SameAsFirstInput());
4354 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004355}
4356
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004357void InstructionCodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004358 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4359 return;
4360 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004361 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004362
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004363 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
4364 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4365}
4366
4367void InstructionCodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004368 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004369 codegen_->AddSlowPath(slow_path);
4370
4371 LocationSummary* locations = instruction->GetLocations();
4372 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004373
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004374 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004375}
4376
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004377void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004378 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004379 GenerateImplicitNullCheck(instruction);
4380 } else {
4381 GenerateExplicitNullCheck(instruction);
4382 }
4383}
4384
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004385void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004386 bool object_array_get_with_read_barrier =
4387 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004388 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004389 new (GetGraph()->GetArena()) LocationSummary(instruction,
4390 object_array_get_with_read_barrier ?
4391 LocationSummary::kCallOnSlowPath :
4392 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004393 locations->SetInAt(0, Location::RequiresRegister());
4394 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004395 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4396 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4397 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004398 // The output overlaps in the case of an object array get with
4399 // read barriers enabled: we do not want the move to overwrite the
4400 // array's location, as we need it to emit the read barrier.
4401 locations->SetOut(
4402 Location::RequiresRegister(),
4403 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004404 }
Roland Levillainc9285912015-12-18 10:38:42 +00004405 // We need a temporary register for the read barrier marking slow
4406 // path in CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier.
4407 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4408 locations->AddTemp(Location::RequiresRegister());
4409 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004410}
4411
4412void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
4413 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004414 Location obj_loc = locations->InAt(0);
4415 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004416 Location index = locations->InAt(1);
Roland Levillainc9285912015-12-18 10:38:42 +00004417 Location out_loc = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004418
Roland Levillainc9285912015-12-18 10:38:42 +00004419 Primitive::Type type = instruction->GetType();
Roland Levillain4d027112015-07-01 15:41:14 +01004420 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004421 case Primitive::kPrimBoolean: {
4422 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004423 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004424 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004425 size_t offset =
4426 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004427 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
4428 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004429 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004430 __ LoadFromOffset(kLoadUnsignedByte, out, IP, data_offset);
4431 }
4432 break;
4433 }
4434
4435 case Primitive::kPrimByte: {
4436 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004437 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004438 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004439 size_t offset =
4440 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004441 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
4442 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004443 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004444 __ LoadFromOffset(kLoadSignedByte, out, IP, data_offset);
4445 }
4446 break;
4447 }
4448
4449 case Primitive::kPrimShort: {
4450 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004451 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004452 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004453 size_t offset =
4454 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004455 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
4456 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004457 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004458 __ LoadFromOffset(kLoadSignedHalfword, out, IP, data_offset);
4459 }
4460 break;
4461 }
4462
4463 case Primitive::kPrimChar: {
4464 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004465 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004466 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004467 size_t offset =
4468 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004469 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
4470 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004471 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004472 __ LoadFromOffset(kLoadUnsignedHalfword, out, IP, data_offset);
4473 }
4474 break;
4475 }
4476
Roland Levillainc9285912015-12-18 10:38:42 +00004477 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004478 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004479 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004480 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004481 size_t offset =
4482 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004483 __ LoadFromOffset(kLoadWord, out, obj, offset);
4484 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004485 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004486 __ LoadFromOffset(kLoadWord, out, IP, data_offset);
4487 }
4488 break;
4489 }
4490
Roland Levillainc9285912015-12-18 10:38:42 +00004491 case Primitive::kPrimNot: {
4492 static_assert(
4493 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4494 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
4495 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4496 // /* HeapReference<Object> */ out =
4497 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4498 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4499 Location temp = locations->GetTemp(0);
4500 // Note that a potential implicit null check is handled in this
4501 // CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier call.
4502 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4503 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4504 } else {
4505 Register out = out_loc.AsRegister<Register>();
4506 if (index.IsConstant()) {
4507 size_t offset =
4508 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4509 __ LoadFromOffset(kLoadWord, out, obj, offset);
4510 codegen_->MaybeRecordImplicitNullCheck(instruction);
4511 // If read barriers are enabled, emit read barriers other than
4512 // Baker's using a slow path (and also unpoison the loaded
4513 // reference, if heap poisoning is enabled).
4514 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4515 } else {
4516 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4517 __ LoadFromOffset(kLoadWord, out, IP, data_offset);
4518 codegen_->MaybeRecordImplicitNullCheck(instruction);
4519 // If read barriers are enabled, emit read barriers other than
4520 // Baker's using a slow path (and also unpoison the loaded
4521 // reference, if heap poisoning is enabled).
4522 codegen_->MaybeGenerateReadBarrierSlow(
4523 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4524 }
4525 }
4526 break;
4527 }
4528
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004529 case Primitive::kPrimLong: {
4530 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004531 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004532 size_t offset =
4533 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004534 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004535 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004536 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00004537 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004538 }
4539 break;
4540 }
4541
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004542 case Primitive::kPrimFloat: {
4543 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004544 SRegister out = out_loc.AsFpuRegister<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004545 if (index.IsConstant()) {
4546 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004547 __ LoadSFromOffset(out, obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004548 } else {
4549 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillainc9285912015-12-18 10:38:42 +00004550 __ LoadSFromOffset(out, IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004551 }
4552 break;
4553 }
4554
4555 case Primitive::kPrimDouble: {
4556 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004557 SRegister out = out_loc.AsFpuRegisterPairLow<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004558 if (index.IsConstant()) {
4559 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004560 __ LoadDFromOffset(FromLowSToD(out), obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004561 } else {
4562 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00004563 __ LoadDFromOffset(FromLowSToD(out), IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004564 }
4565 break;
4566 }
4567
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004568 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004569 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004570 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004571 }
Roland Levillain4d027112015-07-01 15:41:14 +01004572
4573 if (type == Primitive::kPrimNot) {
Roland Levillainc9285912015-12-18 10:38:42 +00004574 // Potential implicit null checks, in the case of reference
4575 // arrays, are handled in the previous switch statement.
4576 } else {
4577 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004578 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004579}
4580
4581void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004582 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004583
4584 bool needs_write_barrier =
4585 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain3b359c72015-11-17 19:35:12 +00004586 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
4587 bool object_array_set_with_read_barrier =
4588 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004589
Nicolas Geoffray39468442014-09-02 15:17:15 +01004590 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004591 instruction,
Roland Levillain3b359c72015-11-17 19:35:12 +00004592 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
4593 LocationSummary::kCallOnSlowPath :
4594 LocationSummary::kNoCall);
4595
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004596 locations->SetInAt(0, Location::RequiresRegister());
4597 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4598 if (Primitive::IsFloatingPointType(value_type)) {
4599 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004600 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004601 locations->SetInAt(2, Location::RequiresRegister());
4602 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004603 if (needs_write_barrier) {
4604 // Temporary registers for the write barrier.
4605 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00004606 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004607 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004608}
4609
4610void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
4611 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004612 Location array_loc = locations->InAt(0);
4613 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004614 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004615 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain3b359c72015-11-17 19:35:12 +00004616 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004617 bool needs_write_barrier =
4618 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004619
4620 switch (value_type) {
4621 case Primitive::kPrimBoolean:
4622 case Primitive::kPrimByte: {
4623 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004624 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004625 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004626 size_t offset =
4627 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004628 __ StoreToOffset(kStoreByte, value, array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004629 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004630 __ add(IP, array, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004631 __ StoreToOffset(kStoreByte, value, IP, data_offset);
4632 }
4633 break;
4634 }
4635
4636 case Primitive::kPrimShort:
4637 case Primitive::kPrimChar: {
4638 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004639 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004640 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004641 size_t offset =
4642 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004643 __ StoreToOffset(kStoreHalfword, value, array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004644 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004645 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004646 __ StoreToOffset(kStoreHalfword, value, IP, data_offset);
4647 }
4648 break;
4649 }
4650
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004651 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004652 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00004653 Location value_loc = locations->InAt(2);
4654 Register value = value_loc.AsRegister<Register>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004655 Register source = value;
4656
4657 if (instruction->InputAt(2)->IsNullConstant()) {
4658 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004659 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004660 size_t offset =
4661 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004662 __ StoreToOffset(kStoreWord, source, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004663 } else {
4664 DCHECK(index.IsRegister()) << index;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004665 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillain4d027112015-07-01 15:41:14 +01004666 __ StoreToOffset(kStoreWord, source, IP, data_offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004667 }
Roland Levillain1407ee72016-01-08 15:56:19 +00004668 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain3b359c72015-11-17 19:35:12 +00004669 DCHECK(!needs_write_barrier);
4670 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004671 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004672 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004673
4674 DCHECK(needs_write_barrier);
4675 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
4676 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
4677 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4678 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4679 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4680 Label done;
4681 SlowPathCode* slow_path = nullptr;
4682
Roland Levillain3b359c72015-11-17 19:35:12 +00004683 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004684 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
4685 codegen_->AddSlowPath(slow_path);
4686 if (instruction->GetValueCanBeNull()) {
4687 Label non_zero;
4688 __ CompareAndBranchIfNonZero(value, &non_zero);
4689 if (index.IsConstant()) {
4690 size_t offset =
4691 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4692 __ StoreToOffset(kStoreWord, value, array, offset);
4693 } else {
4694 DCHECK(index.IsRegister()) << index;
4695 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4696 __ StoreToOffset(kStoreWord, value, IP, data_offset);
4697 }
4698 codegen_->MaybeRecordImplicitNullCheck(instruction);
4699 __ b(&done);
4700 __ Bind(&non_zero);
4701 }
4702
Roland Levillain3b359c72015-11-17 19:35:12 +00004703 if (kEmitCompilerReadBarrier) {
4704 // When read barriers are enabled, the type checking
4705 // instrumentation requires two read barriers:
4706 //
4707 // __ Mov(temp2, temp1);
4708 // // /* HeapReference<Class> */ temp1 = temp1->component_type_
4709 // __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
Roland Levillainc9285912015-12-18 10:38:42 +00004710 // codegen_->GenerateReadBarrierSlow(
Roland Levillain3b359c72015-11-17 19:35:12 +00004711 // instruction, temp1_loc, temp1_loc, temp2_loc, component_offset);
4712 //
4713 // // /* HeapReference<Class> */ temp2 = value->klass_
4714 // __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
Roland Levillainc9285912015-12-18 10:38:42 +00004715 // codegen_->GenerateReadBarrierSlow(
Roland Levillain3b359c72015-11-17 19:35:12 +00004716 // instruction, temp2_loc, temp2_loc, value_loc, class_offset, temp1_loc);
4717 //
4718 // __ cmp(temp1, ShifterOperand(temp2));
4719 //
4720 // However, the second read barrier may trash `temp`, as it
4721 // is a temporary register, and as such would not be saved
4722 // along with live registers before calling the runtime (nor
4723 // restored afterwards). So in this case, we bail out and
4724 // delegate the work to the array set slow path.
4725 //
4726 // TODO: Extend the register allocator to support a new
4727 // "(locally) live temp" location so as to avoid always
4728 // going into the slow path when read barriers are enabled.
4729 __ b(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004730 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004731 // /* HeapReference<Class> */ temp1 = array->klass_
4732 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
4733 codegen_->MaybeRecordImplicitNullCheck(instruction);
4734 __ MaybeUnpoisonHeapReference(temp1);
4735
4736 // /* HeapReference<Class> */ temp1 = temp1->component_type_
4737 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
4738 // /* HeapReference<Class> */ temp2 = value->klass_
4739 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
4740 // If heap poisoning is enabled, no need to unpoison `temp1`
4741 // nor `temp2`, as we are comparing two poisoned references.
4742 __ cmp(temp1, ShifterOperand(temp2));
4743
4744 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4745 Label do_put;
4746 __ b(&do_put, EQ);
4747 // If heap poisoning is enabled, the `temp1` reference has
4748 // not been unpoisoned yet; unpoison it now.
4749 __ MaybeUnpoisonHeapReference(temp1);
4750
4751 // /* HeapReference<Class> */ temp1 = temp1->super_class_
4752 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
4753 // If heap poisoning is enabled, no need to unpoison
4754 // `temp1`, as we are comparing against null below.
4755 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
4756 __ Bind(&do_put);
4757 } else {
4758 __ b(slow_path->GetEntryLabel(), NE);
4759 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004760 }
4761 }
4762
4763 if (kPoisonHeapReferences) {
4764 // Note that in the case where `value` is a null reference,
4765 // we do not enter this block, as a null reference does not
4766 // need poisoning.
4767 DCHECK_EQ(value_type, Primitive::kPrimNot);
4768 __ Mov(temp1, value);
4769 __ PoisonHeapReference(temp1);
4770 source = temp1;
4771 }
4772
4773 if (index.IsConstant()) {
4774 size_t offset =
4775 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4776 __ StoreToOffset(kStoreWord, source, array, offset);
4777 } else {
4778 DCHECK(index.IsRegister()) << index;
4779 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4780 __ StoreToOffset(kStoreWord, source, IP, data_offset);
4781 }
4782
Roland Levillain3b359c72015-11-17 19:35:12 +00004783 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004784 codegen_->MaybeRecordImplicitNullCheck(instruction);
4785 }
4786
4787 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
4788
4789 if (done.IsLinked()) {
4790 __ Bind(&done);
4791 }
4792
4793 if (slow_path != nullptr) {
4794 __ Bind(slow_path->GetExitLabel());
4795 }
4796
4797 break;
4798 }
4799
4800 case Primitive::kPrimInt: {
4801 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4802 Register value = locations->InAt(2).AsRegister<Register>();
4803 if (index.IsConstant()) {
4804 size_t offset =
4805 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4806 __ StoreToOffset(kStoreWord, value, array, offset);
4807 } else {
4808 DCHECK(index.IsRegister()) << index;
4809 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4810 __ StoreToOffset(kStoreWord, value, IP, data_offset);
4811 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004812 break;
4813 }
4814
4815 case Primitive::kPrimLong: {
4816 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004817 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004818 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004819 size_t offset =
4820 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004821 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004822 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004823 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004824 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004825 }
4826 break;
4827 }
4828
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004829 case Primitive::kPrimFloat: {
4830 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4831 Location value = locations->InAt(2);
4832 DCHECK(value.IsFpuRegister());
4833 if (index.IsConstant()) {
4834 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004835 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004836 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004837 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004838 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
4839 }
4840 break;
4841 }
4842
4843 case Primitive::kPrimDouble: {
4844 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4845 Location value = locations->InAt(2);
4846 DCHECK(value.IsFpuRegisterPair());
4847 if (index.IsConstant()) {
4848 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004849 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004850 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004851 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004852 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
4853 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004854
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004855 break;
4856 }
4857
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004858 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004859 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004860 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004861 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004862
Roland Levillain80e67092016-01-08 16:04:55 +00004863 // Objects are handled in the switch.
4864 if (value_type != Primitive::kPrimNot) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004865 codegen_->MaybeRecordImplicitNullCheck(instruction);
4866 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004867}
4868
4869void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004870 LocationSummary* locations =
4871 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004872 locations->SetInAt(0, Location::RequiresRegister());
4873 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004874}
4875
4876void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
4877 LocationSummary* locations = instruction->GetLocations();
4878 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004879 Register obj = locations->InAt(0).AsRegister<Register>();
4880 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004881 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004882 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004883}
4884
4885void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004886 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4887 ? LocationSummary::kCallOnSlowPath
4888 : LocationSummary::kNoCall;
4889 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004890 locations->SetInAt(0, Location::RequiresRegister());
4891 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004892 if (instruction->HasUses()) {
4893 locations->SetOut(Location::SameAsFirstInput());
4894 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004895}
4896
4897void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
4898 LocationSummary* locations = instruction->GetLocations();
Andreas Gampe85b62f22015-09-09 13:15:38 -07004899 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004900 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004901 codegen_->AddSlowPath(slow_path);
4902
Roland Levillain271ab9c2014-11-27 15:23:57 +00004903 Register index = locations->InAt(0).AsRegister<Register>();
4904 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004905
4906 __ cmp(index, ShifterOperand(length));
Roland Levillain4fa13f62015-07-06 18:11:54 +01004907 __ b(slow_path->GetEntryLabel(), HS);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004908}
4909
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004910void CodeGeneratorARM::MarkGCCard(Register temp,
4911 Register card,
4912 Register object,
4913 Register value,
4914 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004915 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004916 if (can_be_null) {
4917 __ CompareAndBranchIfZero(value, &is_null);
4918 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004919 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmWordSize>().Int32Value());
4920 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
4921 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004922 if (can_be_null) {
4923 __ Bind(&is_null);
4924 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004925}
4926
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004927void LocationsBuilderARM::VisitTemporary(HTemporary* temp) {
4928 temp->SetLocations(nullptr);
4929}
4930
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004931void InstructionCodeGeneratorARM::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004932 // Nothing to do, this is driven by the code generator.
4933}
4934
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004935void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004936 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004937}
4938
4939void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004940 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4941}
4942
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004943void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
4944 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4945}
4946
4947void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004948 HBasicBlock* block = instruction->GetBlock();
4949 if (block->GetLoopInformation() != nullptr) {
4950 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4951 // The back edge will generate the suspend check.
4952 return;
4953 }
4954 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4955 // The goto will generate the suspend check.
4956 return;
4957 }
4958 GenerateSuspendCheck(instruction, nullptr);
4959}
4960
4961void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
4962 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004963 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004964 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
4965 if (slow_path == nullptr) {
4966 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
4967 instruction->SetSlowPath(slow_path);
4968 codegen_->AddSlowPath(slow_path);
4969 if (successor != nullptr) {
4970 DCHECK(successor->IsLoopHeader());
4971 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4972 }
4973 } else {
4974 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4975 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004976
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00004977 __ LoadFromOffset(
4978 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmWordSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004979 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004980 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004981 __ Bind(slow_path->GetReturnLabel());
4982 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004983 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004984 __ b(slow_path->GetEntryLabel());
4985 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004986}
4987
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004988ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
4989 return codegen_->GetAssembler();
4990}
4991
4992void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004993 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004994 Location source = move->GetSource();
4995 Location destination = move->GetDestination();
4996
4997 if (source.IsRegister()) {
4998 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004999 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005000 } else if (destination.IsFpuRegister()) {
5001 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005002 } else {
5003 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005004 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005005 SP, destination.GetStackIndex());
5006 }
5007 } else if (source.IsStackSlot()) {
5008 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005009 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005010 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005011 } else if (destination.IsFpuRegister()) {
5012 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005013 } else {
5014 DCHECK(destination.IsStackSlot());
5015 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
5016 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5017 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005018 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005019 if (destination.IsRegister()) {
5020 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
5021 } else if (destination.IsFpuRegister()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005022 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005023 } else {
5024 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005025 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
5026 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005027 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005028 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005029 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
5030 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005031 } else if (destination.IsRegisterPair()) {
5032 DCHECK(ExpectedPairLayout(destination));
5033 __ LoadFromOffset(
5034 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
5035 } else {
5036 DCHECK(destination.IsFpuRegisterPair()) << destination;
5037 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5038 SP,
5039 source.GetStackIndex());
5040 }
5041 } else if (source.IsRegisterPair()) {
5042 if (destination.IsRegisterPair()) {
5043 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
5044 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005045 } else if (destination.IsFpuRegisterPair()) {
5046 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5047 source.AsRegisterPairLow<Register>(),
5048 source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005049 } else {
5050 DCHECK(destination.IsDoubleStackSlot()) << destination;
5051 DCHECK(ExpectedPairLayout(source));
5052 __ StoreToOffset(
5053 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
5054 }
5055 } else if (source.IsFpuRegisterPair()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005056 if (destination.IsRegisterPair()) {
5057 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5058 destination.AsRegisterPairHigh<Register>(),
5059 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5060 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005061 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5062 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5063 } else {
5064 DCHECK(destination.IsDoubleStackSlot()) << destination;
5065 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
5066 SP,
5067 destination.GetStackIndex());
5068 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005069 } else {
5070 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005071 HConstant* constant = source.GetConstant();
5072 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5073 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005074 if (destination.IsRegister()) {
5075 __ LoadImmediate(destination.AsRegister<Register>(), value);
5076 } else {
5077 DCHECK(destination.IsStackSlot());
5078 __ LoadImmediate(IP, value);
5079 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5080 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005081 } else if (constant->IsLongConstant()) {
5082 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005083 if (destination.IsRegisterPair()) {
5084 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
5085 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005086 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005087 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005088 __ LoadImmediate(IP, Low32Bits(value));
5089 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5090 __ LoadImmediate(IP, High32Bits(value));
5091 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5092 }
5093 } else if (constant->IsDoubleConstant()) {
5094 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005095 if (destination.IsFpuRegisterPair()) {
5096 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005097 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005098 DCHECK(destination.IsDoubleStackSlot()) << destination;
5099 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005100 __ LoadImmediate(IP, Low32Bits(int_value));
5101 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5102 __ LoadImmediate(IP, High32Bits(int_value));
5103 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5104 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005105 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005106 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005107 float value = constant->AsFloatConstant()->GetValue();
5108 if (destination.IsFpuRegister()) {
5109 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
5110 } else {
5111 DCHECK(destination.IsStackSlot());
5112 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
5113 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5114 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005115 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005116 }
5117}
5118
5119void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
5120 __ Mov(IP, reg);
5121 __ LoadFromOffset(kLoadWord, reg, SP, mem);
5122 __ StoreToOffset(kStoreWord, IP, SP, mem);
5123}
5124
5125void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
5126 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
5127 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
5128 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
5129 SP, mem1 + stack_offset);
5130 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
5131 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
5132 SP, mem2 + stack_offset);
5133 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
5134}
5135
5136void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005137 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005138 Location source = move->GetSource();
5139 Location destination = move->GetDestination();
5140
5141 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005142 DCHECK_NE(source.AsRegister<Register>(), IP);
5143 DCHECK_NE(destination.AsRegister<Register>(), IP);
5144 __ Mov(IP, source.AsRegister<Register>());
5145 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
5146 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005147 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005148 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005149 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005150 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005151 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5152 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005153 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005154 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005155 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005156 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005157 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005158 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005159 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005160 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005161 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5162 destination.AsRegisterPairHigh<Register>(),
5163 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005164 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005165 Register low_reg = source.IsRegisterPair()
5166 ? source.AsRegisterPairLow<Register>()
5167 : destination.AsRegisterPairLow<Register>();
5168 int mem = source.IsRegisterPair()
5169 ? destination.GetStackIndex()
5170 : source.GetStackIndex();
5171 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005172 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005173 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005174 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005175 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005176 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
5177 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005178 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005179 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005180 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005181 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
5182 DRegister reg = source.IsFpuRegisterPair()
5183 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
5184 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
5185 int mem = source.IsFpuRegisterPair()
5186 ? destination.GetStackIndex()
5187 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005188 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005189 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005190 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005191 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
5192 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
5193 : destination.AsFpuRegister<SRegister>();
5194 int mem = source.IsFpuRegister()
5195 ? destination.GetStackIndex()
5196 : source.GetStackIndex();
5197
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005198 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005199 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005200 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005201 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005202 Exchange(source.GetStackIndex(), destination.GetStackIndex());
5203 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005204 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005205 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005206 }
5207}
5208
5209void ParallelMoveResolverARM::SpillScratch(int reg) {
5210 __ Push(static_cast<Register>(reg));
5211}
5212
5213void ParallelMoveResolverARM::RestoreScratch(int reg) {
5214 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005215}
5216
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005217void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01005218 InvokeRuntimeCallingConvention calling_convention;
5219 CodeGenerator::CreateLoadClassLocationSummary(
5220 cls,
5221 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Roland Levillain3b359c72015-11-17 19:35:12 +00005222 Location::RegisterLocation(R0),
5223 /* code_generator_supports_read_barrier */ true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005224}
5225
5226void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005227 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005228 if (cls->NeedsAccessCheck()) {
5229 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5230 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5231 cls,
5232 cls->GetDexPc(),
5233 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005234 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005235 return;
5236 }
5237
Roland Levillain3b359c72015-11-17 19:35:12 +00005238 Location out_loc = locations->Out();
5239 Register out = out_loc.AsRegister<Register>();
Calin Juravle580b6092015-10-06 17:35:58 +01005240 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005241
Calin Juravle580b6092015-10-06 17:35:58 +01005242 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005243 DCHECK(!cls->CanCallRuntime());
5244 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillainc9285912015-12-18 10:38:42 +00005245 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5246 GenerateGcRootFieldLoad(
5247 cls, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005248 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00005249 // /* GcRoot<mirror::Class>[] */ out =
5250 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005251 __ LoadFromOffset(kLoadWord,
5252 out,
5253 current_method,
Vladimir Marko05792b92015-08-03 11:56:49 +01005254 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
Roland Levillainc9285912015-12-18 10:38:42 +00005255 // /* GcRoot<mirror::Class> */ out = out[type_index]
5256 GenerateGcRootFieldLoad(cls, out_loc, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005257
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005258 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
5259 DCHECK(cls->CanCallRuntime());
5260 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
5261 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5262 codegen_->AddSlowPath(slow_path);
5263 if (!cls->IsInDexCache()) {
5264 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5265 }
5266 if (cls->MustGenerateClinitCheck()) {
5267 GenerateClassInitializationCheck(slow_path, out);
5268 } else {
5269 __ Bind(slow_path->GetExitLabel());
5270 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005271 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005272 }
5273}
5274
5275void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
5276 LocationSummary* locations =
5277 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5278 locations->SetInAt(0, Location::RequiresRegister());
5279 if (check->HasUses()) {
5280 locations->SetOut(Location::SameAsFirstInput());
5281 }
5282}
5283
5284void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005285 // We assume the class is not null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005286 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005287 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005288 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005289 GenerateClassInitializationCheck(slow_path,
5290 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005291}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005292
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005293void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005294 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005295 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
5296 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
5297 __ b(slow_path->GetEntryLabel(), LT);
5298 // Even if the initialized flag is set, we may be in a situation where caches are not synced
5299 // properly. Therefore, we do a memory fence.
5300 __ dmb(ISH);
5301 __ Bind(slow_path->GetExitLabel());
5302}
5303
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005304void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005305 LocationSummary::CallKind call_kind = (!load->IsInDexCache() || kEmitCompilerReadBarrier)
5306 ? LocationSummary::kCallOnSlowPath
5307 : LocationSummary::kNoCall;
5308 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005309 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005310 locations->SetOut(Location::RequiresRegister());
5311}
5312
5313void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005314 LocationSummary* locations = load->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005315 Location out_loc = locations->Out();
5316 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005317 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005318
Roland Levillainc9285912015-12-18 10:38:42 +00005319 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5320 GenerateGcRootFieldLoad(
5321 load, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Roland Levillain3b359c72015-11-17 19:35:12 +00005322 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
Mathieu Chartiereace4582014-11-24 18:29:54 -08005323 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
Roland Levillainc9285912015-12-18 10:38:42 +00005324 // /* GcRoot<mirror::String> */ out = out[string_index]
5325 GenerateGcRootFieldLoad(
5326 load, out_loc, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Roland Levillain3b359c72015-11-17 19:35:12 +00005327
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005328 if (!load->IsInDexCache()) {
5329 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
5330 codegen_->AddSlowPath(slow_path);
5331 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5332 __ Bind(slow_path->GetExitLabel());
5333 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005334}
5335
David Brazdilcb1c0552015-08-04 16:22:25 +01005336static int32_t GetExceptionTlsOffset() {
5337 return Thread::ExceptionOffset<kArmWordSize>().Int32Value();
5338}
5339
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005340void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
5341 LocationSummary* locations =
5342 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5343 locations->SetOut(Location::RequiresRegister());
5344}
5345
5346void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005347 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01005348 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
5349}
5350
5351void LocationsBuilderARM::VisitClearException(HClearException* clear) {
5352 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5353}
5354
5355void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005356 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01005357 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005358}
5359
5360void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
5361 LocationSummary* locations =
5362 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5363 InvokeRuntimeCallingConvention calling_convention;
5364 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5365}
5366
5367void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
5368 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00005369 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005370 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005371}
5372
Roland Levillainc9285912015-12-18 10:38:42 +00005373static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5374 return kEmitCompilerReadBarrier &&
5375 (kUseBakerReadBarrier ||
5376 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5377 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5378 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5379}
5380
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005381void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005382 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain3b359c72015-11-17 19:35:12 +00005383 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5384 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005385 case TypeCheckKind::kExactCheck:
5386 case TypeCheckKind::kAbstractClassCheck:
5387 case TypeCheckKind::kClassHierarchyCheck:
5388 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005389 call_kind =
5390 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005391 break;
5392 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005393 case TypeCheckKind::kUnresolvedCheck:
5394 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005395 call_kind = LocationSummary::kCallOnSlowPath;
5396 break;
5397 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005398
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005399 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain3b359c72015-11-17 19:35:12 +00005400 locations->SetInAt(0, Location::RequiresRegister());
5401 locations->SetInAt(1, Location::RequiresRegister());
5402 // The "out" register is used as a temporary, so it overlaps with the inputs.
5403 // Note that TypeCheckSlowPathARM uses this register too.
5404 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5405 // When read barriers are enabled, we need a temporary register for
5406 // some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00005407 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain3b359c72015-11-17 19:35:12 +00005408 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005409 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005410}
5411
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005412void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00005413 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005414 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005415 Location obj_loc = locations->InAt(0);
5416 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005417 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005418 Location out_loc = locations->Out();
5419 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005420 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillainc9285912015-12-18 10:38:42 +00005421 locations->GetTemp(0) :
5422 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005423 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005424 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5425 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5426 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005427 Label done, zero;
Andreas Gampe85b62f22015-09-09 13:15:38 -07005428 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005429
5430 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005431 // avoid null check if we know obj is not null.
5432 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00005433 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005434 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005435
Roland Levillain3b359c72015-11-17 19:35:12 +00005436 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005437 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005438
Roland Levillainc9285912015-12-18 10:38:42 +00005439 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005440 case TypeCheckKind::kExactCheck: {
5441 __ cmp(out, ShifterOperand(cls));
5442 // Classes must be equal for the instanceof to succeed.
5443 __ b(&zero, NE);
5444 __ LoadImmediate(out, 1);
5445 __ b(&done);
5446 break;
5447 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005448
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005449 case TypeCheckKind::kAbstractClassCheck: {
5450 // If the class is abstract, we eagerly fetch the super class of the
5451 // object to avoid doing a comparison we know will fail.
5452 Label loop;
5453 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005454 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005455 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005456 // If `out` is null, we use it for the result, and jump to `done`.
5457 __ CompareAndBranchIfZero(out, &done);
5458 __ cmp(out, ShifterOperand(cls));
5459 __ b(&loop, NE);
5460 __ LoadImmediate(out, 1);
5461 if (zero.IsLinked()) {
5462 __ b(&done);
5463 }
5464 break;
5465 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005466
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005467 case TypeCheckKind::kClassHierarchyCheck: {
5468 // Walk over the class hierarchy to find a match.
5469 Label loop, success;
5470 __ Bind(&loop);
5471 __ cmp(out, ShifterOperand(cls));
5472 __ b(&success, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005473 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005474 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005475 __ CompareAndBranchIfNonZero(out, &loop);
5476 // If `out` is null, we use it for the result, and jump to `done`.
5477 __ b(&done);
5478 __ Bind(&success);
5479 __ LoadImmediate(out, 1);
5480 if (zero.IsLinked()) {
5481 __ b(&done);
5482 }
5483 break;
5484 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005485
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005486 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005487 // Do an exact check.
5488 Label exact_check;
5489 __ cmp(out, ShifterOperand(cls));
5490 __ b(&exact_check, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005491 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00005492 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005493 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005494 // If `out` is null, we use it for the result, and jump to `done`.
5495 __ CompareAndBranchIfZero(out, &done);
5496 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
5497 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
5498 __ CompareAndBranchIfNonZero(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005499 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005500 __ LoadImmediate(out, 1);
5501 __ b(&done);
5502 break;
5503 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005504
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005505 case TypeCheckKind::kArrayCheck: {
5506 __ cmp(out, ShifterOperand(cls));
5507 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain3b359c72015-11-17 19:35:12 +00005508 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5509 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005510 codegen_->AddSlowPath(slow_path);
5511 __ b(slow_path->GetEntryLabel(), NE);
5512 __ LoadImmediate(out, 1);
5513 if (zero.IsLinked()) {
5514 __ b(&done);
5515 }
5516 break;
5517 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005518
Calin Juravle98893e12015-10-02 21:05:03 +01005519 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005520 case TypeCheckKind::kInterfaceCheck: {
5521 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005522 // into the slow path for the unresolved and interface check
Roland Levillain3b359c72015-11-17 19:35:12 +00005523 // cases.
5524 //
5525 // We cannot directly call the InstanceofNonTrivial runtime
5526 // entry point without resorting to a type checking slow path
5527 // here (i.e. by calling InvokeRuntime directly), as it would
5528 // require to assign fixed registers for the inputs of this
5529 // HInstanceOf instruction (following the runtime calling
5530 // convention), which might be cluttered by the potential first
5531 // read barrier emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00005532 //
5533 // TODO: Introduce a new runtime entry point taking the object
5534 // to test (instead of its class) as argument, and let it deal
5535 // with the read barrier issues. This will let us refactor this
5536 // case of the `switch` code as it was previously (with a direct
5537 // call to the runtime not using a type checking slow path).
5538 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00005539 DCHECK(locations->OnlyCallsOnSlowPath());
5540 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5541 /* is_fatal */ false);
5542 codegen_->AddSlowPath(slow_path);
5543 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005544 if (zero.IsLinked()) {
5545 __ b(&done);
5546 }
5547 break;
5548 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005549 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005550
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005551 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005552 __ Bind(&zero);
5553 __ LoadImmediate(out, 0);
5554 }
5555
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005556 if (done.IsLinked()) {
5557 __ Bind(&done);
5558 }
5559
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005560 if (slow_path != nullptr) {
5561 __ Bind(slow_path->GetExitLabel());
5562 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005563}
5564
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005565void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005566 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5567 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
5568
Roland Levillain3b359c72015-11-17 19:35:12 +00005569 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5570 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005571 case TypeCheckKind::kExactCheck:
5572 case TypeCheckKind::kAbstractClassCheck:
5573 case TypeCheckKind::kClassHierarchyCheck:
5574 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005575 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5576 LocationSummary::kCallOnSlowPath :
5577 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005578 break;
5579 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005580 case TypeCheckKind::kUnresolvedCheck:
5581 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005582 call_kind = LocationSummary::kCallOnSlowPath;
5583 break;
5584 }
5585
Roland Levillain3b359c72015-11-17 19:35:12 +00005586 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5587 locations->SetInAt(0, Location::RequiresRegister());
5588 locations->SetInAt(1, Location::RequiresRegister());
5589 // Note that TypeCheckSlowPathARM uses this "temp" register too.
5590 locations->AddTemp(Location::RequiresRegister());
5591 // When read barriers are enabled, we need an additional temporary
5592 // register for some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00005593 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005594 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005595 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005596}
5597
5598void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00005599 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005600 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005601 Location obj_loc = locations->InAt(0);
5602 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005603 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005604 Location temp_loc = locations->GetTemp(0);
5605 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005606 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillainc9285912015-12-18 10:38:42 +00005607 locations->GetTemp(1) :
5608 Location::NoLocation();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005609 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005610 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5611 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5612 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005613
Roland Levillain3b359c72015-11-17 19:35:12 +00005614 bool is_type_check_slow_path_fatal =
5615 (type_check_kind == TypeCheckKind::kExactCheck ||
5616 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5617 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5618 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5619 !instruction->CanThrowIntoCatchBlock();
5620 SlowPathCode* type_check_slow_path =
5621 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5622 is_type_check_slow_path_fatal);
5623 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005624
5625 Label done;
5626 // Avoid null check if we know obj is not null.
5627 if (instruction->MustDoNullCheck()) {
5628 __ CompareAndBranchIfZero(obj, &done);
5629 }
5630
Roland Levillain3b359c72015-11-17 19:35:12 +00005631 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005632 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005633
Roland Levillain3b359c72015-11-17 19:35:12 +00005634 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005635 case TypeCheckKind::kExactCheck:
5636 case TypeCheckKind::kArrayCheck: {
5637 __ cmp(temp, ShifterOperand(cls));
5638 // Jump to slow path for throwing the exception or doing a
5639 // more involved array check.
Roland Levillain3b359c72015-11-17 19:35:12 +00005640 __ b(type_check_slow_path->GetEntryLabel(), NE);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005641 break;
5642 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005643
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005644 case TypeCheckKind::kAbstractClassCheck: {
5645 // If the class is abstract, we eagerly fetch the super class of the
5646 // object to avoid doing a comparison we know will fail.
Roland Levillain3b359c72015-11-17 19:35:12 +00005647 Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005648 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005649 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005650 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005651
5652 // If the class reference currently in `temp` is not null, jump
5653 // to the `compare_classes` label to compare it with the checked
5654 // class.
5655 __ CompareAndBranchIfNonZero(temp, &compare_classes);
5656 // Otherwise, jump to the slow path to throw the exception.
5657 //
5658 // But before, move back the object's class into `temp` before
5659 // going into the slow path, as it has been overwritten in the
5660 // meantime.
5661 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005662 GenerateReferenceLoadTwoRegisters(
5663 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005664 __ b(type_check_slow_path->GetEntryLabel());
5665
5666 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005667 __ cmp(temp, ShifterOperand(cls));
5668 __ b(&loop, NE);
5669 break;
5670 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005671
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005672 case TypeCheckKind::kClassHierarchyCheck: {
5673 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005674 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005675 __ Bind(&loop);
5676 __ cmp(temp, ShifterOperand(cls));
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005677 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005678
Roland Levillain3b359c72015-11-17 19:35:12 +00005679 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005680 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005681
5682 // If the class reference currently in `temp` is not null, jump
5683 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005684 __ CompareAndBranchIfNonZero(temp, &loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005685 // Otherwise, jump to the slow path to throw the exception.
5686 //
5687 // But before, move back the object's class into `temp` before
5688 // going into the slow path, as it has been overwritten in the
5689 // meantime.
5690 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005691 GenerateReferenceLoadTwoRegisters(
5692 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005693 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005694 break;
5695 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005696
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005697 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005698 // Do an exact check.
Roland Levillain3b359c72015-11-17 19:35:12 +00005699 Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005700 __ cmp(temp, ShifterOperand(cls));
5701 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005702
5703 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00005704 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005705 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005706
5707 // If the component type is not null (i.e. the object is indeed
5708 // an array), jump to label `check_non_primitive_component_type`
5709 // to further check that this component type is not a primitive
5710 // type.
5711 __ CompareAndBranchIfNonZero(temp, &check_non_primitive_component_type);
5712 // Otherwise, jump to the slow path to throw the exception.
5713 //
5714 // But before, move back the object's class into `temp` before
5715 // going into the slow path, as it has been overwritten in the
5716 // meantime.
5717 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005718 GenerateReferenceLoadTwoRegisters(
5719 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005720 __ b(type_check_slow_path->GetEntryLabel());
5721
5722 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005723 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00005724 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
5725 __ CompareAndBranchIfZero(temp, &done);
5726 // Same comment as above regarding `temp` and the slow path.
5727 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005728 GenerateReferenceLoadTwoRegisters(
5729 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005730 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005731 break;
5732 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005733
Calin Juravle98893e12015-10-02 21:05:03 +01005734 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005735 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005736 // We always go into the type check slow path for the unresolved
5737 // and interface check cases.
Roland Levillain3b359c72015-11-17 19:35:12 +00005738 //
5739 // We cannot directly call the CheckCast runtime entry point
5740 // without resorting to a type checking slow path here (i.e. by
5741 // calling InvokeRuntime directly), as it would require to
5742 // assign fixed registers for the inputs of this HInstanceOf
5743 // instruction (following the runtime calling convention), which
5744 // might be cluttered by the potential first read barrier
5745 // emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00005746 //
5747 // TODO: Introduce a new runtime entry point taking the object
5748 // to test (instead of its class) as argument, and let it deal
5749 // with the read barrier issues. This will let us refactor this
5750 // case of the `switch` code as it was previously (with a direct
5751 // call to the runtime not using a type checking slow path).
5752 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00005753 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005754 break;
5755 }
5756 __ Bind(&done);
5757
Roland Levillain3b359c72015-11-17 19:35:12 +00005758 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005759}
5760
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005761void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
5762 LocationSummary* locations =
5763 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5764 InvokeRuntimeCallingConvention calling_convention;
5765 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5766}
5767
5768void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
5769 codegen_->InvokeRuntime(instruction->IsEnter()
5770 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
5771 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00005772 instruction->GetDexPc(),
5773 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005774 if (instruction->IsEnter()) {
5775 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5776 } else {
5777 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5778 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005779}
5780
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005781void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
5782void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
5783void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005784
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005785void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005786 LocationSummary* locations =
5787 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5788 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
5789 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005790 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005791 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005792 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00005793 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005794}
5795
5796void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
5797 HandleBitwiseOperation(instruction);
5798}
5799
5800void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
5801 HandleBitwiseOperation(instruction);
5802}
5803
5804void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
5805 HandleBitwiseOperation(instruction);
5806}
5807
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005808void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
5809 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
5810 if (value == 0xffffffffu) {
5811 if (out != first) {
5812 __ mov(out, ShifterOperand(first));
5813 }
5814 return;
5815 }
5816 if (value == 0u) {
5817 __ mov(out, ShifterOperand(0));
5818 return;
5819 }
5820 ShifterOperand so;
5821 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
5822 __ and_(out, first, so);
5823 } else {
5824 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so));
5825 __ bic(out, first, ShifterOperand(~value));
5826 }
5827}
5828
5829void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
5830 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
5831 if (value == 0u) {
5832 if (out != first) {
5833 __ mov(out, ShifterOperand(first));
5834 }
5835 return;
5836 }
5837 if (value == 0xffffffffu) {
5838 __ mvn(out, ShifterOperand(0));
5839 return;
5840 }
5841 ShifterOperand so;
5842 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
5843 __ orr(out, first, so);
5844 } else {
5845 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
5846 __ orn(out, first, ShifterOperand(~value));
5847 }
5848}
5849
5850void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
5851 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
5852 if (value == 0u) {
5853 if (out != first) {
5854 __ mov(out, ShifterOperand(first));
5855 }
5856 return;
5857 }
5858 __ eor(out, first, ShifterOperand(value));
5859}
5860
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005861void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
5862 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005863 Location first = locations->InAt(0);
5864 Location second = locations->InAt(1);
5865 Location out = locations->Out();
5866
5867 if (second.IsConstant()) {
5868 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
5869 uint32_t value_low = Low32Bits(value);
5870 if (instruction->GetResultType() == Primitive::kPrimInt) {
5871 Register first_reg = first.AsRegister<Register>();
5872 Register out_reg = out.AsRegister<Register>();
5873 if (instruction->IsAnd()) {
5874 GenerateAndConst(out_reg, first_reg, value_low);
5875 } else if (instruction->IsOr()) {
5876 GenerateOrrConst(out_reg, first_reg, value_low);
5877 } else {
5878 DCHECK(instruction->IsXor());
5879 GenerateEorConst(out_reg, first_reg, value_low);
5880 }
5881 } else {
5882 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
5883 uint32_t value_high = High32Bits(value);
5884 Register first_low = first.AsRegisterPairLow<Register>();
5885 Register first_high = first.AsRegisterPairHigh<Register>();
5886 Register out_low = out.AsRegisterPairLow<Register>();
5887 Register out_high = out.AsRegisterPairHigh<Register>();
5888 if (instruction->IsAnd()) {
5889 GenerateAndConst(out_low, first_low, value_low);
5890 GenerateAndConst(out_high, first_high, value_high);
5891 } else if (instruction->IsOr()) {
5892 GenerateOrrConst(out_low, first_low, value_low);
5893 GenerateOrrConst(out_high, first_high, value_high);
5894 } else {
5895 DCHECK(instruction->IsXor());
5896 GenerateEorConst(out_low, first_low, value_low);
5897 GenerateEorConst(out_high, first_high, value_high);
5898 }
5899 }
5900 return;
5901 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005902
5903 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005904 Register first_reg = first.AsRegister<Register>();
5905 ShifterOperand second_reg(second.AsRegister<Register>());
5906 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005907 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005908 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005909 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005910 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005911 } else {
5912 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005913 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005914 }
5915 } else {
5916 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005917 Register first_low = first.AsRegisterPairLow<Register>();
5918 Register first_high = first.AsRegisterPairHigh<Register>();
5919 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
5920 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
5921 Register out_low = out.AsRegisterPairLow<Register>();
5922 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005923 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005924 __ and_(out_low, first_low, second_low);
5925 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005926 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005927 __ orr(out_low, first_low, second_low);
5928 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005929 } else {
5930 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005931 __ eor(out_low, first_low, second_low);
5932 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005933 }
5934 }
5935}
5936
Roland Levillainc9285912015-12-18 10:38:42 +00005937void InstructionCodeGeneratorARM::GenerateReferenceLoadOneRegister(HInstruction* instruction,
5938 Location out,
5939 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005940 Location maybe_temp) {
Roland Levillainc9285912015-12-18 10:38:42 +00005941 Register out_reg = out.AsRegister<Register>();
5942 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005943 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00005944 if (kUseBakerReadBarrier) {
5945 // Load with fast path based Baker's read barrier.
5946 // /* HeapReference<Object> */ out = *(out + offset)
5947 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005948 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00005949 } else {
5950 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005951 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillainc9285912015-12-18 10:38:42 +00005952 // in the following move operation, as we will need it for the
5953 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005954 __ Mov(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillainc9285912015-12-18 10:38:42 +00005955 // /* HeapReference<Object> */ out = *(out + offset)
5956 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005957 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillainc9285912015-12-18 10:38:42 +00005958 }
5959 } else {
5960 // Plain load with no read barrier.
5961 // /* HeapReference<Object> */ out = *(out + offset)
5962 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
5963 __ MaybeUnpoisonHeapReference(out_reg);
5964 }
5965}
5966
5967void InstructionCodeGeneratorARM::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
5968 Location out,
5969 Location obj,
5970 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005971 Location maybe_temp) {
Roland Levillainc9285912015-12-18 10:38:42 +00005972 Register out_reg = out.AsRegister<Register>();
5973 Register obj_reg = obj.AsRegister<Register>();
5974 if (kEmitCompilerReadBarrier) {
5975 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005976 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00005977 // Load with fast path based Baker's read barrier.
5978 // /* HeapReference<Object> */ out = *(obj + offset)
5979 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005980 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00005981 } else {
5982 // Load with slow path based read barrier.
5983 // /* HeapReference<Object> */ out = *(obj + offset)
5984 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
5985 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5986 }
5987 } else {
5988 // Plain load with no read barrier.
5989 // /* HeapReference<Object> */ out = *(obj + offset)
5990 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
5991 __ MaybeUnpoisonHeapReference(out_reg);
5992 }
5993}
5994
5995void InstructionCodeGeneratorARM::GenerateGcRootFieldLoad(HInstruction* instruction,
5996 Location root,
5997 Register obj,
5998 uint32_t offset) {
5999 Register root_reg = root.AsRegister<Register>();
6000 if (kEmitCompilerReadBarrier) {
6001 if (kUseBakerReadBarrier) {
6002 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6003 // Baker's read barrier are used:
6004 //
6005 // root = obj.field;
6006 // if (Thread::Current()->GetIsGcMarking()) {
6007 // root = ReadBarrier::Mark(root)
6008 // }
6009
6010 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6011 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6012 static_assert(
6013 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6014 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6015 "have different sizes.");
6016 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6017 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6018 "have different sizes.");
6019
6020 // Slow path used to mark the GC root `root`.
6021 SlowPathCode* slow_path =
6022 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, root, root);
6023 codegen_->AddSlowPath(slow_path);
6024
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006025 // IP = Thread::Current()->GetIsGcMarking()
Roland Levillainc9285912015-12-18 10:38:42 +00006026 __ LoadFromOffset(
6027 kLoadWord, IP, TR, Thread::IsGcMarkingOffset<kArmWordSize>().Int32Value());
6028 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
6029 __ Bind(slow_path->GetExitLabel());
6030 } else {
6031 // GC root loaded through a slow path for read barriers other
6032 // than Baker's.
6033 // /* GcRoot<mirror::Object>* */ root = obj + offset
6034 __ AddConstant(root_reg, obj, offset);
6035 // /* mirror::Object* */ root = root->Read()
6036 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6037 }
6038 } else {
6039 // Plain GC root load with no read barrier.
6040 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6041 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6042 // Note that GC roots are not affected by heap poisoning, thus we
6043 // do not have to unpoison `root_reg` here.
6044 }
6045}
6046
6047void CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6048 Location ref,
6049 Register obj,
6050 uint32_t offset,
6051 Location temp,
6052 bool needs_null_check) {
6053 DCHECK(kEmitCompilerReadBarrier);
6054 DCHECK(kUseBakerReadBarrier);
6055
6056 // /* HeapReference<Object> */ ref = *(obj + offset)
6057 Location no_index = Location::NoLocation();
6058 GenerateReferenceLoadWithBakerReadBarrier(
6059 instruction, ref, obj, offset, no_index, temp, needs_null_check);
6060}
6061
6062void CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6063 Location ref,
6064 Register obj,
6065 uint32_t data_offset,
6066 Location index,
6067 Location temp,
6068 bool needs_null_check) {
6069 DCHECK(kEmitCompilerReadBarrier);
6070 DCHECK(kUseBakerReadBarrier);
6071
6072 // /* HeapReference<Object> */ ref =
6073 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6074 GenerateReferenceLoadWithBakerReadBarrier(
6075 instruction, ref, obj, data_offset, index, temp, needs_null_check);
6076}
6077
6078void CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6079 Location ref,
6080 Register obj,
6081 uint32_t offset,
6082 Location index,
6083 Location temp,
6084 bool needs_null_check) {
6085 DCHECK(kEmitCompilerReadBarrier);
6086 DCHECK(kUseBakerReadBarrier);
6087
6088 // In slow path based read barriers, the read barrier call is
6089 // inserted after the original load. However, in fast path based
6090 // Baker's read barriers, we need to perform the load of
6091 // mirror::Object::monitor_ *before* the original reference load.
6092 // This load-load ordering is required by the read barrier.
6093 // The fast path/slow path (for Baker's algorithm) should look like:
6094 //
6095 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6096 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6097 // HeapReference<Object> ref = *src; // Original reference load.
6098 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6099 // if (is_gray) {
6100 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6101 // }
6102 //
6103 // Note: the original implementation in ReadBarrier::Barrier is
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006104 // slightly more complex as it performs additional checks that we do
6105 // not do here for performance reasons.
Roland Levillainc9285912015-12-18 10:38:42 +00006106
6107 Register ref_reg = ref.AsRegister<Register>();
6108 Register temp_reg = temp.AsRegister<Register>();
6109 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6110
6111 // /* int32_t */ monitor = obj->monitor_
6112 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
6113 if (needs_null_check) {
6114 MaybeRecordImplicitNullCheck(instruction);
6115 }
6116 // /* LockWord */ lock_word = LockWord(monitor)
6117 static_assert(sizeof(LockWord) == sizeof(int32_t),
6118 "art::LockWord and int32_t have different sizes.");
6119 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
6120 __ Lsr(temp_reg, temp_reg, LockWord::kReadBarrierStateShift);
6121 __ and_(temp_reg, temp_reg, ShifterOperand(LockWord::kReadBarrierStateMask));
6122 static_assert(
6123 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
6124 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
6125
6126 // Introduce a dependency on the high bits of rb_state, which shall
6127 // be all zeroes, to prevent load-load reordering, and without using
6128 // a memory barrier (which would be more expensive).
6129 // IP = rb_state & ~LockWord::kReadBarrierStateMask = 0
6130 __ bic(IP, temp_reg, ShifterOperand(LockWord::kReadBarrierStateMask));
6131 // obj is unchanged by this operation, but its value now depends on
6132 // IP, which depends on temp_reg.
6133 __ add(obj, obj, ShifterOperand(IP));
6134
6135 // The actual reference load.
6136 if (index.IsValid()) {
6137 static_assert(
6138 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6139 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
6140 // /* HeapReference<Object> */ ref =
6141 // *(obj + offset + index * sizeof(HeapReference<Object>))
6142 if (index.IsConstant()) {
6143 size_t computed_offset =
6144 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset;
6145 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
6146 } else {
6147 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
6148 __ LoadFromOffset(kLoadWord, ref_reg, IP, offset);
6149 }
6150 } else {
6151 // /* HeapReference<Object> */ ref = *(obj + offset)
6152 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
6153 }
6154
6155 // Object* ref = ref_addr->AsMirrorPtr()
6156 __ MaybeUnpoisonHeapReference(ref_reg);
6157
6158 // Slow path used to mark the object `ref` when it is gray.
6159 SlowPathCode* slow_path =
6160 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, ref, ref);
6161 AddSlowPath(slow_path);
6162
6163 // if (rb_state == ReadBarrier::gray_ptr_)
6164 // ref = ReadBarrier::Mark(ref);
6165 __ cmp(temp_reg, ShifterOperand(ReadBarrier::gray_ptr_));
6166 __ b(slow_path->GetEntryLabel(), EQ);
6167 __ Bind(slow_path->GetExitLabel());
6168}
6169
6170void CodeGeneratorARM::GenerateReadBarrierSlow(HInstruction* instruction,
6171 Location out,
6172 Location ref,
6173 Location obj,
6174 uint32_t offset,
6175 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006176 DCHECK(kEmitCompilerReadBarrier);
6177
Roland Levillainc9285912015-12-18 10:38:42 +00006178 // Insert a slow path based read barrier *after* the reference load.
6179 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006180 // If heap poisoning is enabled, the unpoisoning of the loaded
6181 // reference will be carried out by the runtime within the slow
6182 // path.
6183 //
6184 // Note that `ref` currently does not get unpoisoned (when heap
6185 // poisoning is enabled), which is alright as the `ref` argument is
6186 // not used by the artReadBarrierSlow entry point.
6187 //
6188 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6189 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6190 ReadBarrierForHeapReferenceSlowPathARM(instruction, out, ref, obj, offset, index);
6191 AddSlowPath(slow_path);
6192
Roland Levillain3b359c72015-11-17 19:35:12 +00006193 __ b(slow_path->GetEntryLabel());
6194 __ Bind(slow_path->GetExitLabel());
6195}
6196
Roland Levillainc9285912015-12-18 10:38:42 +00006197void CodeGeneratorARM::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6198 Location out,
6199 Location ref,
6200 Location obj,
6201 uint32_t offset,
6202 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006203 if (kEmitCompilerReadBarrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00006204 // Baker's read barriers shall be handled by the fast path
6205 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
6206 DCHECK(!kUseBakerReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00006207 // If heap poisoning is enabled, unpoisoning will be taken care of
6208 // by the runtime within the slow path.
Roland Levillainc9285912015-12-18 10:38:42 +00006209 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain3b359c72015-11-17 19:35:12 +00006210 } else if (kPoisonHeapReferences) {
6211 __ UnpoisonHeapReference(out.AsRegister<Register>());
6212 }
6213}
6214
Roland Levillainc9285912015-12-18 10:38:42 +00006215void CodeGeneratorARM::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6216 Location out,
6217 Location root) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006218 DCHECK(kEmitCompilerReadBarrier);
6219
Roland Levillainc9285912015-12-18 10:38:42 +00006220 // Insert a slow path based read barrier *after* the GC root load.
6221 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006222 // Note that GC roots are not affected by heap poisoning, so we do
6223 // not need to do anything special for this here.
6224 SlowPathCode* slow_path =
6225 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM(instruction, out, root);
6226 AddSlowPath(slow_path);
6227
Roland Levillain3b359c72015-11-17 19:35:12 +00006228 __ b(slow_path->GetEntryLabel());
6229 __ Bind(slow_path->GetExitLabel());
6230}
6231
Vladimir Markodc151b22015-10-15 18:02:30 +01006232HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch(
6233 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
6234 MethodReference target_method) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006235 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
6236 // We disable pc-relative load when there is an irreducible loop, as the optimization
6237 // is incompatible with it.
6238 if (GetGraph()->HasIrreducibleLoops() &&
6239 (dispatch_info.method_load_kind ==
6240 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
6241 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
6242 }
6243
6244 if (dispatch_info.code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative) {
Vladimir Markodc151b22015-10-15 18:02:30 +01006245 const DexFile& outer_dex_file = GetGraph()->GetDexFile();
6246 if (&outer_dex_file != target_method.dex_file) {
6247 // Calls across dex files are more likely to exceed the available BL range,
6248 // so use absolute patch with fixup if available and kCallArtMethod otherwise.
6249 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location =
6250 (desired_dispatch_info.method_load_kind ==
6251 HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup)
6252 ? HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup
6253 : HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
6254 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006255 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01006256 code_ptr_location,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006257 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01006258 0u
6259 };
6260 }
6261 }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006262 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01006263}
6264
Vladimir Markob4536b72015-11-24 13:45:23 +00006265Register CodeGeneratorARM::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
6266 Register temp) {
6267 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
6268 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
6269 if (!invoke->GetLocations()->Intrinsified()) {
6270 return location.AsRegister<Register>();
6271 }
6272 // For intrinsics we allow any location, so it may be on the stack.
6273 if (!location.IsRegister()) {
6274 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
6275 return temp;
6276 }
6277 // For register locations, check if the register was saved. If so, get it from the stack.
6278 // Note: There is a chance that the register was saved but not overwritten, so we could
6279 // save one load. However, since this is just an intrinsic slow path we prefer this
6280 // simple and more robust approach rather that trying to determine if that's the case.
6281 SlowPathCode* slow_path = GetCurrentSlowPath();
6282 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
6283 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
6284 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
6285 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
6286 return temp;
6287 }
6288 return location.AsRegister<Register>();
6289}
6290
Nicolas Geoffray38207af2015-06-01 15:46:22 +01006291void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00006292 // For better instruction scheduling we load the direct code pointer before the method pointer.
Vladimir Marko58155012015-08-19 12:49:41 +00006293 switch (invoke->GetCodePtrLocation()) {
Vladimir Marko58155012015-08-19 12:49:41 +00006294 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
6295 // LR = code address from literal pool with link-time patch.
6296 __ LoadLiteral(LR, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
Vladimir Marko58155012015-08-19 12:49:41 +00006297 break;
6298 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
6299 // LR = invoke->GetDirectCodePtr();
6300 __ LoadImmediate(LR, invoke->GetDirectCodePtr());
Vladimir Marko58155012015-08-19 12:49:41 +00006301 break;
6302 default:
6303 break;
6304 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006305
Vladimir Marko58155012015-08-19 12:49:41 +00006306 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
6307 switch (invoke->GetMethodLoadKind()) {
6308 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
6309 // temp = thread->string_init_entrypoint
6310 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, invoke->GetStringInitOffset());
6311 break;
6312 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00006313 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006314 break;
6315 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
6316 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
6317 break;
6318 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
6319 __ LoadLiteral(temp.AsRegister<Register>(),
6320 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
6321 break;
Vladimir Markob4536b72015-11-24 13:45:23 +00006322 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
6323 HArmDexCacheArraysBase* base =
6324 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
6325 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
6326 temp.AsRegister<Register>());
6327 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
6328 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
6329 break;
6330 }
Vladimir Marko58155012015-08-19 12:49:41 +00006331 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00006332 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006333 Register method_reg;
6334 Register reg = temp.AsRegister<Register>();
6335 if (current_method.IsRegister()) {
6336 method_reg = current_method.AsRegister<Register>();
6337 } else {
6338 DCHECK(invoke->GetLocations()->Intrinsified());
6339 DCHECK(!current_method.IsValid());
6340 method_reg = reg;
6341 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
6342 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006343 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
6344 __ LoadFromOffset(kLoadWord,
6345 reg,
6346 method_reg,
6347 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00006348 // temp = temp[index_in_cache]
6349 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
6350 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
6351 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01006352 }
Vladimir Marko58155012015-08-19 12:49:41 +00006353 }
6354
6355 switch (invoke->GetCodePtrLocation()) {
6356 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
6357 __ bl(GetFrameEntryLabel());
6358 break;
6359 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01006360 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006361 __ BindTrackedLabel(&relative_call_patches_.back().label);
Vladimir Markodc151b22015-10-15 18:02:30 +01006362 // Arbitrarily branch to the BL itself, override at link time.
6363 __ bl(&relative_call_patches_.back().label);
6364 break;
Vladimir Marko58155012015-08-19 12:49:41 +00006365 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
6366 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
6367 // LR prepared above for better instruction scheduling.
Vladimir Marko58155012015-08-19 12:49:41 +00006368 // LR()
6369 __ blx(LR);
6370 break;
6371 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
6372 // LR = callee_method->entry_point_from_quick_compiled_code_
6373 __ LoadFromOffset(
6374 kLoadWord, LR, callee_method.AsRegister<Register>(),
6375 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize).Int32Value());
6376 // LR()
6377 __ blx(LR);
6378 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006379 }
6380
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006381 DCHECK(!IsLeafMethod());
6382}
6383
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006384void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
6385 Register temp = temp_location.AsRegister<Register>();
6386 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
6387 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006388
6389 // Use the calling convention instead of the location of the receiver, as
6390 // intrinsics may have put the receiver in a different register. In the intrinsics
6391 // slow path, the arguments have been moved to the right place, so here we are
6392 // guaranteed that the receiver is the first register of the calling convention.
6393 InvokeDexCallingConvention calling_convention;
6394 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006395 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00006396 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006397 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006398 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00006399 // Instead of simply (possibly) unpoisoning `temp` here, we should
6400 // emit a read barrier for the previous class reference load.
6401 // However this is not required in practice, as this is an
6402 // intermediate/temporary reference and because the current
6403 // concurrent copying collector keeps the from-space memory
6404 // intact/accessible until the end of the marking phase (the
6405 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006406 __ MaybeUnpoisonHeapReference(temp);
6407 // temp = temp->GetMethodAt(method_offset);
6408 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
6409 kArmWordSize).Int32Value();
6410 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
6411 // LR = temp->GetEntryPoint();
6412 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
6413 // LR();
6414 __ blx(LR);
6415}
6416
Vladimir Marko58155012015-08-19 12:49:41 +00006417void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
6418 DCHECK(linker_patches->empty());
Vladimir Markob4536b72015-11-24 13:45:23 +00006419 size_t size =
6420 method_patches_.size() +
6421 call_patches_.size() +
6422 relative_call_patches_.size() +
6423 /* MOVW+MOVT for each base */ 2u * dex_cache_arrays_base_labels_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00006424 linker_patches->reserve(size);
6425 for (const auto& entry : method_patches_) {
6426 const MethodReference& target_method = entry.first;
6427 Literal* literal = entry.second;
6428 DCHECK(literal->GetLabel()->IsBound());
6429 uint32_t literal_offset = literal->GetLabel()->Position();
6430 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
6431 target_method.dex_file,
6432 target_method.dex_method_index));
6433 }
6434 for (const auto& entry : call_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::CodePatch(literal_offset,
6440 target_method.dex_file,
6441 target_method.dex_method_index));
6442 }
6443 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
6444 uint32_t literal_offset = info.label.Position();
6445 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
6446 info.target_method.dex_file,
6447 info.target_method.dex_method_index));
6448 }
Vladimir Markob4536b72015-11-24 13:45:23 +00006449 for (const auto& pair : dex_cache_arrays_base_labels_) {
6450 HArmDexCacheArraysBase* base = pair.first;
6451 const DexCacheArraysBaseLabels* labels = &pair.second;
6452 const DexFile& dex_file = base->GetDexFile();
6453 size_t base_element_offset = base->GetElementOffset();
6454 DCHECK(labels->add_pc_label.IsBound());
6455 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(labels->add_pc_label.Position());
6456 // Add MOVW patch.
6457 DCHECK(labels->movw_label.IsBound());
6458 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(labels->movw_label.Position());
6459 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(movw_offset,
6460 &dex_file,
6461 add_pc_offset,
6462 base_element_offset));
6463 // Add MOVT patch.
6464 DCHECK(labels->movt_label.IsBound());
6465 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(labels->movt_label.Position());
6466 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(movt_offset,
6467 &dex_file,
6468 add_pc_offset,
6469 base_element_offset));
6470 }
Vladimir Marko58155012015-08-19 12:49:41 +00006471}
6472
6473Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
6474 MethodToLiteralMap* map) {
6475 // Look up the literal for target_method.
6476 auto lb = map->lower_bound(target_method);
6477 if (lb != map->end() && !map->key_comp()(target_method, lb->first)) {
6478 return lb->second;
6479 }
6480 // We don't have a literal for this method yet, insert a new one.
6481 Literal* literal = __ NewLiteral<uint32_t>(0u);
6482 map->PutBefore(lb, target_method, literal);
6483 return literal;
6484}
6485
6486Literal* CodeGeneratorARM::DeduplicateMethodAddressLiteral(MethodReference target_method) {
6487 return DeduplicateMethodLiteral(target_method, &method_patches_);
6488}
6489
6490Literal* CodeGeneratorARM::DeduplicateMethodCodeLiteral(MethodReference target_method) {
6491 return DeduplicateMethodLiteral(target_method, &call_patches_);
6492}
6493
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006494void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006495 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006496 LOG(FATAL) << "Unreachable";
6497}
6498
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006499void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006500 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006501 LOG(FATAL) << "Unreachable";
6502}
6503
Mark Mendellfe57faa2015-09-18 09:26:15 -04006504// Simple implementation of packed switch - generate cascaded compare/jumps.
6505void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6506 LocationSummary* locations =
6507 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6508 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006509 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006510 codegen_->GetAssembler()->IsThumb()) {
6511 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
6512 if (switch_instr->GetStartValue() != 0) {
6513 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
6514 }
6515 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04006516}
6517
6518void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6519 int32_t lower_bound = switch_instr->GetStartValue();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006520 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006521 LocationSummary* locations = switch_instr->GetLocations();
6522 Register value_reg = locations->InAt(0).AsRegister<Register>();
6523 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6524
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006525 if (num_entries <= kPackedSwitchCompareJumpThreshold || !codegen_->GetAssembler()->IsThumb()) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006526 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006527 Register temp_reg = IP;
6528 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
6529 // the immediate, because IP is used as the destination register. For the other
6530 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
6531 // and they can be encoded in the instruction without making use of IP register.
6532 __ AddConstantSetFlags(temp_reg, value_reg, -lower_bound);
6533
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006534 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006535 // Jump to successors[0] if value == lower_bound.
6536 __ b(codegen_->GetLabelOf(successors[0]), EQ);
6537 int32_t last_index = 0;
6538 for (; num_entries - last_index > 2; last_index += 2) {
6539 __ AddConstantSetFlags(temp_reg, temp_reg, -2);
6540 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6541 __ b(codegen_->GetLabelOf(successors[last_index + 1]), LO);
6542 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6543 __ b(codegen_->GetLabelOf(successors[last_index + 2]), EQ);
6544 }
6545 if (num_entries - last_index == 2) {
6546 // The last missing case_value.
Vladimir Markoac6ac102015-12-17 12:14:00 +00006547 __ CmpConstant(temp_reg, 1);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006548 __ b(codegen_->GetLabelOf(successors[last_index + 1]), EQ);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006549 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04006550
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006551 // And the default for any other value.
6552 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6553 __ b(codegen_->GetLabelOf(default_block));
6554 }
6555 } else {
6556 // Create a table lookup.
6557 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
6558
6559 // Materialize a pointer to the switch table
6560 std::vector<Label*> labels(num_entries);
6561 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6562 for (uint32_t i = 0; i < num_entries; i++) {
6563 labels[i] = codegen_->GetLabelOf(successors[i]);
6564 }
6565 JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg);
6566
6567 // Remove the bias.
6568 Register key_reg;
6569 if (lower_bound != 0) {
6570 key_reg = locations->GetTemp(1).AsRegister<Register>();
6571 __ AddConstant(key_reg, value_reg, -lower_bound);
6572 } else {
6573 key_reg = value_reg;
6574 }
6575
6576 // Check whether the value is in the table, jump to default block if not.
6577 __ CmpConstant(key_reg, num_entries - 1);
6578 __ b(codegen_->GetLabelOf(default_block), Condition::HI);
6579
6580 // Load the displacement from the table.
6581 __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2));
6582
6583 // Dispatch is a direct add to the PC (for Thumb2).
6584 __ EmitJumpTableDispatch(table, temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006585 }
6586}
6587
Vladimir Markob4536b72015-11-24 13:45:23 +00006588void LocationsBuilderARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
6589 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
6590 locations->SetOut(Location::RequiresRegister());
6591 codegen_->AddDexCacheArraysBase(base);
6592}
6593
6594void InstructionCodeGeneratorARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
6595 Register base_reg = base->GetLocations()->Out().AsRegister<Register>();
6596 CodeGeneratorARM::DexCacheArraysBaseLabels* labels = codegen_->GetDexCacheArraysBaseLabels(base);
6597 __ BindTrackedLabel(&labels->movw_label);
6598 __ movw(base_reg, 0u);
6599 __ BindTrackedLabel(&labels->movt_label);
6600 __ movt(base_reg, 0u);
6601 __ BindTrackedLabel(&labels->add_pc_label);
6602 __ add(base_reg, base_reg, ShifterOperand(PC));
6603}
6604
Andreas Gampe85b62f22015-09-09 13:15:38 -07006605void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
6606 if (!trg.IsValid()) {
Roland Levillainc9285912015-12-18 10:38:42 +00006607 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006608 return;
6609 }
6610
6611 DCHECK_NE(type, Primitive::kPrimVoid);
6612
6613 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
6614 if (return_loc.Equals(trg)) {
6615 return;
6616 }
6617
6618 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
6619 // with the last branch.
6620 if (type == Primitive::kPrimLong) {
6621 HParallelMove parallel_move(GetGraph()->GetArena());
6622 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
6623 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
6624 GetMoveResolver()->EmitNativeCode(&parallel_move);
6625 } else if (type == Primitive::kPrimDouble) {
6626 HParallelMove parallel_move(GetGraph()->GetArena());
6627 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
6628 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
6629 GetMoveResolver()->EmitNativeCode(&parallel_move);
6630 } else {
6631 // Let the parallel move resolver take care of all of this.
6632 HParallelMove parallel_move(GetGraph()->GetArena());
6633 parallel_move.AddMove(return_loc, trg, type, nullptr);
6634 GetMoveResolver()->EmitNativeCode(&parallel_move);
6635 }
6636}
6637
Roland Levillain4d027112015-07-01 15:41:14 +01006638#undef __
6639#undef QUICK_ENTRY_POINT
6640
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006641} // namespace arm
6642} // namespace art