blob: 4b9f820bb9cccb38aef5ea612279594fe960b963 [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
Nicolas Geoffray4dee6362015-01-23 18:23:14 +000050// We unconditionally allocate R5 to ensure we can do long operations
51// with baseline.
52static constexpr Register kCoreSavedRegisterForBaseline = R5;
53static constexpr Register kCoreCalleeSaves[] =
Andreas Gampe501fd632015-09-10 16:11:06 -070054 { R5, R6, R7, R8, R10, R11, LR };
Nicolas Geoffray4dee6362015-01-23 18:23:14 +000055static constexpr SRegister kFpuCalleeSaves[] =
56 { S16, S17, S18, S19, S20, S21, S22, S23, S24, S25, S26, S27, S28, S29, S30, S31 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010057
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +000058// D31 cannot be split into two S registers, and the register allocator only works on
59// S registers. Therefore there is no need to block it.
60static constexpr DRegister DTMP = D31;
61
Vladimir Markof3e0ee22015-12-17 15:23:13 +000062static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Andreas Gampe7cffc3b2015-10-19 21:31:53 -070063
Roland Levillain62a46b22015-06-01 18:24:13 +010064#define __ down_cast<ArmAssembler*>(codegen->GetAssembler())->
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010065#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmWordSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010066
Andreas Gampe85b62f22015-09-09 13:15:38 -070067class NullCheckSlowPathARM : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010068 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010069 explicit NullCheckSlowPathARM(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010070
Alexandre Rames67555f72014-11-18 10:55:16 +000071 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010072 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010073 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000074 if (instruction_->CanThrowIntoCatchBlock()) {
75 // Live registers will be restored in the catch block if caught.
76 SaveLiveRegisters(codegen, instruction_->GetLocations());
77 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010078 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +000079 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +000080 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +010081 }
82
Alexandre Rames8158f282015-08-07 10:26:17 +010083 bool IsFatal() const OVERRIDE { return true; }
84
Alexandre Rames9931f312015-06-19 14:47:01 +010085 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM"; }
86
Nicolas Geoffraye5038322014-07-04 09:41:32 +010087 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010088 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010089 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM);
90};
91
Andreas Gampe85b62f22015-09-09 13:15:38 -070092class DivZeroCheckSlowPathARM : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000093 public:
94 explicit DivZeroCheckSlowPathARM(HDivZeroCheck* instruction) : instruction_(instruction) {}
95
Alexandre Rames67555f72014-11-18 10:55:16 +000096 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000097 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
98 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000099 if (instruction_->CanThrowIntoCatchBlock()) {
100 // Live registers will be restored in the catch block if caught.
101 SaveLiveRegisters(codegen, instruction_->GetLocations());
102 }
Calin Juravled0d48522014-11-04 16:40:20 +0000103 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000104 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000105 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +0000106 }
107
Alexandre Rames8158f282015-08-07 10:26:17 +0100108 bool IsFatal() const OVERRIDE { return true; }
109
Alexandre Rames9931f312015-06-19 14:47:01 +0100110 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM"; }
111
Calin Juravled0d48522014-11-04 16:40:20 +0000112 private:
113 HDivZeroCheck* const instruction_;
114 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM);
115};
116
Andreas Gampe85b62f22015-09-09 13:15:38 -0700117class SuspendCheckSlowPathARM : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000118 public:
Alexandre Rames67555f72014-11-18 10:55:16 +0000119 SuspendCheckSlowPathARM(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100120 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000121
Alexandre Rames67555f72014-11-18 10:55:16 +0000122 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100123 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000124 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000125 SaveLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100126 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000127 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000128 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000129 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100130 if (successor_ == nullptr) {
131 __ b(GetReturnLabel());
132 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100133 __ b(arm_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100134 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000135 }
136
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100137 Label* GetReturnLabel() {
138 DCHECK(successor_ == nullptr);
139 return &return_label_;
140 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000141
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100142 HBasicBlock* GetSuccessor() const {
143 return successor_;
144 }
145
Alexandre Rames9931f312015-06-19 14:47:01 +0100146 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM"; }
147
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000148 private:
149 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100150 // If not null, the block to branch to after the suspend check.
151 HBasicBlock* const successor_;
152
153 // If `successor_` is null, the label to branch to after the suspend check.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000154 Label return_label_;
155
156 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM);
157};
158
Andreas Gampe85b62f22015-09-09 13:15:38 -0700159class BoundsCheckSlowPathARM : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100160 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100161 explicit BoundsCheckSlowPathARM(HBoundsCheck* instruction)
162 : instruction_(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100163
Alexandre Rames67555f72014-11-18 10:55:16 +0000164 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100165 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100166 LocationSummary* locations = instruction_->GetLocations();
167
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100168 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000169 if (instruction_->CanThrowIntoCatchBlock()) {
170 // Live registers will be restored in the catch block if caught.
171 SaveLiveRegisters(codegen, instruction_->GetLocations());
172 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000173 // We're moving two locations to locations that could overlap, so we need a parallel
174 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100175 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000176 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100177 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000178 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100179 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100180 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100181 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
182 Primitive::kPrimInt);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100183 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000184 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000185 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100186 }
187
Alexandre Rames8158f282015-08-07 10:26:17 +0100188 bool IsFatal() const OVERRIDE { return true; }
189
Alexandre Rames9931f312015-06-19 14:47:01 +0100190 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM"; }
191
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100192 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100193 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100194
195 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM);
196};
197
Andreas Gampe85b62f22015-09-09 13:15:38 -0700198class LoadClassSlowPathARM : public SlowPathCode {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100199 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000200 LoadClassSlowPathARM(HLoadClass* cls,
201 HInstruction* at,
202 uint32_t dex_pc,
203 bool do_clinit)
204 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
205 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
206 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100207
Alexandre Rames67555f72014-11-18 10:55:16 +0000208 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000209 LocationSummary* locations = at_->GetLocations();
210
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100211 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
212 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000213 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100214
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100215 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000216 __ LoadImmediate(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000217 int32_t entry_point_offset = do_clinit_
218 ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
219 : QUICK_ENTRY_POINT(pInitializeType);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000220 arm_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000221 if (do_clinit_) {
222 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
223 } else {
224 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
225 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000226
227 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000228 Location out = locations->Out();
229 if (out.IsValid()) {
230 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000231 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
232 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000233 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100234 __ b(GetExitLabel());
235 }
236
Alexandre Rames9931f312015-06-19 14:47:01 +0100237 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM"; }
238
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100239 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000240 // The class this slow path will load.
241 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100242
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000243 // The instruction where this slow path is happening.
244 // (Might be the load class or an initialization check).
245 HInstruction* const at_;
246
247 // The dex PC of `at_`.
248 const uint32_t dex_pc_;
249
250 // Whether to initialize the class.
251 const bool do_clinit_;
252
253 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100254};
255
Andreas Gampe85b62f22015-09-09 13:15:38 -0700256class LoadStringSlowPathARM : public SlowPathCode {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000257 public:
258 explicit LoadStringSlowPathARM(HLoadString* instruction) : instruction_(instruction) {}
259
Alexandre Rames67555f72014-11-18 10:55:16 +0000260 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000261 LocationSummary* locations = instruction_->GetLocations();
262 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
263
264 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
265 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000266 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000267
268 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800269 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction_->GetStringIndex());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000270 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000271 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000272 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000273 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
274
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000275 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000276 __ b(GetExitLabel());
277 }
278
Alexandre Rames9931f312015-06-19 14:47:01 +0100279 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM"; }
280
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000281 private:
282 HLoadString* const instruction_;
283
284 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM);
285};
286
Andreas Gampe85b62f22015-09-09 13:15:38 -0700287class TypeCheckSlowPathARM : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000288 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000289 TypeCheckSlowPathARM(HInstruction* instruction, bool is_fatal)
290 : instruction_(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000291
Alexandre Rames67555f72014-11-18 10:55:16 +0000292 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000293 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100294 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
295 : locations->Out();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000296 DCHECK(instruction_->IsCheckCast()
297 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000298
299 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
300 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000301
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000302 if (!is_fatal_) {
303 SaveLiveRegisters(codegen, locations);
304 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000305
306 // We're moving two locations to locations that could overlap, so we need a parallel
307 // move resolver.
308 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000309 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100310 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000311 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100312 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100313 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100314 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
315 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000316
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000317 if (instruction_->IsInstanceOf()) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100318 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
319 instruction_,
320 instruction_->GetDexPc(),
321 this);
Roland Levillain3b359c72015-11-17 19:35:12 +0000322 CheckEntrypointTypes<
323 kQuickInstanceofNonTrivial, uint32_t, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000324 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
325 } else {
326 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100327 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
328 instruction_,
329 instruction_->GetDexPc(),
330 this);
Roland Levillain3b359c72015-11-17 19:35:12 +0000331 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000332 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000333
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000334 if (!is_fatal_) {
335 RestoreLiveRegisters(codegen, locations);
336 __ b(GetExitLabel());
337 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000338 }
339
Alexandre Rames9931f312015-06-19 14:47:01 +0100340 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM"; }
341
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000342 bool IsFatal() const OVERRIDE { return is_fatal_; }
343
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000344 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000345 HInstruction* const instruction_;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000346 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000347
348 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM);
349};
350
Andreas Gampe85b62f22015-09-09 13:15:38 -0700351class DeoptimizationSlowPathARM : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700352 public:
353 explicit DeoptimizationSlowPathARM(HInstruction* instruction)
354 : instruction_(instruction) {}
355
356 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
357 __ Bind(GetEntryLabel());
358 SaveLiveRegisters(codegen, instruction_->GetLocations());
359 DCHECK(instruction_->IsDeoptimize());
360 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
361 uint32_t dex_pc = deoptimize->GetDexPc();
362 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
363 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize), instruction_, dex_pc, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000364 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700365 }
366
Alexandre Rames9931f312015-06-19 14:47:01 +0100367 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM"; }
368
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700369 private:
370 HInstruction* const instruction_;
371 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM);
372};
373
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100374class ArraySetSlowPathARM : public SlowPathCode {
375 public:
376 explicit ArraySetSlowPathARM(HInstruction* instruction) : instruction_(instruction) {}
377
378 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
379 LocationSummary* locations = instruction_->GetLocations();
380 __ Bind(GetEntryLabel());
381 SaveLiveRegisters(codegen, locations);
382
383 InvokeRuntimeCallingConvention calling_convention;
384 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
385 parallel_move.AddMove(
386 locations->InAt(0),
387 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
388 Primitive::kPrimNot,
389 nullptr);
390 parallel_move.AddMove(
391 locations->InAt(1),
392 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
393 Primitive::kPrimInt,
394 nullptr);
395 parallel_move.AddMove(
396 locations->InAt(2),
397 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
398 Primitive::kPrimNot,
399 nullptr);
400 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
401
402 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
403 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
404 instruction_,
405 instruction_->GetDexPc(),
406 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000407 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100408 RestoreLiveRegisters(codegen, locations);
409 __ b(GetExitLabel());
410 }
411
412 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM"; }
413
414 private:
415 HInstruction* const instruction_;
416
417 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM);
418};
419
Roland Levillainc9285912015-12-18 10:38:42 +0000420// Slow path marking an object during a read barrier.
421class ReadBarrierMarkSlowPathARM : public SlowPathCode {
422 public:
423 ReadBarrierMarkSlowPathARM(HInstruction* instruction, Location out, Location obj)
424 : instruction_(instruction), out_(out), obj_(obj) {
425 DCHECK(kEmitCompilerReadBarrier);
426 }
427
428 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM"; }
429
430 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
431 LocationSummary* locations = instruction_->GetLocations();
432 Register reg_out = out_.AsRegister<Register>();
433 DCHECK(locations->CanCall());
434 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
435 DCHECK(instruction_->IsInstanceFieldGet() ||
436 instruction_->IsStaticFieldGet() ||
437 instruction_->IsArrayGet() ||
438 instruction_->IsLoadClass() ||
439 instruction_->IsLoadString() ||
440 instruction_->IsInstanceOf() ||
441 instruction_->IsCheckCast())
442 << "Unexpected instruction in read barrier marking slow path: "
443 << instruction_->DebugName();
444
445 __ Bind(GetEntryLabel());
446 SaveLiveRegisters(codegen, locations);
447
448 InvokeRuntimeCallingConvention calling_convention;
449 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
450 arm_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), obj_);
451 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierMark),
452 instruction_,
453 instruction_->GetDexPc(),
454 this);
455 CheckEntrypointTypes<kQuickReadBarrierMark, mirror::Object*, mirror::Object*>();
456 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
457
458 RestoreLiveRegisters(codegen, locations);
459 __ b(GetExitLabel());
460 }
461
462 private:
463 HInstruction* const instruction_;
464 const Location out_;
465 const Location obj_;
466
467 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM);
468};
469
Roland Levillain3b359c72015-11-17 19:35:12 +0000470// Slow path generating a read barrier for a heap reference.
471class ReadBarrierForHeapReferenceSlowPathARM : public SlowPathCode {
472 public:
473 ReadBarrierForHeapReferenceSlowPathARM(HInstruction* instruction,
474 Location out,
475 Location ref,
476 Location obj,
477 uint32_t offset,
478 Location index)
479 : instruction_(instruction),
480 out_(out),
481 ref_(ref),
482 obj_(obj),
483 offset_(offset),
484 index_(index) {
485 DCHECK(kEmitCompilerReadBarrier);
486 // If `obj` is equal to `out` or `ref`, it means the initial object
487 // has been overwritten by (or after) the heap object reference load
488 // to be instrumented, e.g.:
489 //
490 // __ LoadFromOffset(kLoadWord, out, out, offset);
Roland Levillainc9285912015-12-18 10:38:42 +0000491 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain3b359c72015-11-17 19:35:12 +0000492 //
493 // In that case, we have lost the information about the original
494 // object, and the emitted read barrier cannot work properly.
495 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
496 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
497 }
498
499 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
500 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
501 LocationSummary* locations = instruction_->GetLocations();
502 Register reg_out = out_.AsRegister<Register>();
503 DCHECK(locations->CanCall());
504 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
505 DCHECK(!instruction_->IsInvoke() ||
506 (instruction_->IsInvokeStaticOrDirect() &&
Roland Levillainc9285912015-12-18 10:38:42 +0000507 instruction_->GetLocations()->Intrinsified()))
508 << "Unexpected instruction in read barrier for heap reference slow path: "
509 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000510
511 __ Bind(GetEntryLabel());
512 SaveLiveRegisters(codegen, locations);
513
514 // We may have to change the index's value, but as `index_` is a
515 // constant member (like other "inputs" of this slow path),
516 // introduce a copy of it, `index`.
517 Location index = index_;
518 if (index_.IsValid()) {
519 // Handle `index_` for HArrayGet and intrinsic UnsafeGetObject.
520 if (instruction_->IsArrayGet()) {
521 // Compute the actual memory offset and store it in `index`.
522 Register index_reg = index_.AsRegister<Register>();
523 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
524 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
525 // We are about to change the value of `index_reg` (see the
526 // calls to art::arm::Thumb2Assembler::Lsl and
527 // art::arm::Thumb2Assembler::AddConstant below), but it has
528 // not been saved by the previous call to
529 // art::SlowPathCode::SaveLiveRegisters, as it is a
530 // callee-save register --
531 // art::SlowPathCode::SaveLiveRegisters does not consider
532 // callee-save registers, as it has been designed with the
533 // assumption that callee-save registers are supposed to be
534 // handled by the called function. So, as a callee-save
535 // register, `index_reg` _would_ eventually be saved onto
536 // the stack, but it would be too late: we would have
537 // changed its value earlier. Therefore, we manually save
538 // it here into another freely available register,
539 // `free_reg`, chosen of course among the caller-save
540 // registers (as a callee-save `free_reg` register would
541 // exhibit the same problem).
542 //
543 // Note we could have requested a temporary register from
544 // the register allocator instead; but we prefer not to, as
545 // this is a slow path, and we know we can find a
546 // caller-save register that is available.
547 Register free_reg = FindAvailableCallerSaveRegister(codegen);
548 __ Mov(free_reg, index_reg);
549 index_reg = free_reg;
550 index = Location::RegisterLocation(index_reg);
551 } else {
552 // The initial register stored in `index_` has already been
553 // saved in the call to art::SlowPathCode::SaveLiveRegisters
554 // (as it is not a callee-save register), so we can freely
555 // use it.
556 }
557 // Shifting the index value contained in `index_reg` by the scale
558 // factor (2) cannot overflow in practice, as the runtime is
559 // unable to allocate object arrays with a size larger than
560 // 2^26 - 1 (that is, 2^28 - 4 bytes).
561 __ Lsl(index_reg, index_reg, TIMES_4);
562 static_assert(
563 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
564 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
565 __ AddConstant(index_reg, index_reg, offset_);
566 } else {
567 DCHECK(instruction_->IsInvoke());
568 DCHECK(instruction_->GetLocations()->Intrinsified());
569 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
570 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
571 << instruction_->AsInvoke()->GetIntrinsic();
572 DCHECK_EQ(offset_, 0U);
573 DCHECK(index_.IsRegisterPair());
574 // UnsafeGet's offset location is a register pair, the low
575 // part contains the correct offset.
576 index = index_.ToLow();
577 }
578 }
579
580 // We're moving two or three locations to locations that could
581 // overlap, so we need a parallel move resolver.
582 InvokeRuntimeCallingConvention calling_convention;
583 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
584 parallel_move.AddMove(ref_,
585 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
586 Primitive::kPrimNot,
587 nullptr);
588 parallel_move.AddMove(obj_,
589 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
590 Primitive::kPrimNot,
591 nullptr);
592 if (index.IsValid()) {
593 parallel_move.AddMove(index,
594 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
595 Primitive::kPrimInt,
596 nullptr);
597 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
598 } else {
599 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
600 __ LoadImmediate(calling_convention.GetRegisterAt(2), offset_);
601 }
602 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
603 instruction_,
604 instruction_->GetDexPc(),
605 this);
606 CheckEntrypointTypes<
607 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
608 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
609
610 RestoreLiveRegisters(codegen, locations);
611 __ b(GetExitLabel());
612 }
613
614 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM"; }
615
616 private:
617 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
618 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
619 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
620 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
621 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
622 return static_cast<Register>(i);
623 }
624 }
625 // We shall never fail to find a free caller-save register, as
626 // there are more than two core caller-save registers on ARM
627 // (meaning it is possible to find one which is different from
628 // `ref` and `obj`).
629 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
630 LOG(FATAL) << "Could not find a free caller-save register";
631 UNREACHABLE();
632 }
633
634 HInstruction* const instruction_;
635 const Location out_;
636 const Location ref_;
637 const Location obj_;
638 const uint32_t offset_;
639 // An additional location containing an index to an array.
640 // Only used for HArrayGet and the UnsafeGetObject &
641 // UnsafeGetObjectVolatile intrinsics.
642 const Location index_;
643
644 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM);
645};
646
647// Slow path generating a read barrier for a GC root.
648class ReadBarrierForRootSlowPathARM : public SlowPathCode {
649 public:
650 ReadBarrierForRootSlowPathARM(HInstruction* instruction, Location out, Location root)
Roland Levillainc9285912015-12-18 10:38:42 +0000651 : instruction_(instruction), out_(out), root_(root) {
652 DCHECK(kEmitCompilerReadBarrier);
653 }
Roland Levillain3b359c72015-11-17 19:35:12 +0000654
655 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
656 LocationSummary* locations = instruction_->GetLocations();
657 Register reg_out = out_.AsRegister<Register>();
658 DCHECK(locations->CanCall());
659 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillainc9285912015-12-18 10:38:42 +0000660 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
661 << "Unexpected instruction in read barrier for GC root slow path: "
662 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000663
664 __ Bind(GetEntryLabel());
665 SaveLiveRegisters(codegen, locations);
666
667 InvokeRuntimeCallingConvention calling_convention;
668 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
669 arm_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
670 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
671 instruction_,
672 instruction_->GetDexPc(),
673 this);
674 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
675 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
676
677 RestoreLiveRegisters(codegen, locations);
678 __ b(GetExitLabel());
679 }
680
681 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM"; }
682
683 private:
684 HInstruction* const instruction_;
685 const Location out_;
686 const Location root_;
687
688 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM);
689};
690
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000691#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100692#define __ down_cast<ArmAssembler*>(GetAssembler())->
Dave Allison20dfc792014-06-16 20:44:29 -0700693
Aart Bike9f37602015-10-09 11:15:55 -0700694inline Condition ARMCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700695 switch (cond) {
696 case kCondEQ: return EQ;
697 case kCondNE: return NE;
698 case kCondLT: return LT;
699 case kCondLE: return LE;
700 case kCondGT: return GT;
701 case kCondGE: return GE;
Aart Bike9f37602015-10-09 11:15:55 -0700702 case kCondB: return LO;
703 case kCondBE: return LS;
704 case kCondA: return HI;
705 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -0700706 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100707 LOG(FATAL) << "Unreachable";
708 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700709}
710
Aart Bike9f37602015-10-09 11:15:55 -0700711// Maps signed condition to unsigned condition.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100712inline Condition ARMUnsignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700713 switch (cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100714 case kCondEQ: return EQ;
715 case kCondNE: return NE;
Aart Bike9f37602015-10-09 11:15:55 -0700716 // Signed to unsigned.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100717 case kCondLT: return LO;
718 case kCondLE: return LS;
719 case kCondGT: return HI;
720 case kCondGE: return HS;
Aart Bike9f37602015-10-09 11:15:55 -0700721 // Unsigned remain unchanged.
722 case kCondB: return LO;
723 case kCondBE: return LS;
724 case kCondA: return HI;
725 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -0700726 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100727 LOG(FATAL) << "Unreachable";
728 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700729}
730
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100731void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100732 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100733}
734
735void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100736 stream << SRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100737}
738
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100739size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
740 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
741 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100742}
743
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100744size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
745 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
746 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100747}
748
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000749size_t CodeGeneratorARM::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
750 __ StoreSToOffset(static_cast<SRegister>(reg_id), SP, stack_index);
751 return kArmWordSize;
752}
753
754size_t CodeGeneratorARM::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
755 __ LoadSFromOffset(static_cast<SRegister>(reg_id), SP, stack_index);
756 return kArmWordSize;
757}
758
Calin Juravle34166012014-12-19 17:22:29 +0000759CodeGeneratorARM::CodeGeneratorARM(HGraph* graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000760 const ArmInstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100761 const CompilerOptions& compiler_options,
762 OptimizingCompilerStats* stats)
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000763 : CodeGenerator(graph,
764 kNumberOfCoreRegisters,
765 kNumberOfSRegisters,
766 kNumberOfRegisterPairs,
767 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
768 arraysize(kCoreCalleeSaves)),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000769 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
770 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100771 compiler_options,
772 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100773 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100774 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100775 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100776 move_resolver_(graph->GetArena(), this),
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000777 assembler_(),
Vladimir Marko58155012015-08-19 12:49:41 +0000778 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100779 method_patches_(MethodReferenceComparator(),
780 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
781 call_patches_(MethodReferenceComparator(),
782 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markob4536b72015-11-24 13:45:23 +0000783 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
784 dex_cache_arrays_base_labels_(std::less<HArmDexCacheArraysBase*>(),
785 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Andreas Gampe501fd632015-09-10 16:11:06 -0700786 // Always save the LR register to mimic Quick.
787 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100788}
789
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000790void CodeGeneratorARM::Finalize(CodeAllocator* allocator) {
791 // Ensure that we fix up branches and literal loads and emit the literal pool.
792 __ FinalizeCode();
793
794 // Adjust native pc offsets in stack maps.
795 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
796 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
797 uint32_t new_position = __ GetAdjustedPosition(old_position);
798 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
799 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100800 // Adjust pc offsets for the disassembly information.
801 if (disasm_info_ != nullptr) {
802 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
803 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
804 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
805 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
806 it.second.start = __ GetAdjustedPosition(it.second.start);
807 it.second.end = __ GetAdjustedPosition(it.second.end);
808 }
809 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
810 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
811 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
812 }
813 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000814
815 CodeGenerator::Finalize(allocator);
816}
817
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100818Location CodeGeneratorARM::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100819 switch (type) {
820 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100821 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100822 ArmManagedRegister pair =
823 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100824 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
825 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
826
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100827 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
828 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100829 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100830 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100831 }
832
833 case Primitive::kPrimByte:
834 case Primitive::kPrimBoolean:
835 case Primitive::kPrimChar:
836 case Primitive::kPrimShort:
837 case Primitive::kPrimInt:
838 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100839 int reg = FindFreeEntry(blocked_core_registers_, kNumberOfCoreRegisters);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100840 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100841 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
842 ArmManagedRegister current =
843 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
844 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100845 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100846 }
847 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100848 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100849 }
850
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000851 case Primitive::kPrimFloat: {
852 int reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfSRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100853 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100854 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100855
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000856 case Primitive::kPrimDouble: {
Nicolas Geoffray3c035032014-10-28 10:46:40 +0000857 int reg = FindTwoFreeConsecutiveAlignedEntries(blocked_fpu_registers_, kNumberOfSRegisters);
858 DCHECK_EQ(reg % 2, 0);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000859 return Location::FpuRegisterPairLocation(reg, reg + 1);
860 }
861
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100862 case Primitive::kPrimVoid:
863 LOG(FATAL) << "Unreachable type " << type;
864 }
865
Roland Levillain3b359c72015-11-17 19:35:12 +0000866 return Location::NoLocation();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100867}
868
Nicolas Geoffraya0bb2bd2015-01-26 12:49:35 +0000869void CodeGeneratorARM::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100870 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100871 blocked_register_pairs_[R1_R2] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100872
873 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100874 blocked_core_registers_[SP] = true;
875 blocked_core_registers_[LR] = true;
876 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100877
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100878 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100879 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100880
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100881 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100882 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100883
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000884 if (is_baseline) {
885 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
886 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
887 }
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000888
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000889 blocked_core_registers_[kCoreSavedRegisterForBaseline] = false;
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +0100890 }
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000891
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +0100892 if (is_baseline || GetGraph()->IsDebuggable()) {
893 // Stubs do not save callee-save floating point registers. If the graph
894 // is debuggable, we need to deal with these registers differently. For
895 // now, just block them.
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000896 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
897 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
898 }
899 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100900
901 UpdateBlockedPairRegisters();
902}
903
904void CodeGeneratorARM::UpdateBlockedPairRegisters() const {
905 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
906 ArmManagedRegister current =
907 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
908 if (blocked_core_registers_[current.AsRegisterPairLow()]
909 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
910 blocked_register_pairs_[i] = true;
911 }
912 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100913}
914
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100915InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
916 : HGraphVisitor(graph),
917 assembler_(codegen->GetAssembler()),
918 codegen_(codegen) {}
919
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000920void CodeGeneratorARM::ComputeSpillMask() {
921 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000922 // Save one extra register for baseline. Note that on thumb2, there is no easy
923 // instruction to restore just the PC, so this actually helps both baseline
924 // and non-baseline to save and restore at least two registers at entry and exit.
925 core_spill_mask_ |= (1 << kCoreSavedRegisterForBaseline);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000926 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
927 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
928 // We use vpush and vpop for saving and restoring floating point registers, which take
929 // a SRegister and the number of registers to save/restore after that SRegister. We
930 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
931 // but in the range.
932 if (fpu_spill_mask_ != 0) {
933 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
934 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
935 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
936 fpu_spill_mask_ |= (1 << i);
937 }
938 }
939}
940
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100941static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100942 return dwarf::Reg::ArmCore(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100943}
944
945static dwarf::Reg DWARFReg(SRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100946 return dwarf::Reg::ArmFp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100947}
948
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000949void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +0000950 bool skip_overflow_check =
951 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000952 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000953 __ Bind(&frame_entry_label_);
954
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000955 if (HasEmptyFrame()) {
956 return;
957 }
958
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100959 if (!skip_overflow_check) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000960 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
961 __ LoadFromOffset(kLoadWord, IP, IP, 0);
962 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100963 }
964
Andreas Gampe501fd632015-09-10 16:11:06 -0700965 __ PushList(core_spill_mask_);
966 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
967 __ cfi().RelOffsetForMany(DWARFReg(kMethodRegisterArgument), 0, core_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000968 if (fpu_spill_mask_ != 0) {
969 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
970 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100971 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
David Srbecky9d8606d2015-04-12 09:35:32 +0100972 __ cfi().RelOffsetForMany(DWARFReg(S0), 0, fpu_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000973 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100974 int adjust = GetFrameSize() - FrameEntrySpillSize();
975 __ AddConstant(SP, -adjust);
976 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100977 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, 0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000978}
979
980void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000981 if (HasEmptyFrame()) {
982 __ bx(LR);
983 return;
984 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100985 __ cfi().RememberState();
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100986 int adjust = GetFrameSize() - FrameEntrySpillSize();
987 __ AddConstant(SP, adjust);
988 __ cfi().AdjustCFAOffset(-adjust);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000989 if (fpu_spill_mask_ != 0) {
990 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
991 __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100992 __ cfi().AdjustCFAOffset(-kArmPointerSize * POPCOUNT(fpu_spill_mask_));
993 __ cfi().RestoreMany(DWARFReg(SRegister(0)), fpu_spill_mask_);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000994 }
Andreas Gampe501fd632015-09-10 16:11:06 -0700995 // Pop LR into PC to return.
996 DCHECK_NE(core_spill_mask_ & (1 << LR), 0U);
997 uint32_t pop_mask = (core_spill_mask_ & (~(1 << LR))) | 1 << PC;
998 __ PopList(pop_mask);
David Srbeckyc34dc932015-04-12 09:27:43 +0100999 __ cfi().RestoreState();
1000 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001001}
1002
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001003void CodeGeneratorARM::Bind(HBasicBlock* block) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07001004 Label* label = GetLabelOf(block);
1005 __ BindTrackedLabel(label);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001006}
1007
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001008Location CodeGeneratorARM::GetStackLocation(HLoadLocal* load) const {
1009 switch (load->GetType()) {
1010 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001011 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001012 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001013
1014 case Primitive::kPrimInt:
1015 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001016 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001017 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001018
1019 case Primitive::kPrimBoolean:
1020 case Primitive::kPrimByte:
1021 case Primitive::kPrimChar:
1022 case Primitive::kPrimShort:
1023 case Primitive::kPrimVoid:
1024 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -07001025 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001026 }
1027
1028 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -07001029 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001030}
1031
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001032Location InvokeDexCallingConventionVisitorARM::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001033 switch (type) {
1034 case Primitive::kPrimBoolean:
1035 case Primitive::kPrimByte:
1036 case Primitive::kPrimChar:
1037 case Primitive::kPrimShort:
1038 case Primitive::kPrimInt:
1039 case Primitive::kPrimNot: {
1040 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001041 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001042 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001043 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001044 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001045 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001046 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001047 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001048
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001049 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001050 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001051 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001052 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001053 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001054 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001055 if (calling_convention.GetRegisterAt(index) == R1) {
1056 // Skip R1, and use R2_R3 instead.
1057 gp_index_++;
1058 index++;
1059 }
1060 }
1061 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
1062 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001063 calling_convention.GetRegisterAt(index + 1));
Calin Juravle175dc732015-08-25 15:42:32 +01001064
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001065 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001066 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001067 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001068 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
1069 }
1070 }
1071
1072 case Primitive::kPrimFloat: {
1073 uint32_t stack_index = stack_index_++;
1074 if (float_index_ % 2 == 0) {
1075 float_index_ = std::max(double_index_, float_index_);
1076 }
1077 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
1078 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
1079 } else {
1080 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
1081 }
1082 }
1083
1084 case Primitive::kPrimDouble: {
1085 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
1086 uint32_t stack_index = stack_index_;
1087 stack_index_ += 2;
1088 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
1089 uint32_t index = double_index_;
1090 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001091 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001092 calling_convention.GetFpuRegisterAt(index),
1093 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001094 DCHECK(ExpectedPairLayout(result));
1095 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001096 } else {
1097 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001098 }
1099 }
1100
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001101 case Primitive::kPrimVoid:
1102 LOG(FATAL) << "Unexpected parameter type " << type;
1103 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001104 }
Roland Levillain3b359c72015-11-17 19:35:12 +00001105 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001106}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001107
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001108Location InvokeDexCallingConventionVisitorARM::GetReturnLocation(Primitive::Type type) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001109 switch (type) {
1110 case Primitive::kPrimBoolean:
1111 case Primitive::kPrimByte:
1112 case Primitive::kPrimChar:
1113 case Primitive::kPrimShort:
1114 case Primitive::kPrimInt:
1115 case Primitive::kPrimNot: {
1116 return Location::RegisterLocation(R0);
1117 }
1118
1119 case Primitive::kPrimFloat: {
1120 return Location::FpuRegisterLocation(S0);
1121 }
1122
1123 case Primitive::kPrimLong: {
1124 return Location::RegisterPairLocation(R0, R1);
1125 }
1126
1127 case Primitive::kPrimDouble: {
1128 return Location::FpuRegisterPairLocation(S0, S1);
1129 }
1130
1131 case Primitive::kPrimVoid:
Roland Levillain3b359c72015-11-17 19:35:12 +00001132 return Location::NoLocation();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001133 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001134
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001135 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001136}
1137
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001138Location InvokeDexCallingConventionVisitorARM::GetMethodLocation() const {
1139 return Location::RegisterLocation(kMethodRegisterArgument);
1140}
1141
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001142void CodeGeneratorARM::Move32(Location destination, Location source) {
1143 if (source.Equals(destination)) {
1144 return;
1145 }
1146 if (destination.IsRegister()) {
1147 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001148 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001149 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001150 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001151 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001152 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001153 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001154 } else if (destination.IsFpuRegister()) {
1155 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001156 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001157 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001158 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001159 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001160 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001161 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001162 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001163 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001164 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001165 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001166 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001167 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001168 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001169 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001170 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
1171 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001172 }
1173 }
1174}
1175
1176void CodeGeneratorARM::Move64(Location destination, Location source) {
1177 if (source.Equals(destination)) {
1178 return;
1179 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001180 if (destination.IsRegisterPair()) {
1181 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001182 EmitParallelMoves(
1183 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1184 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001185 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001186 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001187 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1188 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001189 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001190 UNIMPLEMENTED(FATAL);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001191 } else if (source.IsFpuRegisterPair()) {
1192 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
1193 destination.AsRegisterPairHigh<Register>(),
1194 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001195 } else {
1196 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001197 DCHECK(ExpectedPairLayout(destination));
1198 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
1199 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001200 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001201 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001202 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001203 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1204 SP,
1205 source.GetStackIndex());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001206 } else if (source.IsRegisterPair()) {
1207 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1208 source.AsRegisterPairLow<Register>(),
1209 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001210 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001211 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001212 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001213 } else {
1214 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001215 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001216 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001217 if (source.AsRegisterPairLow<Register>() == R1) {
1218 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001219 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
1220 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001221 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001222 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001223 SP, destination.GetStackIndex());
1224 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001225 } else if (source.IsFpuRegisterPair()) {
1226 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
1227 SP,
1228 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001229 } else {
1230 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001231 EmitParallelMoves(
1232 Location::StackSlot(source.GetStackIndex()),
1233 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001234 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001235 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001236 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)),
1237 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001238 }
1239 }
1240}
1241
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001242void CodeGeneratorARM::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001243 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001244 if (instruction->IsCurrentMethod()) {
1245 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
1246 } else if (locations != nullptr && locations->Out().Equals(location)) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001247 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001248 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +00001249 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001250 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
1251 int32_t value = GetInt32ValueOf(const_to_move);
Calin Juravlea21f5982014-11-13 15:53:04 +00001252 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001253 __ LoadImmediate(location.AsRegister<Register>(), value);
Calin Juravlea21f5982014-11-13 15:53:04 +00001254 } else {
1255 DCHECK(location.IsStackSlot());
1256 __ LoadImmediate(IP, value);
1257 __ StoreToOffset(kStoreWord, IP, SP, location.GetStackIndex());
1258 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001259 } else {
Nicolas Geoffray3747b482015-01-19 17:17:16 +00001260 DCHECK(const_to_move->IsLongConstant()) << const_to_move->DebugName();
Calin Juravlea21f5982014-11-13 15:53:04 +00001261 int64_t value = const_to_move->AsLongConstant()->GetValue();
1262 if (location.IsRegisterPair()) {
1263 __ LoadImmediate(location.AsRegisterPairLow<Register>(), Low32Bits(value));
1264 __ LoadImmediate(location.AsRegisterPairHigh<Register>(), High32Bits(value));
1265 } else {
1266 DCHECK(location.IsDoubleStackSlot());
1267 __ LoadImmediate(IP, Low32Bits(value));
1268 __ StoreToOffset(kStoreWord, IP, SP, location.GetStackIndex());
1269 __ LoadImmediate(IP, High32Bits(value));
1270 __ StoreToOffset(kStoreWord, IP, SP, location.GetHighStackIndex(kArmWordSize));
1271 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001272 }
Roland Levillain476df552014-10-09 17:51:36 +01001273 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001274 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
1275 switch (instruction->GetType()) {
1276 case Primitive::kPrimBoolean:
1277 case Primitive::kPrimByte:
1278 case Primitive::kPrimChar:
1279 case Primitive::kPrimShort:
1280 case Primitive::kPrimInt:
1281 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001282 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001283 Move32(location, Location::StackSlot(stack_slot));
1284 break;
1285
1286 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001287 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001288 Move64(location, Location::DoubleStackSlot(stack_slot));
1289 break;
1290
1291 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001292 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001293 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00001294 } else if (instruction->IsTemporary()) {
1295 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001296 if (temp_location.IsStackSlot()) {
1297 Move32(location, temp_location);
1298 } else {
1299 DCHECK(temp_location.IsDoubleStackSlot());
1300 Move64(location, temp_location);
1301 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001302 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001303 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001304 switch (instruction->GetType()) {
1305 case Primitive::kPrimBoolean:
1306 case Primitive::kPrimByte:
1307 case Primitive::kPrimChar:
1308 case Primitive::kPrimShort:
1309 case Primitive::kPrimNot:
1310 case Primitive::kPrimInt:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001311 case Primitive::kPrimFloat:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001312 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001313 break;
1314
1315 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001316 case Primitive::kPrimDouble:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001317 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001318 break;
1319
1320 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001321 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001322 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001323 }
1324}
1325
Calin Juravle175dc732015-08-25 15:42:32 +01001326void CodeGeneratorARM::MoveConstant(Location location, int32_t value) {
1327 DCHECK(location.IsRegister());
1328 __ LoadImmediate(location.AsRegister<Register>(), value);
1329}
1330
Calin Juravlee460d1d2015-09-29 04:52:17 +01001331void CodeGeneratorARM::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
1332 if (Primitive::Is64BitType(dst_type)) {
1333 Move64(dst, src);
1334 } else {
1335 Move32(dst, src);
1336 }
1337}
1338
1339void CodeGeneratorARM::AddLocationAsTemp(Location location, LocationSummary* locations) {
1340 if (location.IsRegister()) {
1341 locations->AddTemp(location);
1342 } else if (location.IsRegisterPair()) {
1343 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1344 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1345 } else {
1346 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1347 }
1348}
1349
Calin Juravle175dc732015-08-25 15:42:32 +01001350void CodeGeneratorARM::InvokeRuntime(QuickEntrypointEnum entrypoint,
1351 HInstruction* instruction,
1352 uint32_t dex_pc,
1353 SlowPathCode* slow_path) {
1354 InvokeRuntime(GetThreadOffset<kArmWordSize>(entrypoint).Int32Value(),
1355 instruction,
1356 dex_pc,
1357 slow_path);
1358}
1359
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001360void CodeGeneratorARM::InvokeRuntime(int32_t entry_point_offset,
1361 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001362 uint32_t dex_pc,
1363 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001364 ValidateInvokeRuntime(instruction, slow_path);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001365 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
1366 __ blx(LR);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001367 RecordPcInfo(instruction, dex_pc, slow_path);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001368}
1369
David Brazdilfc6a86a2015-06-26 10:33:45 +00001370void InstructionCodeGeneratorARM::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001371 DCHECK(!successor->IsExitBlock());
1372
1373 HBasicBlock* block = got->GetBlock();
1374 HInstruction* previous = got->GetPrevious();
1375
1376 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001377 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001378 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1379 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1380 return;
1381 }
1382
1383 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1384 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1385 }
1386 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001387 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001388 }
1389}
1390
David Brazdilfc6a86a2015-06-26 10:33:45 +00001391void LocationsBuilderARM::VisitGoto(HGoto* got) {
1392 got->SetLocations(nullptr);
1393}
1394
1395void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
1396 HandleGoto(got, got->GetSuccessor());
1397}
1398
1399void LocationsBuilderARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1400 try_boundary->SetLocations(nullptr);
1401}
1402
1403void InstructionCodeGeneratorARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1404 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1405 if (!successor->IsExitBlock()) {
1406 HandleGoto(try_boundary, successor);
1407 }
1408}
1409
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001410void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001411 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001412}
1413
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001414void InstructionCodeGeneratorARM::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001415}
1416
Roland Levillain4fa13f62015-07-06 18:11:54 +01001417void InstructionCodeGeneratorARM::GenerateCompareWithImmediate(Register left, int32_t right) {
1418 ShifterOperand operand;
1419 if (GetAssembler()->ShifterOperandCanHold(R0, left, CMP, right, &operand)) {
1420 __ cmp(left, operand);
1421 } else {
1422 Register temp = IP;
1423 __ LoadImmediate(temp, right);
1424 __ cmp(left, ShifterOperand(temp));
1425 }
1426}
1427
1428void InstructionCodeGeneratorARM::GenerateFPJumps(HCondition* cond,
1429 Label* true_label,
1430 Label* false_label) {
1431 __ vmstat(); // transfer FP status register to ARM APSR.
Aart Bike9f37602015-10-09 11:15:55 -07001432 // TODO: merge into a single branch (except "equal or unordered" and "not equal")
Roland Levillain4fa13f62015-07-06 18:11:54 +01001433 if (cond->IsFPConditionTrueIfNaN()) {
1434 __ b(true_label, VS); // VS for unordered.
1435 } else if (cond->IsFPConditionFalseIfNaN()) {
1436 __ b(false_label, VS); // VS for unordered.
1437 }
Aart Bike9f37602015-10-09 11:15:55 -07001438 __ b(true_label, ARMCondition(cond->GetCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001439}
1440
1441void InstructionCodeGeneratorARM::GenerateLongComparesAndJumps(HCondition* cond,
1442 Label* true_label,
1443 Label* false_label) {
1444 LocationSummary* locations = cond->GetLocations();
1445 Location left = locations->InAt(0);
1446 Location right = locations->InAt(1);
1447 IfCondition if_cond = cond->GetCondition();
1448
1449 Register left_high = left.AsRegisterPairHigh<Register>();
1450 Register left_low = left.AsRegisterPairLow<Register>();
1451 IfCondition true_high_cond = if_cond;
1452 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001453 Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
Roland Levillain4fa13f62015-07-06 18:11:54 +01001454
1455 // Set the conditions for the test, remembering that == needs to be
1456 // decided using the low words.
Aart Bike9f37602015-10-09 11:15:55 -07001457 // TODO: consider avoiding jumps with temporary and CMP low+SBC high
Roland Levillain4fa13f62015-07-06 18:11:54 +01001458 switch (if_cond) {
1459 case kCondEQ:
1460 case kCondNE:
1461 // Nothing to do.
1462 break;
1463 case kCondLT:
1464 false_high_cond = kCondGT;
1465 break;
1466 case kCondLE:
1467 true_high_cond = kCondLT;
1468 break;
1469 case kCondGT:
1470 false_high_cond = kCondLT;
1471 break;
1472 case kCondGE:
1473 true_high_cond = kCondGT;
1474 break;
Aart Bike9f37602015-10-09 11:15:55 -07001475 case kCondB:
1476 false_high_cond = kCondA;
1477 break;
1478 case kCondBE:
1479 true_high_cond = kCondB;
1480 break;
1481 case kCondA:
1482 false_high_cond = kCondB;
1483 break;
1484 case kCondAE:
1485 true_high_cond = kCondA;
1486 break;
Roland Levillain4fa13f62015-07-06 18:11:54 +01001487 }
1488 if (right.IsConstant()) {
1489 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1490 int32_t val_low = Low32Bits(value);
1491 int32_t val_high = High32Bits(value);
1492
1493 GenerateCompareWithImmediate(left_high, val_high);
1494 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001495 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001496 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001497 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001498 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001499 __ b(true_label, ARMCondition(true_high_cond));
1500 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001501 }
1502 // Must be equal high, so compare the lows.
1503 GenerateCompareWithImmediate(left_low, val_low);
1504 } else {
1505 Register right_high = right.AsRegisterPairHigh<Register>();
1506 Register right_low = right.AsRegisterPairLow<Register>();
1507
1508 __ cmp(left_high, ShifterOperand(right_high));
1509 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001510 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001511 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001512 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001513 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001514 __ b(true_label, ARMCondition(true_high_cond));
1515 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001516 }
1517 // Must be equal high, so compare the lows.
1518 __ cmp(left_low, ShifterOperand(right_low));
1519 }
1520 // The last comparison might be unsigned.
Aart Bike9f37602015-10-09 11:15:55 -07001521 // TODO: optimize cases where this is always true/false
Roland Levillain4fa13f62015-07-06 18:11:54 +01001522 __ b(true_label, final_condition);
1523}
1524
David Brazdil0debae72015-11-12 18:37:00 +00001525void InstructionCodeGeneratorARM::GenerateCompareTestAndBranch(HCondition* condition,
1526 Label* true_target_in,
1527 Label* false_target_in) {
1528 // Generated branching requires both targets to be explicit. If either of the
1529 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1530 Label fallthrough_target;
1531 Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1532 Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1533
Roland Levillain4fa13f62015-07-06 18:11:54 +01001534 LocationSummary* locations = condition->GetLocations();
1535 Location left = locations->InAt(0);
1536 Location right = locations->InAt(1);
1537
Roland Levillain4fa13f62015-07-06 18:11:54 +01001538 Primitive::Type type = condition->InputAt(0)->GetType();
1539 switch (type) {
1540 case Primitive::kPrimLong:
1541 GenerateLongComparesAndJumps(condition, true_target, false_target);
1542 break;
1543 case Primitive::kPrimFloat:
1544 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
1545 GenerateFPJumps(condition, true_target, false_target);
1546 break;
1547 case Primitive::kPrimDouble:
1548 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
1549 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
1550 GenerateFPJumps(condition, true_target, false_target);
1551 break;
1552 default:
1553 LOG(FATAL) << "Unexpected compare type " << type;
1554 }
1555
David Brazdil0debae72015-11-12 18:37:00 +00001556 if (false_target != &fallthrough_target) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001557 __ b(false_target);
1558 }
David Brazdil0debae72015-11-12 18:37:00 +00001559
1560 if (fallthrough_target.IsLinked()) {
1561 __ Bind(&fallthrough_target);
1562 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001563}
1564
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001565void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001566 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001567 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00001568 Label* false_target) {
1569 HInstruction* cond = instruction->InputAt(condition_input_index);
1570
1571 if (true_target == nullptr && false_target == nullptr) {
1572 // Nothing to do. The code always falls through.
1573 return;
1574 } else if (cond->IsIntConstant()) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001575 // Constant condition, statically compared against 1.
David Brazdil0debae72015-11-12 18:37:00 +00001576 if (cond->AsIntConstant()->IsOne()) {
1577 if (true_target != nullptr) {
1578 __ b(true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001579 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001580 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001581 DCHECK(cond->AsIntConstant()->IsZero());
1582 if (false_target != nullptr) {
1583 __ b(false_target);
1584 }
1585 }
1586 return;
1587 }
1588
1589 // The following code generates these patterns:
1590 // (1) true_target == nullptr && false_target != nullptr
1591 // - opposite condition true => branch to false_target
1592 // (2) true_target != nullptr && false_target == nullptr
1593 // - condition true => branch to true_target
1594 // (3) true_target != nullptr && false_target != nullptr
1595 // - condition true => branch to true_target
1596 // - branch to false_target
1597 if (IsBooleanValueOrMaterializedCondition(cond)) {
1598 // Condition has been materialized, compare the output to 0.
1599 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
1600 DCHECK(cond_val.IsRegister());
1601 if (true_target == nullptr) {
1602 __ CompareAndBranchIfZero(cond_val.AsRegister<Register>(), false_target);
1603 } else {
1604 __ CompareAndBranchIfNonZero(cond_val.AsRegister<Register>(), true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001605 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001606 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001607 // Condition has not been materialized. Use its inputs as the comparison and
1608 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001609 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001610
1611 // If this is a long or FP comparison that has been folded into
1612 // the HCondition, generate the comparison directly.
1613 Primitive::Type type = condition->InputAt(0)->GetType();
1614 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1615 GenerateCompareTestAndBranch(condition, true_target, false_target);
1616 return;
1617 }
1618
1619 LocationSummary* locations = cond->GetLocations();
1620 DCHECK(locations->InAt(0).IsRegister());
1621 Register left = locations->InAt(0).AsRegister<Register>();
1622 Location right = locations->InAt(1);
1623 if (right.IsRegister()) {
1624 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001625 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001626 DCHECK(right.IsConstant());
1627 GenerateCompareWithImmediate(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
1628 }
1629 if (true_target == nullptr) {
1630 __ b(false_target, ARMCondition(condition->GetOppositeCondition()));
1631 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001632 __ b(true_target, ARMCondition(condition->GetCondition()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001633 }
Dave Allison20dfc792014-06-16 20:44:29 -07001634 }
David Brazdil0debae72015-11-12 18:37:00 +00001635
1636 // If neither branch falls through (case 3), the conditional branch to `true_target`
1637 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1638 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001639 __ b(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001640 }
1641}
1642
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001643void LocationsBuilderARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001644 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1645 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001646 locations->SetInAt(0, Location::RequiresRegister());
1647 }
1648}
1649
1650void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001651 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1652 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1653 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1654 nullptr : codegen_->GetLabelOf(true_successor);
1655 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1656 nullptr : codegen_->GetLabelOf(false_successor);
1657 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001658}
1659
1660void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
1661 LocationSummary* locations = new (GetGraph()->GetArena())
1662 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001663 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001664 locations->SetInAt(0, Location::RequiresRegister());
1665 }
1666}
1667
1668void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) {
David Brazdil0debae72015-11-12 18:37:00 +00001669 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DeoptimizationSlowPathARM(deoptimize);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001670 codegen_->AddSlowPath(slow_path);
David Brazdil0debae72015-11-12 18:37:00 +00001671 GenerateTestAndBranch(deoptimize,
1672 /* condition_input_index */ 0,
1673 slow_path->GetEntryLabel(),
1674 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001675}
Dave Allison20dfc792014-06-16 20:44:29 -07001676
David Srbecky0cf44932015-12-09 14:09:59 +00001677void LocationsBuilderARM::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1678 new (GetGraph()->GetArena()) LocationSummary(info);
1679}
1680
1681void InstructionCodeGeneratorARM::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1682 codegen_->RecordPcInfo(info, info->GetDexPc());
1683}
1684
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001685void LocationsBuilderARM::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001686 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001687 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001688 // Handle the long/FP comparisons made in instruction simplification.
1689 switch (cond->InputAt(0)->GetType()) {
1690 case Primitive::kPrimLong:
1691 locations->SetInAt(0, Location::RequiresRegister());
1692 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1693 if (cond->NeedsMaterialization()) {
1694 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1695 }
1696 break;
1697
1698 case Primitive::kPrimFloat:
1699 case Primitive::kPrimDouble:
1700 locations->SetInAt(0, Location::RequiresFpuRegister());
1701 locations->SetInAt(1, Location::RequiresFpuRegister());
1702 if (cond->NeedsMaterialization()) {
1703 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1704 }
1705 break;
1706
1707 default:
1708 locations->SetInAt(0, Location::RequiresRegister());
1709 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1710 if (cond->NeedsMaterialization()) {
1711 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1712 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001713 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001714}
1715
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001716void InstructionCodeGeneratorARM::HandleCondition(HCondition* cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001717 if (!cond->NeedsMaterialization()) {
1718 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001719 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001720
1721 LocationSummary* locations = cond->GetLocations();
1722 Location left = locations->InAt(0);
1723 Location right = locations->InAt(1);
1724 Register out = locations->Out().AsRegister<Register>();
1725 Label true_label, false_label;
1726
1727 switch (cond->InputAt(0)->GetType()) {
1728 default: {
1729 // Integer case.
1730 if (right.IsRegister()) {
1731 __ cmp(left.AsRegister<Register>(), ShifterOperand(right.AsRegister<Register>()));
1732 } else {
1733 DCHECK(right.IsConstant());
1734 GenerateCompareWithImmediate(left.AsRegister<Register>(),
1735 CodeGenerator::GetInt32ValueOf(right.GetConstant()));
1736 }
Aart Bike9f37602015-10-09 11:15:55 -07001737 __ it(ARMCondition(cond->GetCondition()), kItElse);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001738 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(1),
Aart Bike9f37602015-10-09 11:15:55 -07001739 ARMCondition(cond->GetCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001740 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(0),
Aart Bike9f37602015-10-09 11:15:55 -07001741 ARMCondition(cond->GetOppositeCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001742 return;
1743 }
1744 case Primitive::kPrimLong:
1745 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1746 break;
1747 case Primitive::kPrimFloat:
1748 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
1749 GenerateFPJumps(cond, &true_label, &false_label);
1750 break;
1751 case Primitive::kPrimDouble:
1752 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
1753 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
1754 GenerateFPJumps(cond, &true_label, &false_label);
1755 break;
1756 }
1757
1758 // Convert the jumps into the result.
1759 Label done_label;
1760
1761 // False case: result = 0.
1762 __ Bind(&false_label);
1763 __ LoadImmediate(out, 0);
1764 __ b(&done_label);
1765
1766 // True case: result = 1.
1767 __ Bind(&true_label);
1768 __ LoadImmediate(out, 1);
1769 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001770}
1771
1772void LocationsBuilderARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001773 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001774}
1775
1776void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001777 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001778}
1779
1780void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001781 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001782}
1783
1784void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001785 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001786}
1787
1788void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001789 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001790}
1791
1792void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001793 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001794}
1795
1796void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001797 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001798}
1799
1800void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001801 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001802}
1803
1804void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001805 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001806}
1807
1808void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001809 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001810}
1811
1812void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001813 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001814}
1815
1816void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001817 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001818}
1819
Aart Bike9f37602015-10-09 11:15:55 -07001820void LocationsBuilderARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001821 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001822}
1823
1824void InstructionCodeGeneratorARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001825 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001826}
1827
1828void LocationsBuilderARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001829 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001830}
1831
1832void InstructionCodeGeneratorARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001833 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001834}
1835
1836void LocationsBuilderARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001837 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001838}
1839
1840void InstructionCodeGeneratorARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001841 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001842}
1843
1844void LocationsBuilderARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001845 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001846}
1847
1848void InstructionCodeGeneratorARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001849 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001850}
1851
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001852void LocationsBuilderARM::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001853 local->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001854}
1855
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001856void InstructionCodeGeneratorARM::VisitLocal(HLocal* local) {
1857 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001858}
1859
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001860void LocationsBuilderARM::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001861 load->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001862}
1863
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001864void InstructionCodeGeneratorARM::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001865 // Nothing to do, this is driven by the code generator.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001866}
1867
1868void LocationsBuilderARM::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001869 LocationSummary* locations =
1870 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001871 switch (store->InputAt(1)->GetType()) {
1872 case Primitive::kPrimBoolean:
1873 case Primitive::kPrimByte:
1874 case Primitive::kPrimChar:
1875 case Primitive::kPrimShort:
1876 case Primitive::kPrimInt:
1877 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001878 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001879 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1880 break;
1881
1882 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001883 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001884 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1885 break;
1886
1887 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001888 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001889 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001890}
1891
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001892void InstructionCodeGeneratorARM::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001893}
1894
1895void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001896 LocationSummary* locations =
1897 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001898 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001899}
1900
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001901void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001902 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001903}
1904
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001905void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
1906 LocationSummary* locations =
1907 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1908 locations->SetOut(Location::ConstantLocation(constant));
1909}
1910
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001911void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001912 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001913}
1914
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001915void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001916 LocationSummary* locations =
1917 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001918 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001919}
1920
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001921void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001922 // Will be generated at use site.
1923}
1924
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001925void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
1926 LocationSummary* locations =
1927 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1928 locations->SetOut(Location::ConstantLocation(constant));
1929}
1930
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001931void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001932 // Will be generated at use site.
1933}
1934
1935void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
1936 LocationSummary* locations =
1937 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1938 locations->SetOut(Location::ConstantLocation(constant));
1939}
1940
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001941void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001942 // Will be generated at use site.
1943}
1944
Calin Juravle27df7582015-04-17 19:12:31 +01001945void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1946 memory_barrier->SetLocations(nullptr);
1947}
1948
1949void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00001950 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001951}
1952
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001953void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001954 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001955}
1956
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001957void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001958 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001959}
1960
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001961void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001962 LocationSummary* locations =
1963 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001964 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001965}
1966
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001967void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001968 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001969}
1970
Calin Juravle175dc732015-08-25 15:42:32 +01001971void LocationsBuilderARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1972 // The trampoline uses the same calling convention as dex calling conventions,
1973 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1974 // the method_idx.
1975 HandleInvoke(invoke);
1976}
1977
1978void InstructionCodeGeneratorARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1979 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1980}
1981
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001982void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001983 // When we do not run baseline, explicit clinit checks triggered by static
1984 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1985 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001986
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001987 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001988 codegen_->GetAssembler(),
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001989 codegen_->GetInstructionSetFeatures());
1990 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markob4536b72015-11-24 13:45:23 +00001991 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
1992 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
1993 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001994 return;
1995 }
1996
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001997 HandleInvoke(invoke);
Vladimir Markob4536b72015-11-24 13:45:23 +00001998
1999 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
2000 if (invoke->HasPcRelativeDexCache()) {
2001 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
2002 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002003}
2004
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002005static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
2006 if (invoke->GetLocations()->Intrinsified()) {
2007 IntrinsicCodeGeneratorARM intrinsic(codegen);
2008 intrinsic.Dispatch(invoke);
2009 return true;
2010 }
2011 return false;
2012}
2013
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002014void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002015 // When we do not run baseline, explicit clinit checks triggered by static
2016 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2017 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002018
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002019 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2020 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002021 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002022
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002023 LocationSummary* locations = invoke->GetLocations();
2024 codegen_->GenerateStaticOrDirectCall(
2025 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002026 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002027}
2028
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002029void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002030 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002031 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002032}
2033
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002034void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002035 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01002036 codegen_->GetAssembler(),
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002037 codegen_->GetInstructionSetFeatures());
2038 if (intrinsic.TryDispatch(invoke)) {
2039 return;
2040 }
2041
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002042 HandleInvoke(invoke);
2043}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002044
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002045void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002046 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2047 return;
2048 }
2049
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002050 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002051 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002052 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002053}
2054
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002055void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
2056 HandleInvoke(invoke);
2057 // Add the hidden argument.
2058 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
2059}
2060
2061void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
2062 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain3b359c72015-11-17 19:35:12 +00002063 LocationSummary* locations = invoke->GetLocations();
2064 Register temp = locations->GetTemp(0).AsRegister<Register>();
2065 Register hidden_reg = locations->GetTemp(1).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002066 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2067 invoke->GetImtIndex() % mirror::Class::kImtSize, kArmPointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002068 Location receiver = locations->InAt(0);
2069 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2070
Roland Levillain3b359c72015-11-17 19:35:12 +00002071 // Set the hidden argument. This is safe to do this here, as R12
2072 // won't be modified thereafter, before the `blx` (call) instruction.
2073 DCHECK_EQ(R12, hidden_reg);
2074 __ LoadImmediate(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002075
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002076 if (receiver.IsStackSlot()) {
2077 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
Roland Levillain3b359c72015-11-17 19:35:12 +00002078 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002079 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
2080 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00002081 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002082 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002083 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002084 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00002085 // Instead of simply (possibly) unpoisoning `temp` here, we should
2086 // emit a read barrier for the previous class reference load.
2087 // However this is not required in practice, as this is an
2088 // intermediate/temporary reference and because the current
2089 // concurrent copying collector keeps the from-space memory
2090 // intact/accessible until the end of the marking phase (the
2091 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002092 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002093 // temp = temp->GetImtEntryAt(method_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00002094 uint32_t entry_point =
2095 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002096 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
2097 // LR = temp->GetEntryPoint();
2098 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
2099 // LR();
2100 __ blx(LR);
2101 DCHECK(!codegen_->IsLeafMethod());
2102 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2103}
2104
Roland Levillain88cb1752014-10-20 16:36:47 +01002105void LocationsBuilderARM::VisitNeg(HNeg* neg) {
2106 LocationSummary* locations =
2107 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2108 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002109 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01002110 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002111 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2112 break;
2113 }
2114 case Primitive::kPrimLong: {
2115 locations->SetInAt(0, Location::RequiresRegister());
2116 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01002117 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002118 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002119
Roland Levillain88cb1752014-10-20 16:36:47 +01002120 case Primitive::kPrimFloat:
2121 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002122 locations->SetInAt(0, Location::RequiresFpuRegister());
2123 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01002124 break;
2125
2126 default:
2127 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2128 }
2129}
2130
2131void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
2132 LocationSummary* locations = neg->GetLocations();
2133 Location out = locations->Out();
2134 Location in = locations->InAt(0);
2135 switch (neg->GetResultType()) {
2136 case Primitive::kPrimInt:
2137 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002138 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01002139 break;
2140
2141 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002142 DCHECK(in.IsRegisterPair());
2143 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
2144 __ rsbs(out.AsRegisterPairLow<Register>(),
2145 in.AsRegisterPairLow<Register>(),
2146 ShifterOperand(0));
2147 // We cannot emit an RSC (Reverse Subtract with Carry)
2148 // instruction here, as it does not exist in the Thumb-2
2149 // instruction set. We use the following approach
2150 // using SBC and SUB instead.
2151 //
2152 // out.hi = -C
2153 __ sbc(out.AsRegisterPairHigh<Register>(),
2154 out.AsRegisterPairHigh<Register>(),
2155 ShifterOperand(out.AsRegisterPairHigh<Register>()));
2156 // out.hi = out.hi - in.hi
2157 __ sub(out.AsRegisterPairHigh<Register>(),
2158 out.AsRegisterPairHigh<Register>(),
2159 ShifterOperand(in.AsRegisterPairHigh<Register>()));
2160 break;
2161
Roland Levillain88cb1752014-10-20 16:36:47 +01002162 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002163 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002164 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002165 break;
2166
Roland Levillain88cb1752014-10-20 16:36:47 +01002167 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002168 DCHECK(in.IsFpuRegisterPair());
2169 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2170 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01002171 break;
2172
2173 default:
2174 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2175 }
2176}
2177
Roland Levillaindff1f282014-11-05 14:15:05 +00002178void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002179 Primitive::Type result_type = conversion->GetResultType();
2180 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002181 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002182
Roland Levillain5b3ee562015-04-14 16:02:41 +01002183 // The float-to-long, double-to-long and long-to-float type conversions
2184 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002185 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01002186 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2187 && result_type == Primitive::kPrimLong)
2188 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Roland Levillain624279f2014-12-04 11:54:28 +00002189 ? LocationSummary::kCall
2190 : LocationSummary::kNoCall;
2191 LocationSummary* locations =
2192 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2193
David Brazdilb2bd1c52015-03-25 11:17:37 +00002194 // The Java language does not allow treating boolean as an integral type but
2195 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002196
Roland Levillaindff1f282014-11-05 14:15:05 +00002197 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002198 case Primitive::kPrimByte:
2199 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002200 case Primitive::kPrimBoolean:
2201 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002202 case Primitive::kPrimShort:
2203 case Primitive::kPrimInt:
2204 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002205 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002206 locations->SetInAt(0, Location::RequiresRegister());
2207 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2208 break;
2209
2210 default:
2211 LOG(FATAL) << "Unexpected type conversion from " << input_type
2212 << " to " << result_type;
2213 }
2214 break;
2215
Roland Levillain01a8d712014-11-14 16:27:39 +00002216 case Primitive::kPrimShort:
2217 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002218 case Primitive::kPrimBoolean:
2219 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002220 case Primitive::kPrimByte:
2221 case Primitive::kPrimInt:
2222 case Primitive::kPrimChar:
2223 // Processing a Dex `int-to-short' instruction.
2224 locations->SetInAt(0, Location::RequiresRegister());
2225 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2226 break;
2227
2228 default:
2229 LOG(FATAL) << "Unexpected type conversion from " << input_type
2230 << " to " << result_type;
2231 }
2232 break;
2233
Roland Levillain946e1432014-11-11 17:35:19 +00002234 case Primitive::kPrimInt:
2235 switch (input_type) {
2236 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002237 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002238 locations->SetInAt(0, Location::Any());
2239 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2240 break;
2241
2242 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002243 // Processing a Dex `float-to-int' instruction.
2244 locations->SetInAt(0, Location::RequiresFpuRegister());
2245 locations->SetOut(Location::RequiresRegister());
2246 locations->AddTemp(Location::RequiresFpuRegister());
2247 break;
2248
Roland Levillain946e1432014-11-11 17:35:19 +00002249 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002250 // Processing a Dex `double-to-int' instruction.
2251 locations->SetInAt(0, Location::RequiresFpuRegister());
2252 locations->SetOut(Location::RequiresRegister());
2253 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002254 break;
2255
2256 default:
2257 LOG(FATAL) << "Unexpected type conversion from " << input_type
2258 << " to " << result_type;
2259 }
2260 break;
2261
Roland Levillaindff1f282014-11-05 14:15:05 +00002262 case Primitive::kPrimLong:
2263 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002264 case Primitive::kPrimBoolean:
2265 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002266 case Primitive::kPrimByte:
2267 case Primitive::kPrimShort:
2268 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002269 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002270 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002271 locations->SetInAt(0, Location::RequiresRegister());
2272 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2273 break;
2274
Roland Levillain624279f2014-12-04 11:54:28 +00002275 case Primitive::kPrimFloat: {
2276 // Processing a Dex `float-to-long' instruction.
2277 InvokeRuntimeCallingConvention calling_convention;
2278 locations->SetInAt(0, Location::FpuRegisterLocation(
2279 calling_convention.GetFpuRegisterAt(0)));
2280 locations->SetOut(Location::RegisterPairLocation(R0, R1));
2281 break;
2282 }
2283
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002284 case Primitive::kPrimDouble: {
2285 // Processing a Dex `double-to-long' instruction.
2286 InvokeRuntimeCallingConvention calling_convention;
2287 locations->SetInAt(0, Location::FpuRegisterPairLocation(
2288 calling_convention.GetFpuRegisterAt(0),
2289 calling_convention.GetFpuRegisterAt(1)));
2290 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00002291 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002292 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002293
2294 default:
2295 LOG(FATAL) << "Unexpected type conversion from " << input_type
2296 << " to " << result_type;
2297 }
2298 break;
2299
Roland Levillain981e4542014-11-14 11:47:14 +00002300 case Primitive::kPrimChar:
2301 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002302 case Primitive::kPrimBoolean:
2303 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002304 case Primitive::kPrimByte:
2305 case Primitive::kPrimShort:
2306 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002307 // Processing a Dex `int-to-char' instruction.
2308 locations->SetInAt(0, Location::RequiresRegister());
2309 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2310 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::kPrimFloat:
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-float' instruction.
2327 locations->SetInAt(0, Location::RequiresRegister());
2328 locations->SetOut(Location::RequiresFpuRegister());
2329 break;
2330
Roland Levillain5b3ee562015-04-14 16:02:41 +01002331 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00002332 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002333 InvokeRuntimeCallingConvention calling_convention;
2334 locations->SetInAt(0, Location::RegisterPairLocation(
2335 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2336 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00002337 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01002338 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002339
Roland Levillaincff13742014-11-17 14:32:17 +00002340 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002341 // Processing a Dex `double-to-float' instruction.
2342 locations->SetInAt(0, Location::RequiresFpuRegister());
2343 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002344 break;
2345
2346 default:
2347 LOG(FATAL) << "Unexpected type conversion from " << input_type
2348 << " to " << result_type;
2349 };
2350 break;
2351
Roland Levillaindff1f282014-11-05 14:15:05 +00002352 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002353 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002354 case Primitive::kPrimBoolean:
2355 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002356 case Primitive::kPrimByte:
2357 case Primitive::kPrimShort:
2358 case Primitive::kPrimInt:
2359 case Primitive::kPrimChar:
2360 // Processing a Dex `int-to-double' instruction.
2361 locations->SetInAt(0, Location::RequiresRegister());
2362 locations->SetOut(Location::RequiresFpuRegister());
2363 break;
2364
2365 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002366 // Processing a Dex `long-to-double' instruction.
2367 locations->SetInAt(0, Location::RequiresRegister());
2368 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01002369 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002370 locations->AddTemp(Location::RequiresFpuRegister());
2371 break;
2372
Roland Levillaincff13742014-11-17 14:32:17 +00002373 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002374 // Processing a Dex `float-to-double' instruction.
2375 locations->SetInAt(0, Location::RequiresFpuRegister());
2376 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002377 break;
2378
2379 default:
2380 LOG(FATAL) << "Unexpected type conversion from " << input_type
2381 << " to " << result_type;
2382 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002383 break;
2384
2385 default:
2386 LOG(FATAL) << "Unexpected type conversion from " << input_type
2387 << " to " << result_type;
2388 }
2389}
2390
2391void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
2392 LocationSummary* locations = conversion->GetLocations();
2393 Location out = locations->Out();
2394 Location in = locations->InAt(0);
2395 Primitive::Type result_type = conversion->GetResultType();
2396 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002397 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002398 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002399 case Primitive::kPrimByte:
2400 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002401 case Primitive::kPrimBoolean:
2402 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002403 case Primitive::kPrimShort:
2404 case Primitive::kPrimInt:
2405 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002406 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002407 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002408 break;
2409
2410 default:
2411 LOG(FATAL) << "Unexpected type conversion from " << input_type
2412 << " to " << result_type;
2413 }
2414 break;
2415
Roland Levillain01a8d712014-11-14 16:27:39 +00002416 case Primitive::kPrimShort:
2417 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002418 case Primitive::kPrimBoolean:
2419 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002420 case Primitive::kPrimByte:
2421 case Primitive::kPrimInt:
2422 case Primitive::kPrimChar:
2423 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002424 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00002425 break;
2426
2427 default:
2428 LOG(FATAL) << "Unexpected type conversion from " << input_type
2429 << " to " << result_type;
2430 }
2431 break;
2432
Roland Levillain946e1432014-11-11 17:35:19 +00002433 case Primitive::kPrimInt:
2434 switch (input_type) {
2435 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002436 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002437 DCHECK(out.IsRegister());
2438 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002439 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002440 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002441 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00002442 } else {
2443 DCHECK(in.IsConstant());
2444 DCHECK(in.GetConstant()->IsLongConstant());
2445 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002446 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00002447 }
2448 break;
2449
Roland Levillain3f8f9362014-12-02 17:45:01 +00002450 case Primitive::kPrimFloat: {
2451 // Processing a Dex `float-to-int' instruction.
2452 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
2453 __ vmovs(temp, in.AsFpuRegister<SRegister>());
2454 __ vcvtis(temp, temp);
2455 __ vmovrs(out.AsRegister<Register>(), temp);
2456 break;
2457 }
2458
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002459 case Primitive::kPrimDouble: {
2460 // Processing a Dex `double-to-int' instruction.
2461 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
2462 DRegister temp_d = FromLowSToD(temp_s);
2463 __ vmovd(temp_d, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
2464 __ vcvtid(temp_s, temp_d);
2465 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00002466 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002467 }
Roland Levillain946e1432014-11-11 17:35:19 +00002468
2469 default:
2470 LOG(FATAL) << "Unexpected type conversion from " << input_type
2471 << " to " << result_type;
2472 }
2473 break;
2474
Roland Levillaindff1f282014-11-05 14:15:05 +00002475 case Primitive::kPrimLong:
2476 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002477 case Primitive::kPrimBoolean:
2478 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002479 case Primitive::kPrimByte:
2480 case Primitive::kPrimShort:
2481 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002482 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002483 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002484 DCHECK(out.IsRegisterPair());
2485 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002486 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002487 // Sign extension.
2488 __ Asr(out.AsRegisterPairHigh<Register>(),
2489 out.AsRegisterPairLow<Register>(),
2490 31);
2491 break;
2492
2493 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002494 // Processing a Dex `float-to-long' instruction.
Roland Levillain624279f2014-12-04 11:54:28 +00002495 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2496 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002497 conversion->GetDexPc(),
2498 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002499 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002500 break;
2501
Roland Levillaindff1f282014-11-05 14:15:05 +00002502 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002503 // Processing a Dex `double-to-long' instruction.
2504 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2505 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002506 conversion->GetDexPc(),
2507 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002508 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002509 break;
2510
2511 default:
2512 LOG(FATAL) << "Unexpected type conversion from " << input_type
2513 << " to " << result_type;
2514 }
2515 break;
2516
Roland Levillain981e4542014-11-14 11:47:14 +00002517 case Primitive::kPrimChar:
2518 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002519 case Primitive::kPrimBoolean:
2520 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002521 case Primitive::kPrimByte:
2522 case Primitive::kPrimShort:
2523 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002524 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002525 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00002526 break;
2527
2528 default:
2529 LOG(FATAL) << "Unexpected type conversion from " << input_type
2530 << " to " << result_type;
2531 }
2532 break;
2533
Roland Levillaindff1f282014-11-05 14:15:05 +00002534 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002535 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002536 case Primitive::kPrimBoolean:
2537 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002538 case Primitive::kPrimByte:
2539 case Primitive::kPrimShort:
2540 case Primitive::kPrimInt:
2541 case Primitive::kPrimChar: {
2542 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002543 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
2544 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002545 break;
2546 }
2547
Roland Levillain5b3ee562015-04-14 16:02:41 +01002548 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002549 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002550 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pL2f),
2551 conversion,
2552 conversion->GetDexPc(),
2553 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002554 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
Roland Levillain6d0e4832014-11-27 18:31:21 +00002555 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002556
Roland Levillaincff13742014-11-17 14:32:17 +00002557 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002558 // Processing a Dex `double-to-float' instruction.
2559 __ vcvtsd(out.AsFpuRegister<SRegister>(),
2560 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00002561 break;
2562
2563 default:
2564 LOG(FATAL) << "Unexpected type conversion from " << input_type
2565 << " to " << result_type;
2566 };
2567 break;
2568
Roland Levillaindff1f282014-11-05 14:15:05 +00002569 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002570 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002571 case Primitive::kPrimBoolean:
2572 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002573 case Primitive::kPrimByte:
2574 case Primitive::kPrimShort:
2575 case Primitive::kPrimInt:
2576 case Primitive::kPrimChar: {
2577 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002578 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002579 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2580 out.AsFpuRegisterPairLow<SRegister>());
2581 break;
2582 }
2583
Roland Levillain647b9ed2014-11-27 12:06:00 +00002584 case Primitive::kPrimLong: {
2585 // Processing a Dex `long-to-double' instruction.
2586 Register low = in.AsRegisterPairLow<Register>();
2587 Register high = in.AsRegisterPairHigh<Register>();
2588 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
2589 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002590 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00002591 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002592 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
2593 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002594
Roland Levillain682393c2015-04-14 15:57:52 +01002595 // temp_d = int-to-double(high)
2596 __ vmovsr(temp_s, high);
2597 __ vcvtdi(temp_d, temp_s);
2598 // constant_d = k2Pow32EncodingForDouble
2599 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
2600 // out_d = unsigned-to-double(low)
2601 __ vmovsr(out_s, low);
2602 __ vcvtdu(out_d, out_s);
2603 // out_d += temp_d * constant_d
2604 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002605 break;
2606 }
2607
Roland Levillaincff13742014-11-17 14:32:17 +00002608 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002609 // Processing a Dex `float-to-double' instruction.
2610 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2611 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002612 break;
2613
2614 default:
2615 LOG(FATAL) << "Unexpected type conversion from " << input_type
2616 << " to " << result_type;
2617 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002618 break;
2619
2620 default:
2621 LOG(FATAL) << "Unexpected type conversion from " << input_type
2622 << " to " << result_type;
2623 }
2624}
2625
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002626void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002627 LocationSummary* locations =
2628 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002629 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002630 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002631 locations->SetInAt(0, Location::RequiresRegister());
2632 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002633 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2634 break;
2635 }
2636
2637 case Primitive::kPrimLong: {
2638 locations->SetInAt(0, Location::RequiresRegister());
2639 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002640 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002641 break;
2642 }
2643
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002644 case Primitive::kPrimFloat:
2645 case Primitive::kPrimDouble: {
2646 locations->SetInAt(0, Location::RequiresFpuRegister());
2647 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002648 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002649 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002650 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002651
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002652 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002653 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002654 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002655}
2656
2657void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
2658 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002659 Location out = locations->Out();
2660 Location first = locations->InAt(0);
2661 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002662 switch (add->GetResultType()) {
2663 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002664 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002665 __ add(out.AsRegister<Register>(),
2666 first.AsRegister<Register>(),
2667 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002668 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002669 __ AddConstant(out.AsRegister<Register>(),
2670 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002671 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002672 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002673 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002674
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002675 case Primitive::kPrimLong: {
2676 DCHECK(second.IsRegisterPair());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002677 __ adds(out.AsRegisterPairLow<Register>(),
2678 first.AsRegisterPairLow<Register>(),
2679 ShifterOperand(second.AsRegisterPairLow<Register>()));
2680 __ adc(out.AsRegisterPairHigh<Register>(),
2681 first.AsRegisterPairHigh<Register>(),
2682 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002683 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002684 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002685
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002686 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00002687 __ vadds(out.AsFpuRegister<SRegister>(),
2688 first.AsFpuRegister<SRegister>(),
2689 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002690 break;
2691
2692 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002693 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2694 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2695 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002696 break;
2697
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002698 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002699 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002700 }
2701}
2702
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002703void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002704 LocationSummary* locations =
2705 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002706 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002707 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002708 locations->SetInAt(0, Location::RequiresRegister());
2709 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002710 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2711 break;
2712 }
2713
2714 case Primitive::kPrimLong: {
2715 locations->SetInAt(0, Location::RequiresRegister());
2716 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002717 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002718 break;
2719 }
Calin Juravle11351682014-10-23 15:38:15 +01002720 case Primitive::kPrimFloat:
2721 case Primitive::kPrimDouble: {
2722 locations->SetInAt(0, Location::RequiresFpuRegister());
2723 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002724 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002725 break;
Calin Juravle11351682014-10-23 15:38:15 +01002726 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002727 default:
Calin Juravle11351682014-10-23 15:38:15 +01002728 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002729 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002730}
2731
2732void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
2733 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002734 Location out = locations->Out();
2735 Location first = locations->InAt(0);
2736 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002737 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002738 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002739 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002740 __ sub(out.AsRegister<Register>(),
2741 first.AsRegister<Register>(),
2742 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002743 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002744 __ AddConstant(out.AsRegister<Register>(),
2745 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01002746 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002747 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002748 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002749 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002750
Calin Juravle11351682014-10-23 15:38:15 +01002751 case Primitive::kPrimLong: {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002752 DCHECK(second.IsRegisterPair());
Calin Juravle11351682014-10-23 15:38:15 +01002753 __ subs(out.AsRegisterPairLow<Register>(),
2754 first.AsRegisterPairLow<Register>(),
2755 ShifterOperand(second.AsRegisterPairLow<Register>()));
2756 __ sbc(out.AsRegisterPairHigh<Register>(),
2757 first.AsRegisterPairHigh<Register>(),
2758 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002759 break;
Calin Juravle11351682014-10-23 15:38:15 +01002760 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002761
Calin Juravle11351682014-10-23 15:38:15 +01002762 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002763 __ vsubs(out.AsFpuRegister<SRegister>(),
2764 first.AsFpuRegister<SRegister>(),
2765 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002766 break;
Calin Juravle11351682014-10-23 15:38:15 +01002767 }
2768
2769 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002770 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2771 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2772 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01002773 break;
2774 }
2775
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002776
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002777 default:
Calin Juravle11351682014-10-23 15:38:15 +01002778 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002779 }
2780}
2781
Calin Juravle34bacdf2014-10-07 20:23:36 +01002782void LocationsBuilderARM::VisitMul(HMul* mul) {
2783 LocationSummary* locations =
2784 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2785 switch (mul->GetResultType()) {
2786 case Primitive::kPrimInt:
2787 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002788 locations->SetInAt(0, Location::RequiresRegister());
2789 locations->SetInAt(1, Location::RequiresRegister());
2790 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002791 break;
2792 }
2793
Calin Juravleb5bfa962014-10-21 18:02:24 +01002794 case Primitive::kPrimFloat:
2795 case Primitive::kPrimDouble: {
2796 locations->SetInAt(0, Location::RequiresFpuRegister());
2797 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002798 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002799 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002800 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002801
2802 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002803 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002804 }
2805}
2806
2807void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
2808 LocationSummary* locations = mul->GetLocations();
2809 Location out = locations->Out();
2810 Location first = locations->InAt(0);
2811 Location second = locations->InAt(1);
2812 switch (mul->GetResultType()) {
2813 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00002814 __ mul(out.AsRegister<Register>(),
2815 first.AsRegister<Register>(),
2816 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002817 break;
2818 }
2819 case Primitive::kPrimLong: {
2820 Register out_hi = out.AsRegisterPairHigh<Register>();
2821 Register out_lo = out.AsRegisterPairLow<Register>();
2822 Register in1_hi = first.AsRegisterPairHigh<Register>();
2823 Register in1_lo = first.AsRegisterPairLow<Register>();
2824 Register in2_hi = second.AsRegisterPairHigh<Register>();
2825 Register in2_lo = second.AsRegisterPairLow<Register>();
2826
2827 // Extra checks to protect caused by the existence of R1_R2.
2828 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
2829 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
2830 DCHECK_NE(out_hi, in1_lo);
2831 DCHECK_NE(out_hi, in2_lo);
2832
2833 // input: in1 - 64 bits, in2 - 64 bits
2834 // output: out
2835 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2836 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2837 // parts: out.lo = (in1.lo * in2.lo)[31:0]
2838
2839 // IP <- in1.lo * in2.hi
2840 __ mul(IP, in1_lo, in2_hi);
2841 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2842 __ mla(out_hi, in1_hi, in2_lo, IP);
2843 // out.lo <- (in1.lo * in2.lo)[31:0];
2844 __ umull(out_lo, IP, in1_lo, in2_lo);
2845 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2846 __ add(out_hi, out_hi, ShifterOperand(IP));
2847 break;
2848 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002849
2850 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002851 __ vmuls(out.AsFpuRegister<SRegister>(),
2852 first.AsFpuRegister<SRegister>(),
2853 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002854 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002855 }
2856
2857 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002858 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2859 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2860 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01002861 break;
2862 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002863
2864 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002865 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002866 }
2867}
2868
Zheng Xuc6667102015-05-15 16:08:45 +08002869void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2870 DCHECK(instruction->IsDiv() || instruction->IsRem());
2871 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2872
2873 LocationSummary* locations = instruction->GetLocations();
2874 Location second = locations->InAt(1);
2875 DCHECK(second.IsConstant());
2876
2877 Register out = locations->Out().AsRegister<Register>();
2878 Register dividend = locations->InAt(0).AsRegister<Register>();
2879 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2880 DCHECK(imm == 1 || imm == -1);
2881
2882 if (instruction->IsRem()) {
2883 __ LoadImmediate(out, 0);
2884 } else {
2885 if (imm == 1) {
2886 __ Mov(out, dividend);
2887 } else {
2888 __ rsb(out, dividend, ShifterOperand(0));
2889 }
2890 }
2891}
2892
2893void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(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 temp = locations->GetTemp(0).AsRegister<Register>();
2904 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Vladimir Marko80afd022015-05-19 18:08:00 +01002905 uint32_t abs_imm = static_cast<uint32_t>(std::abs(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002906 DCHECK(IsPowerOfTwo(abs_imm));
2907 int ctz_imm = CTZ(abs_imm);
2908
2909 if (ctz_imm == 1) {
2910 __ Lsr(temp, dividend, 32 - ctz_imm);
2911 } else {
2912 __ Asr(temp, dividend, 31);
2913 __ Lsr(temp, temp, 32 - ctz_imm);
2914 }
2915 __ add(out, temp, ShifterOperand(dividend));
2916
2917 if (instruction->IsDiv()) {
2918 __ Asr(out, out, ctz_imm);
2919 if (imm < 0) {
2920 __ rsb(out, out, ShifterOperand(0));
2921 }
2922 } else {
2923 __ ubfx(out, out, 0, ctz_imm);
2924 __ sub(out, out, ShifterOperand(temp));
2925 }
2926}
2927
2928void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2929 DCHECK(instruction->IsDiv() || instruction->IsRem());
2930 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2931
2932 LocationSummary* locations = instruction->GetLocations();
2933 Location second = locations->InAt(1);
2934 DCHECK(second.IsConstant());
2935
2936 Register out = locations->Out().AsRegister<Register>();
2937 Register dividend = locations->InAt(0).AsRegister<Register>();
2938 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2939 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
2940 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2941
2942 int64_t magic;
2943 int shift;
2944 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2945
2946 __ LoadImmediate(temp1, magic);
2947 __ smull(temp2, temp1, dividend, temp1);
2948
2949 if (imm > 0 && magic < 0) {
2950 __ add(temp1, temp1, ShifterOperand(dividend));
2951 } else if (imm < 0 && magic > 0) {
2952 __ sub(temp1, temp1, ShifterOperand(dividend));
2953 }
2954
2955 if (shift != 0) {
2956 __ Asr(temp1, temp1, shift);
2957 }
2958
2959 if (instruction->IsDiv()) {
2960 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
2961 } else {
2962 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
2963 // TODO: Strength reduction for mls.
2964 __ LoadImmediate(temp2, imm);
2965 __ mls(out, temp1, temp2, dividend);
2966 }
2967}
2968
2969void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
2970 DCHECK(instruction->IsDiv() || instruction->IsRem());
2971 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2972
2973 LocationSummary* locations = instruction->GetLocations();
2974 Location second = locations->InAt(1);
2975 DCHECK(second.IsConstant());
2976
2977 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2978 if (imm == 0) {
2979 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2980 } else if (imm == 1 || imm == -1) {
2981 DivRemOneOrMinusOne(instruction);
2982 } else if (IsPowerOfTwo(std::abs(imm))) {
2983 DivRemByPowerOfTwo(instruction);
2984 } else {
2985 DCHECK(imm <= -2 || imm >= 2);
2986 GenerateDivRemWithAnyConstant(instruction);
2987 }
2988}
2989
Calin Juravle7c4954d2014-10-28 16:57:40 +00002990void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002991 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2992 if (div->GetResultType() == Primitive::kPrimLong) {
2993 // pLdiv runtime call.
2994 call_kind = LocationSummary::kCall;
Zheng Xuc6667102015-05-15 16:08:45 +08002995 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
2996 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002997 } else if (div->GetResultType() == Primitive::kPrimInt &&
2998 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
2999 // pIdivmod runtime call.
3000 call_kind = LocationSummary::kCall;
3001 }
3002
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003003 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3004
Calin Juravle7c4954d2014-10-28 16:57:40 +00003005 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003006 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003007 if (div->InputAt(1)->IsConstant()) {
3008 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003009 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08003010 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3011 int32_t abs_imm = std::abs(div->InputAt(1)->AsIntConstant()->GetValue());
3012 if (abs_imm <= 1) {
3013 // No temp register required.
3014 } else {
3015 locations->AddTemp(Location::RequiresRegister());
3016 if (!IsPowerOfTwo(abs_imm)) {
3017 locations->AddTemp(Location::RequiresRegister());
3018 }
3019 }
3020 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003021 locations->SetInAt(0, Location::RequiresRegister());
3022 locations->SetInAt(1, Location::RequiresRegister());
3023 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3024 } else {
3025 InvokeRuntimeCallingConvention calling_convention;
3026 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3027 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3028 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
3029 // we only need the former.
3030 locations->SetOut(Location::RegisterLocation(R0));
3031 }
Calin Juravled0d48522014-11-04 16:40:20 +00003032 break;
3033 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003034 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003035 InvokeRuntimeCallingConvention calling_convention;
3036 locations->SetInAt(0, Location::RegisterPairLocation(
3037 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3038 locations->SetInAt(1, Location::RegisterPairLocation(
3039 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003040 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003041 break;
3042 }
3043 case Primitive::kPrimFloat:
3044 case Primitive::kPrimDouble: {
3045 locations->SetInAt(0, Location::RequiresFpuRegister());
3046 locations->SetInAt(1, Location::RequiresFpuRegister());
3047 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3048 break;
3049 }
3050
3051 default:
3052 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3053 }
3054}
3055
3056void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
3057 LocationSummary* locations = div->GetLocations();
3058 Location out = locations->Out();
3059 Location first = locations->InAt(0);
3060 Location second = locations->InAt(1);
3061
3062 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003063 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003064 if (second.IsConstant()) {
3065 GenerateDivRemConstantIntegral(div);
3066 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003067 __ sdiv(out.AsRegister<Register>(),
3068 first.AsRegister<Register>(),
3069 second.AsRegister<Register>());
3070 } else {
3071 InvokeRuntimeCallingConvention calling_convention;
3072 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3073 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3074 DCHECK_EQ(R0, out.AsRegister<Register>());
3075
3076 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), div, div->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003077 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003078 }
Calin Juravled0d48522014-11-04 16:40:20 +00003079 break;
3080 }
3081
Calin Juravle7c4954d2014-10-28 16:57:40 +00003082 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003083 InvokeRuntimeCallingConvention calling_convention;
3084 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3085 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3086 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3087 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3088 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003089 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003090
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003091 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv), div, div->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003092 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravle7c4954d2014-10-28 16:57:40 +00003093 break;
3094 }
3095
3096 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003097 __ vdivs(out.AsFpuRegister<SRegister>(),
3098 first.AsFpuRegister<SRegister>(),
3099 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003100 break;
3101 }
3102
3103 case Primitive::kPrimDouble: {
3104 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3105 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3106 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
3107 break;
3108 }
3109
3110 default:
3111 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3112 }
3113}
3114
Calin Juravlebacfec32014-11-14 15:54:36 +00003115void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003116 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003117
3118 // Most remainders are implemented in the runtime.
3119 LocationSummary::CallKind call_kind = LocationSummary::kCall;
Zheng Xuc6667102015-05-15 16:08:45 +08003120 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
3121 // sdiv will be replaced by other instruction sequence.
3122 call_kind = LocationSummary::kNoCall;
3123 } else if ((rem->GetResultType() == Primitive::kPrimInt)
3124 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003125 // Have hardware divide instruction for int, do it with three instructions.
3126 call_kind = LocationSummary::kNoCall;
3127 }
3128
Calin Juravlebacfec32014-11-14 15:54:36 +00003129 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3130
Calin Juravled2ec87d2014-12-08 14:24:46 +00003131 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003132 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003133 if (rem->InputAt(1)->IsConstant()) {
3134 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003135 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08003136 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3137 int32_t abs_imm = std::abs(rem->InputAt(1)->AsIntConstant()->GetValue());
3138 if (abs_imm <= 1) {
3139 // No temp register required.
3140 } else {
3141 locations->AddTemp(Location::RequiresRegister());
3142 if (!IsPowerOfTwo(abs_imm)) {
3143 locations->AddTemp(Location::RequiresRegister());
3144 }
3145 }
3146 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003147 locations->SetInAt(0, Location::RequiresRegister());
3148 locations->SetInAt(1, Location::RequiresRegister());
3149 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3150 locations->AddTemp(Location::RequiresRegister());
3151 } else {
3152 InvokeRuntimeCallingConvention calling_convention;
3153 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3154 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3155 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
3156 // we only need the latter.
3157 locations->SetOut(Location::RegisterLocation(R1));
3158 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003159 break;
3160 }
3161 case Primitive::kPrimLong: {
3162 InvokeRuntimeCallingConvention calling_convention;
3163 locations->SetInAt(0, Location::RegisterPairLocation(
3164 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3165 locations->SetInAt(1, Location::RegisterPairLocation(
3166 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3167 // The runtime helper puts the output in R2,R3.
3168 locations->SetOut(Location::RegisterPairLocation(R2, R3));
3169 break;
3170 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00003171 case Primitive::kPrimFloat: {
3172 InvokeRuntimeCallingConvention calling_convention;
3173 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3174 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3175 locations->SetOut(Location::FpuRegisterLocation(S0));
3176 break;
3177 }
3178
Calin Juravlebacfec32014-11-14 15:54:36 +00003179 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003180 InvokeRuntimeCallingConvention calling_convention;
3181 locations->SetInAt(0, Location::FpuRegisterPairLocation(
3182 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
3183 locations->SetInAt(1, Location::FpuRegisterPairLocation(
3184 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
3185 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00003186 break;
3187 }
3188
3189 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003190 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003191 }
3192}
3193
3194void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
3195 LocationSummary* locations = rem->GetLocations();
3196 Location out = locations->Out();
3197 Location first = locations->InAt(0);
3198 Location second = locations->InAt(1);
3199
Calin Juravled2ec87d2014-12-08 14:24:46 +00003200 Primitive::Type type = rem->GetResultType();
3201 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003202 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003203 if (second.IsConstant()) {
3204 GenerateDivRemConstantIntegral(rem);
3205 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003206 Register reg1 = first.AsRegister<Register>();
3207 Register reg2 = second.AsRegister<Register>();
3208 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003209
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003210 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003211 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003212 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003213 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003214 } else {
3215 InvokeRuntimeCallingConvention calling_convention;
3216 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3217 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3218 DCHECK_EQ(R1, out.AsRegister<Register>());
3219
3220 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003221 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003222 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003223 break;
3224 }
3225
3226 case Primitive::kPrimLong: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003227 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003228 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003229 break;
3230 }
3231
Calin Juravled2ec87d2014-12-08 14:24:46 +00003232 case Primitive::kPrimFloat: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003233 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003234 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Calin Juravled2ec87d2014-12-08 14:24:46 +00003235 break;
3236 }
3237
Calin Juravlebacfec32014-11-14 15:54:36 +00003238 case Primitive::kPrimDouble: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003239 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003240 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003241 break;
3242 }
3243
3244 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003245 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003246 }
3247}
3248
Calin Juravled0d48522014-11-04 16:40:20 +00003249void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003250 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3251 ? LocationSummary::kCallOnSlowPath
3252 : LocationSummary::kNoCall;
3253 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003254 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00003255 if (instruction->HasUses()) {
3256 locations->SetOut(Location::SameAsFirstInput());
3257 }
3258}
3259
3260void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003261 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003262 codegen_->AddSlowPath(slow_path);
3263
3264 LocationSummary* locations = instruction->GetLocations();
3265 Location value = locations->InAt(0);
3266
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003267 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003268 case Primitive::kPrimByte:
3269 case Primitive::kPrimChar:
3270 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003271 case Primitive::kPrimInt: {
3272 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003273 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003274 } else {
3275 DCHECK(value.IsConstant()) << value;
3276 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3277 __ b(slow_path->GetEntryLabel());
3278 }
3279 }
3280 break;
3281 }
3282 case Primitive::kPrimLong: {
3283 if (value.IsRegisterPair()) {
3284 __ orrs(IP,
3285 value.AsRegisterPairLow<Register>(),
3286 ShifterOperand(value.AsRegisterPairHigh<Register>()));
3287 __ b(slow_path->GetEntryLabel(), EQ);
3288 } else {
3289 DCHECK(value.IsConstant()) << value;
3290 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3291 __ b(slow_path->GetEntryLabel());
3292 }
3293 }
3294 break;
3295 default:
3296 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3297 }
3298 }
Calin Juravled0d48522014-11-04 16:40:20 +00003299}
3300
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003301void InstructionCodeGeneratorARM::HandleIntegerRotate(LocationSummary* locations) {
3302 Register in = locations->InAt(0).AsRegister<Register>();
3303 Location rhs = locations->InAt(1);
3304 Register out = locations->Out().AsRegister<Register>();
3305
3306 if (rhs.IsConstant()) {
3307 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
3308 // so map all rotations to a +ve. equivalent in that range.
3309 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
3310 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
3311 if (rot) {
3312 // Rotate, mapping left rotations to right equivalents if necessary.
3313 // (e.g. left by 2 bits == right by 30.)
3314 __ Ror(out, in, rot);
3315 } else if (out != in) {
3316 __ Mov(out, in);
3317 }
3318 } else {
3319 __ Ror(out, in, rhs.AsRegister<Register>());
3320 }
3321}
3322
3323// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
3324// rotates by swapping input regs (effectively rotating by the first 32-bits of
3325// a larger rotation) or flipping direction (thus treating larger right/left
3326// rotations as sub-word sized rotations in the other direction) as appropriate.
3327void InstructionCodeGeneratorARM::HandleLongRotate(LocationSummary* locations) {
3328 Register in_reg_lo = locations->InAt(0).AsRegisterPairLow<Register>();
3329 Register in_reg_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
3330 Location rhs = locations->InAt(1);
3331 Register out_reg_lo = locations->Out().AsRegisterPairLow<Register>();
3332 Register out_reg_hi = locations->Out().AsRegisterPairHigh<Register>();
3333
3334 if (rhs.IsConstant()) {
3335 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
3336 // Map all rotations to +ve. equivalents on the interval [0,63].
3337 rot &= kMaxLongShiftValue;
3338 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
3339 // logic below to a simple pair of binary orr.
3340 // (e.g. 34 bits == in_reg swap + 2 bits right.)
3341 if (rot >= kArmBitsPerWord) {
3342 rot -= kArmBitsPerWord;
3343 std::swap(in_reg_hi, in_reg_lo);
3344 }
3345 // Rotate, or mov to out for zero or word size rotations.
3346 if (rot != 0u) {
3347 __ Lsr(out_reg_hi, in_reg_hi, rot);
3348 __ orr(out_reg_hi, out_reg_hi, ShifterOperand(in_reg_lo, arm::LSL, kArmBitsPerWord - rot));
3349 __ Lsr(out_reg_lo, in_reg_lo, rot);
3350 __ orr(out_reg_lo, out_reg_lo, ShifterOperand(in_reg_hi, arm::LSL, kArmBitsPerWord - rot));
3351 } else {
3352 __ Mov(out_reg_lo, in_reg_lo);
3353 __ Mov(out_reg_hi, in_reg_hi);
3354 }
3355 } else {
3356 Register shift_right = locations->GetTemp(0).AsRegister<Register>();
3357 Register shift_left = locations->GetTemp(1).AsRegister<Register>();
3358 Label end;
3359 Label shift_by_32_plus_shift_right;
3360
3361 __ and_(shift_right, rhs.AsRegister<Register>(), ShifterOperand(0x1F));
3362 __ Lsrs(shift_left, rhs.AsRegister<Register>(), 6);
3363 __ rsb(shift_left, shift_right, ShifterOperand(kArmBitsPerWord), AL, kCcKeep);
3364 __ b(&shift_by_32_plus_shift_right, CC);
3365
3366 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
3367 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
3368 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
3369 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3370 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3371 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3372 __ Lsr(shift_left, in_reg_hi, shift_right);
3373 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_left));
3374 __ b(&end);
3375
3376 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
3377 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
3378 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
3379 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
3380 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3381 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3382 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3383 __ Lsl(shift_right, in_reg_hi, shift_left);
3384 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_right));
3385
3386 __ Bind(&end);
3387 }
3388}
3389void LocationsBuilderARM::HandleRotate(HRor* ror) {
3390 LocationSummary* locations =
3391 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3392 switch (ror->GetResultType()) {
3393 case Primitive::kPrimInt: {
3394 locations->SetInAt(0, Location::RequiresRegister());
3395 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
3396 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3397 break;
3398 }
3399 case Primitive::kPrimLong: {
3400 locations->SetInAt(0, Location::RequiresRegister());
3401 if (ror->InputAt(1)->IsConstant()) {
3402 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
3403 } else {
3404 locations->SetInAt(1, Location::RequiresRegister());
3405 locations->AddTemp(Location::RequiresRegister());
3406 locations->AddTemp(Location::RequiresRegister());
3407 }
3408 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3409 break;
3410 }
3411 default:
3412 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3413 }
3414}
3415
3416void InstructionCodeGeneratorARM::HandleRotate(HRor* ror) {
3417 LocationSummary* locations = ror->GetLocations();
3418 Primitive::Type type = ror->GetResultType();
3419 switch (type) {
3420 case Primitive::kPrimInt: {
3421 HandleIntegerRotate(locations);
3422 break;
3423 }
3424 case Primitive::kPrimLong: {
3425 HandleLongRotate(locations);
3426 break;
3427 }
3428 default:
3429 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00003430 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003431 }
3432}
3433
3434void LocationsBuilderARM::VisitRor(HRor* op) {
Vladimir Marko351dddf2015-12-11 16:34:46 +00003435 HandleRotate(op);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003436}
3437
3438void InstructionCodeGeneratorARM::VisitRor(HRor* op) {
Vladimir Marko351dddf2015-12-11 16:34:46 +00003439 HandleRotate(op);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003440}
3441
Calin Juravle9aec02f2014-11-18 23:06:35 +00003442void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
3443 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3444
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003445 LocationSummary* locations =
3446 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003447
3448 switch (op->GetResultType()) {
3449 case Primitive::kPrimInt: {
3450 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003451 if (op->InputAt(1)->IsConstant()) {
3452 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3453 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3454 } else {
3455 locations->SetInAt(1, Location::RequiresRegister());
3456 // Make the output overlap, as it will be used to hold the masked
3457 // second input.
3458 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3459 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003460 break;
3461 }
3462 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003463 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003464 if (op->InputAt(1)->IsConstant()) {
3465 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3466 // For simplicity, use kOutputOverlap even though we only require that low registers
3467 // don't clash with high registers which the register allocator currently guarantees.
3468 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3469 } else {
3470 locations->SetInAt(1, Location::RequiresRegister());
3471 locations->AddTemp(Location::RequiresRegister());
3472 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3473 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003474 break;
3475 }
3476 default:
3477 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3478 }
3479}
3480
3481void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
3482 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3483
3484 LocationSummary* locations = op->GetLocations();
3485 Location out = locations->Out();
3486 Location first = locations->InAt(0);
3487 Location second = locations->InAt(1);
3488
3489 Primitive::Type type = op->GetResultType();
3490 switch (type) {
3491 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003492 Register out_reg = out.AsRegister<Register>();
3493 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003494 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003495 Register second_reg = second.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +00003496 // ARM doesn't mask the shift count so we need to do it ourselves.
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003497 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftValue));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003498 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003499 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003500 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003501 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003502 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003503 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003504 }
3505 } else {
3506 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
3507 uint32_t shift_value = static_cast<uint32_t>(cst & kMaxIntShiftValue);
Roland Levillainc9285912015-12-18 10:38:42 +00003508 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
Calin Juravle9aec02f2014-11-18 23:06:35 +00003509 __ Mov(out_reg, first_reg);
3510 } else if (op->IsShl()) {
3511 __ Lsl(out_reg, first_reg, shift_value);
3512 } else if (op->IsShr()) {
3513 __ Asr(out_reg, first_reg, shift_value);
3514 } else {
3515 __ Lsr(out_reg, first_reg, shift_value);
3516 }
3517 }
3518 break;
3519 }
3520 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003521 Register o_h = out.AsRegisterPairHigh<Register>();
3522 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003523
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003524 Register high = first.AsRegisterPairHigh<Register>();
3525 Register low = first.AsRegisterPairLow<Register>();
3526
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003527 if (second.IsRegister()) {
3528 Register temp = locations->GetTemp(0).AsRegister<Register>();
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003529
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003530 Register second_reg = second.AsRegister<Register>();
3531
3532 if (op->IsShl()) {
3533 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftValue));
3534 // Shift the high part
3535 __ Lsl(o_h, high, o_l);
3536 // Shift the low part and `or` what overflew on the high part
3537 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
3538 __ Lsr(temp, low, temp);
3539 __ orr(o_h, o_h, ShifterOperand(temp));
3540 // If the shift is > 32 bits, override the high part
3541 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
3542 __ it(PL);
3543 __ Lsl(o_h, low, temp, PL);
3544 // Shift the low part
3545 __ Lsl(o_l, low, o_l);
3546 } else if (op->IsShr()) {
3547 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftValue));
3548 // Shift the low part
3549 __ Lsr(o_l, low, o_h);
3550 // Shift the high part and `or` what underflew on the low part
3551 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3552 __ Lsl(temp, high, temp);
3553 __ orr(o_l, o_l, ShifterOperand(temp));
3554 // If the shift is > 32 bits, override the low part
3555 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3556 __ it(PL);
3557 __ Asr(o_l, high, temp, PL);
3558 // Shift the high part
3559 __ Asr(o_h, high, o_h);
3560 } else {
3561 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftValue));
3562 // same as Shr except we use `Lsr`s and not `Asr`s
3563 __ Lsr(o_l, low, o_h);
3564 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3565 __ Lsl(temp, high, temp);
3566 __ orr(o_l, o_l, ShifterOperand(temp));
3567 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3568 __ it(PL);
3569 __ Lsr(o_l, high, temp, PL);
3570 __ Lsr(o_h, high, o_h);
3571 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003572 } else {
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003573 // Register allocator doesn't create partial overlap.
3574 DCHECK_NE(o_l, high);
3575 DCHECK_NE(o_h, low);
3576 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
3577 uint32_t shift_value = static_cast<uint32_t>(cst & kMaxLongShiftValue);
3578 if (shift_value > 32) {
3579 if (op->IsShl()) {
3580 __ Lsl(o_h, low, shift_value - 32);
3581 __ LoadImmediate(o_l, 0);
3582 } else if (op->IsShr()) {
3583 __ Asr(o_l, high, shift_value - 32);
3584 __ Asr(o_h, high, 31);
3585 } else {
3586 __ Lsr(o_l, high, shift_value - 32);
3587 __ LoadImmediate(o_h, 0);
3588 }
3589 } else if (shift_value == 32) {
3590 if (op->IsShl()) {
3591 __ mov(o_h, ShifterOperand(low));
3592 __ LoadImmediate(o_l, 0);
3593 } else if (op->IsShr()) {
3594 __ mov(o_l, ShifterOperand(high));
3595 __ Asr(o_h, high, 31);
3596 } else {
3597 __ mov(o_l, ShifterOperand(high));
3598 __ LoadImmediate(o_h, 0);
3599 }
Vladimir Markof9d741e2015-11-20 15:08:11 +00003600 } else if (shift_value == 1) {
3601 if (op->IsShl()) {
3602 __ Lsls(o_l, low, 1);
3603 __ adc(o_h, high, ShifterOperand(high));
3604 } else if (op->IsShr()) {
3605 __ Asrs(o_h, high, 1);
3606 __ Rrx(o_l, low);
3607 } else {
3608 __ Lsrs(o_h, high, 1);
3609 __ Rrx(o_l, low);
3610 }
3611 } else {
3612 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003613 if (op->IsShl()) {
3614 __ Lsl(o_h, high, shift_value);
3615 __ orr(o_h, o_h, ShifterOperand(low, LSR, 32 - shift_value));
3616 __ Lsl(o_l, low, shift_value);
3617 } else if (op->IsShr()) {
3618 __ Lsr(o_l, low, shift_value);
3619 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3620 __ Asr(o_h, high, shift_value);
3621 } else {
3622 __ Lsr(o_l, low, shift_value);
3623 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3624 __ Lsr(o_h, high, shift_value);
3625 }
3626 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003627 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003628 break;
3629 }
3630 default:
3631 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003632 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003633 }
3634}
3635
3636void LocationsBuilderARM::VisitShl(HShl* shl) {
3637 HandleShift(shl);
3638}
3639
3640void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
3641 HandleShift(shl);
3642}
3643
3644void LocationsBuilderARM::VisitShr(HShr* shr) {
3645 HandleShift(shr);
3646}
3647
3648void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
3649 HandleShift(shr);
3650}
3651
3652void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
3653 HandleShift(ushr);
3654}
3655
3656void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
3657 HandleShift(ushr);
3658}
3659
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003660void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003661 LocationSummary* locations =
3662 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003663 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003664 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3665 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003666 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003667}
3668
3669void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003670 // Note: if heap poisoning is enabled, the entry point takes cares
3671 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003672 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003673 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003674 instruction->GetDexPc(),
3675 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003676 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003677}
3678
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003679void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
3680 LocationSummary* locations =
3681 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3682 InvokeRuntimeCallingConvention calling_convention;
3683 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003684 locations->SetOut(Location::RegisterLocation(R0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003685 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003686 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003687}
3688
3689void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
3690 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003691 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003692 // Note: if heap poisoning is enabled, the entry point takes cares
3693 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003694 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003695 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003696 instruction->GetDexPc(),
3697 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003698 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003699}
3700
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003701void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003702 LocationSummary* locations =
3703 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003704 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3705 if (location.IsStackSlot()) {
3706 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3707 } else if (location.IsDoubleStackSlot()) {
3708 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003709 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003710 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003711}
3712
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003713void InstructionCodeGeneratorARM::VisitParameterValue(
3714 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003715 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003716}
3717
3718void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
3719 LocationSummary* locations =
3720 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3721 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3722}
3723
3724void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3725 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003726}
3727
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003728void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003729 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003730 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003731 locations->SetInAt(0, Location::RequiresRegister());
3732 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003733}
3734
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003735void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
3736 LocationSummary* locations = not_->GetLocations();
3737 Location out = locations->Out();
3738 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003739 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003740 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003741 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003742 break;
3743
3744 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003745 __ mvn(out.AsRegisterPairLow<Register>(),
3746 ShifterOperand(in.AsRegisterPairLow<Register>()));
3747 __ mvn(out.AsRegisterPairHigh<Register>(),
3748 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003749 break;
3750
3751 default:
3752 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3753 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003754}
3755
David Brazdil66d126e2015-04-03 16:02:44 +01003756void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
3757 LocationSummary* locations =
3758 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3759 locations->SetInAt(0, Location::RequiresRegister());
3760 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3761}
3762
3763void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003764 LocationSummary* locations = bool_not->GetLocations();
3765 Location out = locations->Out();
3766 Location in = locations->InAt(0);
3767 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
3768}
3769
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003770void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003771 LocationSummary* locations =
3772 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003773 switch (compare->InputAt(0)->GetType()) {
3774 case Primitive::kPrimLong: {
3775 locations->SetInAt(0, Location::RequiresRegister());
3776 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003777 // Output overlaps because it is written before doing the low comparison.
3778 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00003779 break;
3780 }
3781 case Primitive::kPrimFloat:
3782 case Primitive::kPrimDouble: {
3783 locations->SetInAt(0, Location::RequiresFpuRegister());
3784 locations->SetInAt(1, Location::RequiresFpuRegister());
3785 locations->SetOut(Location::RequiresRegister());
3786 break;
3787 }
3788 default:
3789 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3790 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003791}
3792
3793void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003794 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003795 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003796 Location left = locations->InAt(0);
3797 Location right = locations->InAt(1);
3798
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003799 Label less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00003800 Primitive::Type type = compare->InputAt(0)->GetType();
3801 switch (type) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003802 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003803 __ cmp(left.AsRegisterPairHigh<Register>(),
3804 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003805 __ b(&less, LT);
3806 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003807 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00003808 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003809 __ cmp(left.AsRegisterPairLow<Register>(),
3810 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Calin Juravleddb7df22014-11-25 20:56:51 +00003811 break;
3812 }
3813 case Primitive::kPrimFloat:
3814 case Primitive::kPrimDouble: {
3815 __ LoadImmediate(out, 0);
3816 if (type == Primitive::kPrimFloat) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003817 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003818 } else {
3819 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
3820 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
3821 }
3822 __ vmstat(); // transfer FP status register to ARM APSR.
3823 __ b(compare->IsGtBias() ? &greater : &less, VS); // VS for unordered.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003824 break;
3825 }
3826 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003827 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003828 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003829 __ b(&done, EQ);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003830 __ b(&less, LO); // LO is for both: unsigned compare for longs and 'less than' for floats.
Calin Juravleddb7df22014-11-25 20:56:51 +00003831
3832 __ Bind(&greater);
3833 __ LoadImmediate(out, 1);
3834 __ b(&done);
3835
3836 __ Bind(&less);
3837 __ LoadImmediate(out, -1);
3838
3839 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003840}
3841
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003842void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003843 LocationSummary* locations =
3844 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003845 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3846 locations->SetInAt(i, Location::Any());
3847 }
3848 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003849}
3850
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003851void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003852 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003853}
3854
Roland Levillainc9285912015-12-18 10:38:42 +00003855void CodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
3856 // TODO (ported from quick): revisit ARM barrier kinds.
3857 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
Calin Juravle52c48962014-12-16 17:02:57 +00003858 switch (kind) {
3859 case MemBarrierKind::kAnyStore:
3860 case MemBarrierKind::kLoadAny:
3861 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003862 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00003863 break;
3864 }
3865 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003866 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00003867 break;
3868 }
3869 default:
3870 LOG(FATAL) << "Unexpected memory barrier " << kind;
3871 }
Kenny Root1d8199d2015-06-02 11:01:10 -07003872 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00003873}
3874
3875void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
3876 uint32_t offset,
3877 Register out_lo,
3878 Register out_hi) {
3879 if (offset != 0) {
Roland Levillain3b359c72015-11-17 19:35:12 +00003880 // Ensure `out_lo` is different from `addr`, so that loading
3881 // `offset` into `out_lo` does not clutter `addr`.
3882 DCHECK_NE(out_lo, addr);
Calin Juravle52c48962014-12-16 17:02:57 +00003883 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003884 __ add(IP, addr, ShifterOperand(out_lo));
3885 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003886 }
3887 __ ldrexd(out_lo, out_hi, addr);
3888}
3889
3890void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
3891 uint32_t offset,
3892 Register value_lo,
3893 Register value_hi,
3894 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00003895 Register temp2,
3896 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003897 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00003898 if (offset != 0) {
3899 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003900 __ add(IP, addr, ShifterOperand(temp1));
3901 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003902 }
3903 __ Bind(&fail);
3904 // We need a load followed by store. (The address used in a STREX instruction must
3905 // be the same as the address in the most recently executed LDREX instruction.)
3906 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00003907 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003908 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003909 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00003910}
3911
3912void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3913 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3914
Nicolas Geoffray39468442014-09-02 15:17:15 +01003915 LocationSummary* locations =
3916 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003917 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003918
Calin Juravle52c48962014-12-16 17:02:57 +00003919 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003920 if (Primitive::IsFloatingPointType(field_type)) {
3921 locations->SetInAt(1, Location::RequiresFpuRegister());
3922 } else {
3923 locations->SetInAt(1, Location::RequiresRegister());
3924 }
3925
Calin Juravle52c48962014-12-16 17:02:57 +00003926 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00003927 bool generate_volatile = field_info.IsVolatile()
3928 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003929 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01003930 bool needs_write_barrier =
3931 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003932 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00003933 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01003934 if (needs_write_barrier) {
3935 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003936 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003937 } else if (generate_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00003938 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00003939 // - registers need to be consecutive
3940 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00003941 // We don't test for ARM yet, and the assertion makes sure that we
3942 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00003943 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3944
3945 locations->AddTemp(Location::RequiresRegister());
3946 locations->AddTemp(Location::RequiresRegister());
3947 if (field_type == Primitive::kPrimDouble) {
3948 // For doubles we need two more registers to copy the value.
3949 locations->AddTemp(Location::RegisterLocation(R2));
3950 locations->AddTemp(Location::RegisterLocation(R3));
3951 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003952 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003953}
3954
Calin Juravle52c48962014-12-16 17:02:57 +00003955void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003956 const FieldInfo& field_info,
3957 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003958 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3959
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003960 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003961 Register base = locations->InAt(0).AsRegister<Register>();
3962 Location value = locations->InAt(1);
3963
3964 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003965 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00003966 Primitive::Type field_type = field_info.GetFieldType();
3967 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01003968 bool needs_write_barrier =
3969 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003970
3971 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00003972 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00003973 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003974
3975 switch (field_type) {
3976 case Primitive::kPrimBoolean:
3977 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003978 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003979 break;
3980 }
3981
3982 case Primitive::kPrimShort:
3983 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003984 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003985 break;
3986 }
3987
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003988 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003989 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01003990 if (kPoisonHeapReferences && needs_write_barrier) {
3991 // Note that in the case where `value` is a null reference,
3992 // we do not enter this block, as a null reference does not
3993 // need poisoning.
3994 DCHECK_EQ(field_type, Primitive::kPrimNot);
3995 Register temp = locations->GetTemp(0).AsRegister<Register>();
3996 __ Mov(temp, value.AsRegister<Register>());
3997 __ PoisonHeapReference(temp);
3998 __ StoreToOffset(kStoreWord, temp, base, offset);
3999 } else {
4000 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
4001 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004002 break;
4003 }
4004
4005 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00004006 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004007 GenerateWideAtomicStore(base, offset,
4008 value.AsRegisterPairLow<Register>(),
4009 value.AsRegisterPairHigh<Register>(),
4010 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00004011 locations->GetTemp(1).AsRegister<Register>(),
4012 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004013 } else {
4014 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004015 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004016 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004017 break;
4018 }
4019
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004020 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004021 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004022 break;
4023 }
4024
4025 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004026 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004027 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004028 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
4029 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
4030
4031 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
4032
4033 GenerateWideAtomicStore(base, offset,
4034 value_reg_lo,
4035 value_reg_hi,
4036 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00004037 locations->GetTemp(3).AsRegister<Register>(),
4038 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004039 } else {
4040 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004041 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004042 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004043 break;
4044 }
4045
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004046 case Primitive::kPrimVoid:
4047 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004048 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004049 }
Calin Juravle52c48962014-12-16 17:02:57 +00004050
Calin Juravle77520bc2015-01-12 18:45:46 +00004051 // Longs and doubles are handled in the switch.
4052 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
4053 codegen_->MaybeRecordImplicitNullCheck(instruction);
4054 }
4055
4056 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4057 Register temp = locations->GetTemp(0).AsRegister<Register>();
4058 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004059 codegen_->MarkGCCard(
4060 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004061 }
4062
Calin Juravle52c48962014-12-16 17:02:57 +00004063 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004064 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004065 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004066}
4067
Calin Juravle52c48962014-12-16 17:02:57 +00004068void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4069 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain3b359c72015-11-17 19:35:12 +00004070
4071 bool object_field_get_with_read_barrier =
4072 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004073 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004074 new (GetGraph()->GetArena()) LocationSummary(instruction,
4075 object_field_get_with_read_barrier ?
4076 LocationSummary::kCallOnSlowPath :
4077 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004078 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004079
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004080 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00004081 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004082 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain3b359c72015-11-17 19:35:12 +00004083 // The output overlaps in case of volatile long: we don't want the
4084 // code generated by GenerateWideAtomicLoad to overwrite the
4085 // object's location. Likewise, in the case of an object field get
4086 // with read barriers enabled, we do not want the load to overwrite
4087 // the object's location, as we need it to emit the read barrier.
4088 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
4089 object_field_get_with_read_barrier;
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01004090
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004091 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4092 locations->SetOut(Location::RequiresFpuRegister());
4093 } else {
4094 locations->SetOut(Location::RequiresRegister(),
4095 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
4096 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004097 if (volatile_for_double) {
Roland Levillainc9285912015-12-18 10:38:42 +00004098 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00004099 // - registers need to be consecutive
4100 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00004101 // We don't test for ARM yet, and the assertion makes sure that we
4102 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00004103 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4104 locations->AddTemp(Location::RequiresRegister());
4105 locations->AddTemp(Location::RequiresRegister());
Roland Levillainc9285912015-12-18 10:38:42 +00004106 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4107 // We need a temporary register for the read barrier marking slow
4108 // path in CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier.
4109 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004110 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004111}
4112
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004113Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
4114 Opcode opcode) {
4115 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
4116 if (constant->IsConstant() &&
4117 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
4118 return Location::ConstantLocation(constant->AsConstant());
4119 }
4120 return Location::RequiresRegister();
4121}
4122
4123bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
4124 Opcode opcode) {
4125 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
4126 if (Primitive::Is64BitType(input_cst->GetType())) {
4127 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode) &&
4128 CanEncodeConstantAsImmediate(High32Bits(value), opcode);
4129 } else {
4130 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
4131 }
4132}
4133
4134bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value, Opcode opcode) {
4135 ShifterOperand so;
4136 ArmAssembler* assembler = codegen_->GetAssembler();
4137 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, &so)) {
4138 return true;
4139 }
4140 Opcode neg_opcode = kNoOperand;
4141 switch (opcode) {
4142 case AND:
4143 neg_opcode = BIC;
4144 break;
4145 case ORR:
4146 neg_opcode = ORN;
4147 break;
4148 default:
4149 return false;
4150 }
4151 return assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, neg_opcode, ~value, &so);
4152}
4153
Calin Juravle52c48962014-12-16 17:02:57 +00004154void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
4155 const FieldInfo& field_info) {
4156 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004157
Calin Juravle52c48962014-12-16 17:02:57 +00004158 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004159 Location base_loc = locations->InAt(0);
4160 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004161 Location out = locations->Out();
4162 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004163 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004164 Primitive::Type field_type = field_info.GetFieldType();
4165 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4166
4167 switch (field_type) {
Roland Levillainc9285912015-12-18 10:38:42 +00004168 case Primitive::kPrimBoolean:
Calin Juravle52c48962014-12-16 17:02:57 +00004169 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004170 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004171
Roland Levillainc9285912015-12-18 10:38:42 +00004172 case Primitive::kPrimByte:
Calin Juravle52c48962014-12-16 17:02:57 +00004173 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004174 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004175
Roland Levillainc9285912015-12-18 10:38:42 +00004176 case Primitive::kPrimShort:
Calin Juravle52c48962014-12-16 17:02:57 +00004177 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004178 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004179
Roland Levillainc9285912015-12-18 10:38:42 +00004180 case Primitive::kPrimChar:
Calin Juravle52c48962014-12-16 17:02:57 +00004181 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004182 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004183
4184 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004185 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004186 break;
Roland Levillainc9285912015-12-18 10:38:42 +00004187
4188 case Primitive::kPrimNot: {
4189 // /* HeapReference<Object> */ out = *(base + offset)
4190 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4191 Location temp_loc = locations->GetTemp(0);
4192 // Note that a potential implicit null check is handled in this
4193 // CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier call.
4194 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4195 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4196 if (is_volatile) {
4197 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4198 }
4199 } else {
4200 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
4201 codegen_->MaybeRecordImplicitNullCheck(instruction);
4202 if (is_volatile) {
4203 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4204 }
4205 // If read barriers are enabled, emit read barriers other than
4206 // Baker's using a slow path (and also unpoison the loaded
4207 // reference, if heap poisoning is enabled).
4208 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4209 }
4210 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004211 }
4212
Roland Levillainc9285912015-12-18 10:38:42 +00004213 case Primitive::kPrimLong:
Calin Juravle34166012014-12-19 17:22:29 +00004214 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004215 GenerateWideAtomicLoad(base, offset,
4216 out.AsRegisterPairLow<Register>(),
4217 out.AsRegisterPairHigh<Register>());
4218 } else {
4219 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
4220 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004221 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004222
Roland Levillainc9285912015-12-18 10:38:42 +00004223 case Primitive::kPrimFloat:
Calin Juravle52c48962014-12-16 17:02:57 +00004224 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004225 break;
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004226
4227 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004228 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004229 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004230 Register lo = locations->GetTemp(0).AsRegister<Register>();
4231 Register hi = locations->GetTemp(1).AsRegister<Register>();
4232 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00004233 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004234 __ vmovdrr(out_reg, lo, hi);
4235 } else {
4236 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004237 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004238 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004239 break;
4240 }
4241
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004242 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004243 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004244 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004245 }
Calin Juravle52c48962014-12-16 17:02:57 +00004246
Roland Levillainc9285912015-12-18 10:38:42 +00004247 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
4248 // Potential implicit null checks, in the case of reference or
4249 // double fields, are handled in the previous switch statement.
4250 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004251 codegen_->MaybeRecordImplicitNullCheck(instruction);
4252 }
4253
Calin Juravle52c48962014-12-16 17:02:57 +00004254 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004255 if (field_type == Primitive::kPrimNot) {
4256 // Memory barriers, in the case of references, are also handled
4257 // in the previous switch statement.
4258 } else {
4259 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4260 }
Roland Levillain4d027112015-07-01 15:41:14 +01004261 }
Calin Juravle52c48962014-12-16 17:02:57 +00004262}
4263
4264void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4265 HandleFieldSet(instruction, instruction->GetFieldInfo());
4266}
4267
4268void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004269 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004270}
4271
4272void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4273 HandleFieldGet(instruction, instruction->GetFieldInfo());
4274}
4275
4276void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4277 HandleFieldGet(instruction, instruction->GetFieldInfo());
4278}
4279
4280void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4281 HandleFieldGet(instruction, instruction->GetFieldInfo());
4282}
4283
4284void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4285 HandleFieldGet(instruction, instruction->GetFieldInfo());
4286}
4287
4288void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4289 HandleFieldSet(instruction, instruction->GetFieldInfo());
4290}
4291
4292void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004293 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004294}
4295
Calin Juravlee460d1d2015-09-29 04:52:17 +01004296void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
4297 HUnresolvedInstanceFieldGet* instruction) {
4298 FieldAccessCallingConventionARM calling_convention;
4299 codegen_->CreateUnresolvedFieldLocationSummary(
4300 instruction, instruction->GetFieldType(), calling_convention);
4301}
4302
4303void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
4304 HUnresolvedInstanceFieldGet* instruction) {
4305 FieldAccessCallingConventionARM calling_convention;
4306 codegen_->GenerateUnresolvedFieldAccess(instruction,
4307 instruction->GetFieldType(),
4308 instruction->GetFieldIndex(),
4309 instruction->GetDexPc(),
4310 calling_convention);
4311}
4312
4313void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
4314 HUnresolvedInstanceFieldSet* instruction) {
4315 FieldAccessCallingConventionARM calling_convention;
4316 codegen_->CreateUnresolvedFieldLocationSummary(
4317 instruction, instruction->GetFieldType(), calling_convention);
4318}
4319
4320void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
4321 HUnresolvedInstanceFieldSet* instruction) {
4322 FieldAccessCallingConventionARM calling_convention;
4323 codegen_->GenerateUnresolvedFieldAccess(instruction,
4324 instruction->GetFieldType(),
4325 instruction->GetFieldIndex(),
4326 instruction->GetDexPc(),
4327 calling_convention);
4328}
4329
4330void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
4331 HUnresolvedStaticFieldGet* instruction) {
4332 FieldAccessCallingConventionARM calling_convention;
4333 codegen_->CreateUnresolvedFieldLocationSummary(
4334 instruction, instruction->GetFieldType(), calling_convention);
4335}
4336
4337void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
4338 HUnresolvedStaticFieldGet* instruction) {
4339 FieldAccessCallingConventionARM calling_convention;
4340 codegen_->GenerateUnresolvedFieldAccess(instruction,
4341 instruction->GetFieldType(),
4342 instruction->GetFieldIndex(),
4343 instruction->GetDexPc(),
4344 calling_convention);
4345}
4346
4347void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
4348 HUnresolvedStaticFieldSet* instruction) {
4349 FieldAccessCallingConventionARM calling_convention;
4350 codegen_->CreateUnresolvedFieldLocationSummary(
4351 instruction, instruction->GetFieldType(), calling_convention);
4352}
4353
4354void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
4355 HUnresolvedStaticFieldSet* instruction) {
4356 FieldAccessCallingConventionARM calling_convention;
4357 codegen_->GenerateUnresolvedFieldAccess(instruction,
4358 instruction->GetFieldType(),
4359 instruction->GetFieldIndex(),
4360 instruction->GetDexPc(),
4361 calling_convention);
4362}
4363
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004364void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004365 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4366 ? LocationSummary::kCallOnSlowPath
4367 : LocationSummary::kNoCall;
4368 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravle77520bc2015-01-12 18:45:46 +00004369 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004370 if (instruction->HasUses()) {
4371 locations->SetOut(Location::SameAsFirstInput());
4372 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004373}
4374
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004375void InstructionCodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004376 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4377 return;
4378 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004379 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004380
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004381 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
4382 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4383}
4384
4385void InstructionCodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004386 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004387 codegen_->AddSlowPath(slow_path);
4388
4389 LocationSummary* locations = instruction->GetLocations();
4390 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004391
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004392 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004393}
4394
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004395void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004396 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004397 GenerateImplicitNullCheck(instruction);
4398 } else {
4399 GenerateExplicitNullCheck(instruction);
4400 }
4401}
4402
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004403void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004404 bool object_array_get_with_read_barrier =
4405 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004406 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004407 new (GetGraph()->GetArena()) LocationSummary(instruction,
4408 object_array_get_with_read_barrier ?
4409 LocationSummary::kCallOnSlowPath :
4410 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004411 locations->SetInAt(0, Location::RequiresRegister());
4412 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004413 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4414 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4415 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004416 // The output overlaps in the case of an object array get with
4417 // read barriers enabled: we do not want the move to overwrite the
4418 // array's location, as we need it to emit the read barrier.
4419 locations->SetOut(
4420 Location::RequiresRegister(),
4421 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004422 }
Roland Levillainc9285912015-12-18 10:38:42 +00004423 // We need a temporary register for the read barrier marking slow
4424 // path in CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier.
4425 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4426 locations->AddTemp(Location::RequiresRegister());
4427 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004428}
4429
4430void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
4431 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004432 Location obj_loc = locations->InAt(0);
4433 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004434 Location index = locations->InAt(1);
Roland Levillainc9285912015-12-18 10:38:42 +00004435 Location out_loc = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004436
Roland Levillainc9285912015-12-18 10:38:42 +00004437 Primitive::Type type = instruction->GetType();
Roland Levillain4d027112015-07-01 15:41:14 +01004438 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004439 case Primitive::kPrimBoolean: {
4440 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004441 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004442 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004443 size_t offset =
4444 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004445 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
4446 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004447 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004448 __ LoadFromOffset(kLoadUnsignedByte, out, IP, data_offset);
4449 }
4450 break;
4451 }
4452
4453 case Primitive::kPrimByte: {
4454 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004455 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004456 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004457 size_t offset =
4458 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004459 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
4460 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004461 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004462 __ LoadFromOffset(kLoadSignedByte, out, IP, data_offset);
4463 }
4464 break;
4465 }
4466
4467 case Primitive::kPrimShort: {
4468 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004469 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004470 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004471 size_t offset =
4472 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004473 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
4474 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004475 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004476 __ LoadFromOffset(kLoadSignedHalfword, out, IP, data_offset);
4477 }
4478 break;
4479 }
4480
4481 case Primitive::kPrimChar: {
4482 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004483 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004484 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004485 size_t offset =
4486 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004487 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
4488 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004489 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004490 __ LoadFromOffset(kLoadUnsignedHalfword, out, IP, data_offset);
4491 }
4492 break;
4493 }
4494
Roland Levillainc9285912015-12-18 10:38:42 +00004495 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004496 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004497 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004498 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004499 size_t offset =
4500 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004501 __ LoadFromOffset(kLoadWord, out, obj, offset);
4502 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004503 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004504 __ LoadFromOffset(kLoadWord, out, IP, data_offset);
4505 }
4506 break;
4507 }
4508
Roland Levillainc9285912015-12-18 10:38:42 +00004509 case Primitive::kPrimNot: {
4510 static_assert(
4511 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4512 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
4513 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4514 // /* HeapReference<Object> */ out =
4515 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4516 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4517 Location temp = locations->GetTemp(0);
4518 // Note that a potential implicit null check is handled in this
4519 // CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier call.
4520 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4521 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4522 } else {
4523 Register out = out_loc.AsRegister<Register>();
4524 if (index.IsConstant()) {
4525 size_t offset =
4526 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4527 __ LoadFromOffset(kLoadWord, out, obj, offset);
4528 codegen_->MaybeRecordImplicitNullCheck(instruction);
4529 // If read barriers are enabled, emit read barriers other than
4530 // Baker's using a slow path (and also unpoison the loaded
4531 // reference, if heap poisoning is enabled).
4532 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4533 } else {
4534 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4535 __ LoadFromOffset(kLoadWord, out, IP, data_offset);
4536 codegen_->MaybeRecordImplicitNullCheck(instruction);
4537 // If read barriers are enabled, emit read barriers other than
4538 // Baker's using a slow path (and also unpoison the loaded
4539 // reference, if heap poisoning is enabled).
4540 codegen_->MaybeGenerateReadBarrierSlow(
4541 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4542 }
4543 }
4544 break;
4545 }
4546
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004547 case Primitive::kPrimLong: {
4548 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004549 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004550 size_t offset =
4551 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004552 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004553 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004554 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00004555 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004556 }
4557 break;
4558 }
4559
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004560 case Primitive::kPrimFloat: {
4561 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004562 SRegister out = out_loc.AsFpuRegister<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004563 if (index.IsConstant()) {
4564 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004565 __ LoadSFromOffset(out, obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004566 } else {
4567 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillainc9285912015-12-18 10:38:42 +00004568 __ LoadSFromOffset(out, IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004569 }
4570 break;
4571 }
4572
4573 case Primitive::kPrimDouble: {
4574 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004575 SRegister out = out_loc.AsFpuRegisterPairLow<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004576 if (index.IsConstant()) {
4577 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004578 __ LoadDFromOffset(FromLowSToD(out), obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004579 } else {
4580 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00004581 __ LoadDFromOffset(FromLowSToD(out), IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004582 }
4583 break;
4584 }
4585
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004586 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004587 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004588 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004589 }
Roland Levillain4d027112015-07-01 15:41:14 +01004590
4591 if (type == Primitive::kPrimNot) {
Roland Levillainc9285912015-12-18 10:38:42 +00004592 // Potential implicit null checks, in the case of reference
4593 // arrays, are handled in the previous switch statement.
4594 } else {
4595 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004596 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004597}
4598
4599void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004600 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004601
4602 bool needs_write_barrier =
4603 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain3b359c72015-11-17 19:35:12 +00004604 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
4605 bool object_array_set_with_read_barrier =
4606 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004607
Nicolas Geoffray39468442014-09-02 15:17:15 +01004608 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004609 instruction,
Roland Levillain3b359c72015-11-17 19:35:12 +00004610 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
4611 LocationSummary::kCallOnSlowPath :
4612 LocationSummary::kNoCall);
4613
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004614 locations->SetInAt(0, Location::RequiresRegister());
4615 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4616 if (Primitive::IsFloatingPointType(value_type)) {
4617 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004618 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004619 locations->SetInAt(2, Location::RequiresRegister());
4620 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004621 if (needs_write_barrier) {
4622 // Temporary registers for the write barrier.
4623 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00004624 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004625 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004626}
4627
4628void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
4629 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004630 Location array_loc = locations->InAt(0);
4631 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004632 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004633 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain3b359c72015-11-17 19:35:12 +00004634 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004635 bool needs_write_barrier =
4636 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004637
4638 switch (value_type) {
4639 case Primitive::kPrimBoolean:
4640 case Primitive::kPrimByte: {
4641 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004642 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004643 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004644 size_t offset =
4645 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004646 __ StoreToOffset(kStoreByte, value, array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004647 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004648 __ add(IP, array, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004649 __ StoreToOffset(kStoreByte, value, IP, data_offset);
4650 }
4651 break;
4652 }
4653
4654 case Primitive::kPrimShort:
4655 case Primitive::kPrimChar: {
4656 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004657 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004658 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004659 size_t offset =
4660 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004661 __ StoreToOffset(kStoreHalfword, value, array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004662 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004663 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004664 __ StoreToOffset(kStoreHalfword, value, IP, data_offset);
4665 }
4666 break;
4667 }
4668
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004669 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004670 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00004671 Location value_loc = locations->InAt(2);
4672 Register value = value_loc.AsRegister<Register>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004673 Register source = value;
4674
4675 if (instruction->InputAt(2)->IsNullConstant()) {
4676 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004677 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004678 size_t offset =
4679 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004680 __ StoreToOffset(kStoreWord, source, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004681 } else {
4682 DCHECK(index.IsRegister()) << index;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004683 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillain4d027112015-07-01 15:41:14 +01004684 __ StoreToOffset(kStoreWord, source, IP, data_offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004685 }
Roland Levillain3b359c72015-11-17 19:35:12 +00004686 DCHECK(!needs_write_barrier);
4687 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004688 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004689 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004690
4691 DCHECK(needs_write_barrier);
4692 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
4693 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
4694 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4695 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4696 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4697 Label done;
4698 SlowPathCode* slow_path = nullptr;
4699
Roland Levillain3b359c72015-11-17 19:35:12 +00004700 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004701 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
4702 codegen_->AddSlowPath(slow_path);
4703 if (instruction->GetValueCanBeNull()) {
4704 Label non_zero;
4705 __ CompareAndBranchIfNonZero(value, &non_zero);
4706 if (index.IsConstant()) {
4707 size_t offset =
4708 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4709 __ StoreToOffset(kStoreWord, value, array, offset);
4710 } else {
4711 DCHECK(index.IsRegister()) << index;
4712 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4713 __ StoreToOffset(kStoreWord, value, IP, data_offset);
4714 }
4715 codegen_->MaybeRecordImplicitNullCheck(instruction);
4716 __ b(&done);
4717 __ Bind(&non_zero);
4718 }
4719
Roland Levillain3b359c72015-11-17 19:35:12 +00004720 if (kEmitCompilerReadBarrier) {
4721 // When read barriers are enabled, the type checking
4722 // instrumentation requires two read barriers:
4723 //
4724 // __ Mov(temp2, temp1);
4725 // // /* HeapReference<Class> */ temp1 = temp1->component_type_
4726 // __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
Roland Levillainc9285912015-12-18 10:38:42 +00004727 // codegen_->GenerateReadBarrierSlow(
Roland Levillain3b359c72015-11-17 19:35:12 +00004728 // instruction, temp1_loc, temp1_loc, temp2_loc, component_offset);
4729 //
4730 // // /* HeapReference<Class> */ temp2 = value->klass_
4731 // __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
Roland Levillainc9285912015-12-18 10:38:42 +00004732 // codegen_->GenerateReadBarrierSlow(
Roland Levillain3b359c72015-11-17 19:35:12 +00004733 // instruction, temp2_loc, temp2_loc, value_loc, class_offset, temp1_loc);
4734 //
4735 // __ cmp(temp1, ShifterOperand(temp2));
4736 //
4737 // However, the second read barrier may trash `temp`, as it
4738 // is a temporary register, and as such would not be saved
4739 // along with live registers before calling the runtime (nor
4740 // restored afterwards). So in this case, we bail out and
4741 // delegate the work to the array set slow path.
4742 //
4743 // TODO: Extend the register allocator to support a new
4744 // "(locally) live temp" location so as to avoid always
4745 // going into the slow path when read barriers are enabled.
4746 __ b(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004747 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004748 // /* HeapReference<Class> */ temp1 = array->klass_
4749 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
4750 codegen_->MaybeRecordImplicitNullCheck(instruction);
4751 __ MaybeUnpoisonHeapReference(temp1);
4752
4753 // /* HeapReference<Class> */ temp1 = temp1->component_type_
4754 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
4755 // /* HeapReference<Class> */ temp2 = value->klass_
4756 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
4757 // If heap poisoning is enabled, no need to unpoison `temp1`
4758 // nor `temp2`, as we are comparing two poisoned references.
4759 __ cmp(temp1, ShifterOperand(temp2));
4760
4761 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4762 Label do_put;
4763 __ b(&do_put, EQ);
4764 // If heap poisoning is enabled, the `temp1` reference has
4765 // not been unpoisoned yet; unpoison it now.
4766 __ MaybeUnpoisonHeapReference(temp1);
4767
4768 // /* HeapReference<Class> */ temp1 = temp1->super_class_
4769 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
4770 // If heap poisoning is enabled, no need to unpoison
4771 // `temp1`, as we are comparing against null below.
4772 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
4773 __ Bind(&do_put);
4774 } else {
4775 __ b(slow_path->GetEntryLabel(), NE);
4776 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004777 }
4778 }
4779
4780 if (kPoisonHeapReferences) {
4781 // Note that in the case where `value` is a null reference,
4782 // we do not enter this block, as a null reference does not
4783 // need poisoning.
4784 DCHECK_EQ(value_type, Primitive::kPrimNot);
4785 __ Mov(temp1, value);
4786 __ PoisonHeapReference(temp1);
4787 source = temp1;
4788 }
4789
4790 if (index.IsConstant()) {
4791 size_t offset =
4792 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4793 __ StoreToOffset(kStoreWord, source, array, offset);
4794 } else {
4795 DCHECK(index.IsRegister()) << index;
4796 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4797 __ StoreToOffset(kStoreWord, source, IP, data_offset);
4798 }
4799
Roland Levillain3b359c72015-11-17 19:35:12 +00004800 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004801 codegen_->MaybeRecordImplicitNullCheck(instruction);
4802 }
4803
4804 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
4805
4806 if (done.IsLinked()) {
4807 __ Bind(&done);
4808 }
4809
4810 if (slow_path != nullptr) {
4811 __ Bind(slow_path->GetExitLabel());
4812 }
4813
4814 break;
4815 }
4816
4817 case Primitive::kPrimInt: {
4818 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4819 Register value = locations->InAt(2).AsRegister<Register>();
4820 if (index.IsConstant()) {
4821 size_t offset =
4822 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4823 __ StoreToOffset(kStoreWord, value, array, offset);
4824 } else {
4825 DCHECK(index.IsRegister()) << index;
4826 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4827 __ StoreToOffset(kStoreWord, value, IP, data_offset);
4828 }
4829
4830 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004831 break;
4832 }
4833
4834 case Primitive::kPrimLong: {
4835 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004836 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004837 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004838 size_t offset =
4839 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004840 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004841 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004842 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004843 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004844 }
4845 break;
4846 }
4847
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004848 case Primitive::kPrimFloat: {
4849 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4850 Location value = locations->InAt(2);
4851 DCHECK(value.IsFpuRegister());
4852 if (index.IsConstant()) {
4853 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004854 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004855 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004856 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004857 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
4858 }
4859 break;
4860 }
4861
4862 case Primitive::kPrimDouble: {
4863 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4864 Location value = locations->InAt(2);
4865 DCHECK(value.IsFpuRegisterPair());
4866 if (index.IsConstant()) {
4867 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004868 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004869 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004870 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004871 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
4872 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004873
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004874 break;
4875 }
4876
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004877 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004878 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004879 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004880 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004881
4882 // Ints and objects are handled in the switch.
4883 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
4884 codegen_->MaybeRecordImplicitNullCheck(instruction);
4885 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004886}
4887
4888void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004889 LocationSummary* locations =
4890 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004891 locations->SetInAt(0, Location::RequiresRegister());
4892 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004893}
4894
4895void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
4896 LocationSummary* locations = instruction->GetLocations();
4897 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004898 Register obj = locations->InAt(0).AsRegister<Register>();
4899 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004900 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004901 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004902}
4903
4904void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004905 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4906 ? LocationSummary::kCallOnSlowPath
4907 : LocationSummary::kNoCall;
4908 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004909 locations->SetInAt(0, Location::RequiresRegister());
4910 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004911 if (instruction->HasUses()) {
4912 locations->SetOut(Location::SameAsFirstInput());
4913 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004914}
4915
4916void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
4917 LocationSummary* locations = instruction->GetLocations();
Andreas Gampe85b62f22015-09-09 13:15:38 -07004918 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004919 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004920 codegen_->AddSlowPath(slow_path);
4921
Roland Levillain271ab9c2014-11-27 15:23:57 +00004922 Register index = locations->InAt(0).AsRegister<Register>();
4923 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004924
4925 __ cmp(index, ShifterOperand(length));
Roland Levillain4fa13f62015-07-06 18:11:54 +01004926 __ b(slow_path->GetEntryLabel(), HS);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004927}
4928
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004929void CodeGeneratorARM::MarkGCCard(Register temp,
4930 Register card,
4931 Register object,
4932 Register value,
4933 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004934 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004935 if (can_be_null) {
4936 __ CompareAndBranchIfZero(value, &is_null);
4937 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004938 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmWordSize>().Int32Value());
4939 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
4940 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004941 if (can_be_null) {
4942 __ Bind(&is_null);
4943 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004944}
4945
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004946void LocationsBuilderARM::VisitTemporary(HTemporary* temp) {
4947 temp->SetLocations(nullptr);
4948}
4949
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004950void InstructionCodeGeneratorARM::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004951 // Nothing to do, this is driven by the code generator.
4952}
4953
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004954void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004955 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004956}
4957
4958void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004959 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4960}
4961
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004962void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
4963 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4964}
4965
4966void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004967 HBasicBlock* block = instruction->GetBlock();
4968 if (block->GetLoopInformation() != nullptr) {
4969 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4970 // The back edge will generate the suspend check.
4971 return;
4972 }
4973 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4974 // The goto will generate the suspend check.
4975 return;
4976 }
4977 GenerateSuspendCheck(instruction, nullptr);
4978}
4979
4980void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
4981 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004982 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004983 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
4984 if (slow_path == nullptr) {
4985 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
4986 instruction->SetSlowPath(slow_path);
4987 codegen_->AddSlowPath(slow_path);
4988 if (successor != nullptr) {
4989 DCHECK(successor->IsLoopHeader());
4990 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4991 }
4992 } else {
4993 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4994 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004995
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00004996 __ LoadFromOffset(
4997 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmWordSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004998 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004999 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005000 __ Bind(slow_path->GetReturnLabel());
5001 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005002 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005003 __ b(slow_path->GetEntryLabel());
5004 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005005}
5006
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005007ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
5008 return codegen_->GetAssembler();
5009}
5010
5011void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005012 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005013 Location source = move->GetSource();
5014 Location destination = move->GetDestination();
5015
5016 if (source.IsRegister()) {
5017 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005018 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005019 } else {
5020 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005021 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005022 SP, destination.GetStackIndex());
5023 }
5024 } else if (source.IsStackSlot()) {
5025 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005026 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005027 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005028 } else if (destination.IsFpuRegister()) {
5029 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005030 } else {
5031 DCHECK(destination.IsStackSlot());
5032 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
5033 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5034 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005035 } else if (source.IsFpuRegister()) {
5036 if (destination.IsFpuRegister()) {
5037 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005038 } else {
5039 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005040 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
5041 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005042 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005043 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005044 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
5045 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005046 } else if (destination.IsRegisterPair()) {
5047 DCHECK(ExpectedPairLayout(destination));
5048 __ LoadFromOffset(
5049 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
5050 } else {
5051 DCHECK(destination.IsFpuRegisterPair()) << destination;
5052 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5053 SP,
5054 source.GetStackIndex());
5055 }
5056 } else if (source.IsRegisterPair()) {
5057 if (destination.IsRegisterPair()) {
5058 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
5059 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
5060 } else {
5061 DCHECK(destination.IsDoubleStackSlot()) << destination;
5062 DCHECK(ExpectedPairLayout(source));
5063 __ StoreToOffset(
5064 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
5065 }
5066 } else if (source.IsFpuRegisterPair()) {
5067 if (destination.IsFpuRegisterPair()) {
5068 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5069 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5070 } else {
5071 DCHECK(destination.IsDoubleStackSlot()) << destination;
5072 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
5073 SP,
5074 destination.GetStackIndex());
5075 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005076 } else {
5077 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005078 HConstant* constant = source.GetConstant();
5079 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5080 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005081 if (destination.IsRegister()) {
5082 __ LoadImmediate(destination.AsRegister<Register>(), value);
5083 } else {
5084 DCHECK(destination.IsStackSlot());
5085 __ LoadImmediate(IP, value);
5086 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5087 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005088 } else if (constant->IsLongConstant()) {
5089 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005090 if (destination.IsRegisterPair()) {
5091 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
5092 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005093 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005094 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005095 __ LoadImmediate(IP, Low32Bits(value));
5096 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5097 __ LoadImmediate(IP, High32Bits(value));
5098 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5099 }
5100 } else if (constant->IsDoubleConstant()) {
5101 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005102 if (destination.IsFpuRegisterPair()) {
5103 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005104 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005105 DCHECK(destination.IsDoubleStackSlot()) << destination;
5106 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005107 __ LoadImmediate(IP, Low32Bits(int_value));
5108 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5109 __ LoadImmediate(IP, High32Bits(int_value));
5110 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5111 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005112 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005113 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005114 float value = constant->AsFloatConstant()->GetValue();
5115 if (destination.IsFpuRegister()) {
5116 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
5117 } else {
5118 DCHECK(destination.IsStackSlot());
5119 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
5120 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5121 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005122 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005123 }
5124}
5125
5126void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
5127 __ Mov(IP, reg);
5128 __ LoadFromOffset(kLoadWord, reg, SP, mem);
5129 __ StoreToOffset(kStoreWord, IP, SP, mem);
5130}
5131
5132void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
5133 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
5134 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
5135 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
5136 SP, mem1 + stack_offset);
5137 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
5138 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
5139 SP, mem2 + stack_offset);
5140 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
5141}
5142
5143void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005144 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005145 Location source = move->GetSource();
5146 Location destination = move->GetDestination();
5147
5148 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005149 DCHECK_NE(source.AsRegister<Register>(), IP);
5150 DCHECK_NE(destination.AsRegister<Register>(), IP);
5151 __ Mov(IP, source.AsRegister<Register>());
5152 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
5153 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005154 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005155 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005156 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005157 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005158 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5159 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005160 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005161 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005162 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005163 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005164 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005165 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005166 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005167 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005168 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5169 destination.AsRegisterPairHigh<Register>(),
5170 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005171 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005172 Register low_reg = source.IsRegisterPair()
5173 ? source.AsRegisterPairLow<Register>()
5174 : destination.AsRegisterPairLow<Register>();
5175 int mem = source.IsRegisterPair()
5176 ? destination.GetStackIndex()
5177 : source.GetStackIndex();
5178 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005179 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005180 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005181 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005182 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005183 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
5184 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005185 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005186 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005187 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005188 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
5189 DRegister reg = source.IsFpuRegisterPair()
5190 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
5191 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
5192 int mem = source.IsFpuRegisterPair()
5193 ? destination.GetStackIndex()
5194 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005195 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005196 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005197 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005198 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
5199 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
5200 : destination.AsFpuRegister<SRegister>();
5201 int mem = source.IsFpuRegister()
5202 ? destination.GetStackIndex()
5203 : source.GetStackIndex();
5204
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005205 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005206 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005207 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005208 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005209 Exchange(source.GetStackIndex(), destination.GetStackIndex());
5210 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005211 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005212 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005213 }
5214}
5215
5216void ParallelMoveResolverARM::SpillScratch(int reg) {
5217 __ Push(static_cast<Register>(reg));
5218}
5219
5220void ParallelMoveResolverARM::RestoreScratch(int reg) {
5221 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005222}
5223
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005224void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01005225 InvokeRuntimeCallingConvention calling_convention;
5226 CodeGenerator::CreateLoadClassLocationSummary(
5227 cls,
5228 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Roland Levillain3b359c72015-11-17 19:35:12 +00005229 Location::RegisterLocation(R0),
5230 /* code_generator_supports_read_barrier */ true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005231}
5232
5233void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005234 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005235 if (cls->NeedsAccessCheck()) {
5236 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5237 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5238 cls,
5239 cls->GetDexPc(),
5240 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005241 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005242 return;
5243 }
5244
Roland Levillain3b359c72015-11-17 19:35:12 +00005245 Location out_loc = locations->Out();
5246 Register out = out_loc.AsRegister<Register>();
Calin Juravle580b6092015-10-06 17:35:58 +01005247 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005248
Calin Juravle580b6092015-10-06 17:35:58 +01005249 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005250 DCHECK(!cls->CanCallRuntime());
5251 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillainc9285912015-12-18 10:38:42 +00005252 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5253 GenerateGcRootFieldLoad(
5254 cls, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005255 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00005256 // /* GcRoot<mirror::Class>[] */ out =
5257 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005258 __ LoadFromOffset(kLoadWord,
5259 out,
5260 current_method,
Vladimir Marko05792b92015-08-03 11:56:49 +01005261 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
Roland Levillainc9285912015-12-18 10:38:42 +00005262 // /* GcRoot<mirror::Class> */ out = out[type_index]
5263 GenerateGcRootFieldLoad(cls, out_loc, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005264
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005265 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
5266 DCHECK(cls->CanCallRuntime());
5267 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
5268 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5269 codegen_->AddSlowPath(slow_path);
5270 if (!cls->IsInDexCache()) {
5271 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5272 }
5273 if (cls->MustGenerateClinitCheck()) {
5274 GenerateClassInitializationCheck(slow_path, out);
5275 } else {
5276 __ Bind(slow_path->GetExitLabel());
5277 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005278 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005279 }
5280}
5281
5282void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
5283 LocationSummary* locations =
5284 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5285 locations->SetInAt(0, Location::RequiresRegister());
5286 if (check->HasUses()) {
5287 locations->SetOut(Location::SameAsFirstInput());
5288 }
5289}
5290
5291void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005292 // We assume the class is not null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005293 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005294 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005295 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005296 GenerateClassInitializationCheck(slow_path,
5297 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005298}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005299
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005300void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005301 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005302 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
5303 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
5304 __ b(slow_path->GetEntryLabel(), LT);
5305 // Even if the initialized flag is set, we may be in a situation where caches are not synced
5306 // properly. Therefore, we do a memory fence.
5307 __ dmb(ISH);
5308 __ Bind(slow_path->GetExitLabel());
5309}
5310
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005311void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005312 LocationSummary::CallKind call_kind = (!load->IsInDexCache() || kEmitCompilerReadBarrier)
5313 ? LocationSummary::kCallOnSlowPath
5314 : LocationSummary::kNoCall;
5315 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005316 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005317 locations->SetOut(Location::RequiresRegister());
5318}
5319
5320void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005321 LocationSummary* locations = load->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005322 Location out_loc = locations->Out();
5323 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005324 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005325
Roland Levillainc9285912015-12-18 10:38:42 +00005326 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5327 GenerateGcRootFieldLoad(
5328 load, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Roland Levillain3b359c72015-11-17 19:35:12 +00005329 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
Mathieu Chartiereace4582014-11-24 18:29:54 -08005330 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
Roland Levillainc9285912015-12-18 10:38:42 +00005331 // /* GcRoot<mirror::String> */ out = out[string_index]
5332 GenerateGcRootFieldLoad(
5333 load, out_loc, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Roland Levillain3b359c72015-11-17 19:35:12 +00005334
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005335 if (!load->IsInDexCache()) {
5336 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
5337 codegen_->AddSlowPath(slow_path);
5338 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5339 __ Bind(slow_path->GetExitLabel());
5340 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005341}
5342
David Brazdilcb1c0552015-08-04 16:22:25 +01005343static int32_t GetExceptionTlsOffset() {
5344 return Thread::ExceptionOffset<kArmWordSize>().Int32Value();
5345}
5346
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005347void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
5348 LocationSummary* locations =
5349 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5350 locations->SetOut(Location::RequiresRegister());
5351}
5352
5353void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005354 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01005355 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
5356}
5357
5358void LocationsBuilderARM::VisitClearException(HClearException* clear) {
5359 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5360}
5361
5362void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005363 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01005364 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005365}
5366
5367void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
5368 LocationSummary* locations =
5369 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5370 InvokeRuntimeCallingConvention calling_convention;
5371 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5372}
5373
5374void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
5375 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00005376 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005377 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005378}
5379
Roland Levillainc9285912015-12-18 10:38:42 +00005380static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5381 return kEmitCompilerReadBarrier &&
5382 (kUseBakerReadBarrier ||
5383 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5384 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5385 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5386}
5387
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005388void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005389 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain3b359c72015-11-17 19:35:12 +00005390 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5391 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005392 case TypeCheckKind::kExactCheck:
5393 case TypeCheckKind::kAbstractClassCheck:
5394 case TypeCheckKind::kClassHierarchyCheck:
5395 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005396 call_kind =
5397 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005398 break;
5399 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005400 case TypeCheckKind::kUnresolvedCheck:
5401 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005402 call_kind = LocationSummary::kCallOnSlowPath;
5403 break;
5404 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005405
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005406 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain3b359c72015-11-17 19:35:12 +00005407 locations->SetInAt(0, Location::RequiresRegister());
5408 locations->SetInAt(1, Location::RequiresRegister());
5409 // The "out" register is used as a temporary, so it overlaps with the inputs.
5410 // Note that TypeCheckSlowPathARM uses this register too.
5411 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5412 // When read barriers are enabled, we need a temporary register for
5413 // some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00005414 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain3b359c72015-11-17 19:35:12 +00005415 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005416 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005417}
5418
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005419void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00005420 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005421 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005422 Location obj_loc = locations->InAt(0);
5423 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005424 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005425 Location out_loc = locations->Out();
5426 Register out = out_loc.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +00005427 Location temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
5428 locations->GetTemp(0) :
5429 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005430 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005431 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5432 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5433 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005434 Label done, zero;
Andreas Gampe85b62f22015-09-09 13:15:38 -07005435 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005436
5437 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005438 // avoid null check if we know obj is not null.
5439 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00005440 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005441 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005442
Roland Levillain3b359c72015-11-17 19:35:12 +00005443 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillainc9285912015-12-18 10:38:42 +00005444 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005445
Roland Levillainc9285912015-12-18 10:38:42 +00005446 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005447 case TypeCheckKind::kExactCheck: {
5448 __ cmp(out, ShifterOperand(cls));
5449 // Classes must be equal for the instanceof to succeed.
5450 __ b(&zero, NE);
5451 __ LoadImmediate(out, 1);
5452 __ b(&done);
5453 break;
5454 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005455
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005456 case TypeCheckKind::kAbstractClassCheck: {
5457 // If the class is abstract, we eagerly fetch the super class of the
5458 // object to avoid doing a comparison we know will fail.
5459 Label loop;
5460 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005461 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillainc9285912015-12-18 10:38:42 +00005462 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005463 // If `out` is null, we use it for the result, and jump to `done`.
5464 __ CompareAndBranchIfZero(out, &done);
5465 __ cmp(out, ShifterOperand(cls));
5466 __ b(&loop, NE);
5467 __ LoadImmediate(out, 1);
5468 if (zero.IsLinked()) {
5469 __ b(&done);
5470 }
5471 break;
5472 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005473
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005474 case TypeCheckKind::kClassHierarchyCheck: {
5475 // Walk over the class hierarchy to find a match.
5476 Label loop, success;
5477 __ Bind(&loop);
5478 __ cmp(out, ShifterOperand(cls));
5479 __ b(&success, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005480 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillainc9285912015-12-18 10:38:42 +00005481 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005482 __ CompareAndBranchIfNonZero(out, &loop);
5483 // If `out` is null, we use it for the result, and jump to `done`.
5484 __ b(&done);
5485 __ Bind(&success);
5486 __ LoadImmediate(out, 1);
5487 if (zero.IsLinked()) {
5488 __ b(&done);
5489 }
5490 break;
5491 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005492
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005493 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005494 // Do an exact check.
5495 Label exact_check;
5496 __ cmp(out, ShifterOperand(cls));
5497 __ b(&exact_check, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005498 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00005499 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillainc9285912015-12-18 10:38:42 +00005500 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005501 // If `out` is null, we use it for the result, and jump to `done`.
5502 __ CompareAndBranchIfZero(out, &done);
5503 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
5504 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
5505 __ CompareAndBranchIfNonZero(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005506 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005507 __ LoadImmediate(out, 1);
5508 __ b(&done);
5509 break;
5510 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005511
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005512 case TypeCheckKind::kArrayCheck: {
5513 __ cmp(out, ShifterOperand(cls));
5514 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain3b359c72015-11-17 19:35:12 +00005515 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5516 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005517 codegen_->AddSlowPath(slow_path);
5518 __ b(slow_path->GetEntryLabel(), NE);
5519 __ LoadImmediate(out, 1);
5520 if (zero.IsLinked()) {
5521 __ b(&done);
5522 }
5523 break;
5524 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005525
Calin Juravle98893e12015-10-02 21:05:03 +01005526 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005527 case TypeCheckKind::kInterfaceCheck: {
5528 // Note that we indeed only call on slow path, but we always go
5529 // into the slow path for the unresolved & interface check
5530 // cases.
5531 //
5532 // We cannot directly call the InstanceofNonTrivial runtime
5533 // entry point without resorting to a type checking slow path
5534 // here (i.e. by calling InvokeRuntime directly), as it would
5535 // require to assign fixed registers for the inputs of this
5536 // HInstanceOf instruction (following the runtime calling
5537 // convention), which might be cluttered by the potential first
5538 // read barrier emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00005539 //
5540 // TODO: Introduce a new runtime entry point taking the object
5541 // to test (instead of its class) as argument, and let it deal
5542 // with the read barrier issues. This will let us refactor this
5543 // case of the `switch` code as it was previously (with a direct
5544 // call to the runtime not using a type checking slow path).
5545 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00005546 DCHECK(locations->OnlyCallsOnSlowPath());
5547 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5548 /* is_fatal */ false);
5549 codegen_->AddSlowPath(slow_path);
5550 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005551 if (zero.IsLinked()) {
5552 __ b(&done);
5553 }
5554 break;
5555 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005556 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005557
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005558 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005559 __ Bind(&zero);
5560 __ LoadImmediate(out, 0);
5561 }
5562
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005563 if (done.IsLinked()) {
5564 __ Bind(&done);
5565 }
5566
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005567 if (slow_path != nullptr) {
5568 __ Bind(slow_path->GetExitLabel());
5569 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005570}
5571
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005572void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005573 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5574 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
5575
Roland Levillain3b359c72015-11-17 19:35:12 +00005576 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5577 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005578 case TypeCheckKind::kExactCheck:
5579 case TypeCheckKind::kAbstractClassCheck:
5580 case TypeCheckKind::kClassHierarchyCheck:
5581 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005582 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5583 LocationSummary::kCallOnSlowPath :
5584 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005585 break;
5586 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005587 case TypeCheckKind::kUnresolvedCheck:
5588 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005589 call_kind = LocationSummary::kCallOnSlowPath;
5590 break;
5591 }
5592
Roland Levillain3b359c72015-11-17 19:35:12 +00005593 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5594 locations->SetInAt(0, Location::RequiresRegister());
5595 locations->SetInAt(1, Location::RequiresRegister());
5596 // Note that TypeCheckSlowPathARM uses this "temp" register too.
5597 locations->AddTemp(Location::RequiresRegister());
5598 // When read barriers are enabled, we need an additional temporary
5599 // register for some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00005600 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005601 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005602 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005603}
5604
5605void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00005606 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005607 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005608 Location obj_loc = locations->InAt(0);
5609 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005610 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005611 Location temp_loc = locations->GetTemp(0);
5612 Register temp = temp_loc.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +00005613 Location temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
5614 locations->GetTemp(1) :
5615 Location::NoLocation();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005616 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005617 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5618 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5619 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005620
Roland Levillain3b359c72015-11-17 19:35:12 +00005621 bool is_type_check_slow_path_fatal =
5622 (type_check_kind == TypeCheckKind::kExactCheck ||
5623 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5624 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5625 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5626 !instruction->CanThrowIntoCatchBlock();
5627 SlowPathCode* type_check_slow_path =
5628 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5629 is_type_check_slow_path_fatal);
5630 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005631
5632 Label done;
5633 // Avoid null check if we know obj is not null.
5634 if (instruction->MustDoNullCheck()) {
5635 __ CompareAndBranchIfZero(obj, &done);
5636 }
5637
Roland Levillain3b359c72015-11-17 19:35:12 +00005638 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillainc9285912015-12-18 10:38:42 +00005639 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005640
Roland Levillain3b359c72015-11-17 19:35:12 +00005641 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005642 case TypeCheckKind::kExactCheck:
5643 case TypeCheckKind::kArrayCheck: {
5644 __ cmp(temp, ShifterOperand(cls));
5645 // Jump to slow path for throwing the exception or doing a
5646 // more involved array check.
Roland Levillain3b359c72015-11-17 19:35:12 +00005647 __ b(type_check_slow_path->GetEntryLabel(), NE);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005648 break;
5649 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005650
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005651 case TypeCheckKind::kAbstractClassCheck: {
5652 // If the class is abstract, we eagerly fetch the super class of the
5653 // object to avoid doing a comparison we know will fail.
Roland Levillain3b359c72015-11-17 19:35:12 +00005654 Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005655 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005656 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillainc9285912015-12-18 10:38:42 +00005657 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005658
5659 // If the class reference currently in `temp` is not null, jump
5660 // to the `compare_classes` label to compare it with the checked
5661 // class.
5662 __ CompareAndBranchIfNonZero(temp, &compare_classes);
5663 // Otherwise, jump to the slow path to throw the exception.
5664 //
5665 // But before, move back the object's class into `temp` before
5666 // going into the slow path, as it has been overwritten in the
5667 // meantime.
5668 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillainc9285912015-12-18 10:38:42 +00005669 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005670 __ b(type_check_slow_path->GetEntryLabel());
5671
5672 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005673 __ cmp(temp, ShifterOperand(cls));
5674 __ b(&loop, NE);
5675 break;
5676 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005677
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005678 case TypeCheckKind::kClassHierarchyCheck: {
5679 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005680 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005681 __ Bind(&loop);
5682 __ cmp(temp, ShifterOperand(cls));
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005683 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005684
Roland Levillain3b359c72015-11-17 19:35:12 +00005685 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillainc9285912015-12-18 10:38:42 +00005686 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005687
5688 // If the class reference currently in `temp` is not null, jump
5689 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005690 __ CompareAndBranchIfNonZero(temp, &loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005691 // Otherwise, jump to the slow path to throw the exception.
5692 //
5693 // But before, move back the object's class into `temp` before
5694 // going into the slow path, as it has been overwritten in the
5695 // meantime.
5696 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillainc9285912015-12-18 10:38:42 +00005697 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005698 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005699 break;
5700 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005701
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005702 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005703 // Do an exact check.
Roland Levillain3b359c72015-11-17 19:35:12 +00005704 Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005705 __ cmp(temp, ShifterOperand(cls));
5706 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005707
5708 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00005709 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillainc9285912015-12-18 10:38:42 +00005710 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005711
5712 // If the component type is not null (i.e. the object is indeed
5713 // an array), jump to label `check_non_primitive_component_type`
5714 // to further check that this component type is not a primitive
5715 // type.
5716 __ CompareAndBranchIfNonZero(temp, &check_non_primitive_component_type);
5717 // Otherwise, jump to the slow path to throw the exception.
5718 //
5719 // But before, move back the object's class into `temp` before
5720 // going into the slow path, as it has been overwritten in the
5721 // meantime.
5722 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillainc9285912015-12-18 10:38:42 +00005723 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005724 __ b(type_check_slow_path->GetEntryLabel());
5725
5726 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005727 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00005728 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
5729 __ CompareAndBranchIfZero(temp, &done);
5730 // Same comment as above regarding `temp` and the slow path.
5731 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillainc9285912015-12-18 10:38:42 +00005732 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005733 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005734 break;
5735 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005736
Calin Juravle98893e12015-10-02 21:05:03 +01005737 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005738 case TypeCheckKind::kInterfaceCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005739 // We always go into the type check slow path for the unresolved &
5740 // interface check cases.
5741 //
5742 // We cannot directly call the CheckCast runtime entry point
5743 // without resorting to a type checking slow path here (i.e. by
5744 // calling InvokeRuntime directly), as it would require to
5745 // assign fixed registers for the inputs of this HInstanceOf
5746 // instruction (following the runtime calling convention), which
5747 // might be cluttered by the potential first read barrier
5748 // emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00005749 //
5750 // TODO: Introduce a new runtime entry point taking the object
5751 // to test (instead of its class) as argument, and let it deal
5752 // with the read barrier issues. This will let us refactor this
5753 // case of the `switch` code as it was previously (with a direct
5754 // call to the runtime not using a type checking slow path).
5755 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00005756 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005757 break;
5758 }
5759 __ Bind(&done);
5760
Roland Levillain3b359c72015-11-17 19:35:12 +00005761 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005762}
5763
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005764void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
5765 LocationSummary* locations =
5766 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5767 InvokeRuntimeCallingConvention calling_convention;
5768 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5769}
5770
5771void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
5772 codegen_->InvokeRuntime(instruction->IsEnter()
5773 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
5774 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00005775 instruction->GetDexPc(),
5776 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005777 if (instruction->IsEnter()) {
5778 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5779 } else {
5780 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5781 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005782}
5783
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005784void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
5785void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
5786void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005787
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005788void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005789 LocationSummary* locations =
5790 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5791 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
5792 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005793 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005794 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005795 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00005796 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005797}
5798
5799void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
5800 HandleBitwiseOperation(instruction);
5801}
5802
5803void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
5804 HandleBitwiseOperation(instruction);
5805}
5806
5807void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
5808 HandleBitwiseOperation(instruction);
5809}
5810
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005811void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
5812 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
5813 if (value == 0xffffffffu) {
5814 if (out != first) {
5815 __ mov(out, ShifterOperand(first));
5816 }
5817 return;
5818 }
5819 if (value == 0u) {
5820 __ mov(out, ShifterOperand(0));
5821 return;
5822 }
5823 ShifterOperand so;
5824 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
5825 __ and_(out, first, so);
5826 } else {
5827 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so));
5828 __ bic(out, first, ShifterOperand(~value));
5829 }
5830}
5831
5832void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
5833 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
5834 if (value == 0u) {
5835 if (out != first) {
5836 __ mov(out, ShifterOperand(first));
5837 }
5838 return;
5839 }
5840 if (value == 0xffffffffu) {
5841 __ mvn(out, ShifterOperand(0));
5842 return;
5843 }
5844 ShifterOperand so;
5845 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
5846 __ orr(out, first, so);
5847 } else {
5848 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
5849 __ orn(out, first, ShifterOperand(~value));
5850 }
5851}
5852
5853void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
5854 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
5855 if (value == 0u) {
5856 if (out != first) {
5857 __ mov(out, ShifterOperand(first));
5858 }
5859 return;
5860 }
5861 __ eor(out, first, ShifterOperand(value));
5862}
5863
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005864void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
5865 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005866 Location first = locations->InAt(0);
5867 Location second = locations->InAt(1);
5868 Location out = locations->Out();
5869
5870 if (second.IsConstant()) {
5871 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
5872 uint32_t value_low = Low32Bits(value);
5873 if (instruction->GetResultType() == Primitive::kPrimInt) {
5874 Register first_reg = first.AsRegister<Register>();
5875 Register out_reg = out.AsRegister<Register>();
5876 if (instruction->IsAnd()) {
5877 GenerateAndConst(out_reg, first_reg, value_low);
5878 } else if (instruction->IsOr()) {
5879 GenerateOrrConst(out_reg, first_reg, value_low);
5880 } else {
5881 DCHECK(instruction->IsXor());
5882 GenerateEorConst(out_reg, first_reg, value_low);
5883 }
5884 } else {
5885 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
5886 uint32_t value_high = High32Bits(value);
5887 Register first_low = first.AsRegisterPairLow<Register>();
5888 Register first_high = first.AsRegisterPairHigh<Register>();
5889 Register out_low = out.AsRegisterPairLow<Register>();
5890 Register out_high = out.AsRegisterPairHigh<Register>();
5891 if (instruction->IsAnd()) {
5892 GenerateAndConst(out_low, first_low, value_low);
5893 GenerateAndConst(out_high, first_high, value_high);
5894 } else if (instruction->IsOr()) {
5895 GenerateOrrConst(out_low, first_low, value_low);
5896 GenerateOrrConst(out_high, first_high, value_high);
5897 } else {
5898 DCHECK(instruction->IsXor());
5899 GenerateEorConst(out_low, first_low, value_low);
5900 GenerateEorConst(out_high, first_high, value_high);
5901 }
5902 }
5903 return;
5904 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005905
5906 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005907 Register first_reg = first.AsRegister<Register>();
5908 ShifterOperand second_reg(second.AsRegister<Register>());
5909 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005910 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005911 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005912 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005913 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005914 } else {
5915 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005916 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005917 }
5918 } else {
5919 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005920 Register first_low = first.AsRegisterPairLow<Register>();
5921 Register first_high = first.AsRegisterPairHigh<Register>();
5922 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
5923 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
5924 Register out_low = out.AsRegisterPairLow<Register>();
5925 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005926 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005927 __ and_(out_low, first_low, second_low);
5928 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005929 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005930 __ orr(out_low, first_low, second_low);
5931 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005932 } else {
5933 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005934 __ eor(out_low, first_low, second_low);
5935 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005936 }
5937 }
5938}
5939
Roland Levillainc9285912015-12-18 10:38:42 +00005940void InstructionCodeGeneratorARM::GenerateReferenceLoadOneRegister(HInstruction* instruction,
5941 Location out,
5942 uint32_t offset,
5943 Location temp) {
5944 Register out_reg = out.AsRegister<Register>();
5945 if (kEmitCompilerReadBarrier) {
5946 if (kUseBakerReadBarrier) {
5947 // Load with fast path based Baker's read barrier.
5948 // /* HeapReference<Object> */ out = *(out + offset)
5949 codegen_->GenerateFieldLoadWithBakerReadBarrier(
5950 instruction, out, out_reg, offset, temp, /* needs_null_check */ false);
5951 } else {
5952 // Load with slow path based read barrier.
5953 // Save the value of `out` into `temp` before overwriting it
5954 // in the following move operation, as we will need it for the
5955 // read barrier below.
5956 __ Mov(temp.AsRegister<Register>(), out_reg);
5957 // /* HeapReference<Object> */ out = *(out + offset)
5958 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
5959 codegen_->GenerateReadBarrierSlow(instruction, out, out, temp, offset);
5960 }
5961 } else {
5962 // Plain load with no read barrier.
5963 // /* HeapReference<Object> */ out = *(out + offset)
5964 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
5965 __ MaybeUnpoisonHeapReference(out_reg);
5966 }
5967}
5968
5969void InstructionCodeGeneratorARM::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
5970 Location out,
5971 Location obj,
5972 uint32_t offset,
5973 Location temp) {
5974 Register out_reg = out.AsRegister<Register>();
5975 Register obj_reg = obj.AsRegister<Register>();
5976 if (kEmitCompilerReadBarrier) {
5977 if (kUseBakerReadBarrier) {
5978 // Load with fast path based Baker's read barrier.
5979 // /* HeapReference<Object> */ out = *(obj + offset)
5980 codegen_->GenerateFieldLoadWithBakerReadBarrier(
5981 instruction, out, obj_reg, offset, temp, /* needs_null_check */ false);
5982 } else {
5983 // Load with slow path based read barrier.
5984 // /* HeapReference<Object> */ out = *(obj + offset)
5985 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
5986 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5987 }
5988 } else {
5989 // Plain load with no read barrier.
5990 // /* HeapReference<Object> */ out = *(obj + offset)
5991 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
5992 __ MaybeUnpoisonHeapReference(out_reg);
5993 }
5994}
5995
5996void InstructionCodeGeneratorARM::GenerateGcRootFieldLoad(HInstruction* instruction,
5997 Location root,
5998 Register obj,
5999 uint32_t offset) {
6000 Register root_reg = root.AsRegister<Register>();
6001 if (kEmitCompilerReadBarrier) {
6002 if (kUseBakerReadBarrier) {
6003 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6004 // Baker's read barrier are used:
6005 //
6006 // root = obj.field;
6007 // if (Thread::Current()->GetIsGcMarking()) {
6008 // root = ReadBarrier::Mark(root)
6009 // }
6010
6011 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6012 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6013 static_assert(
6014 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6015 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6016 "have different sizes.");
6017 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6018 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6019 "have different sizes.");
6020
6021 // Slow path used to mark the GC root `root`.
6022 SlowPathCode* slow_path =
6023 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, root, root);
6024 codegen_->AddSlowPath(slow_path);
6025
6026 __ 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
6104 // slightly more complex as:
6105 // - it implements the load-load fence using a data dependency on
6106 // the high-bits of rb_state, which are expected to be all zeroes;
6107 // - it performs additional checks that we do not do here for
6108 // performance reasons.
6109
6110 Register ref_reg = ref.AsRegister<Register>();
6111 Register temp_reg = temp.AsRegister<Register>();
6112 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6113
6114 // /* int32_t */ monitor = obj->monitor_
6115 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
6116 if (needs_null_check) {
6117 MaybeRecordImplicitNullCheck(instruction);
6118 }
6119 // /* LockWord */ lock_word = LockWord(monitor)
6120 static_assert(sizeof(LockWord) == sizeof(int32_t),
6121 "art::LockWord and int32_t have different sizes.");
6122 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
6123 __ Lsr(temp_reg, temp_reg, LockWord::kReadBarrierStateShift);
6124 __ and_(temp_reg, temp_reg, ShifterOperand(LockWord::kReadBarrierStateMask));
6125 static_assert(
6126 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
6127 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
6128
6129 // Introduce a dependency on the high bits of rb_state, which shall
6130 // be all zeroes, to prevent load-load reordering, and without using
6131 // a memory barrier (which would be more expensive).
6132 // IP = rb_state & ~LockWord::kReadBarrierStateMask = 0
6133 __ bic(IP, temp_reg, ShifterOperand(LockWord::kReadBarrierStateMask));
6134 // obj is unchanged by this operation, but its value now depends on
6135 // IP, which depends on temp_reg.
6136 __ add(obj, obj, ShifterOperand(IP));
6137
6138 // The actual reference load.
6139 if (index.IsValid()) {
6140 static_assert(
6141 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6142 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
6143 // /* HeapReference<Object> */ ref =
6144 // *(obj + offset + index * sizeof(HeapReference<Object>))
6145 if (index.IsConstant()) {
6146 size_t computed_offset =
6147 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset;
6148 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
6149 } else {
6150 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
6151 __ LoadFromOffset(kLoadWord, ref_reg, IP, offset);
6152 }
6153 } else {
6154 // /* HeapReference<Object> */ ref = *(obj + offset)
6155 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
6156 }
6157
6158 // Object* ref = ref_addr->AsMirrorPtr()
6159 __ MaybeUnpoisonHeapReference(ref_reg);
6160
6161 // Slow path used to mark the object `ref` when it is gray.
6162 SlowPathCode* slow_path =
6163 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, ref, ref);
6164 AddSlowPath(slow_path);
6165
6166 // if (rb_state == ReadBarrier::gray_ptr_)
6167 // ref = ReadBarrier::Mark(ref);
6168 __ cmp(temp_reg, ShifterOperand(ReadBarrier::gray_ptr_));
6169 __ b(slow_path->GetEntryLabel(), EQ);
6170 __ Bind(slow_path->GetExitLabel());
6171}
6172
6173void CodeGeneratorARM::GenerateReadBarrierSlow(HInstruction* instruction,
6174 Location out,
6175 Location ref,
6176 Location obj,
6177 uint32_t offset,
6178 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006179 DCHECK(kEmitCompilerReadBarrier);
6180
Roland Levillainc9285912015-12-18 10:38:42 +00006181 // Insert a slow path based read barrier *after* the reference load.
6182 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006183 // If heap poisoning is enabled, the unpoisoning of the loaded
6184 // reference will be carried out by the runtime within the slow
6185 // path.
6186 //
6187 // Note that `ref` currently does not get unpoisoned (when heap
6188 // poisoning is enabled), which is alright as the `ref` argument is
6189 // not used by the artReadBarrierSlow entry point.
6190 //
6191 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6192 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6193 ReadBarrierForHeapReferenceSlowPathARM(instruction, out, ref, obj, offset, index);
6194 AddSlowPath(slow_path);
6195
Roland Levillain3b359c72015-11-17 19:35:12 +00006196 __ b(slow_path->GetEntryLabel());
6197 __ Bind(slow_path->GetExitLabel());
6198}
6199
Roland Levillainc9285912015-12-18 10:38:42 +00006200void CodeGeneratorARM::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6201 Location out,
6202 Location ref,
6203 Location obj,
6204 uint32_t offset,
6205 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006206 if (kEmitCompilerReadBarrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00006207 // Baker's read barriers shall be handled by the fast path
6208 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
6209 DCHECK(!kUseBakerReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00006210 // If heap poisoning is enabled, unpoisoning will be taken care of
6211 // by the runtime within the slow path.
Roland Levillainc9285912015-12-18 10:38:42 +00006212 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain3b359c72015-11-17 19:35:12 +00006213 } else if (kPoisonHeapReferences) {
6214 __ UnpoisonHeapReference(out.AsRegister<Register>());
6215 }
6216}
6217
Roland Levillainc9285912015-12-18 10:38:42 +00006218void CodeGeneratorARM::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6219 Location out,
6220 Location root) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006221 DCHECK(kEmitCompilerReadBarrier);
6222
Roland Levillainc9285912015-12-18 10:38:42 +00006223 // Insert a slow path based read barrier *after* the GC root load.
6224 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006225 // Note that GC roots are not affected by heap poisoning, so we do
6226 // not need to do anything special for this here.
6227 SlowPathCode* slow_path =
6228 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM(instruction, out, root);
6229 AddSlowPath(slow_path);
6230
Roland Levillain3b359c72015-11-17 19:35:12 +00006231 __ b(slow_path->GetEntryLabel());
6232 __ Bind(slow_path->GetExitLabel());
6233}
6234
Vladimir Markodc151b22015-10-15 18:02:30 +01006235HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch(
6236 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
6237 MethodReference target_method) {
Vladimir Markodc151b22015-10-15 18:02:30 +01006238 if (desired_dispatch_info.code_ptr_location ==
6239 HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative) {
6240 const DexFile& outer_dex_file = GetGraph()->GetDexFile();
6241 if (&outer_dex_file != target_method.dex_file) {
6242 // Calls across dex files are more likely to exceed the available BL range,
6243 // so use absolute patch with fixup if available and kCallArtMethod otherwise.
6244 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location =
6245 (desired_dispatch_info.method_load_kind ==
6246 HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup)
6247 ? HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup
6248 : HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
6249 return HInvokeStaticOrDirect::DispatchInfo {
6250 desired_dispatch_info.method_load_kind,
6251 code_ptr_location,
6252 desired_dispatch_info.method_load_data,
6253 0u
6254 };
6255 }
6256 }
6257 return desired_dispatch_info;
6258}
6259
Vladimir Markob4536b72015-11-24 13:45:23 +00006260Register CodeGeneratorARM::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
6261 Register temp) {
6262 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
6263 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
6264 if (!invoke->GetLocations()->Intrinsified()) {
6265 return location.AsRegister<Register>();
6266 }
6267 // For intrinsics we allow any location, so it may be on the stack.
6268 if (!location.IsRegister()) {
6269 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
6270 return temp;
6271 }
6272 // For register locations, check if the register was saved. If so, get it from the stack.
6273 // Note: There is a chance that the register was saved but not overwritten, so we could
6274 // save one load. However, since this is just an intrinsic slow path we prefer this
6275 // simple and more robust approach rather that trying to determine if that's the case.
6276 SlowPathCode* slow_path = GetCurrentSlowPath();
6277 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
6278 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
6279 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
6280 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
6281 return temp;
6282 }
6283 return location.AsRegister<Register>();
6284}
6285
Nicolas Geoffray38207af2015-06-01 15:46:22 +01006286void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00006287 // For better instruction scheduling we load the direct code pointer before the method pointer.
Vladimir Marko58155012015-08-19 12:49:41 +00006288 switch (invoke->GetCodePtrLocation()) {
Vladimir Marko58155012015-08-19 12:49:41 +00006289 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
6290 // LR = code address from literal pool with link-time patch.
6291 __ LoadLiteral(LR, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
Vladimir Marko58155012015-08-19 12:49:41 +00006292 break;
6293 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
6294 // LR = invoke->GetDirectCodePtr();
6295 __ LoadImmediate(LR, invoke->GetDirectCodePtr());
Vladimir Marko58155012015-08-19 12:49:41 +00006296 break;
6297 default:
6298 break;
6299 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006300
Vladimir Marko58155012015-08-19 12:49:41 +00006301 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
6302 switch (invoke->GetMethodLoadKind()) {
6303 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
6304 // temp = thread->string_init_entrypoint
6305 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, invoke->GetStringInitOffset());
6306 break;
6307 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00006308 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006309 break;
6310 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
6311 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
6312 break;
6313 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
6314 __ LoadLiteral(temp.AsRegister<Register>(),
6315 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
6316 break;
Vladimir Markob4536b72015-11-24 13:45:23 +00006317 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
6318 HArmDexCacheArraysBase* base =
6319 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
6320 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
6321 temp.AsRegister<Register>());
6322 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
6323 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
6324 break;
6325 }
Vladimir Marko58155012015-08-19 12:49:41 +00006326 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00006327 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006328 Register method_reg;
6329 Register reg = temp.AsRegister<Register>();
6330 if (current_method.IsRegister()) {
6331 method_reg = current_method.AsRegister<Register>();
6332 } else {
6333 DCHECK(invoke->GetLocations()->Intrinsified());
6334 DCHECK(!current_method.IsValid());
6335 method_reg = reg;
6336 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
6337 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006338 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
6339 __ LoadFromOffset(kLoadWord,
6340 reg,
6341 method_reg,
6342 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00006343 // temp = temp[index_in_cache]
6344 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
6345 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
6346 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01006347 }
Vladimir Marko58155012015-08-19 12:49:41 +00006348 }
6349
6350 switch (invoke->GetCodePtrLocation()) {
6351 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
6352 __ bl(GetFrameEntryLabel());
6353 break;
6354 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01006355 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006356 __ BindTrackedLabel(&relative_call_patches_.back().label);
Vladimir Markodc151b22015-10-15 18:02:30 +01006357 // Arbitrarily branch to the BL itself, override at link time.
6358 __ bl(&relative_call_patches_.back().label);
6359 break;
Vladimir Marko58155012015-08-19 12:49:41 +00006360 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
6361 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
6362 // LR prepared above for better instruction scheduling.
Vladimir Marko58155012015-08-19 12:49:41 +00006363 // LR()
6364 __ blx(LR);
6365 break;
6366 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
6367 // LR = callee_method->entry_point_from_quick_compiled_code_
6368 __ LoadFromOffset(
6369 kLoadWord, LR, callee_method.AsRegister<Register>(),
6370 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize).Int32Value());
6371 // LR()
6372 __ blx(LR);
6373 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006374 }
6375
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006376 DCHECK(!IsLeafMethod());
6377}
6378
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006379void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
6380 Register temp = temp_location.AsRegister<Register>();
6381 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
6382 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006383
6384 // Use the calling convention instead of the location of the receiver, as
6385 // intrinsics may have put the receiver in a different register. In the intrinsics
6386 // slow path, the arguments have been moved to the right place, so here we are
6387 // guaranteed that the receiver is the first register of the calling convention.
6388 InvokeDexCallingConvention calling_convention;
6389 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006390 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00006391 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006392 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006393 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00006394 // Instead of simply (possibly) unpoisoning `temp` here, we should
6395 // emit a read barrier for the previous class reference load.
6396 // However this is not required in practice, as this is an
6397 // intermediate/temporary reference and because the current
6398 // concurrent copying collector keeps the from-space memory
6399 // intact/accessible until the end of the marking phase (the
6400 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006401 __ MaybeUnpoisonHeapReference(temp);
6402 // temp = temp->GetMethodAt(method_offset);
6403 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
6404 kArmWordSize).Int32Value();
6405 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
6406 // LR = temp->GetEntryPoint();
6407 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
6408 // LR();
6409 __ blx(LR);
6410}
6411
Vladimir Marko58155012015-08-19 12:49:41 +00006412void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
6413 DCHECK(linker_patches->empty());
Vladimir Markob4536b72015-11-24 13:45:23 +00006414 size_t size =
6415 method_patches_.size() +
6416 call_patches_.size() +
6417 relative_call_patches_.size() +
6418 /* MOVW+MOVT for each base */ 2u * dex_cache_arrays_base_labels_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00006419 linker_patches->reserve(size);
6420 for (const auto& entry : method_patches_) {
6421 const MethodReference& target_method = entry.first;
6422 Literal* literal = entry.second;
6423 DCHECK(literal->GetLabel()->IsBound());
6424 uint32_t literal_offset = literal->GetLabel()->Position();
6425 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
6426 target_method.dex_file,
6427 target_method.dex_method_index));
6428 }
6429 for (const auto& entry : call_patches_) {
6430 const MethodReference& target_method = entry.first;
6431 Literal* literal = entry.second;
6432 DCHECK(literal->GetLabel()->IsBound());
6433 uint32_t literal_offset = literal->GetLabel()->Position();
6434 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
6435 target_method.dex_file,
6436 target_method.dex_method_index));
6437 }
6438 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
6439 uint32_t literal_offset = info.label.Position();
6440 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
6441 info.target_method.dex_file,
6442 info.target_method.dex_method_index));
6443 }
Vladimir Markob4536b72015-11-24 13:45:23 +00006444 for (const auto& pair : dex_cache_arrays_base_labels_) {
6445 HArmDexCacheArraysBase* base = pair.first;
6446 const DexCacheArraysBaseLabels* labels = &pair.second;
6447 const DexFile& dex_file = base->GetDexFile();
6448 size_t base_element_offset = base->GetElementOffset();
6449 DCHECK(labels->add_pc_label.IsBound());
6450 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(labels->add_pc_label.Position());
6451 // Add MOVW patch.
6452 DCHECK(labels->movw_label.IsBound());
6453 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(labels->movw_label.Position());
6454 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(movw_offset,
6455 &dex_file,
6456 add_pc_offset,
6457 base_element_offset));
6458 // Add MOVT patch.
6459 DCHECK(labels->movt_label.IsBound());
6460 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(labels->movt_label.Position());
6461 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(movt_offset,
6462 &dex_file,
6463 add_pc_offset,
6464 base_element_offset));
6465 }
Vladimir Marko58155012015-08-19 12:49:41 +00006466}
6467
6468Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
6469 MethodToLiteralMap* map) {
6470 // Look up the literal for target_method.
6471 auto lb = map->lower_bound(target_method);
6472 if (lb != map->end() && !map->key_comp()(target_method, lb->first)) {
6473 return lb->second;
6474 }
6475 // We don't have a literal for this method yet, insert a new one.
6476 Literal* literal = __ NewLiteral<uint32_t>(0u);
6477 map->PutBefore(lb, target_method, literal);
6478 return literal;
6479}
6480
6481Literal* CodeGeneratorARM::DeduplicateMethodAddressLiteral(MethodReference target_method) {
6482 return DeduplicateMethodLiteral(target_method, &method_patches_);
6483}
6484
6485Literal* CodeGeneratorARM::DeduplicateMethodCodeLiteral(MethodReference target_method) {
6486 return DeduplicateMethodLiteral(target_method, &call_patches_);
6487}
6488
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006489void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006490 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006491 LOG(FATAL) << "Unreachable";
6492}
6493
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006494void InstructionCodeGeneratorARM::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
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01006499void LocationsBuilderARM::VisitFakeString(HFakeString* instruction) {
6500 DCHECK(codegen_->IsBaseline());
6501 LocationSummary* locations =
6502 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6503 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
6504}
6505
6506void InstructionCodeGeneratorARM::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
6507 DCHECK(codegen_->IsBaseline());
6508 // Will be generated at use site.
6509}
6510
Mark Mendellfe57faa2015-09-18 09:26:15 -04006511// Simple implementation of packed switch - generate cascaded compare/jumps.
6512void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6513 LocationSummary* locations =
6514 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6515 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006516 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006517 codegen_->GetAssembler()->IsThumb()) {
6518 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
6519 if (switch_instr->GetStartValue() != 0) {
6520 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
6521 }
6522 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04006523}
6524
6525void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6526 int32_t lower_bound = switch_instr->GetStartValue();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006527 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006528 LocationSummary* locations = switch_instr->GetLocations();
6529 Register value_reg = locations->InAt(0).AsRegister<Register>();
6530 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6531
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006532 if (num_entries <= kPackedSwitchCompareJumpThreshold || !codegen_->GetAssembler()->IsThumb()) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006533 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006534 Register temp_reg = IP;
6535 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
6536 // the immediate, because IP is used as the destination register. For the other
6537 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
6538 // and they can be encoded in the instruction without making use of IP register.
6539 __ AddConstantSetFlags(temp_reg, value_reg, -lower_bound);
6540
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006541 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006542 // Jump to successors[0] if value == lower_bound.
6543 __ b(codegen_->GetLabelOf(successors[0]), EQ);
6544 int32_t last_index = 0;
6545 for (; num_entries - last_index > 2; last_index += 2) {
6546 __ AddConstantSetFlags(temp_reg, temp_reg, -2);
6547 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6548 __ b(codegen_->GetLabelOf(successors[last_index + 1]), LO);
6549 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6550 __ b(codegen_->GetLabelOf(successors[last_index + 2]), EQ);
6551 }
6552 if (num_entries - last_index == 2) {
6553 // The last missing case_value.
6554 GenerateCompareWithImmediate(temp_reg, 1);
6555 __ b(codegen_->GetLabelOf(successors[last_index + 1]), EQ);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006556 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04006557
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006558 // And the default for any other value.
6559 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6560 __ b(codegen_->GetLabelOf(default_block));
6561 }
6562 } else {
6563 // Create a table lookup.
6564 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
6565
6566 // Materialize a pointer to the switch table
6567 std::vector<Label*> labels(num_entries);
6568 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6569 for (uint32_t i = 0; i < num_entries; i++) {
6570 labels[i] = codegen_->GetLabelOf(successors[i]);
6571 }
6572 JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg);
6573
6574 // Remove the bias.
6575 Register key_reg;
6576 if (lower_bound != 0) {
6577 key_reg = locations->GetTemp(1).AsRegister<Register>();
6578 __ AddConstant(key_reg, value_reg, -lower_bound);
6579 } else {
6580 key_reg = value_reg;
6581 }
6582
6583 // Check whether the value is in the table, jump to default block if not.
6584 __ CmpConstant(key_reg, num_entries - 1);
6585 __ b(codegen_->GetLabelOf(default_block), Condition::HI);
6586
6587 // Load the displacement from the table.
6588 __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2));
6589
6590 // Dispatch is a direct add to the PC (for Thumb2).
6591 __ EmitJumpTableDispatch(table, temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006592 }
6593}
6594
Vladimir Markob4536b72015-11-24 13:45:23 +00006595void LocationsBuilderARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
6596 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
6597 locations->SetOut(Location::RequiresRegister());
6598 codegen_->AddDexCacheArraysBase(base);
6599}
6600
6601void InstructionCodeGeneratorARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
6602 Register base_reg = base->GetLocations()->Out().AsRegister<Register>();
6603 CodeGeneratorARM::DexCacheArraysBaseLabels* labels = codegen_->GetDexCacheArraysBaseLabels(base);
6604 __ BindTrackedLabel(&labels->movw_label);
6605 __ movw(base_reg, 0u);
6606 __ BindTrackedLabel(&labels->movt_label);
6607 __ movt(base_reg, 0u);
6608 __ BindTrackedLabel(&labels->add_pc_label);
6609 __ add(base_reg, base_reg, ShifterOperand(PC));
6610}
6611
Andreas Gampe85b62f22015-09-09 13:15:38 -07006612void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
6613 if (!trg.IsValid()) {
Roland Levillainc9285912015-12-18 10:38:42 +00006614 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006615 return;
6616 }
6617
6618 DCHECK_NE(type, Primitive::kPrimVoid);
6619
6620 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
6621 if (return_loc.Equals(trg)) {
6622 return;
6623 }
6624
6625 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
6626 // with the last branch.
6627 if (type == Primitive::kPrimLong) {
6628 HParallelMove parallel_move(GetGraph()->GetArena());
6629 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
6630 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
6631 GetMoveResolver()->EmitNativeCode(&parallel_move);
6632 } else if (type == Primitive::kPrimDouble) {
6633 HParallelMove parallel_move(GetGraph()->GetArena());
6634 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
6635 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
6636 GetMoveResolver()->EmitNativeCode(&parallel_move);
6637 } else {
6638 // Let the parallel move resolver take care of all of this.
6639 HParallelMove parallel_move(GetGraph()->GetArena());
6640 parallel_move.AddMove(return_loc, trg, type, nullptr);
6641 GetMoveResolver()->EmitNativeCode(&parallel_move);
6642 }
6643}
6644
Roland Levillain4d027112015-07-01 15:41:14 +01006645#undef __
6646#undef QUICK_ENTRY_POINT
6647
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006648} // namespace arm
6649} // namespace art