blob: 24f06a3a655791fc2fa6c181e42772bcca104bf6 [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) {
David Srbeckyb7070a22016-01-08 18:13:53 +00001682 if (codegen_->HasStackMapAtCurrentPc()) {
1683 // Ensure that we do not collide with the stack map of the previous instruction.
1684 __ nop();
1685 }
David Srbecky0cf44932015-12-09 14:09:59 +00001686 codegen_->RecordPcInfo(info, info->GetDexPc());
1687}
1688
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001689void LocationsBuilderARM::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001690 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001691 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001692 // Handle the long/FP comparisons made in instruction simplification.
1693 switch (cond->InputAt(0)->GetType()) {
1694 case Primitive::kPrimLong:
1695 locations->SetInAt(0, Location::RequiresRegister());
1696 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1697 if (cond->NeedsMaterialization()) {
1698 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1699 }
1700 break;
1701
1702 case Primitive::kPrimFloat:
1703 case Primitive::kPrimDouble:
1704 locations->SetInAt(0, Location::RequiresFpuRegister());
1705 locations->SetInAt(1, Location::RequiresFpuRegister());
1706 if (cond->NeedsMaterialization()) {
1707 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1708 }
1709 break;
1710
1711 default:
1712 locations->SetInAt(0, Location::RequiresRegister());
1713 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1714 if (cond->NeedsMaterialization()) {
1715 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1716 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001717 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001718}
1719
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001720void InstructionCodeGeneratorARM::HandleCondition(HCondition* cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001721 if (!cond->NeedsMaterialization()) {
1722 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001723 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001724
1725 LocationSummary* locations = cond->GetLocations();
1726 Location left = locations->InAt(0);
1727 Location right = locations->InAt(1);
1728 Register out = locations->Out().AsRegister<Register>();
1729 Label true_label, false_label;
1730
1731 switch (cond->InputAt(0)->GetType()) {
1732 default: {
1733 // Integer case.
1734 if (right.IsRegister()) {
1735 __ cmp(left.AsRegister<Register>(), ShifterOperand(right.AsRegister<Register>()));
1736 } else {
1737 DCHECK(right.IsConstant());
1738 GenerateCompareWithImmediate(left.AsRegister<Register>(),
1739 CodeGenerator::GetInt32ValueOf(right.GetConstant()));
1740 }
Aart Bike9f37602015-10-09 11:15:55 -07001741 __ it(ARMCondition(cond->GetCondition()), kItElse);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001742 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(1),
Aart Bike9f37602015-10-09 11:15:55 -07001743 ARMCondition(cond->GetCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001744 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(0),
Aart Bike9f37602015-10-09 11:15:55 -07001745 ARMCondition(cond->GetOppositeCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001746 return;
1747 }
1748 case Primitive::kPrimLong:
1749 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1750 break;
1751 case Primitive::kPrimFloat:
1752 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
1753 GenerateFPJumps(cond, &true_label, &false_label);
1754 break;
1755 case Primitive::kPrimDouble:
1756 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
1757 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
1758 GenerateFPJumps(cond, &true_label, &false_label);
1759 break;
1760 }
1761
1762 // Convert the jumps into the result.
1763 Label done_label;
1764
1765 // False case: result = 0.
1766 __ Bind(&false_label);
1767 __ LoadImmediate(out, 0);
1768 __ b(&done_label);
1769
1770 // True case: result = 1.
1771 __ Bind(&true_label);
1772 __ LoadImmediate(out, 1);
1773 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001774}
1775
1776void LocationsBuilderARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001777 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001778}
1779
1780void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001781 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001782}
1783
1784void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001785 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001786}
1787
1788void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001789 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001790}
1791
1792void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001793 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001794}
1795
1796void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001797 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001798}
1799
1800void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001801 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001802}
1803
1804void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001805 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001806}
1807
1808void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001809 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001810}
1811
1812void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001813 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001814}
1815
1816void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001817 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001818}
1819
1820void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001821 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001822}
1823
Aart Bike9f37602015-10-09 11:15:55 -07001824void LocationsBuilderARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001825 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001826}
1827
1828void InstructionCodeGeneratorARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001829 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001830}
1831
1832void LocationsBuilderARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001833 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001834}
1835
1836void InstructionCodeGeneratorARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001837 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001838}
1839
1840void LocationsBuilderARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001841 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001842}
1843
1844void InstructionCodeGeneratorARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001845 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001846}
1847
1848void LocationsBuilderARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001849 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001850}
1851
1852void InstructionCodeGeneratorARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001853 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001854}
1855
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001856void LocationsBuilderARM::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001857 local->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001858}
1859
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001860void InstructionCodeGeneratorARM::VisitLocal(HLocal* local) {
1861 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001862}
1863
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001864void LocationsBuilderARM::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001865 load->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001866}
1867
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001868void InstructionCodeGeneratorARM::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001869 // Nothing to do, this is driven by the code generator.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001870}
1871
1872void LocationsBuilderARM::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001873 LocationSummary* locations =
1874 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001875 switch (store->InputAt(1)->GetType()) {
1876 case Primitive::kPrimBoolean:
1877 case Primitive::kPrimByte:
1878 case Primitive::kPrimChar:
1879 case Primitive::kPrimShort:
1880 case Primitive::kPrimInt:
1881 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001882 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001883 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1884 break;
1885
1886 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001887 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001888 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1889 break;
1890
1891 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001892 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001893 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001894}
1895
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001896void InstructionCodeGeneratorARM::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001897}
1898
1899void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001900 LocationSummary* locations =
1901 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001902 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001903}
1904
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001905void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001906 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001907}
1908
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001909void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
1910 LocationSummary* locations =
1911 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1912 locations->SetOut(Location::ConstantLocation(constant));
1913}
1914
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001915void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001916 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001917}
1918
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001919void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001920 LocationSummary* locations =
1921 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001922 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001923}
1924
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001925void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001926 // Will be generated at use site.
1927}
1928
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001929void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
1930 LocationSummary* locations =
1931 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1932 locations->SetOut(Location::ConstantLocation(constant));
1933}
1934
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001935void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001936 // Will be generated at use site.
1937}
1938
1939void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
1940 LocationSummary* locations =
1941 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1942 locations->SetOut(Location::ConstantLocation(constant));
1943}
1944
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001945void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001946 // Will be generated at use site.
1947}
1948
Calin Juravle27df7582015-04-17 19:12:31 +01001949void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1950 memory_barrier->SetLocations(nullptr);
1951}
1952
1953void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00001954 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001955}
1956
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001957void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001958 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001959}
1960
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001961void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001962 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001963}
1964
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001965void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001966 LocationSummary* locations =
1967 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001968 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001969}
1970
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001971void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001972 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001973}
1974
Calin Juravle175dc732015-08-25 15:42:32 +01001975void LocationsBuilderARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1976 // The trampoline uses the same calling convention as dex calling conventions,
1977 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1978 // the method_idx.
1979 HandleInvoke(invoke);
1980}
1981
1982void InstructionCodeGeneratorARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1983 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1984}
1985
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001986void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001987 // When we do not run baseline, explicit clinit checks triggered by static
1988 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1989 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001990
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001991 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001992 codegen_->GetAssembler(),
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001993 codegen_->GetInstructionSetFeatures());
1994 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markob4536b72015-11-24 13:45:23 +00001995 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
1996 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
1997 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001998 return;
1999 }
2000
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002001 HandleInvoke(invoke);
Vladimir Markob4536b72015-11-24 13:45:23 +00002002
2003 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
2004 if (invoke->HasPcRelativeDexCache()) {
2005 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
2006 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002007}
2008
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002009static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
2010 if (invoke->GetLocations()->Intrinsified()) {
2011 IntrinsicCodeGeneratorARM intrinsic(codegen);
2012 intrinsic.Dispatch(invoke);
2013 return true;
2014 }
2015 return false;
2016}
2017
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002018void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002019 // When we do not run baseline, explicit clinit checks triggered by static
2020 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2021 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002022
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002023 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2024 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002025 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002026
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002027 LocationSummary* locations = invoke->GetLocations();
2028 codegen_->GenerateStaticOrDirectCall(
2029 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002030 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002031}
2032
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002033void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002034 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002035 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002036}
2037
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002038void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002039 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01002040 codegen_->GetAssembler(),
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002041 codegen_->GetInstructionSetFeatures());
2042 if (intrinsic.TryDispatch(invoke)) {
2043 return;
2044 }
2045
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002046 HandleInvoke(invoke);
2047}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002048
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002049void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002050 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2051 return;
2052 }
2053
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002054 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002055 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002056 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002057}
2058
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002059void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
2060 HandleInvoke(invoke);
2061 // Add the hidden argument.
2062 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
2063}
2064
2065void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
2066 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain3b359c72015-11-17 19:35:12 +00002067 LocationSummary* locations = invoke->GetLocations();
2068 Register temp = locations->GetTemp(0).AsRegister<Register>();
2069 Register hidden_reg = locations->GetTemp(1).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002070 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2071 invoke->GetImtIndex() % mirror::Class::kImtSize, kArmPointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002072 Location receiver = locations->InAt(0);
2073 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2074
Roland Levillain3b359c72015-11-17 19:35:12 +00002075 // Set the hidden argument. This is safe to do this here, as R12
2076 // won't be modified thereafter, before the `blx` (call) instruction.
2077 DCHECK_EQ(R12, hidden_reg);
2078 __ LoadImmediate(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002079
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002080 if (receiver.IsStackSlot()) {
2081 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
Roland Levillain3b359c72015-11-17 19:35:12 +00002082 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002083 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
2084 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00002085 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002086 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002087 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002088 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00002089 // Instead of simply (possibly) unpoisoning `temp` here, we should
2090 // emit a read barrier for the previous class reference load.
2091 // However this is not required in practice, as this is an
2092 // intermediate/temporary reference and because the current
2093 // concurrent copying collector keeps the from-space memory
2094 // intact/accessible until the end of the marking phase (the
2095 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002096 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002097 // temp = temp->GetImtEntryAt(method_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00002098 uint32_t entry_point =
2099 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002100 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
2101 // LR = temp->GetEntryPoint();
2102 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
2103 // LR();
2104 __ blx(LR);
2105 DCHECK(!codegen_->IsLeafMethod());
2106 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2107}
2108
Roland Levillain88cb1752014-10-20 16:36:47 +01002109void LocationsBuilderARM::VisitNeg(HNeg* neg) {
2110 LocationSummary* locations =
2111 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2112 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002113 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01002114 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002115 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2116 break;
2117 }
2118 case Primitive::kPrimLong: {
2119 locations->SetInAt(0, Location::RequiresRegister());
2120 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01002121 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002122 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002123
Roland Levillain88cb1752014-10-20 16:36:47 +01002124 case Primitive::kPrimFloat:
2125 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002126 locations->SetInAt(0, Location::RequiresFpuRegister());
2127 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01002128 break;
2129
2130 default:
2131 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2132 }
2133}
2134
2135void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
2136 LocationSummary* locations = neg->GetLocations();
2137 Location out = locations->Out();
2138 Location in = locations->InAt(0);
2139 switch (neg->GetResultType()) {
2140 case Primitive::kPrimInt:
2141 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002142 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01002143 break;
2144
2145 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002146 DCHECK(in.IsRegisterPair());
2147 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
2148 __ rsbs(out.AsRegisterPairLow<Register>(),
2149 in.AsRegisterPairLow<Register>(),
2150 ShifterOperand(0));
2151 // We cannot emit an RSC (Reverse Subtract with Carry)
2152 // instruction here, as it does not exist in the Thumb-2
2153 // instruction set. We use the following approach
2154 // using SBC and SUB instead.
2155 //
2156 // out.hi = -C
2157 __ sbc(out.AsRegisterPairHigh<Register>(),
2158 out.AsRegisterPairHigh<Register>(),
2159 ShifterOperand(out.AsRegisterPairHigh<Register>()));
2160 // out.hi = out.hi - in.hi
2161 __ sub(out.AsRegisterPairHigh<Register>(),
2162 out.AsRegisterPairHigh<Register>(),
2163 ShifterOperand(in.AsRegisterPairHigh<Register>()));
2164 break;
2165
Roland Levillain88cb1752014-10-20 16:36:47 +01002166 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002167 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002168 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002169 break;
2170
Roland Levillain88cb1752014-10-20 16:36:47 +01002171 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002172 DCHECK(in.IsFpuRegisterPair());
2173 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2174 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01002175 break;
2176
2177 default:
2178 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2179 }
2180}
2181
Roland Levillaindff1f282014-11-05 14:15:05 +00002182void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002183 Primitive::Type result_type = conversion->GetResultType();
2184 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002185 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002186
Roland Levillain5b3ee562015-04-14 16:02:41 +01002187 // The float-to-long, double-to-long and long-to-float type conversions
2188 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002189 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01002190 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2191 && result_type == Primitive::kPrimLong)
2192 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Roland Levillain624279f2014-12-04 11:54:28 +00002193 ? LocationSummary::kCall
2194 : LocationSummary::kNoCall;
2195 LocationSummary* locations =
2196 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2197
David Brazdilb2bd1c52015-03-25 11:17:37 +00002198 // The Java language does not allow treating boolean as an integral type but
2199 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002200
Roland Levillaindff1f282014-11-05 14:15:05 +00002201 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002202 case Primitive::kPrimByte:
2203 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002204 case Primitive::kPrimBoolean:
2205 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002206 case Primitive::kPrimShort:
2207 case Primitive::kPrimInt:
2208 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002209 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002210 locations->SetInAt(0, Location::RequiresRegister());
2211 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2212 break;
2213
2214 default:
2215 LOG(FATAL) << "Unexpected type conversion from " << input_type
2216 << " to " << result_type;
2217 }
2218 break;
2219
Roland Levillain01a8d712014-11-14 16:27:39 +00002220 case Primitive::kPrimShort:
2221 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002222 case Primitive::kPrimBoolean:
2223 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002224 case Primitive::kPrimByte:
2225 case Primitive::kPrimInt:
2226 case Primitive::kPrimChar:
2227 // Processing a Dex `int-to-short' instruction.
2228 locations->SetInAt(0, Location::RequiresRegister());
2229 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2230 break;
2231
2232 default:
2233 LOG(FATAL) << "Unexpected type conversion from " << input_type
2234 << " to " << result_type;
2235 }
2236 break;
2237
Roland Levillain946e1432014-11-11 17:35:19 +00002238 case Primitive::kPrimInt:
2239 switch (input_type) {
2240 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002241 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002242 locations->SetInAt(0, Location::Any());
2243 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2244 break;
2245
2246 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002247 // Processing a Dex `float-to-int' instruction.
2248 locations->SetInAt(0, Location::RequiresFpuRegister());
2249 locations->SetOut(Location::RequiresRegister());
2250 locations->AddTemp(Location::RequiresFpuRegister());
2251 break;
2252
Roland Levillain946e1432014-11-11 17:35:19 +00002253 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002254 // Processing a Dex `double-to-int' instruction.
2255 locations->SetInAt(0, Location::RequiresFpuRegister());
2256 locations->SetOut(Location::RequiresRegister());
2257 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002258 break;
2259
2260 default:
2261 LOG(FATAL) << "Unexpected type conversion from " << input_type
2262 << " to " << result_type;
2263 }
2264 break;
2265
Roland Levillaindff1f282014-11-05 14:15:05 +00002266 case Primitive::kPrimLong:
2267 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002268 case Primitive::kPrimBoolean:
2269 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002270 case Primitive::kPrimByte:
2271 case Primitive::kPrimShort:
2272 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002273 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002274 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002275 locations->SetInAt(0, Location::RequiresRegister());
2276 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2277 break;
2278
Roland Levillain624279f2014-12-04 11:54:28 +00002279 case Primitive::kPrimFloat: {
2280 // Processing a Dex `float-to-long' instruction.
2281 InvokeRuntimeCallingConvention calling_convention;
2282 locations->SetInAt(0, Location::FpuRegisterLocation(
2283 calling_convention.GetFpuRegisterAt(0)));
2284 locations->SetOut(Location::RegisterPairLocation(R0, R1));
2285 break;
2286 }
2287
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002288 case Primitive::kPrimDouble: {
2289 // Processing a Dex `double-to-long' instruction.
2290 InvokeRuntimeCallingConvention calling_convention;
2291 locations->SetInAt(0, Location::FpuRegisterPairLocation(
2292 calling_convention.GetFpuRegisterAt(0),
2293 calling_convention.GetFpuRegisterAt(1)));
2294 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00002295 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002296 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002297
2298 default:
2299 LOG(FATAL) << "Unexpected type conversion from " << input_type
2300 << " to " << result_type;
2301 }
2302 break;
2303
Roland Levillain981e4542014-11-14 11:47:14 +00002304 case Primitive::kPrimChar:
2305 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002306 case Primitive::kPrimBoolean:
2307 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002308 case Primitive::kPrimByte:
2309 case Primitive::kPrimShort:
2310 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002311 // Processing a Dex `int-to-char' instruction.
2312 locations->SetInAt(0, Location::RequiresRegister());
2313 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2314 break;
2315
2316 default:
2317 LOG(FATAL) << "Unexpected type conversion from " << input_type
2318 << " to " << result_type;
2319 }
2320 break;
2321
Roland Levillaindff1f282014-11-05 14:15:05 +00002322 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002323 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002324 case Primitive::kPrimBoolean:
2325 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002326 case Primitive::kPrimByte:
2327 case Primitive::kPrimShort:
2328 case Primitive::kPrimInt:
2329 case Primitive::kPrimChar:
2330 // Processing a Dex `int-to-float' instruction.
2331 locations->SetInAt(0, Location::RequiresRegister());
2332 locations->SetOut(Location::RequiresFpuRegister());
2333 break;
2334
Roland Levillain5b3ee562015-04-14 16:02:41 +01002335 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00002336 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002337 InvokeRuntimeCallingConvention calling_convention;
2338 locations->SetInAt(0, Location::RegisterPairLocation(
2339 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2340 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00002341 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01002342 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002343
Roland Levillaincff13742014-11-17 14:32:17 +00002344 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002345 // Processing a Dex `double-to-float' instruction.
2346 locations->SetInAt(0, Location::RequiresFpuRegister());
2347 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002348 break;
2349
2350 default:
2351 LOG(FATAL) << "Unexpected type conversion from " << input_type
2352 << " to " << result_type;
2353 };
2354 break;
2355
Roland Levillaindff1f282014-11-05 14:15:05 +00002356 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002357 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002358 case Primitive::kPrimBoolean:
2359 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002360 case Primitive::kPrimByte:
2361 case Primitive::kPrimShort:
2362 case Primitive::kPrimInt:
2363 case Primitive::kPrimChar:
2364 // Processing a Dex `int-to-double' instruction.
2365 locations->SetInAt(0, Location::RequiresRegister());
2366 locations->SetOut(Location::RequiresFpuRegister());
2367 break;
2368
2369 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002370 // Processing a Dex `long-to-double' instruction.
2371 locations->SetInAt(0, Location::RequiresRegister());
2372 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01002373 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002374 locations->AddTemp(Location::RequiresFpuRegister());
2375 break;
2376
Roland Levillaincff13742014-11-17 14:32:17 +00002377 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002378 // Processing a Dex `float-to-double' instruction.
2379 locations->SetInAt(0, Location::RequiresFpuRegister());
2380 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002381 break;
2382
2383 default:
2384 LOG(FATAL) << "Unexpected type conversion from " << input_type
2385 << " to " << result_type;
2386 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002387 break;
2388
2389 default:
2390 LOG(FATAL) << "Unexpected type conversion from " << input_type
2391 << " to " << result_type;
2392 }
2393}
2394
2395void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
2396 LocationSummary* locations = conversion->GetLocations();
2397 Location out = locations->Out();
2398 Location in = locations->InAt(0);
2399 Primitive::Type result_type = conversion->GetResultType();
2400 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002401 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002402 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002403 case Primitive::kPrimByte:
2404 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002405 case Primitive::kPrimBoolean:
2406 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002407 case Primitive::kPrimShort:
2408 case Primitive::kPrimInt:
2409 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002410 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002411 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002412 break;
2413
2414 default:
2415 LOG(FATAL) << "Unexpected type conversion from " << input_type
2416 << " to " << result_type;
2417 }
2418 break;
2419
Roland Levillain01a8d712014-11-14 16:27:39 +00002420 case Primitive::kPrimShort:
2421 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002422 case Primitive::kPrimBoolean:
2423 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002424 case Primitive::kPrimByte:
2425 case Primitive::kPrimInt:
2426 case Primitive::kPrimChar:
2427 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002428 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00002429 break;
2430
2431 default:
2432 LOG(FATAL) << "Unexpected type conversion from " << input_type
2433 << " to " << result_type;
2434 }
2435 break;
2436
Roland Levillain946e1432014-11-11 17:35:19 +00002437 case Primitive::kPrimInt:
2438 switch (input_type) {
2439 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002440 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002441 DCHECK(out.IsRegister());
2442 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002443 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002444 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002445 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00002446 } else {
2447 DCHECK(in.IsConstant());
2448 DCHECK(in.GetConstant()->IsLongConstant());
2449 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002450 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00002451 }
2452 break;
2453
Roland Levillain3f8f9362014-12-02 17:45:01 +00002454 case Primitive::kPrimFloat: {
2455 // Processing a Dex `float-to-int' instruction.
2456 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
2457 __ vmovs(temp, in.AsFpuRegister<SRegister>());
2458 __ vcvtis(temp, temp);
2459 __ vmovrs(out.AsRegister<Register>(), temp);
2460 break;
2461 }
2462
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002463 case Primitive::kPrimDouble: {
2464 // Processing a Dex `double-to-int' instruction.
2465 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
2466 DRegister temp_d = FromLowSToD(temp_s);
2467 __ vmovd(temp_d, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
2468 __ vcvtid(temp_s, temp_d);
2469 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00002470 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002471 }
Roland Levillain946e1432014-11-11 17:35:19 +00002472
2473 default:
2474 LOG(FATAL) << "Unexpected type conversion from " << input_type
2475 << " to " << result_type;
2476 }
2477 break;
2478
Roland Levillaindff1f282014-11-05 14:15:05 +00002479 case Primitive::kPrimLong:
2480 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002481 case Primitive::kPrimBoolean:
2482 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002483 case Primitive::kPrimByte:
2484 case Primitive::kPrimShort:
2485 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002486 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002487 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002488 DCHECK(out.IsRegisterPair());
2489 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002490 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002491 // Sign extension.
2492 __ Asr(out.AsRegisterPairHigh<Register>(),
2493 out.AsRegisterPairLow<Register>(),
2494 31);
2495 break;
2496
2497 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002498 // Processing a Dex `float-to-long' instruction.
Roland Levillain624279f2014-12-04 11:54:28 +00002499 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2500 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002501 conversion->GetDexPc(),
2502 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002503 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002504 break;
2505
Roland Levillaindff1f282014-11-05 14:15:05 +00002506 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002507 // Processing a Dex `double-to-long' instruction.
2508 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2509 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002510 conversion->GetDexPc(),
2511 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002512 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002513 break;
2514
2515 default:
2516 LOG(FATAL) << "Unexpected type conversion from " << input_type
2517 << " to " << result_type;
2518 }
2519 break;
2520
Roland Levillain981e4542014-11-14 11:47:14 +00002521 case Primitive::kPrimChar:
2522 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002523 case Primitive::kPrimBoolean:
2524 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002525 case Primitive::kPrimByte:
2526 case Primitive::kPrimShort:
2527 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002528 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002529 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00002530 break;
2531
2532 default:
2533 LOG(FATAL) << "Unexpected type conversion from " << input_type
2534 << " to " << result_type;
2535 }
2536 break;
2537
Roland Levillaindff1f282014-11-05 14:15:05 +00002538 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002539 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002540 case Primitive::kPrimBoolean:
2541 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002542 case Primitive::kPrimByte:
2543 case Primitive::kPrimShort:
2544 case Primitive::kPrimInt:
2545 case Primitive::kPrimChar: {
2546 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002547 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
2548 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002549 break;
2550 }
2551
Roland Levillain5b3ee562015-04-14 16:02:41 +01002552 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002553 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002554 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pL2f),
2555 conversion,
2556 conversion->GetDexPc(),
2557 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002558 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
Roland Levillain6d0e4832014-11-27 18:31:21 +00002559 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002560
Roland Levillaincff13742014-11-17 14:32:17 +00002561 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002562 // Processing a Dex `double-to-float' instruction.
2563 __ vcvtsd(out.AsFpuRegister<SRegister>(),
2564 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00002565 break;
2566
2567 default:
2568 LOG(FATAL) << "Unexpected type conversion from " << input_type
2569 << " to " << result_type;
2570 };
2571 break;
2572
Roland Levillaindff1f282014-11-05 14:15:05 +00002573 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002574 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002575 case Primitive::kPrimBoolean:
2576 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002577 case Primitive::kPrimByte:
2578 case Primitive::kPrimShort:
2579 case Primitive::kPrimInt:
2580 case Primitive::kPrimChar: {
2581 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002582 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002583 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2584 out.AsFpuRegisterPairLow<SRegister>());
2585 break;
2586 }
2587
Roland Levillain647b9ed2014-11-27 12:06:00 +00002588 case Primitive::kPrimLong: {
2589 // Processing a Dex `long-to-double' instruction.
2590 Register low = in.AsRegisterPairLow<Register>();
2591 Register high = in.AsRegisterPairHigh<Register>();
2592 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
2593 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002594 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00002595 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002596 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
2597 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002598
Roland Levillain682393c2015-04-14 15:57:52 +01002599 // temp_d = int-to-double(high)
2600 __ vmovsr(temp_s, high);
2601 __ vcvtdi(temp_d, temp_s);
2602 // constant_d = k2Pow32EncodingForDouble
2603 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
2604 // out_d = unsigned-to-double(low)
2605 __ vmovsr(out_s, low);
2606 __ vcvtdu(out_d, out_s);
2607 // out_d += temp_d * constant_d
2608 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002609 break;
2610 }
2611
Roland Levillaincff13742014-11-17 14:32:17 +00002612 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002613 // Processing a Dex `float-to-double' instruction.
2614 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2615 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002616 break;
2617
2618 default:
2619 LOG(FATAL) << "Unexpected type conversion from " << input_type
2620 << " to " << result_type;
2621 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002622 break;
2623
2624 default:
2625 LOG(FATAL) << "Unexpected type conversion from " << input_type
2626 << " to " << result_type;
2627 }
2628}
2629
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002630void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002631 LocationSummary* locations =
2632 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002633 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002634 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002635 locations->SetInAt(0, Location::RequiresRegister());
2636 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002637 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2638 break;
2639 }
2640
2641 case Primitive::kPrimLong: {
2642 locations->SetInAt(0, Location::RequiresRegister());
2643 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002644 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002645 break;
2646 }
2647
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002648 case Primitive::kPrimFloat:
2649 case Primitive::kPrimDouble: {
2650 locations->SetInAt(0, Location::RequiresFpuRegister());
2651 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002652 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002653 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002654 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002655
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002656 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002657 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002658 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002659}
2660
2661void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
2662 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002663 Location out = locations->Out();
2664 Location first = locations->InAt(0);
2665 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002666 switch (add->GetResultType()) {
2667 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002668 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002669 __ add(out.AsRegister<Register>(),
2670 first.AsRegister<Register>(),
2671 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002672 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002673 __ AddConstant(out.AsRegister<Register>(),
2674 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002675 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002676 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002677 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002678
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002679 case Primitive::kPrimLong: {
2680 DCHECK(second.IsRegisterPair());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002681 __ adds(out.AsRegisterPairLow<Register>(),
2682 first.AsRegisterPairLow<Register>(),
2683 ShifterOperand(second.AsRegisterPairLow<Register>()));
2684 __ adc(out.AsRegisterPairHigh<Register>(),
2685 first.AsRegisterPairHigh<Register>(),
2686 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002687 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002688 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002689
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002690 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00002691 __ vadds(out.AsFpuRegister<SRegister>(),
2692 first.AsFpuRegister<SRegister>(),
2693 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002694 break;
2695
2696 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002697 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2698 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2699 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002700 break;
2701
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002702 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002703 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002704 }
2705}
2706
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002707void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002708 LocationSummary* locations =
2709 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002710 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002711 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002712 locations->SetInAt(0, Location::RequiresRegister());
2713 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002714 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2715 break;
2716 }
2717
2718 case Primitive::kPrimLong: {
2719 locations->SetInAt(0, Location::RequiresRegister());
2720 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002721 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002722 break;
2723 }
Calin Juravle11351682014-10-23 15:38:15 +01002724 case Primitive::kPrimFloat:
2725 case Primitive::kPrimDouble: {
2726 locations->SetInAt(0, Location::RequiresFpuRegister());
2727 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002728 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002729 break;
Calin Juravle11351682014-10-23 15:38:15 +01002730 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002731 default:
Calin Juravle11351682014-10-23 15:38:15 +01002732 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002733 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002734}
2735
2736void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
2737 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002738 Location out = locations->Out();
2739 Location first = locations->InAt(0);
2740 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002741 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002742 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002743 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002744 __ sub(out.AsRegister<Register>(),
2745 first.AsRegister<Register>(),
2746 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002747 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002748 __ AddConstant(out.AsRegister<Register>(),
2749 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01002750 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002751 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002752 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002753 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002754
Calin Juravle11351682014-10-23 15:38:15 +01002755 case Primitive::kPrimLong: {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002756 DCHECK(second.IsRegisterPair());
Calin Juravle11351682014-10-23 15:38:15 +01002757 __ subs(out.AsRegisterPairLow<Register>(),
2758 first.AsRegisterPairLow<Register>(),
2759 ShifterOperand(second.AsRegisterPairLow<Register>()));
2760 __ sbc(out.AsRegisterPairHigh<Register>(),
2761 first.AsRegisterPairHigh<Register>(),
2762 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002763 break;
Calin Juravle11351682014-10-23 15:38:15 +01002764 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002765
Calin Juravle11351682014-10-23 15:38:15 +01002766 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002767 __ vsubs(out.AsFpuRegister<SRegister>(),
2768 first.AsFpuRegister<SRegister>(),
2769 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002770 break;
Calin Juravle11351682014-10-23 15:38:15 +01002771 }
2772
2773 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002774 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2775 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2776 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01002777 break;
2778 }
2779
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002780
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002781 default:
Calin Juravle11351682014-10-23 15:38:15 +01002782 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002783 }
2784}
2785
Calin Juravle34bacdf2014-10-07 20:23:36 +01002786void LocationsBuilderARM::VisitMul(HMul* mul) {
2787 LocationSummary* locations =
2788 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2789 switch (mul->GetResultType()) {
2790 case Primitive::kPrimInt:
2791 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002792 locations->SetInAt(0, Location::RequiresRegister());
2793 locations->SetInAt(1, Location::RequiresRegister());
2794 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002795 break;
2796 }
2797
Calin Juravleb5bfa962014-10-21 18:02:24 +01002798 case Primitive::kPrimFloat:
2799 case Primitive::kPrimDouble: {
2800 locations->SetInAt(0, Location::RequiresFpuRegister());
2801 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002802 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002803 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002804 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002805
2806 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002807 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002808 }
2809}
2810
2811void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
2812 LocationSummary* locations = mul->GetLocations();
2813 Location out = locations->Out();
2814 Location first = locations->InAt(0);
2815 Location second = locations->InAt(1);
2816 switch (mul->GetResultType()) {
2817 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00002818 __ mul(out.AsRegister<Register>(),
2819 first.AsRegister<Register>(),
2820 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002821 break;
2822 }
2823 case Primitive::kPrimLong: {
2824 Register out_hi = out.AsRegisterPairHigh<Register>();
2825 Register out_lo = out.AsRegisterPairLow<Register>();
2826 Register in1_hi = first.AsRegisterPairHigh<Register>();
2827 Register in1_lo = first.AsRegisterPairLow<Register>();
2828 Register in2_hi = second.AsRegisterPairHigh<Register>();
2829 Register in2_lo = second.AsRegisterPairLow<Register>();
2830
2831 // Extra checks to protect caused by the existence of R1_R2.
2832 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
2833 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
2834 DCHECK_NE(out_hi, in1_lo);
2835 DCHECK_NE(out_hi, in2_lo);
2836
2837 // input: in1 - 64 bits, in2 - 64 bits
2838 // output: out
2839 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2840 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2841 // parts: out.lo = (in1.lo * in2.lo)[31:0]
2842
2843 // IP <- in1.lo * in2.hi
2844 __ mul(IP, in1_lo, in2_hi);
2845 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2846 __ mla(out_hi, in1_hi, in2_lo, IP);
2847 // out.lo <- (in1.lo * in2.lo)[31:0];
2848 __ umull(out_lo, IP, in1_lo, in2_lo);
2849 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2850 __ add(out_hi, out_hi, ShifterOperand(IP));
2851 break;
2852 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002853
2854 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002855 __ vmuls(out.AsFpuRegister<SRegister>(),
2856 first.AsFpuRegister<SRegister>(),
2857 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002858 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002859 }
2860
2861 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002862 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2863 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2864 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01002865 break;
2866 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002867
2868 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002869 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002870 }
2871}
2872
Zheng Xuc6667102015-05-15 16:08:45 +08002873void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2874 DCHECK(instruction->IsDiv() || instruction->IsRem());
2875 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2876
2877 LocationSummary* locations = instruction->GetLocations();
2878 Location second = locations->InAt(1);
2879 DCHECK(second.IsConstant());
2880
2881 Register out = locations->Out().AsRegister<Register>();
2882 Register dividend = locations->InAt(0).AsRegister<Register>();
2883 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2884 DCHECK(imm == 1 || imm == -1);
2885
2886 if (instruction->IsRem()) {
2887 __ LoadImmediate(out, 0);
2888 } else {
2889 if (imm == 1) {
2890 __ Mov(out, dividend);
2891 } else {
2892 __ rsb(out, dividend, ShifterOperand(0));
2893 }
2894 }
2895}
2896
2897void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2898 DCHECK(instruction->IsDiv() || instruction->IsRem());
2899 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2900
2901 LocationSummary* locations = instruction->GetLocations();
2902 Location second = locations->InAt(1);
2903 DCHECK(second.IsConstant());
2904
2905 Register out = locations->Out().AsRegister<Register>();
2906 Register dividend = locations->InAt(0).AsRegister<Register>();
2907 Register temp = locations->GetTemp(0).AsRegister<Register>();
2908 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002909 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002910 int ctz_imm = CTZ(abs_imm);
2911
2912 if (ctz_imm == 1) {
2913 __ Lsr(temp, dividend, 32 - ctz_imm);
2914 } else {
2915 __ Asr(temp, dividend, 31);
2916 __ Lsr(temp, temp, 32 - ctz_imm);
2917 }
2918 __ add(out, temp, ShifterOperand(dividend));
2919
2920 if (instruction->IsDiv()) {
2921 __ Asr(out, out, ctz_imm);
2922 if (imm < 0) {
2923 __ rsb(out, out, ShifterOperand(0));
2924 }
2925 } else {
2926 __ ubfx(out, out, 0, ctz_imm);
2927 __ sub(out, out, ShifterOperand(temp));
2928 }
2929}
2930
2931void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2932 DCHECK(instruction->IsDiv() || instruction->IsRem());
2933 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2934
2935 LocationSummary* locations = instruction->GetLocations();
2936 Location second = locations->InAt(1);
2937 DCHECK(second.IsConstant());
2938
2939 Register out = locations->Out().AsRegister<Register>();
2940 Register dividend = locations->InAt(0).AsRegister<Register>();
2941 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2942 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
2943 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2944
2945 int64_t magic;
2946 int shift;
2947 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2948
2949 __ LoadImmediate(temp1, magic);
2950 __ smull(temp2, temp1, dividend, temp1);
2951
2952 if (imm > 0 && magic < 0) {
2953 __ add(temp1, temp1, ShifterOperand(dividend));
2954 } else if (imm < 0 && magic > 0) {
2955 __ sub(temp1, temp1, ShifterOperand(dividend));
2956 }
2957
2958 if (shift != 0) {
2959 __ Asr(temp1, temp1, shift);
2960 }
2961
2962 if (instruction->IsDiv()) {
2963 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
2964 } else {
2965 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
2966 // TODO: Strength reduction for mls.
2967 __ LoadImmediate(temp2, imm);
2968 __ mls(out, temp1, temp2, dividend);
2969 }
2970}
2971
2972void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
2973 DCHECK(instruction->IsDiv() || instruction->IsRem());
2974 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2975
2976 LocationSummary* locations = instruction->GetLocations();
2977 Location second = locations->InAt(1);
2978 DCHECK(second.IsConstant());
2979
2980 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2981 if (imm == 0) {
2982 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2983 } else if (imm == 1 || imm == -1) {
2984 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002985 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002986 DivRemByPowerOfTwo(instruction);
2987 } else {
2988 DCHECK(imm <= -2 || imm >= 2);
2989 GenerateDivRemWithAnyConstant(instruction);
2990 }
2991}
2992
Calin Juravle7c4954d2014-10-28 16:57:40 +00002993void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002994 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2995 if (div->GetResultType() == Primitive::kPrimLong) {
2996 // pLdiv runtime call.
2997 call_kind = LocationSummary::kCall;
Zheng Xuc6667102015-05-15 16:08:45 +08002998 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
2999 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003000 } else if (div->GetResultType() == Primitive::kPrimInt &&
3001 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3002 // pIdivmod runtime call.
3003 call_kind = LocationSummary::kCall;
3004 }
3005
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003006 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3007
Calin Juravle7c4954d2014-10-28 16:57:40 +00003008 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003009 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003010 if (div->InputAt(1)->IsConstant()) {
3011 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003012 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08003013 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003014 int32_t value = div->InputAt(1)->AsIntConstant()->GetValue();
3015 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08003016 // No temp register required.
3017 } else {
3018 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003019 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003020 locations->AddTemp(Location::RequiresRegister());
3021 }
3022 }
3023 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003024 locations->SetInAt(0, Location::RequiresRegister());
3025 locations->SetInAt(1, Location::RequiresRegister());
3026 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3027 } else {
3028 InvokeRuntimeCallingConvention calling_convention;
3029 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3030 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3031 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
3032 // we only need the former.
3033 locations->SetOut(Location::RegisterLocation(R0));
3034 }
Calin Juravled0d48522014-11-04 16:40:20 +00003035 break;
3036 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003037 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003038 InvokeRuntimeCallingConvention calling_convention;
3039 locations->SetInAt(0, Location::RegisterPairLocation(
3040 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3041 locations->SetInAt(1, Location::RegisterPairLocation(
3042 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003043 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003044 break;
3045 }
3046 case Primitive::kPrimFloat:
3047 case Primitive::kPrimDouble: {
3048 locations->SetInAt(0, Location::RequiresFpuRegister());
3049 locations->SetInAt(1, Location::RequiresFpuRegister());
3050 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3051 break;
3052 }
3053
3054 default:
3055 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3056 }
3057}
3058
3059void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
3060 LocationSummary* locations = div->GetLocations();
3061 Location out = locations->Out();
3062 Location first = locations->InAt(0);
3063 Location second = locations->InAt(1);
3064
3065 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003066 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003067 if (second.IsConstant()) {
3068 GenerateDivRemConstantIntegral(div);
3069 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003070 __ sdiv(out.AsRegister<Register>(),
3071 first.AsRegister<Register>(),
3072 second.AsRegister<Register>());
3073 } else {
3074 InvokeRuntimeCallingConvention calling_convention;
3075 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3076 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3077 DCHECK_EQ(R0, out.AsRegister<Register>());
3078
3079 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), div, div->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003080 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003081 }
Calin Juravled0d48522014-11-04 16:40:20 +00003082 break;
3083 }
3084
Calin Juravle7c4954d2014-10-28 16:57:40 +00003085 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003086 InvokeRuntimeCallingConvention calling_convention;
3087 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3088 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3089 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3090 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3091 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003092 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003093
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003094 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv), div, div->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003095 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravle7c4954d2014-10-28 16:57:40 +00003096 break;
3097 }
3098
3099 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003100 __ vdivs(out.AsFpuRegister<SRegister>(),
3101 first.AsFpuRegister<SRegister>(),
3102 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003103 break;
3104 }
3105
3106 case Primitive::kPrimDouble: {
3107 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3108 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3109 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
3110 break;
3111 }
3112
3113 default:
3114 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3115 }
3116}
3117
Calin Juravlebacfec32014-11-14 15:54:36 +00003118void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003119 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003120
3121 // Most remainders are implemented in the runtime.
3122 LocationSummary::CallKind call_kind = LocationSummary::kCall;
Zheng Xuc6667102015-05-15 16:08:45 +08003123 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
3124 // sdiv will be replaced by other instruction sequence.
3125 call_kind = LocationSummary::kNoCall;
3126 } else if ((rem->GetResultType() == Primitive::kPrimInt)
3127 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003128 // Have hardware divide instruction for int, do it with three instructions.
3129 call_kind = LocationSummary::kNoCall;
3130 }
3131
Calin Juravlebacfec32014-11-14 15:54:36 +00003132 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3133
Calin Juravled2ec87d2014-12-08 14:24:46 +00003134 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003135 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003136 if (rem->InputAt(1)->IsConstant()) {
3137 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003138 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08003139 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003140 int32_t value = rem->InputAt(1)->AsIntConstant()->GetValue();
3141 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08003142 // No temp register required.
3143 } else {
3144 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003145 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003146 locations->AddTemp(Location::RequiresRegister());
3147 }
3148 }
3149 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003150 locations->SetInAt(0, Location::RequiresRegister());
3151 locations->SetInAt(1, Location::RequiresRegister());
3152 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3153 locations->AddTemp(Location::RequiresRegister());
3154 } else {
3155 InvokeRuntimeCallingConvention calling_convention;
3156 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3157 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3158 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
3159 // we only need the latter.
3160 locations->SetOut(Location::RegisterLocation(R1));
3161 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003162 break;
3163 }
3164 case Primitive::kPrimLong: {
3165 InvokeRuntimeCallingConvention calling_convention;
3166 locations->SetInAt(0, Location::RegisterPairLocation(
3167 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3168 locations->SetInAt(1, Location::RegisterPairLocation(
3169 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3170 // The runtime helper puts the output in R2,R3.
3171 locations->SetOut(Location::RegisterPairLocation(R2, R3));
3172 break;
3173 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00003174 case Primitive::kPrimFloat: {
3175 InvokeRuntimeCallingConvention calling_convention;
3176 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3177 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3178 locations->SetOut(Location::FpuRegisterLocation(S0));
3179 break;
3180 }
3181
Calin Juravlebacfec32014-11-14 15:54:36 +00003182 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003183 InvokeRuntimeCallingConvention calling_convention;
3184 locations->SetInAt(0, Location::FpuRegisterPairLocation(
3185 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
3186 locations->SetInAt(1, Location::FpuRegisterPairLocation(
3187 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
3188 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00003189 break;
3190 }
3191
3192 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003193 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003194 }
3195}
3196
3197void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
3198 LocationSummary* locations = rem->GetLocations();
3199 Location out = locations->Out();
3200 Location first = locations->InAt(0);
3201 Location second = locations->InAt(1);
3202
Calin Juravled2ec87d2014-12-08 14:24:46 +00003203 Primitive::Type type = rem->GetResultType();
3204 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003205 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003206 if (second.IsConstant()) {
3207 GenerateDivRemConstantIntegral(rem);
3208 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003209 Register reg1 = first.AsRegister<Register>();
3210 Register reg2 = second.AsRegister<Register>();
3211 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003212
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003213 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003214 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003215 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003216 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003217 } else {
3218 InvokeRuntimeCallingConvention calling_convention;
3219 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3220 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3221 DCHECK_EQ(R1, out.AsRegister<Register>());
3222
3223 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003224 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003225 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003226 break;
3227 }
3228
3229 case Primitive::kPrimLong: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003230 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003231 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003232 break;
3233 }
3234
Calin Juravled2ec87d2014-12-08 14:24:46 +00003235 case Primitive::kPrimFloat: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003236 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003237 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Calin Juravled2ec87d2014-12-08 14:24:46 +00003238 break;
3239 }
3240
Calin Juravlebacfec32014-11-14 15:54:36 +00003241 case Primitive::kPrimDouble: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003242 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003243 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003244 break;
3245 }
3246
3247 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003248 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003249 }
3250}
3251
Calin Juravled0d48522014-11-04 16:40:20 +00003252void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003253 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3254 ? LocationSummary::kCallOnSlowPath
3255 : LocationSummary::kNoCall;
3256 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003257 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00003258 if (instruction->HasUses()) {
3259 locations->SetOut(Location::SameAsFirstInput());
3260 }
3261}
3262
3263void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003264 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003265 codegen_->AddSlowPath(slow_path);
3266
3267 LocationSummary* locations = instruction->GetLocations();
3268 Location value = locations->InAt(0);
3269
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003270 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003271 case Primitive::kPrimByte:
3272 case Primitive::kPrimChar:
3273 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003274 case Primitive::kPrimInt: {
3275 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003276 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003277 } else {
3278 DCHECK(value.IsConstant()) << value;
3279 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3280 __ b(slow_path->GetEntryLabel());
3281 }
3282 }
3283 break;
3284 }
3285 case Primitive::kPrimLong: {
3286 if (value.IsRegisterPair()) {
3287 __ orrs(IP,
3288 value.AsRegisterPairLow<Register>(),
3289 ShifterOperand(value.AsRegisterPairHigh<Register>()));
3290 __ b(slow_path->GetEntryLabel(), EQ);
3291 } else {
3292 DCHECK(value.IsConstant()) << value;
3293 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3294 __ b(slow_path->GetEntryLabel());
3295 }
3296 }
3297 break;
3298 default:
3299 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3300 }
3301 }
Calin Juravled0d48522014-11-04 16:40:20 +00003302}
3303
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003304void InstructionCodeGeneratorARM::HandleIntegerRotate(LocationSummary* locations) {
3305 Register in = locations->InAt(0).AsRegister<Register>();
3306 Location rhs = locations->InAt(1);
3307 Register out = locations->Out().AsRegister<Register>();
3308
3309 if (rhs.IsConstant()) {
3310 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
3311 // so map all rotations to a +ve. equivalent in that range.
3312 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
3313 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
3314 if (rot) {
3315 // Rotate, mapping left rotations to right equivalents if necessary.
3316 // (e.g. left by 2 bits == right by 30.)
3317 __ Ror(out, in, rot);
3318 } else if (out != in) {
3319 __ Mov(out, in);
3320 }
3321 } else {
3322 __ Ror(out, in, rhs.AsRegister<Register>());
3323 }
3324}
3325
3326// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
3327// rotates by swapping input regs (effectively rotating by the first 32-bits of
3328// a larger rotation) or flipping direction (thus treating larger right/left
3329// rotations as sub-word sized rotations in the other direction) as appropriate.
3330void InstructionCodeGeneratorARM::HandleLongRotate(LocationSummary* locations) {
3331 Register in_reg_lo = locations->InAt(0).AsRegisterPairLow<Register>();
3332 Register in_reg_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
3333 Location rhs = locations->InAt(1);
3334 Register out_reg_lo = locations->Out().AsRegisterPairLow<Register>();
3335 Register out_reg_hi = locations->Out().AsRegisterPairHigh<Register>();
3336
3337 if (rhs.IsConstant()) {
3338 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
3339 // Map all rotations to +ve. equivalents on the interval [0,63].
3340 rot &= kMaxLongShiftValue;
3341 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
3342 // logic below to a simple pair of binary orr.
3343 // (e.g. 34 bits == in_reg swap + 2 bits right.)
3344 if (rot >= kArmBitsPerWord) {
3345 rot -= kArmBitsPerWord;
3346 std::swap(in_reg_hi, in_reg_lo);
3347 }
3348 // Rotate, or mov to out for zero or word size rotations.
3349 if (rot != 0u) {
3350 __ Lsr(out_reg_hi, in_reg_hi, rot);
3351 __ orr(out_reg_hi, out_reg_hi, ShifterOperand(in_reg_lo, arm::LSL, kArmBitsPerWord - rot));
3352 __ Lsr(out_reg_lo, in_reg_lo, rot);
3353 __ orr(out_reg_lo, out_reg_lo, ShifterOperand(in_reg_hi, arm::LSL, kArmBitsPerWord - rot));
3354 } else {
3355 __ Mov(out_reg_lo, in_reg_lo);
3356 __ Mov(out_reg_hi, in_reg_hi);
3357 }
3358 } else {
3359 Register shift_right = locations->GetTemp(0).AsRegister<Register>();
3360 Register shift_left = locations->GetTemp(1).AsRegister<Register>();
3361 Label end;
3362 Label shift_by_32_plus_shift_right;
3363
3364 __ and_(shift_right, rhs.AsRegister<Register>(), ShifterOperand(0x1F));
3365 __ Lsrs(shift_left, rhs.AsRegister<Register>(), 6);
3366 __ rsb(shift_left, shift_right, ShifterOperand(kArmBitsPerWord), AL, kCcKeep);
3367 __ b(&shift_by_32_plus_shift_right, CC);
3368
3369 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
3370 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
3371 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
3372 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3373 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3374 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3375 __ Lsr(shift_left, in_reg_hi, shift_right);
3376 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_left));
3377 __ b(&end);
3378
3379 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
3380 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
3381 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
3382 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
3383 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3384 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3385 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3386 __ Lsl(shift_right, in_reg_hi, shift_left);
3387 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_right));
3388
3389 __ Bind(&end);
3390 }
3391}
3392void LocationsBuilderARM::HandleRotate(HRor* ror) {
3393 LocationSummary* locations =
3394 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3395 switch (ror->GetResultType()) {
3396 case Primitive::kPrimInt: {
3397 locations->SetInAt(0, Location::RequiresRegister());
3398 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
3399 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3400 break;
3401 }
3402 case Primitive::kPrimLong: {
3403 locations->SetInAt(0, Location::RequiresRegister());
3404 if (ror->InputAt(1)->IsConstant()) {
3405 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
3406 } else {
3407 locations->SetInAt(1, Location::RequiresRegister());
3408 locations->AddTemp(Location::RequiresRegister());
3409 locations->AddTemp(Location::RequiresRegister());
3410 }
3411 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3412 break;
3413 }
3414 default:
3415 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3416 }
3417}
3418
3419void InstructionCodeGeneratorARM::HandleRotate(HRor* ror) {
3420 LocationSummary* locations = ror->GetLocations();
3421 Primitive::Type type = ror->GetResultType();
3422 switch (type) {
3423 case Primitive::kPrimInt: {
3424 HandleIntegerRotate(locations);
3425 break;
3426 }
3427 case Primitive::kPrimLong: {
3428 HandleLongRotate(locations);
3429 break;
3430 }
3431 default:
3432 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00003433 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003434 }
3435}
3436
3437void LocationsBuilderARM::VisitRor(HRor* op) {
Vladimir Marko351dddf2015-12-11 16:34:46 +00003438 HandleRotate(op);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003439}
3440
3441void InstructionCodeGeneratorARM::VisitRor(HRor* op) {
Vladimir Marko351dddf2015-12-11 16:34:46 +00003442 HandleRotate(op);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003443}
3444
Calin Juravle9aec02f2014-11-18 23:06:35 +00003445void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
3446 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3447
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003448 LocationSummary* locations =
3449 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003450
3451 switch (op->GetResultType()) {
3452 case Primitive::kPrimInt: {
3453 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003454 if (op->InputAt(1)->IsConstant()) {
3455 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3456 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3457 } else {
3458 locations->SetInAt(1, Location::RequiresRegister());
3459 // Make the output overlap, as it will be used to hold the masked
3460 // second input.
3461 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3462 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003463 break;
3464 }
3465 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003466 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003467 if (op->InputAt(1)->IsConstant()) {
3468 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3469 // For simplicity, use kOutputOverlap even though we only require that low registers
3470 // don't clash with high registers which the register allocator currently guarantees.
3471 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3472 } else {
3473 locations->SetInAt(1, Location::RequiresRegister());
3474 locations->AddTemp(Location::RequiresRegister());
3475 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3476 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003477 break;
3478 }
3479 default:
3480 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3481 }
3482}
3483
3484void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
3485 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3486
3487 LocationSummary* locations = op->GetLocations();
3488 Location out = locations->Out();
3489 Location first = locations->InAt(0);
3490 Location second = locations->InAt(1);
3491
3492 Primitive::Type type = op->GetResultType();
3493 switch (type) {
3494 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003495 Register out_reg = out.AsRegister<Register>();
3496 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003497 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003498 Register second_reg = second.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +00003499 // ARM doesn't mask the shift count so we need to do it ourselves.
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003500 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftValue));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003501 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003502 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003503 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003504 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003505 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003506 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003507 }
3508 } else {
3509 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
3510 uint32_t shift_value = static_cast<uint32_t>(cst & kMaxIntShiftValue);
Roland Levillainc9285912015-12-18 10:38:42 +00003511 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
Calin Juravle9aec02f2014-11-18 23:06:35 +00003512 __ Mov(out_reg, first_reg);
3513 } else if (op->IsShl()) {
3514 __ Lsl(out_reg, first_reg, shift_value);
3515 } else if (op->IsShr()) {
3516 __ Asr(out_reg, first_reg, shift_value);
3517 } else {
3518 __ Lsr(out_reg, first_reg, shift_value);
3519 }
3520 }
3521 break;
3522 }
3523 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003524 Register o_h = out.AsRegisterPairHigh<Register>();
3525 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003526
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003527 Register high = first.AsRegisterPairHigh<Register>();
3528 Register low = first.AsRegisterPairLow<Register>();
3529
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003530 if (second.IsRegister()) {
3531 Register temp = locations->GetTemp(0).AsRegister<Register>();
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003532
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003533 Register second_reg = second.AsRegister<Register>();
3534
3535 if (op->IsShl()) {
3536 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftValue));
3537 // Shift the high part
3538 __ Lsl(o_h, high, o_l);
3539 // Shift the low part and `or` what overflew on the high part
3540 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
3541 __ Lsr(temp, low, temp);
3542 __ orr(o_h, o_h, ShifterOperand(temp));
3543 // If the shift is > 32 bits, override the high part
3544 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
3545 __ it(PL);
3546 __ Lsl(o_h, low, temp, PL);
3547 // Shift the low part
3548 __ Lsl(o_l, low, o_l);
3549 } else if (op->IsShr()) {
3550 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftValue));
3551 // Shift the low part
3552 __ Lsr(o_l, low, o_h);
3553 // Shift the high part and `or` what underflew on the low part
3554 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3555 __ Lsl(temp, high, temp);
3556 __ orr(o_l, o_l, ShifterOperand(temp));
3557 // If the shift is > 32 bits, override the low part
3558 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3559 __ it(PL);
3560 __ Asr(o_l, high, temp, PL);
3561 // Shift the high part
3562 __ Asr(o_h, high, o_h);
3563 } else {
3564 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftValue));
3565 // same as Shr except we use `Lsr`s and not `Asr`s
3566 __ Lsr(o_l, low, o_h);
3567 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3568 __ Lsl(temp, high, temp);
3569 __ orr(o_l, o_l, ShifterOperand(temp));
3570 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3571 __ it(PL);
3572 __ Lsr(o_l, high, temp, PL);
3573 __ Lsr(o_h, high, o_h);
3574 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003575 } else {
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003576 // Register allocator doesn't create partial overlap.
3577 DCHECK_NE(o_l, high);
3578 DCHECK_NE(o_h, low);
3579 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
3580 uint32_t shift_value = static_cast<uint32_t>(cst & kMaxLongShiftValue);
3581 if (shift_value > 32) {
3582 if (op->IsShl()) {
3583 __ Lsl(o_h, low, shift_value - 32);
3584 __ LoadImmediate(o_l, 0);
3585 } else if (op->IsShr()) {
3586 __ Asr(o_l, high, shift_value - 32);
3587 __ Asr(o_h, high, 31);
3588 } else {
3589 __ Lsr(o_l, high, shift_value - 32);
3590 __ LoadImmediate(o_h, 0);
3591 }
3592 } else if (shift_value == 32) {
3593 if (op->IsShl()) {
3594 __ mov(o_h, ShifterOperand(low));
3595 __ LoadImmediate(o_l, 0);
3596 } else if (op->IsShr()) {
3597 __ mov(o_l, ShifterOperand(high));
3598 __ Asr(o_h, high, 31);
3599 } else {
3600 __ mov(o_l, ShifterOperand(high));
3601 __ LoadImmediate(o_h, 0);
3602 }
Vladimir Markof9d741e2015-11-20 15:08:11 +00003603 } else if (shift_value == 1) {
3604 if (op->IsShl()) {
3605 __ Lsls(o_l, low, 1);
3606 __ adc(o_h, high, ShifterOperand(high));
3607 } else if (op->IsShr()) {
3608 __ Asrs(o_h, high, 1);
3609 __ Rrx(o_l, low);
3610 } else {
3611 __ Lsrs(o_h, high, 1);
3612 __ Rrx(o_l, low);
3613 }
3614 } else {
3615 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003616 if (op->IsShl()) {
3617 __ Lsl(o_h, high, shift_value);
3618 __ orr(o_h, o_h, ShifterOperand(low, LSR, 32 - shift_value));
3619 __ Lsl(o_l, low, shift_value);
3620 } else if (op->IsShr()) {
3621 __ Lsr(o_l, low, shift_value);
3622 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3623 __ Asr(o_h, high, shift_value);
3624 } else {
3625 __ Lsr(o_l, low, shift_value);
3626 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3627 __ Lsr(o_h, high, shift_value);
3628 }
3629 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003630 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003631 break;
3632 }
3633 default:
3634 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003635 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003636 }
3637}
3638
3639void LocationsBuilderARM::VisitShl(HShl* shl) {
3640 HandleShift(shl);
3641}
3642
3643void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
3644 HandleShift(shl);
3645}
3646
3647void LocationsBuilderARM::VisitShr(HShr* shr) {
3648 HandleShift(shr);
3649}
3650
3651void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
3652 HandleShift(shr);
3653}
3654
3655void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
3656 HandleShift(ushr);
3657}
3658
3659void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
3660 HandleShift(ushr);
3661}
3662
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003663void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003664 LocationSummary* locations =
3665 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003666 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003667 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3668 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003669 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003670}
3671
3672void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003673 // Note: if heap poisoning is enabled, the entry point takes cares
3674 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003675 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003676 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003677 instruction->GetDexPc(),
3678 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003679 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003680}
3681
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003682void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
3683 LocationSummary* locations =
3684 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3685 InvokeRuntimeCallingConvention calling_convention;
3686 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003687 locations->SetOut(Location::RegisterLocation(R0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003688 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003689 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003690}
3691
3692void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
3693 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003694 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003695 // Note: if heap poisoning is enabled, the entry point takes cares
3696 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003697 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003698 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003699 instruction->GetDexPc(),
3700 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003701 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003702}
3703
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003704void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003705 LocationSummary* locations =
3706 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003707 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3708 if (location.IsStackSlot()) {
3709 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3710 } else if (location.IsDoubleStackSlot()) {
3711 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003712 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003713 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003714}
3715
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003716void InstructionCodeGeneratorARM::VisitParameterValue(
3717 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003718 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003719}
3720
3721void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
3722 LocationSummary* locations =
3723 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3724 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3725}
3726
3727void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3728 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003729}
3730
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003731void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003732 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003733 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003734 locations->SetInAt(0, Location::RequiresRegister());
3735 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003736}
3737
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003738void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
3739 LocationSummary* locations = not_->GetLocations();
3740 Location out = locations->Out();
3741 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003742 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003743 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003744 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003745 break;
3746
3747 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003748 __ mvn(out.AsRegisterPairLow<Register>(),
3749 ShifterOperand(in.AsRegisterPairLow<Register>()));
3750 __ mvn(out.AsRegisterPairHigh<Register>(),
3751 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003752 break;
3753
3754 default:
3755 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3756 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003757}
3758
David Brazdil66d126e2015-04-03 16:02:44 +01003759void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
3760 LocationSummary* locations =
3761 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3762 locations->SetInAt(0, Location::RequiresRegister());
3763 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3764}
3765
3766void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003767 LocationSummary* locations = bool_not->GetLocations();
3768 Location out = locations->Out();
3769 Location in = locations->InAt(0);
3770 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
3771}
3772
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003773void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003774 LocationSummary* locations =
3775 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003776 switch (compare->InputAt(0)->GetType()) {
3777 case Primitive::kPrimLong: {
3778 locations->SetInAt(0, Location::RequiresRegister());
3779 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003780 // Output overlaps because it is written before doing the low comparison.
3781 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00003782 break;
3783 }
3784 case Primitive::kPrimFloat:
3785 case Primitive::kPrimDouble: {
3786 locations->SetInAt(0, Location::RequiresFpuRegister());
3787 locations->SetInAt(1, Location::RequiresFpuRegister());
3788 locations->SetOut(Location::RequiresRegister());
3789 break;
3790 }
3791 default:
3792 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3793 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003794}
3795
3796void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003797 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003798 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003799 Location left = locations->InAt(0);
3800 Location right = locations->InAt(1);
3801
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003802 Label less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00003803 Primitive::Type type = compare->InputAt(0)->GetType();
3804 switch (type) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003805 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003806 __ cmp(left.AsRegisterPairHigh<Register>(),
3807 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003808 __ b(&less, LT);
3809 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003810 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00003811 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003812 __ cmp(left.AsRegisterPairLow<Register>(),
3813 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Calin Juravleddb7df22014-11-25 20:56:51 +00003814 break;
3815 }
3816 case Primitive::kPrimFloat:
3817 case Primitive::kPrimDouble: {
3818 __ LoadImmediate(out, 0);
3819 if (type == Primitive::kPrimFloat) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003820 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003821 } else {
3822 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
3823 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
3824 }
3825 __ vmstat(); // transfer FP status register to ARM APSR.
3826 __ b(compare->IsGtBias() ? &greater : &less, VS); // VS for unordered.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003827 break;
3828 }
3829 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003830 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003831 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003832 __ b(&done, EQ);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003833 __ b(&less, LO); // LO is for both: unsigned compare for longs and 'less than' for floats.
Calin Juravleddb7df22014-11-25 20:56:51 +00003834
3835 __ Bind(&greater);
3836 __ LoadImmediate(out, 1);
3837 __ b(&done);
3838
3839 __ Bind(&less);
3840 __ LoadImmediate(out, -1);
3841
3842 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003843}
3844
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003845void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003846 LocationSummary* locations =
3847 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003848 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3849 locations->SetInAt(i, Location::Any());
3850 }
3851 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003852}
3853
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003854void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003855 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003856}
3857
Roland Levillainc9285912015-12-18 10:38:42 +00003858void CodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
3859 // TODO (ported from quick): revisit ARM barrier kinds.
3860 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
Calin Juravle52c48962014-12-16 17:02:57 +00003861 switch (kind) {
3862 case MemBarrierKind::kAnyStore:
3863 case MemBarrierKind::kLoadAny:
3864 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003865 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00003866 break;
3867 }
3868 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003869 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00003870 break;
3871 }
3872 default:
3873 LOG(FATAL) << "Unexpected memory barrier " << kind;
3874 }
Kenny Root1d8199d2015-06-02 11:01:10 -07003875 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00003876}
3877
3878void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
3879 uint32_t offset,
3880 Register out_lo,
3881 Register out_hi) {
3882 if (offset != 0) {
Roland Levillain3b359c72015-11-17 19:35:12 +00003883 // Ensure `out_lo` is different from `addr`, so that loading
3884 // `offset` into `out_lo` does not clutter `addr`.
3885 DCHECK_NE(out_lo, addr);
Calin Juravle52c48962014-12-16 17:02:57 +00003886 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003887 __ add(IP, addr, ShifterOperand(out_lo));
3888 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003889 }
3890 __ ldrexd(out_lo, out_hi, addr);
3891}
3892
3893void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
3894 uint32_t offset,
3895 Register value_lo,
3896 Register value_hi,
3897 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00003898 Register temp2,
3899 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003900 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00003901 if (offset != 0) {
3902 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003903 __ add(IP, addr, ShifterOperand(temp1));
3904 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003905 }
3906 __ Bind(&fail);
3907 // We need a load followed by store. (The address used in a STREX instruction must
3908 // be the same as the address in the most recently executed LDREX instruction.)
3909 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00003910 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003911 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003912 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00003913}
3914
3915void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3916 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3917
Nicolas Geoffray39468442014-09-02 15:17:15 +01003918 LocationSummary* locations =
3919 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003920 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003921
Calin Juravle52c48962014-12-16 17:02:57 +00003922 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003923 if (Primitive::IsFloatingPointType(field_type)) {
3924 locations->SetInAt(1, Location::RequiresFpuRegister());
3925 } else {
3926 locations->SetInAt(1, Location::RequiresRegister());
3927 }
3928
Calin Juravle52c48962014-12-16 17:02:57 +00003929 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00003930 bool generate_volatile = field_info.IsVolatile()
3931 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003932 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01003933 bool needs_write_barrier =
3934 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003935 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00003936 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01003937 if (needs_write_barrier) {
3938 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003939 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003940 } else if (generate_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00003941 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00003942 // - registers need to be consecutive
3943 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00003944 // We don't test for ARM yet, and the assertion makes sure that we
3945 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00003946 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3947
3948 locations->AddTemp(Location::RequiresRegister());
3949 locations->AddTemp(Location::RequiresRegister());
3950 if (field_type == Primitive::kPrimDouble) {
3951 // For doubles we need two more registers to copy the value.
3952 locations->AddTemp(Location::RegisterLocation(R2));
3953 locations->AddTemp(Location::RegisterLocation(R3));
3954 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003955 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003956}
3957
Calin Juravle52c48962014-12-16 17:02:57 +00003958void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003959 const FieldInfo& field_info,
3960 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003961 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3962
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003963 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003964 Register base = locations->InAt(0).AsRegister<Register>();
3965 Location value = locations->InAt(1);
3966
3967 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003968 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00003969 Primitive::Type field_type = field_info.GetFieldType();
3970 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01003971 bool needs_write_barrier =
3972 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003973
3974 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00003975 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00003976 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003977
3978 switch (field_type) {
3979 case Primitive::kPrimBoolean:
3980 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003981 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003982 break;
3983 }
3984
3985 case Primitive::kPrimShort:
3986 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003987 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003988 break;
3989 }
3990
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003991 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003992 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01003993 if (kPoisonHeapReferences && needs_write_barrier) {
3994 // Note that in the case where `value` is a null reference,
3995 // we do not enter this block, as a null reference does not
3996 // need poisoning.
3997 DCHECK_EQ(field_type, Primitive::kPrimNot);
3998 Register temp = locations->GetTemp(0).AsRegister<Register>();
3999 __ Mov(temp, value.AsRegister<Register>());
4000 __ PoisonHeapReference(temp);
4001 __ StoreToOffset(kStoreWord, temp, base, offset);
4002 } else {
4003 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
4004 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004005 break;
4006 }
4007
4008 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00004009 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004010 GenerateWideAtomicStore(base, offset,
4011 value.AsRegisterPairLow<Register>(),
4012 value.AsRegisterPairHigh<Register>(),
4013 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00004014 locations->GetTemp(1).AsRegister<Register>(),
4015 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004016 } else {
4017 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004018 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004019 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004020 break;
4021 }
4022
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004023 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004024 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004025 break;
4026 }
4027
4028 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004029 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004030 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004031 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
4032 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
4033
4034 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
4035
4036 GenerateWideAtomicStore(base, offset,
4037 value_reg_lo,
4038 value_reg_hi,
4039 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00004040 locations->GetTemp(3).AsRegister<Register>(),
4041 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004042 } else {
4043 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004044 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004045 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004046 break;
4047 }
4048
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004049 case Primitive::kPrimVoid:
4050 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004051 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004052 }
Calin Juravle52c48962014-12-16 17:02:57 +00004053
Calin Juravle77520bc2015-01-12 18:45:46 +00004054 // Longs and doubles are handled in the switch.
4055 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
4056 codegen_->MaybeRecordImplicitNullCheck(instruction);
4057 }
4058
4059 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4060 Register temp = locations->GetTemp(0).AsRegister<Register>();
4061 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004062 codegen_->MarkGCCard(
4063 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004064 }
4065
Calin Juravle52c48962014-12-16 17:02:57 +00004066 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004067 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004068 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004069}
4070
Calin Juravle52c48962014-12-16 17:02:57 +00004071void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4072 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain3b359c72015-11-17 19:35:12 +00004073
4074 bool object_field_get_with_read_barrier =
4075 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004076 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004077 new (GetGraph()->GetArena()) LocationSummary(instruction,
4078 object_field_get_with_read_barrier ?
4079 LocationSummary::kCallOnSlowPath :
4080 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004081 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004082
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004083 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00004084 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004085 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain3b359c72015-11-17 19:35:12 +00004086 // The output overlaps in case of volatile long: we don't want the
4087 // code generated by GenerateWideAtomicLoad to overwrite the
4088 // object's location. Likewise, in the case of an object field get
4089 // with read barriers enabled, we do not want the load to overwrite
4090 // the object's location, as we need it to emit the read barrier.
4091 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
4092 object_field_get_with_read_barrier;
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01004093
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004094 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4095 locations->SetOut(Location::RequiresFpuRegister());
4096 } else {
4097 locations->SetOut(Location::RequiresRegister(),
4098 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
4099 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004100 if (volatile_for_double) {
Roland Levillainc9285912015-12-18 10:38:42 +00004101 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00004102 // - registers need to be consecutive
4103 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00004104 // We don't test for ARM yet, and the assertion makes sure that we
4105 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00004106 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4107 locations->AddTemp(Location::RequiresRegister());
4108 locations->AddTemp(Location::RequiresRegister());
Roland Levillainc9285912015-12-18 10:38:42 +00004109 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4110 // We need a temporary register for the read barrier marking slow
4111 // path in CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier.
4112 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004113 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004114}
4115
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004116Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
4117 Opcode opcode) {
4118 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
4119 if (constant->IsConstant() &&
4120 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
4121 return Location::ConstantLocation(constant->AsConstant());
4122 }
4123 return Location::RequiresRegister();
4124}
4125
4126bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
4127 Opcode opcode) {
4128 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
4129 if (Primitive::Is64BitType(input_cst->GetType())) {
4130 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode) &&
4131 CanEncodeConstantAsImmediate(High32Bits(value), opcode);
4132 } else {
4133 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
4134 }
4135}
4136
4137bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value, Opcode opcode) {
4138 ShifterOperand so;
4139 ArmAssembler* assembler = codegen_->GetAssembler();
4140 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, &so)) {
4141 return true;
4142 }
4143 Opcode neg_opcode = kNoOperand;
4144 switch (opcode) {
4145 case AND:
4146 neg_opcode = BIC;
4147 break;
4148 case ORR:
4149 neg_opcode = ORN;
4150 break;
4151 default:
4152 return false;
4153 }
4154 return assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, neg_opcode, ~value, &so);
4155}
4156
Calin Juravle52c48962014-12-16 17:02:57 +00004157void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
4158 const FieldInfo& field_info) {
4159 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004160
Calin Juravle52c48962014-12-16 17:02:57 +00004161 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004162 Location base_loc = locations->InAt(0);
4163 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004164 Location out = locations->Out();
4165 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004166 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004167 Primitive::Type field_type = field_info.GetFieldType();
4168 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4169
4170 switch (field_type) {
Roland Levillainc9285912015-12-18 10:38:42 +00004171 case Primitive::kPrimBoolean:
Calin Juravle52c48962014-12-16 17:02:57 +00004172 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004173 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004174
Roland Levillainc9285912015-12-18 10:38:42 +00004175 case Primitive::kPrimByte:
Calin Juravle52c48962014-12-16 17:02:57 +00004176 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004177 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004178
Roland Levillainc9285912015-12-18 10:38:42 +00004179 case Primitive::kPrimShort:
Calin Juravle52c48962014-12-16 17:02:57 +00004180 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004181 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004182
Roland Levillainc9285912015-12-18 10:38:42 +00004183 case Primitive::kPrimChar:
Calin Juravle52c48962014-12-16 17:02:57 +00004184 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004185 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004186
4187 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004188 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004189 break;
Roland Levillainc9285912015-12-18 10:38:42 +00004190
4191 case Primitive::kPrimNot: {
4192 // /* HeapReference<Object> */ out = *(base + offset)
4193 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4194 Location temp_loc = locations->GetTemp(0);
4195 // Note that a potential implicit null check is handled in this
4196 // CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier call.
4197 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4198 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4199 if (is_volatile) {
4200 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4201 }
4202 } else {
4203 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
4204 codegen_->MaybeRecordImplicitNullCheck(instruction);
4205 if (is_volatile) {
4206 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4207 }
4208 // If read barriers are enabled, emit read barriers other than
4209 // Baker's using a slow path (and also unpoison the loaded
4210 // reference, if heap poisoning is enabled).
4211 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4212 }
4213 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004214 }
4215
Roland Levillainc9285912015-12-18 10:38:42 +00004216 case Primitive::kPrimLong:
Calin Juravle34166012014-12-19 17:22:29 +00004217 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004218 GenerateWideAtomicLoad(base, offset,
4219 out.AsRegisterPairLow<Register>(),
4220 out.AsRegisterPairHigh<Register>());
4221 } else {
4222 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
4223 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004224 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004225
Roland Levillainc9285912015-12-18 10:38:42 +00004226 case Primitive::kPrimFloat:
Calin Juravle52c48962014-12-16 17:02:57 +00004227 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004228 break;
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004229
4230 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004231 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004232 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004233 Register lo = locations->GetTemp(0).AsRegister<Register>();
4234 Register hi = locations->GetTemp(1).AsRegister<Register>();
4235 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00004236 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004237 __ vmovdrr(out_reg, lo, hi);
4238 } else {
4239 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004240 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004241 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004242 break;
4243 }
4244
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004245 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004246 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004247 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004248 }
Calin Juravle52c48962014-12-16 17:02:57 +00004249
Roland Levillainc9285912015-12-18 10:38:42 +00004250 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
4251 // Potential implicit null checks, in the case of reference or
4252 // double fields, are handled in the previous switch statement.
4253 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004254 codegen_->MaybeRecordImplicitNullCheck(instruction);
4255 }
4256
Calin Juravle52c48962014-12-16 17:02:57 +00004257 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004258 if (field_type == Primitive::kPrimNot) {
4259 // Memory barriers, in the case of references, are also handled
4260 // in the previous switch statement.
4261 } else {
4262 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4263 }
Roland Levillain4d027112015-07-01 15:41:14 +01004264 }
Calin Juravle52c48962014-12-16 17:02:57 +00004265}
4266
4267void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4268 HandleFieldSet(instruction, instruction->GetFieldInfo());
4269}
4270
4271void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004272 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004273}
4274
4275void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4276 HandleFieldGet(instruction, instruction->GetFieldInfo());
4277}
4278
4279void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4280 HandleFieldGet(instruction, instruction->GetFieldInfo());
4281}
4282
4283void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4284 HandleFieldGet(instruction, instruction->GetFieldInfo());
4285}
4286
4287void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4288 HandleFieldGet(instruction, instruction->GetFieldInfo());
4289}
4290
4291void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4292 HandleFieldSet(instruction, instruction->GetFieldInfo());
4293}
4294
4295void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004296 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004297}
4298
Calin Juravlee460d1d2015-09-29 04:52:17 +01004299void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
4300 HUnresolvedInstanceFieldGet* instruction) {
4301 FieldAccessCallingConventionARM calling_convention;
4302 codegen_->CreateUnresolvedFieldLocationSummary(
4303 instruction, instruction->GetFieldType(), calling_convention);
4304}
4305
4306void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
4307 HUnresolvedInstanceFieldGet* instruction) {
4308 FieldAccessCallingConventionARM calling_convention;
4309 codegen_->GenerateUnresolvedFieldAccess(instruction,
4310 instruction->GetFieldType(),
4311 instruction->GetFieldIndex(),
4312 instruction->GetDexPc(),
4313 calling_convention);
4314}
4315
4316void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
4317 HUnresolvedInstanceFieldSet* instruction) {
4318 FieldAccessCallingConventionARM calling_convention;
4319 codegen_->CreateUnresolvedFieldLocationSummary(
4320 instruction, instruction->GetFieldType(), calling_convention);
4321}
4322
4323void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
4324 HUnresolvedInstanceFieldSet* instruction) {
4325 FieldAccessCallingConventionARM calling_convention;
4326 codegen_->GenerateUnresolvedFieldAccess(instruction,
4327 instruction->GetFieldType(),
4328 instruction->GetFieldIndex(),
4329 instruction->GetDexPc(),
4330 calling_convention);
4331}
4332
4333void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
4334 HUnresolvedStaticFieldGet* instruction) {
4335 FieldAccessCallingConventionARM calling_convention;
4336 codegen_->CreateUnresolvedFieldLocationSummary(
4337 instruction, instruction->GetFieldType(), calling_convention);
4338}
4339
4340void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
4341 HUnresolvedStaticFieldGet* instruction) {
4342 FieldAccessCallingConventionARM calling_convention;
4343 codegen_->GenerateUnresolvedFieldAccess(instruction,
4344 instruction->GetFieldType(),
4345 instruction->GetFieldIndex(),
4346 instruction->GetDexPc(),
4347 calling_convention);
4348}
4349
4350void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
4351 HUnresolvedStaticFieldSet* instruction) {
4352 FieldAccessCallingConventionARM calling_convention;
4353 codegen_->CreateUnresolvedFieldLocationSummary(
4354 instruction, instruction->GetFieldType(), calling_convention);
4355}
4356
4357void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
4358 HUnresolvedStaticFieldSet* instruction) {
4359 FieldAccessCallingConventionARM calling_convention;
4360 codegen_->GenerateUnresolvedFieldAccess(instruction,
4361 instruction->GetFieldType(),
4362 instruction->GetFieldIndex(),
4363 instruction->GetDexPc(),
4364 calling_convention);
4365}
4366
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004367void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004368 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4369 ? LocationSummary::kCallOnSlowPath
4370 : LocationSummary::kNoCall;
4371 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravle77520bc2015-01-12 18:45:46 +00004372 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004373 if (instruction->HasUses()) {
4374 locations->SetOut(Location::SameAsFirstInput());
4375 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004376}
4377
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004378void InstructionCodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004379 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4380 return;
4381 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004382 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004383
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004384 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
4385 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4386}
4387
4388void InstructionCodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004389 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004390 codegen_->AddSlowPath(slow_path);
4391
4392 LocationSummary* locations = instruction->GetLocations();
4393 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004394
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004395 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004396}
4397
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004398void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004399 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004400 GenerateImplicitNullCheck(instruction);
4401 } else {
4402 GenerateExplicitNullCheck(instruction);
4403 }
4404}
4405
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004406void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004407 bool object_array_get_with_read_barrier =
4408 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004409 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004410 new (GetGraph()->GetArena()) LocationSummary(instruction,
4411 object_array_get_with_read_barrier ?
4412 LocationSummary::kCallOnSlowPath :
4413 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004414 locations->SetInAt(0, Location::RequiresRegister());
4415 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004416 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4417 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4418 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004419 // The output overlaps in the case of an object array get with
4420 // read barriers enabled: we do not want the move to overwrite the
4421 // array's location, as we need it to emit the read barrier.
4422 locations->SetOut(
4423 Location::RequiresRegister(),
4424 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004425 }
Roland Levillainc9285912015-12-18 10:38:42 +00004426 // We need a temporary register for the read barrier marking slow
4427 // path in CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier.
4428 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4429 locations->AddTemp(Location::RequiresRegister());
4430 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004431}
4432
4433void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
4434 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004435 Location obj_loc = locations->InAt(0);
4436 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004437 Location index = locations->InAt(1);
Roland Levillainc9285912015-12-18 10:38:42 +00004438 Location out_loc = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004439
Roland Levillainc9285912015-12-18 10:38:42 +00004440 Primitive::Type type = instruction->GetType();
Roland Levillain4d027112015-07-01 15:41:14 +01004441 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004442 case Primitive::kPrimBoolean: {
4443 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004444 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004445 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004446 size_t offset =
4447 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004448 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
4449 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004450 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004451 __ LoadFromOffset(kLoadUnsignedByte, out, IP, data_offset);
4452 }
4453 break;
4454 }
4455
4456 case Primitive::kPrimByte: {
4457 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004458 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004459 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004460 size_t offset =
4461 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004462 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
4463 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004464 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004465 __ LoadFromOffset(kLoadSignedByte, out, IP, data_offset);
4466 }
4467 break;
4468 }
4469
4470 case Primitive::kPrimShort: {
4471 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004472 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004473 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004474 size_t offset =
4475 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004476 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
4477 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004478 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004479 __ LoadFromOffset(kLoadSignedHalfword, out, IP, data_offset);
4480 }
4481 break;
4482 }
4483
4484 case Primitive::kPrimChar: {
4485 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004486 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004487 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004488 size_t offset =
4489 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004490 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
4491 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004492 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004493 __ LoadFromOffset(kLoadUnsignedHalfword, out, IP, data_offset);
4494 }
4495 break;
4496 }
4497
Roland Levillainc9285912015-12-18 10:38:42 +00004498 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004499 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004500 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004501 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004502 size_t offset =
4503 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004504 __ LoadFromOffset(kLoadWord, out, obj, offset);
4505 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004506 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004507 __ LoadFromOffset(kLoadWord, out, IP, data_offset);
4508 }
4509 break;
4510 }
4511
Roland Levillainc9285912015-12-18 10:38:42 +00004512 case Primitive::kPrimNot: {
4513 static_assert(
4514 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4515 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
4516 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4517 // /* HeapReference<Object> */ out =
4518 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4519 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4520 Location temp = locations->GetTemp(0);
4521 // Note that a potential implicit null check is handled in this
4522 // CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier call.
4523 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4524 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4525 } else {
4526 Register out = out_loc.AsRegister<Register>();
4527 if (index.IsConstant()) {
4528 size_t offset =
4529 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4530 __ LoadFromOffset(kLoadWord, out, obj, offset);
4531 codegen_->MaybeRecordImplicitNullCheck(instruction);
4532 // If read barriers are enabled, emit read barriers other than
4533 // Baker's using a slow path (and also unpoison the loaded
4534 // reference, if heap poisoning is enabled).
4535 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4536 } else {
4537 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4538 __ LoadFromOffset(kLoadWord, out, IP, data_offset);
4539 codegen_->MaybeRecordImplicitNullCheck(instruction);
4540 // If read barriers are enabled, emit read barriers other than
4541 // Baker's using a slow path (and also unpoison the loaded
4542 // reference, if heap poisoning is enabled).
4543 codegen_->MaybeGenerateReadBarrierSlow(
4544 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4545 }
4546 }
4547 break;
4548 }
4549
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004550 case Primitive::kPrimLong: {
4551 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004552 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004553 size_t offset =
4554 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004555 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004556 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004557 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00004558 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004559 }
4560 break;
4561 }
4562
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004563 case Primitive::kPrimFloat: {
4564 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004565 SRegister out = out_loc.AsFpuRegister<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004566 if (index.IsConstant()) {
4567 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004568 __ LoadSFromOffset(out, obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004569 } else {
4570 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillainc9285912015-12-18 10:38:42 +00004571 __ LoadSFromOffset(out, IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004572 }
4573 break;
4574 }
4575
4576 case Primitive::kPrimDouble: {
4577 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004578 SRegister out = out_loc.AsFpuRegisterPairLow<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004579 if (index.IsConstant()) {
4580 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004581 __ LoadDFromOffset(FromLowSToD(out), obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004582 } else {
4583 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00004584 __ LoadDFromOffset(FromLowSToD(out), IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004585 }
4586 break;
4587 }
4588
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004589 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004590 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004591 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004592 }
Roland Levillain4d027112015-07-01 15:41:14 +01004593
4594 if (type == Primitive::kPrimNot) {
Roland Levillainc9285912015-12-18 10:38:42 +00004595 // Potential implicit null checks, in the case of reference
4596 // arrays, are handled in the previous switch statement.
4597 } else {
4598 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004599 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004600}
4601
4602void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004603 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004604
4605 bool needs_write_barrier =
4606 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain3b359c72015-11-17 19:35:12 +00004607 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
4608 bool object_array_set_with_read_barrier =
4609 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004610
Nicolas Geoffray39468442014-09-02 15:17:15 +01004611 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004612 instruction,
Roland Levillain3b359c72015-11-17 19:35:12 +00004613 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
4614 LocationSummary::kCallOnSlowPath :
4615 LocationSummary::kNoCall);
4616
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004617 locations->SetInAt(0, Location::RequiresRegister());
4618 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4619 if (Primitive::IsFloatingPointType(value_type)) {
4620 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004621 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004622 locations->SetInAt(2, Location::RequiresRegister());
4623 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004624 if (needs_write_barrier) {
4625 // Temporary registers for the write barrier.
4626 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00004627 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004628 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004629}
4630
4631void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
4632 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004633 Location array_loc = locations->InAt(0);
4634 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004635 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004636 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain3b359c72015-11-17 19:35:12 +00004637 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004638 bool needs_write_barrier =
4639 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004640
4641 switch (value_type) {
4642 case Primitive::kPrimBoolean:
4643 case Primitive::kPrimByte: {
4644 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004645 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004646 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004647 size_t offset =
4648 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004649 __ StoreToOffset(kStoreByte, value, array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004650 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004651 __ add(IP, array, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004652 __ StoreToOffset(kStoreByte, value, IP, data_offset);
4653 }
4654 break;
4655 }
4656
4657 case Primitive::kPrimShort:
4658 case Primitive::kPrimChar: {
4659 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004660 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004661 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004662 size_t offset =
4663 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004664 __ StoreToOffset(kStoreHalfword, value, array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004665 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004666 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004667 __ StoreToOffset(kStoreHalfword, value, IP, data_offset);
4668 }
4669 break;
4670 }
4671
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004672 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004673 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00004674 Location value_loc = locations->InAt(2);
4675 Register value = value_loc.AsRegister<Register>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004676 Register source = value;
4677
4678 if (instruction->InputAt(2)->IsNullConstant()) {
4679 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004680 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004681 size_t offset =
4682 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004683 __ StoreToOffset(kStoreWord, source, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004684 } else {
4685 DCHECK(index.IsRegister()) << index;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004686 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillain4d027112015-07-01 15:41:14 +01004687 __ StoreToOffset(kStoreWord, source, IP, data_offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004688 }
Roland Levillain1407ee72016-01-08 15:56:19 +00004689 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain3b359c72015-11-17 19:35:12 +00004690 DCHECK(!needs_write_barrier);
4691 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004692 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004693 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004694
4695 DCHECK(needs_write_barrier);
4696 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
4697 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
4698 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4699 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4700 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4701 Label done;
4702 SlowPathCode* slow_path = nullptr;
4703
Roland Levillain3b359c72015-11-17 19:35:12 +00004704 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004705 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
4706 codegen_->AddSlowPath(slow_path);
4707 if (instruction->GetValueCanBeNull()) {
4708 Label non_zero;
4709 __ CompareAndBranchIfNonZero(value, &non_zero);
4710 if (index.IsConstant()) {
4711 size_t offset =
4712 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4713 __ StoreToOffset(kStoreWord, value, array, offset);
4714 } else {
4715 DCHECK(index.IsRegister()) << index;
4716 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4717 __ StoreToOffset(kStoreWord, value, IP, data_offset);
4718 }
4719 codegen_->MaybeRecordImplicitNullCheck(instruction);
4720 __ b(&done);
4721 __ Bind(&non_zero);
4722 }
4723
Roland Levillain3b359c72015-11-17 19:35:12 +00004724 if (kEmitCompilerReadBarrier) {
4725 // When read barriers are enabled, the type checking
4726 // instrumentation requires two read barriers:
4727 //
4728 // __ Mov(temp2, temp1);
4729 // // /* HeapReference<Class> */ temp1 = temp1->component_type_
4730 // __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
Roland Levillainc9285912015-12-18 10:38:42 +00004731 // codegen_->GenerateReadBarrierSlow(
Roland Levillain3b359c72015-11-17 19:35:12 +00004732 // instruction, temp1_loc, temp1_loc, temp2_loc, component_offset);
4733 //
4734 // // /* HeapReference<Class> */ temp2 = value->klass_
4735 // __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
Roland Levillainc9285912015-12-18 10:38:42 +00004736 // codegen_->GenerateReadBarrierSlow(
Roland Levillain3b359c72015-11-17 19:35:12 +00004737 // instruction, temp2_loc, temp2_loc, value_loc, class_offset, temp1_loc);
4738 //
4739 // __ cmp(temp1, ShifterOperand(temp2));
4740 //
4741 // However, the second read barrier may trash `temp`, as it
4742 // is a temporary register, and as such would not be saved
4743 // along with live registers before calling the runtime (nor
4744 // restored afterwards). So in this case, we bail out and
4745 // delegate the work to the array set slow path.
4746 //
4747 // TODO: Extend the register allocator to support a new
4748 // "(locally) live temp" location so as to avoid always
4749 // going into the slow path when read barriers are enabled.
4750 __ b(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004751 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004752 // /* HeapReference<Class> */ temp1 = array->klass_
4753 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
4754 codegen_->MaybeRecordImplicitNullCheck(instruction);
4755 __ MaybeUnpoisonHeapReference(temp1);
4756
4757 // /* HeapReference<Class> */ temp1 = temp1->component_type_
4758 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
4759 // /* HeapReference<Class> */ temp2 = value->klass_
4760 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
4761 // If heap poisoning is enabled, no need to unpoison `temp1`
4762 // nor `temp2`, as we are comparing two poisoned references.
4763 __ cmp(temp1, ShifterOperand(temp2));
4764
4765 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4766 Label do_put;
4767 __ b(&do_put, EQ);
4768 // If heap poisoning is enabled, the `temp1` reference has
4769 // not been unpoisoned yet; unpoison it now.
4770 __ MaybeUnpoisonHeapReference(temp1);
4771
4772 // /* HeapReference<Class> */ temp1 = temp1->super_class_
4773 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
4774 // If heap poisoning is enabled, no need to unpoison
4775 // `temp1`, as we are comparing against null below.
4776 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
4777 __ Bind(&do_put);
4778 } else {
4779 __ b(slow_path->GetEntryLabel(), NE);
4780 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004781 }
4782 }
4783
4784 if (kPoisonHeapReferences) {
4785 // Note that in the case where `value` is a null reference,
4786 // we do not enter this block, as a null reference does not
4787 // need poisoning.
4788 DCHECK_EQ(value_type, Primitive::kPrimNot);
4789 __ Mov(temp1, value);
4790 __ PoisonHeapReference(temp1);
4791 source = temp1;
4792 }
4793
4794 if (index.IsConstant()) {
4795 size_t offset =
4796 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4797 __ StoreToOffset(kStoreWord, source, array, offset);
4798 } else {
4799 DCHECK(index.IsRegister()) << index;
4800 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4801 __ StoreToOffset(kStoreWord, source, IP, data_offset);
4802 }
4803
Roland Levillain3b359c72015-11-17 19:35:12 +00004804 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004805 codegen_->MaybeRecordImplicitNullCheck(instruction);
4806 }
4807
4808 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
4809
4810 if (done.IsLinked()) {
4811 __ Bind(&done);
4812 }
4813
4814 if (slow_path != nullptr) {
4815 __ Bind(slow_path->GetExitLabel());
4816 }
4817
4818 break;
4819 }
4820
4821 case Primitive::kPrimInt: {
4822 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4823 Register value = locations->InAt(2).AsRegister<Register>();
4824 if (index.IsConstant()) {
4825 size_t offset =
4826 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4827 __ StoreToOffset(kStoreWord, value, array, offset);
4828 } else {
4829 DCHECK(index.IsRegister()) << index;
4830 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4831 __ StoreToOffset(kStoreWord, value, IP, data_offset);
4832 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004833 break;
4834 }
4835
4836 case Primitive::kPrimLong: {
4837 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004838 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004839 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004840 size_t offset =
4841 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004842 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004843 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004844 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004845 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004846 }
4847 break;
4848 }
4849
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004850 case Primitive::kPrimFloat: {
4851 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4852 Location value = locations->InAt(2);
4853 DCHECK(value.IsFpuRegister());
4854 if (index.IsConstant()) {
4855 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004856 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004857 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004858 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004859 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
4860 }
4861 break;
4862 }
4863
4864 case Primitive::kPrimDouble: {
4865 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4866 Location value = locations->InAt(2);
4867 DCHECK(value.IsFpuRegisterPair());
4868 if (index.IsConstant()) {
4869 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004870 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004871 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004872 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004873 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
4874 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004875
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004876 break;
4877 }
4878
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004879 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004880 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004881 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004882 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004883
Roland Levillain80e67092016-01-08 16:04:55 +00004884 // Objects are handled in the switch.
4885 if (value_type != Primitive::kPrimNot) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004886 codegen_->MaybeRecordImplicitNullCheck(instruction);
4887 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004888}
4889
4890void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004891 LocationSummary* locations =
4892 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004893 locations->SetInAt(0, Location::RequiresRegister());
4894 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004895}
4896
4897void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
4898 LocationSummary* locations = instruction->GetLocations();
4899 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004900 Register obj = locations->InAt(0).AsRegister<Register>();
4901 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004902 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004903 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004904}
4905
4906void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004907 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4908 ? LocationSummary::kCallOnSlowPath
4909 : LocationSummary::kNoCall;
4910 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004911 locations->SetInAt(0, Location::RequiresRegister());
4912 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004913 if (instruction->HasUses()) {
4914 locations->SetOut(Location::SameAsFirstInput());
4915 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004916}
4917
4918void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
4919 LocationSummary* locations = instruction->GetLocations();
Andreas Gampe85b62f22015-09-09 13:15:38 -07004920 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004921 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004922 codegen_->AddSlowPath(slow_path);
4923
Roland Levillain271ab9c2014-11-27 15:23:57 +00004924 Register index = locations->InAt(0).AsRegister<Register>();
4925 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004926
4927 __ cmp(index, ShifterOperand(length));
Roland Levillain4fa13f62015-07-06 18:11:54 +01004928 __ b(slow_path->GetEntryLabel(), HS);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004929}
4930
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004931void CodeGeneratorARM::MarkGCCard(Register temp,
4932 Register card,
4933 Register object,
4934 Register value,
4935 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004936 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004937 if (can_be_null) {
4938 __ CompareAndBranchIfZero(value, &is_null);
4939 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004940 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmWordSize>().Int32Value());
4941 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
4942 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004943 if (can_be_null) {
4944 __ Bind(&is_null);
4945 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004946}
4947
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004948void LocationsBuilderARM::VisitTemporary(HTemporary* temp) {
4949 temp->SetLocations(nullptr);
4950}
4951
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004952void InstructionCodeGeneratorARM::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004953 // Nothing to do, this is driven by the code generator.
4954}
4955
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004956void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004957 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004958}
4959
4960void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004961 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4962}
4963
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004964void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
4965 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4966}
4967
4968void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004969 HBasicBlock* block = instruction->GetBlock();
4970 if (block->GetLoopInformation() != nullptr) {
4971 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4972 // The back edge will generate the suspend check.
4973 return;
4974 }
4975 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4976 // The goto will generate the suspend check.
4977 return;
4978 }
4979 GenerateSuspendCheck(instruction, nullptr);
4980}
4981
4982void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
4983 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004984 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004985 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
4986 if (slow_path == nullptr) {
4987 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
4988 instruction->SetSlowPath(slow_path);
4989 codegen_->AddSlowPath(slow_path);
4990 if (successor != nullptr) {
4991 DCHECK(successor->IsLoopHeader());
4992 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4993 }
4994 } else {
4995 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4996 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004997
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00004998 __ LoadFromOffset(
4999 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmWordSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005000 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005001 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005002 __ Bind(slow_path->GetReturnLabel());
5003 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005004 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005005 __ b(slow_path->GetEntryLabel());
5006 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005007}
5008
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005009ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
5010 return codegen_->GetAssembler();
5011}
5012
5013void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005014 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005015 Location source = move->GetSource();
5016 Location destination = move->GetDestination();
5017
5018 if (source.IsRegister()) {
5019 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005020 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005021 } else {
5022 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005023 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005024 SP, destination.GetStackIndex());
5025 }
5026 } else if (source.IsStackSlot()) {
5027 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005028 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005029 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005030 } else if (destination.IsFpuRegister()) {
5031 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005032 } else {
5033 DCHECK(destination.IsStackSlot());
5034 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
5035 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5036 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005037 } else if (source.IsFpuRegister()) {
5038 if (destination.IsFpuRegister()) {
5039 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005040 } else {
5041 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005042 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
5043 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005044 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005045 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005046 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
5047 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005048 } else if (destination.IsRegisterPair()) {
5049 DCHECK(ExpectedPairLayout(destination));
5050 __ LoadFromOffset(
5051 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
5052 } else {
5053 DCHECK(destination.IsFpuRegisterPair()) << destination;
5054 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5055 SP,
5056 source.GetStackIndex());
5057 }
5058 } else if (source.IsRegisterPair()) {
5059 if (destination.IsRegisterPair()) {
5060 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
5061 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
5062 } else {
5063 DCHECK(destination.IsDoubleStackSlot()) << destination;
5064 DCHECK(ExpectedPairLayout(source));
5065 __ StoreToOffset(
5066 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
5067 }
5068 } else if (source.IsFpuRegisterPair()) {
5069 if (destination.IsFpuRegisterPair()) {
5070 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5071 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5072 } else {
5073 DCHECK(destination.IsDoubleStackSlot()) << destination;
5074 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
5075 SP,
5076 destination.GetStackIndex());
5077 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005078 } else {
5079 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005080 HConstant* constant = source.GetConstant();
5081 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5082 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005083 if (destination.IsRegister()) {
5084 __ LoadImmediate(destination.AsRegister<Register>(), value);
5085 } else {
5086 DCHECK(destination.IsStackSlot());
5087 __ LoadImmediate(IP, value);
5088 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5089 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005090 } else if (constant->IsLongConstant()) {
5091 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005092 if (destination.IsRegisterPair()) {
5093 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
5094 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005095 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005096 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005097 __ LoadImmediate(IP, Low32Bits(value));
5098 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5099 __ LoadImmediate(IP, High32Bits(value));
5100 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5101 }
5102 } else if (constant->IsDoubleConstant()) {
5103 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005104 if (destination.IsFpuRegisterPair()) {
5105 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005106 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005107 DCHECK(destination.IsDoubleStackSlot()) << destination;
5108 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005109 __ LoadImmediate(IP, Low32Bits(int_value));
5110 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5111 __ LoadImmediate(IP, High32Bits(int_value));
5112 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5113 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005114 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005115 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005116 float value = constant->AsFloatConstant()->GetValue();
5117 if (destination.IsFpuRegister()) {
5118 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
5119 } else {
5120 DCHECK(destination.IsStackSlot());
5121 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
5122 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5123 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005124 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005125 }
5126}
5127
5128void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
5129 __ Mov(IP, reg);
5130 __ LoadFromOffset(kLoadWord, reg, SP, mem);
5131 __ StoreToOffset(kStoreWord, IP, SP, mem);
5132}
5133
5134void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
5135 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
5136 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
5137 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
5138 SP, mem1 + stack_offset);
5139 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
5140 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
5141 SP, mem2 + stack_offset);
5142 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
5143}
5144
5145void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005146 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005147 Location source = move->GetSource();
5148 Location destination = move->GetDestination();
5149
5150 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005151 DCHECK_NE(source.AsRegister<Register>(), IP);
5152 DCHECK_NE(destination.AsRegister<Register>(), IP);
5153 __ Mov(IP, source.AsRegister<Register>());
5154 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
5155 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005156 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005157 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005158 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005159 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005160 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5161 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005162 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005163 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005164 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005165 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005166 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005167 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005168 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005169 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005170 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5171 destination.AsRegisterPairHigh<Register>(),
5172 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005173 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005174 Register low_reg = source.IsRegisterPair()
5175 ? source.AsRegisterPairLow<Register>()
5176 : destination.AsRegisterPairLow<Register>();
5177 int mem = source.IsRegisterPair()
5178 ? destination.GetStackIndex()
5179 : source.GetStackIndex();
5180 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005181 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005182 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005183 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005184 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005185 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
5186 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005187 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005188 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005189 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005190 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
5191 DRegister reg = source.IsFpuRegisterPair()
5192 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
5193 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
5194 int mem = source.IsFpuRegisterPair()
5195 ? destination.GetStackIndex()
5196 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005197 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005198 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005199 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005200 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
5201 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
5202 : destination.AsFpuRegister<SRegister>();
5203 int mem = source.IsFpuRegister()
5204 ? destination.GetStackIndex()
5205 : source.GetStackIndex();
5206
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005207 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005208 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005209 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005210 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005211 Exchange(source.GetStackIndex(), destination.GetStackIndex());
5212 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005213 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005214 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005215 }
5216}
5217
5218void ParallelMoveResolverARM::SpillScratch(int reg) {
5219 __ Push(static_cast<Register>(reg));
5220}
5221
5222void ParallelMoveResolverARM::RestoreScratch(int reg) {
5223 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005224}
5225
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005226void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01005227 InvokeRuntimeCallingConvention calling_convention;
5228 CodeGenerator::CreateLoadClassLocationSummary(
5229 cls,
5230 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Roland Levillain3b359c72015-11-17 19:35:12 +00005231 Location::RegisterLocation(R0),
5232 /* code_generator_supports_read_barrier */ true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005233}
5234
5235void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005236 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005237 if (cls->NeedsAccessCheck()) {
5238 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5239 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5240 cls,
5241 cls->GetDexPc(),
5242 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005243 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005244 return;
5245 }
5246
Roland Levillain3b359c72015-11-17 19:35:12 +00005247 Location out_loc = locations->Out();
5248 Register out = out_loc.AsRegister<Register>();
Calin Juravle580b6092015-10-06 17:35:58 +01005249 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005250
Calin Juravle580b6092015-10-06 17:35:58 +01005251 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005252 DCHECK(!cls->CanCallRuntime());
5253 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillainc9285912015-12-18 10:38:42 +00005254 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5255 GenerateGcRootFieldLoad(
5256 cls, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005257 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00005258 // /* GcRoot<mirror::Class>[] */ out =
5259 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005260 __ LoadFromOffset(kLoadWord,
5261 out,
5262 current_method,
Vladimir Marko05792b92015-08-03 11:56:49 +01005263 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
Roland Levillainc9285912015-12-18 10:38:42 +00005264 // /* GcRoot<mirror::Class> */ out = out[type_index]
5265 GenerateGcRootFieldLoad(cls, out_loc, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005266
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005267 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
5268 DCHECK(cls->CanCallRuntime());
5269 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
5270 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5271 codegen_->AddSlowPath(slow_path);
5272 if (!cls->IsInDexCache()) {
5273 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5274 }
5275 if (cls->MustGenerateClinitCheck()) {
5276 GenerateClassInitializationCheck(slow_path, out);
5277 } else {
5278 __ Bind(slow_path->GetExitLabel());
5279 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005280 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005281 }
5282}
5283
5284void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
5285 LocationSummary* locations =
5286 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5287 locations->SetInAt(0, Location::RequiresRegister());
5288 if (check->HasUses()) {
5289 locations->SetOut(Location::SameAsFirstInput());
5290 }
5291}
5292
5293void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005294 // We assume the class is not null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005295 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005296 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005297 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005298 GenerateClassInitializationCheck(slow_path,
5299 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005300}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005301
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005302void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005303 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005304 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
5305 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
5306 __ b(slow_path->GetEntryLabel(), LT);
5307 // Even if the initialized flag is set, we may be in a situation where caches are not synced
5308 // properly. Therefore, we do a memory fence.
5309 __ dmb(ISH);
5310 __ Bind(slow_path->GetExitLabel());
5311}
5312
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005313void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005314 LocationSummary::CallKind call_kind = (!load->IsInDexCache() || kEmitCompilerReadBarrier)
5315 ? LocationSummary::kCallOnSlowPath
5316 : LocationSummary::kNoCall;
5317 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005318 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005319 locations->SetOut(Location::RequiresRegister());
5320}
5321
5322void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005323 LocationSummary* locations = load->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005324 Location out_loc = locations->Out();
5325 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005326 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005327
Roland Levillainc9285912015-12-18 10:38:42 +00005328 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5329 GenerateGcRootFieldLoad(
5330 load, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Roland Levillain3b359c72015-11-17 19:35:12 +00005331 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
Mathieu Chartiereace4582014-11-24 18:29:54 -08005332 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
Roland Levillainc9285912015-12-18 10:38:42 +00005333 // /* GcRoot<mirror::String> */ out = out[string_index]
5334 GenerateGcRootFieldLoad(
5335 load, out_loc, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Roland Levillain3b359c72015-11-17 19:35:12 +00005336
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005337 if (!load->IsInDexCache()) {
5338 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
5339 codegen_->AddSlowPath(slow_path);
5340 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5341 __ Bind(slow_path->GetExitLabel());
5342 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005343}
5344
David Brazdilcb1c0552015-08-04 16:22:25 +01005345static int32_t GetExceptionTlsOffset() {
5346 return Thread::ExceptionOffset<kArmWordSize>().Int32Value();
5347}
5348
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005349void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
5350 LocationSummary* locations =
5351 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5352 locations->SetOut(Location::RequiresRegister());
5353}
5354
5355void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005356 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01005357 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
5358}
5359
5360void LocationsBuilderARM::VisitClearException(HClearException* clear) {
5361 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5362}
5363
5364void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005365 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01005366 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005367}
5368
5369void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
5370 LocationSummary* locations =
5371 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5372 InvokeRuntimeCallingConvention calling_convention;
5373 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5374}
5375
5376void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
5377 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00005378 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005379 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005380}
5381
Roland Levillainc9285912015-12-18 10:38:42 +00005382static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5383 return kEmitCompilerReadBarrier &&
5384 (kUseBakerReadBarrier ||
5385 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5386 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5387 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5388}
5389
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005390void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005391 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain3b359c72015-11-17 19:35:12 +00005392 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5393 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005394 case TypeCheckKind::kExactCheck:
5395 case TypeCheckKind::kAbstractClassCheck:
5396 case TypeCheckKind::kClassHierarchyCheck:
5397 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005398 call_kind =
5399 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005400 break;
5401 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005402 case TypeCheckKind::kUnresolvedCheck:
5403 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005404 call_kind = LocationSummary::kCallOnSlowPath;
5405 break;
5406 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005407
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005408 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain3b359c72015-11-17 19:35:12 +00005409 locations->SetInAt(0, Location::RequiresRegister());
5410 locations->SetInAt(1, Location::RequiresRegister());
5411 // The "out" register is used as a temporary, so it overlaps with the inputs.
5412 // Note that TypeCheckSlowPathARM uses this register too.
5413 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5414 // When read barriers are enabled, we need a temporary register for
5415 // some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00005416 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain3b359c72015-11-17 19:35:12 +00005417 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005418 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005419}
5420
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005421void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00005422 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005423 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005424 Location obj_loc = locations->InAt(0);
5425 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005426 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005427 Location out_loc = locations->Out();
5428 Register out = out_loc.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +00005429 Location temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
5430 locations->GetTemp(0) :
5431 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005432 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005433 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5434 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5435 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005436 Label done, zero;
Andreas Gampe85b62f22015-09-09 13:15:38 -07005437 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005438
5439 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005440 // avoid null check if we know obj is not null.
5441 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00005442 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005443 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005444
Roland Levillain3b359c72015-11-17 19:35:12 +00005445 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillainc9285912015-12-18 10:38:42 +00005446 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005447
Roland Levillainc9285912015-12-18 10:38:42 +00005448 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005449 case TypeCheckKind::kExactCheck: {
5450 __ cmp(out, ShifterOperand(cls));
5451 // Classes must be equal for the instanceof to succeed.
5452 __ b(&zero, NE);
5453 __ LoadImmediate(out, 1);
5454 __ b(&done);
5455 break;
5456 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005457
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005458 case TypeCheckKind::kAbstractClassCheck: {
5459 // If the class is abstract, we eagerly fetch the super class of the
5460 // object to avoid doing a comparison we know will fail.
5461 Label loop;
5462 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005463 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillainc9285912015-12-18 10:38:42 +00005464 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005465 // If `out` is null, we use it for the result, and jump to `done`.
5466 __ CompareAndBranchIfZero(out, &done);
5467 __ cmp(out, ShifterOperand(cls));
5468 __ b(&loop, NE);
5469 __ LoadImmediate(out, 1);
5470 if (zero.IsLinked()) {
5471 __ b(&done);
5472 }
5473 break;
5474 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005475
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005476 case TypeCheckKind::kClassHierarchyCheck: {
5477 // Walk over the class hierarchy to find a match.
5478 Label loop, success;
5479 __ Bind(&loop);
5480 __ cmp(out, ShifterOperand(cls));
5481 __ b(&success, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005482 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillainc9285912015-12-18 10:38:42 +00005483 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005484 __ CompareAndBranchIfNonZero(out, &loop);
5485 // If `out` is null, we use it for the result, and jump to `done`.
5486 __ b(&done);
5487 __ Bind(&success);
5488 __ LoadImmediate(out, 1);
5489 if (zero.IsLinked()) {
5490 __ b(&done);
5491 }
5492 break;
5493 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005494
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005495 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005496 // Do an exact check.
5497 Label exact_check;
5498 __ cmp(out, ShifterOperand(cls));
5499 __ b(&exact_check, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005500 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00005501 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillainc9285912015-12-18 10:38:42 +00005502 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005503 // If `out` is null, we use it for the result, and jump to `done`.
5504 __ CompareAndBranchIfZero(out, &done);
5505 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
5506 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
5507 __ CompareAndBranchIfNonZero(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005508 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005509 __ LoadImmediate(out, 1);
5510 __ b(&done);
5511 break;
5512 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005513
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005514 case TypeCheckKind::kArrayCheck: {
5515 __ cmp(out, ShifterOperand(cls));
5516 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain3b359c72015-11-17 19:35:12 +00005517 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5518 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005519 codegen_->AddSlowPath(slow_path);
5520 __ b(slow_path->GetEntryLabel(), NE);
5521 __ LoadImmediate(out, 1);
5522 if (zero.IsLinked()) {
5523 __ b(&done);
5524 }
5525 break;
5526 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005527
Calin Juravle98893e12015-10-02 21:05:03 +01005528 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005529 case TypeCheckKind::kInterfaceCheck: {
5530 // Note that we indeed only call on slow path, but we always go
5531 // into the slow path for the unresolved & interface check
5532 // cases.
5533 //
5534 // We cannot directly call the InstanceofNonTrivial runtime
5535 // entry point without resorting to a type checking slow path
5536 // here (i.e. by calling InvokeRuntime directly), as it would
5537 // require to assign fixed registers for the inputs of this
5538 // HInstanceOf instruction (following the runtime calling
5539 // convention), which might be cluttered by the potential first
5540 // read barrier emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00005541 //
5542 // TODO: Introduce a new runtime entry point taking the object
5543 // to test (instead of its class) as argument, and let it deal
5544 // with the read barrier issues. This will let us refactor this
5545 // case of the `switch` code as it was previously (with a direct
5546 // call to the runtime not using a type checking slow path).
5547 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00005548 DCHECK(locations->OnlyCallsOnSlowPath());
5549 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5550 /* is_fatal */ false);
5551 codegen_->AddSlowPath(slow_path);
5552 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005553 if (zero.IsLinked()) {
5554 __ b(&done);
5555 }
5556 break;
5557 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005558 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005559
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005560 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005561 __ Bind(&zero);
5562 __ LoadImmediate(out, 0);
5563 }
5564
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005565 if (done.IsLinked()) {
5566 __ Bind(&done);
5567 }
5568
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005569 if (slow_path != nullptr) {
5570 __ Bind(slow_path->GetExitLabel());
5571 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005572}
5573
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005574void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005575 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5576 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
5577
Roland Levillain3b359c72015-11-17 19:35:12 +00005578 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5579 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005580 case TypeCheckKind::kExactCheck:
5581 case TypeCheckKind::kAbstractClassCheck:
5582 case TypeCheckKind::kClassHierarchyCheck:
5583 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005584 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5585 LocationSummary::kCallOnSlowPath :
5586 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005587 break;
5588 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005589 case TypeCheckKind::kUnresolvedCheck:
5590 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005591 call_kind = LocationSummary::kCallOnSlowPath;
5592 break;
5593 }
5594
Roland Levillain3b359c72015-11-17 19:35:12 +00005595 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5596 locations->SetInAt(0, Location::RequiresRegister());
5597 locations->SetInAt(1, Location::RequiresRegister());
5598 // Note that TypeCheckSlowPathARM uses this "temp" register too.
5599 locations->AddTemp(Location::RequiresRegister());
5600 // When read barriers are enabled, we need an additional temporary
5601 // register for some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00005602 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005603 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005604 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005605}
5606
5607void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00005608 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005609 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005610 Location obj_loc = locations->InAt(0);
5611 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005612 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005613 Location temp_loc = locations->GetTemp(0);
5614 Register temp = temp_loc.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +00005615 Location temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
5616 locations->GetTemp(1) :
5617 Location::NoLocation();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005618 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005619 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5620 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5621 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005622
Roland Levillain3b359c72015-11-17 19:35:12 +00005623 bool is_type_check_slow_path_fatal =
5624 (type_check_kind == TypeCheckKind::kExactCheck ||
5625 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5626 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5627 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5628 !instruction->CanThrowIntoCatchBlock();
5629 SlowPathCode* type_check_slow_path =
5630 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5631 is_type_check_slow_path_fatal);
5632 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005633
5634 Label done;
5635 // Avoid null check if we know obj is not null.
5636 if (instruction->MustDoNullCheck()) {
5637 __ CompareAndBranchIfZero(obj, &done);
5638 }
5639
Roland Levillain3b359c72015-11-17 19:35:12 +00005640 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillainc9285912015-12-18 10:38:42 +00005641 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005642
Roland Levillain3b359c72015-11-17 19:35:12 +00005643 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005644 case TypeCheckKind::kExactCheck:
5645 case TypeCheckKind::kArrayCheck: {
5646 __ cmp(temp, ShifterOperand(cls));
5647 // Jump to slow path for throwing the exception or doing a
5648 // more involved array check.
Roland Levillain3b359c72015-11-17 19:35:12 +00005649 __ b(type_check_slow_path->GetEntryLabel(), NE);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005650 break;
5651 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005652
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005653 case TypeCheckKind::kAbstractClassCheck: {
5654 // If the class is abstract, we eagerly fetch the super class of the
5655 // object to avoid doing a comparison we know will fail.
Roland Levillain3b359c72015-11-17 19:35:12 +00005656 Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005657 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005658 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillainc9285912015-12-18 10:38:42 +00005659 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005660
5661 // If the class reference currently in `temp` is not null, jump
5662 // to the `compare_classes` label to compare it with the checked
5663 // class.
5664 __ CompareAndBranchIfNonZero(temp, &compare_classes);
5665 // Otherwise, jump to the slow path to throw the exception.
5666 //
5667 // But before, move back the object's class into `temp` before
5668 // going into the slow path, as it has been overwritten in the
5669 // meantime.
5670 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillainc9285912015-12-18 10:38:42 +00005671 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005672 __ b(type_check_slow_path->GetEntryLabel());
5673
5674 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005675 __ cmp(temp, ShifterOperand(cls));
5676 __ b(&loop, NE);
5677 break;
5678 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005679
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005680 case TypeCheckKind::kClassHierarchyCheck: {
5681 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005682 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005683 __ Bind(&loop);
5684 __ cmp(temp, ShifterOperand(cls));
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005685 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005686
Roland Levillain3b359c72015-11-17 19:35:12 +00005687 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillainc9285912015-12-18 10:38:42 +00005688 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005689
5690 // If the class reference currently in `temp` is not null, jump
5691 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005692 __ CompareAndBranchIfNonZero(temp, &loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005693 // Otherwise, jump to the slow path to throw the exception.
5694 //
5695 // But before, move back the object's class into `temp` before
5696 // going into the slow path, as it has been overwritten in the
5697 // meantime.
5698 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillainc9285912015-12-18 10:38:42 +00005699 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005700 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005701 break;
5702 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005703
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005704 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005705 // Do an exact check.
Roland Levillain3b359c72015-11-17 19:35:12 +00005706 Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005707 __ cmp(temp, ShifterOperand(cls));
5708 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005709
5710 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00005711 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillainc9285912015-12-18 10:38:42 +00005712 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005713
5714 // If the component type is not null (i.e. the object is indeed
5715 // an array), jump to label `check_non_primitive_component_type`
5716 // to further check that this component type is not a primitive
5717 // type.
5718 __ CompareAndBranchIfNonZero(temp, &check_non_primitive_component_type);
5719 // Otherwise, jump to the slow path to throw the exception.
5720 //
5721 // But before, move back the object's class into `temp` before
5722 // going into the slow path, as it has been overwritten in the
5723 // meantime.
5724 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillainc9285912015-12-18 10:38:42 +00005725 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005726 __ b(type_check_slow_path->GetEntryLabel());
5727
5728 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005729 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00005730 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
5731 __ CompareAndBranchIfZero(temp, &done);
5732 // Same comment as above regarding `temp` and the slow path.
5733 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillainc9285912015-12-18 10:38:42 +00005734 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005735 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005736 break;
5737 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005738
Calin Juravle98893e12015-10-02 21:05:03 +01005739 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005740 case TypeCheckKind::kInterfaceCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005741 // We always go into the type check slow path for the unresolved &
5742 // interface check cases.
5743 //
5744 // We cannot directly call the CheckCast runtime entry point
5745 // without resorting to a type checking slow path here (i.e. by
5746 // calling InvokeRuntime directly), as it would require to
5747 // assign fixed registers for the inputs of this HInstanceOf
5748 // instruction (following the runtime calling convention), which
5749 // might be cluttered by the potential first read barrier
5750 // emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00005751 //
5752 // TODO: Introduce a new runtime entry point taking the object
5753 // to test (instead of its class) as argument, and let it deal
5754 // with the read barrier issues. This will let us refactor this
5755 // case of the `switch` code as it was previously (with a direct
5756 // call to the runtime not using a type checking slow path).
5757 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00005758 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005759 break;
5760 }
5761 __ Bind(&done);
5762
Roland Levillain3b359c72015-11-17 19:35:12 +00005763 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005764}
5765
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005766void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
5767 LocationSummary* locations =
5768 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5769 InvokeRuntimeCallingConvention calling_convention;
5770 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5771}
5772
5773void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
5774 codegen_->InvokeRuntime(instruction->IsEnter()
5775 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
5776 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00005777 instruction->GetDexPc(),
5778 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005779 if (instruction->IsEnter()) {
5780 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5781 } else {
5782 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5783 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005784}
5785
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005786void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
5787void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
5788void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005789
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005790void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005791 LocationSummary* locations =
5792 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5793 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
5794 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005795 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005796 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005797 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00005798 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005799}
5800
5801void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
5802 HandleBitwiseOperation(instruction);
5803}
5804
5805void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
5806 HandleBitwiseOperation(instruction);
5807}
5808
5809void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
5810 HandleBitwiseOperation(instruction);
5811}
5812
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005813void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
5814 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
5815 if (value == 0xffffffffu) {
5816 if (out != first) {
5817 __ mov(out, ShifterOperand(first));
5818 }
5819 return;
5820 }
5821 if (value == 0u) {
5822 __ mov(out, ShifterOperand(0));
5823 return;
5824 }
5825 ShifterOperand so;
5826 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
5827 __ and_(out, first, so);
5828 } else {
5829 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so));
5830 __ bic(out, first, ShifterOperand(~value));
5831 }
5832}
5833
5834void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
5835 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
5836 if (value == 0u) {
5837 if (out != first) {
5838 __ mov(out, ShifterOperand(first));
5839 }
5840 return;
5841 }
5842 if (value == 0xffffffffu) {
5843 __ mvn(out, ShifterOperand(0));
5844 return;
5845 }
5846 ShifterOperand so;
5847 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
5848 __ orr(out, first, so);
5849 } else {
5850 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
5851 __ orn(out, first, ShifterOperand(~value));
5852 }
5853}
5854
5855void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
5856 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
5857 if (value == 0u) {
5858 if (out != first) {
5859 __ mov(out, ShifterOperand(first));
5860 }
5861 return;
5862 }
5863 __ eor(out, first, ShifterOperand(value));
5864}
5865
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005866void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
5867 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005868 Location first = locations->InAt(0);
5869 Location second = locations->InAt(1);
5870 Location out = locations->Out();
5871
5872 if (second.IsConstant()) {
5873 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
5874 uint32_t value_low = Low32Bits(value);
5875 if (instruction->GetResultType() == Primitive::kPrimInt) {
5876 Register first_reg = first.AsRegister<Register>();
5877 Register out_reg = out.AsRegister<Register>();
5878 if (instruction->IsAnd()) {
5879 GenerateAndConst(out_reg, first_reg, value_low);
5880 } else if (instruction->IsOr()) {
5881 GenerateOrrConst(out_reg, first_reg, value_low);
5882 } else {
5883 DCHECK(instruction->IsXor());
5884 GenerateEorConst(out_reg, first_reg, value_low);
5885 }
5886 } else {
5887 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
5888 uint32_t value_high = High32Bits(value);
5889 Register first_low = first.AsRegisterPairLow<Register>();
5890 Register first_high = first.AsRegisterPairHigh<Register>();
5891 Register out_low = out.AsRegisterPairLow<Register>();
5892 Register out_high = out.AsRegisterPairHigh<Register>();
5893 if (instruction->IsAnd()) {
5894 GenerateAndConst(out_low, first_low, value_low);
5895 GenerateAndConst(out_high, first_high, value_high);
5896 } else if (instruction->IsOr()) {
5897 GenerateOrrConst(out_low, first_low, value_low);
5898 GenerateOrrConst(out_high, first_high, value_high);
5899 } else {
5900 DCHECK(instruction->IsXor());
5901 GenerateEorConst(out_low, first_low, value_low);
5902 GenerateEorConst(out_high, first_high, value_high);
5903 }
5904 }
5905 return;
5906 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005907
5908 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005909 Register first_reg = first.AsRegister<Register>();
5910 ShifterOperand second_reg(second.AsRegister<Register>());
5911 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005912 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005913 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005914 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005915 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005916 } else {
5917 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005918 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005919 }
5920 } else {
5921 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005922 Register first_low = first.AsRegisterPairLow<Register>();
5923 Register first_high = first.AsRegisterPairHigh<Register>();
5924 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
5925 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
5926 Register out_low = out.AsRegisterPairLow<Register>();
5927 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005928 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005929 __ and_(out_low, first_low, second_low);
5930 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005931 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005932 __ orr(out_low, first_low, second_low);
5933 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005934 } else {
5935 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005936 __ eor(out_low, first_low, second_low);
5937 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005938 }
5939 }
5940}
5941
Roland Levillainc9285912015-12-18 10:38:42 +00005942void InstructionCodeGeneratorARM::GenerateReferenceLoadOneRegister(HInstruction* instruction,
5943 Location out,
5944 uint32_t offset,
5945 Location temp) {
5946 Register out_reg = out.AsRegister<Register>();
5947 if (kEmitCompilerReadBarrier) {
5948 if (kUseBakerReadBarrier) {
5949 // Load with fast path based Baker's read barrier.
5950 // /* HeapReference<Object> */ out = *(out + offset)
5951 codegen_->GenerateFieldLoadWithBakerReadBarrier(
5952 instruction, out, out_reg, offset, temp, /* needs_null_check */ false);
5953 } else {
5954 // Load with slow path based read barrier.
5955 // Save the value of `out` into `temp` before overwriting it
5956 // in the following move operation, as we will need it for the
5957 // read barrier below.
5958 __ Mov(temp.AsRegister<Register>(), out_reg);
5959 // /* HeapReference<Object> */ out = *(out + offset)
5960 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
5961 codegen_->GenerateReadBarrierSlow(instruction, out, out, temp, offset);
5962 }
5963 } else {
5964 // Plain load with no read barrier.
5965 // /* HeapReference<Object> */ out = *(out + offset)
5966 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
5967 __ MaybeUnpoisonHeapReference(out_reg);
5968 }
5969}
5970
5971void InstructionCodeGeneratorARM::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
5972 Location out,
5973 Location obj,
5974 uint32_t offset,
5975 Location temp) {
5976 Register out_reg = out.AsRegister<Register>();
5977 Register obj_reg = obj.AsRegister<Register>();
5978 if (kEmitCompilerReadBarrier) {
5979 if (kUseBakerReadBarrier) {
5980 // Load with fast path based Baker's read barrier.
5981 // /* HeapReference<Object> */ out = *(obj + offset)
5982 codegen_->GenerateFieldLoadWithBakerReadBarrier(
5983 instruction, out, obj_reg, offset, temp, /* needs_null_check */ false);
5984 } else {
5985 // Load with slow path based read barrier.
5986 // /* HeapReference<Object> */ out = *(obj + offset)
5987 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
5988 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5989 }
5990 } else {
5991 // Plain load with no read barrier.
5992 // /* HeapReference<Object> */ out = *(obj + offset)
5993 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
5994 __ MaybeUnpoisonHeapReference(out_reg);
5995 }
5996}
5997
5998void InstructionCodeGeneratorARM::GenerateGcRootFieldLoad(HInstruction* instruction,
5999 Location root,
6000 Register obj,
6001 uint32_t offset) {
6002 Register root_reg = root.AsRegister<Register>();
6003 if (kEmitCompilerReadBarrier) {
6004 if (kUseBakerReadBarrier) {
6005 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6006 // Baker's read barrier are used:
6007 //
6008 // root = obj.field;
6009 // if (Thread::Current()->GetIsGcMarking()) {
6010 // root = ReadBarrier::Mark(root)
6011 // }
6012
6013 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6014 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6015 static_assert(
6016 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6017 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6018 "have different sizes.");
6019 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6020 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6021 "have different sizes.");
6022
6023 // Slow path used to mark the GC root `root`.
6024 SlowPathCode* slow_path =
6025 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, root, root);
6026 codegen_->AddSlowPath(slow_path);
6027
6028 __ LoadFromOffset(
6029 kLoadWord, IP, TR, Thread::IsGcMarkingOffset<kArmWordSize>().Int32Value());
6030 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
6031 __ Bind(slow_path->GetExitLabel());
6032 } else {
6033 // GC root loaded through a slow path for read barriers other
6034 // than Baker's.
6035 // /* GcRoot<mirror::Object>* */ root = obj + offset
6036 __ AddConstant(root_reg, obj, offset);
6037 // /* mirror::Object* */ root = root->Read()
6038 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6039 }
6040 } else {
6041 // Plain GC root load with no read barrier.
6042 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6043 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6044 // Note that GC roots are not affected by heap poisoning, thus we
6045 // do not have to unpoison `root_reg` here.
6046 }
6047}
6048
6049void CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6050 Location ref,
6051 Register obj,
6052 uint32_t offset,
6053 Location temp,
6054 bool needs_null_check) {
6055 DCHECK(kEmitCompilerReadBarrier);
6056 DCHECK(kUseBakerReadBarrier);
6057
6058 // /* HeapReference<Object> */ ref = *(obj + offset)
6059 Location no_index = Location::NoLocation();
6060 GenerateReferenceLoadWithBakerReadBarrier(
6061 instruction, ref, obj, offset, no_index, temp, needs_null_check);
6062}
6063
6064void CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6065 Location ref,
6066 Register obj,
6067 uint32_t data_offset,
6068 Location index,
6069 Location temp,
6070 bool needs_null_check) {
6071 DCHECK(kEmitCompilerReadBarrier);
6072 DCHECK(kUseBakerReadBarrier);
6073
6074 // /* HeapReference<Object> */ ref =
6075 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6076 GenerateReferenceLoadWithBakerReadBarrier(
6077 instruction, ref, obj, data_offset, index, temp, needs_null_check);
6078}
6079
6080void CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6081 Location ref,
6082 Register obj,
6083 uint32_t offset,
6084 Location index,
6085 Location temp,
6086 bool needs_null_check) {
6087 DCHECK(kEmitCompilerReadBarrier);
6088 DCHECK(kUseBakerReadBarrier);
6089
6090 // In slow path based read barriers, the read barrier call is
6091 // inserted after the original load. However, in fast path based
6092 // Baker's read barriers, we need to perform the load of
6093 // mirror::Object::monitor_ *before* the original reference load.
6094 // This load-load ordering is required by the read barrier.
6095 // The fast path/slow path (for Baker's algorithm) should look like:
6096 //
6097 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6098 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6099 // HeapReference<Object> ref = *src; // Original reference load.
6100 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6101 // if (is_gray) {
6102 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6103 // }
6104 //
6105 // Note: the original implementation in ReadBarrier::Barrier is
6106 // slightly more complex as:
6107 // - it implements the load-load fence using a data dependency on
6108 // the high-bits of rb_state, which are expected to be all zeroes;
6109 // - it performs additional checks that we do not do here for
6110 // performance reasons.
6111
6112 Register ref_reg = ref.AsRegister<Register>();
6113 Register temp_reg = temp.AsRegister<Register>();
6114 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6115
6116 // /* int32_t */ monitor = obj->monitor_
6117 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
6118 if (needs_null_check) {
6119 MaybeRecordImplicitNullCheck(instruction);
6120 }
6121 // /* LockWord */ lock_word = LockWord(monitor)
6122 static_assert(sizeof(LockWord) == sizeof(int32_t),
6123 "art::LockWord and int32_t have different sizes.");
6124 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
6125 __ Lsr(temp_reg, temp_reg, LockWord::kReadBarrierStateShift);
6126 __ and_(temp_reg, temp_reg, ShifterOperand(LockWord::kReadBarrierStateMask));
6127 static_assert(
6128 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
6129 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
6130
6131 // Introduce a dependency on the high bits of rb_state, which shall
6132 // be all zeroes, to prevent load-load reordering, and without using
6133 // a memory barrier (which would be more expensive).
6134 // IP = rb_state & ~LockWord::kReadBarrierStateMask = 0
6135 __ bic(IP, temp_reg, ShifterOperand(LockWord::kReadBarrierStateMask));
6136 // obj is unchanged by this operation, but its value now depends on
6137 // IP, which depends on temp_reg.
6138 __ add(obj, obj, ShifterOperand(IP));
6139
6140 // The actual reference load.
6141 if (index.IsValid()) {
6142 static_assert(
6143 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6144 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
6145 // /* HeapReference<Object> */ ref =
6146 // *(obj + offset + index * sizeof(HeapReference<Object>))
6147 if (index.IsConstant()) {
6148 size_t computed_offset =
6149 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset;
6150 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
6151 } else {
6152 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
6153 __ LoadFromOffset(kLoadWord, ref_reg, IP, offset);
6154 }
6155 } else {
6156 // /* HeapReference<Object> */ ref = *(obj + offset)
6157 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
6158 }
6159
6160 // Object* ref = ref_addr->AsMirrorPtr()
6161 __ MaybeUnpoisonHeapReference(ref_reg);
6162
6163 // Slow path used to mark the object `ref` when it is gray.
6164 SlowPathCode* slow_path =
6165 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, ref, ref);
6166 AddSlowPath(slow_path);
6167
6168 // if (rb_state == ReadBarrier::gray_ptr_)
6169 // ref = ReadBarrier::Mark(ref);
6170 __ cmp(temp_reg, ShifterOperand(ReadBarrier::gray_ptr_));
6171 __ b(slow_path->GetEntryLabel(), EQ);
6172 __ Bind(slow_path->GetExitLabel());
6173}
6174
6175void CodeGeneratorARM::GenerateReadBarrierSlow(HInstruction* instruction,
6176 Location out,
6177 Location ref,
6178 Location obj,
6179 uint32_t offset,
6180 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006181 DCHECK(kEmitCompilerReadBarrier);
6182
Roland Levillainc9285912015-12-18 10:38:42 +00006183 // Insert a slow path based read barrier *after* the reference load.
6184 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006185 // If heap poisoning is enabled, the unpoisoning of the loaded
6186 // reference will be carried out by the runtime within the slow
6187 // path.
6188 //
6189 // Note that `ref` currently does not get unpoisoned (when heap
6190 // poisoning is enabled), which is alright as the `ref` argument is
6191 // not used by the artReadBarrierSlow entry point.
6192 //
6193 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6194 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6195 ReadBarrierForHeapReferenceSlowPathARM(instruction, out, ref, obj, offset, index);
6196 AddSlowPath(slow_path);
6197
Roland Levillain3b359c72015-11-17 19:35:12 +00006198 __ b(slow_path->GetEntryLabel());
6199 __ Bind(slow_path->GetExitLabel());
6200}
6201
Roland Levillainc9285912015-12-18 10:38:42 +00006202void CodeGeneratorARM::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6203 Location out,
6204 Location ref,
6205 Location obj,
6206 uint32_t offset,
6207 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006208 if (kEmitCompilerReadBarrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00006209 // Baker's read barriers shall be handled by the fast path
6210 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
6211 DCHECK(!kUseBakerReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00006212 // If heap poisoning is enabled, unpoisoning will be taken care of
6213 // by the runtime within the slow path.
Roland Levillainc9285912015-12-18 10:38:42 +00006214 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain3b359c72015-11-17 19:35:12 +00006215 } else if (kPoisonHeapReferences) {
6216 __ UnpoisonHeapReference(out.AsRegister<Register>());
6217 }
6218}
6219
Roland Levillainc9285912015-12-18 10:38:42 +00006220void CodeGeneratorARM::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6221 Location out,
6222 Location root) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006223 DCHECK(kEmitCompilerReadBarrier);
6224
Roland Levillainc9285912015-12-18 10:38:42 +00006225 // Insert a slow path based read barrier *after* the GC root load.
6226 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006227 // Note that GC roots are not affected by heap poisoning, so we do
6228 // not need to do anything special for this here.
6229 SlowPathCode* slow_path =
6230 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM(instruction, out, root);
6231 AddSlowPath(slow_path);
6232
Roland Levillain3b359c72015-11-17 19:35:12 +00006233 __ b(slow_path->GetEntryLabel());
6234 __ Bind(slow_path->GetExitLabel());
6235}
6236
Vladimir Markodc151b22015-10-15 18:02:30 +01006237HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch(
6238 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
6239 MethodReference target_method) {
Vladimir Markodc151b22015-10-15 18:02:30 +01006240 if (desired_dispatch_info.code_ptr_location ==
6241 HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative) {
6242 const DexFile& outer_dex_file = GetGraph()->GetDexFile();
6243 if (&outer_dex_file != target_method.dex_file) {
6244 // Calls across dex files are more likely to exceed the available BL range,
6245 // so use absolute patch with fixup if available and kCallArtMethod otherwise.
6246 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location =
6247 (desired_dispatch_info.method_load_kind ==
6248 HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup)
6249 ? HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup
6250 : HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
6251 return HInvokeStaticOrDirect::DispatchInfo {
6252 desired_dispatch_info.method_load_kind,
6253 code_ptr_location,
6254 desired_dispatch_info.method_load_data,
6255 0u
6256 };
6257 }
6258 }
6259 return desired_dispatch_info;
6260}
6261
Vladimir Markob4536b72015-11-24 13:45:23 +00006262Register CodeGeneratorARM::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
6263 Register temp) {
6264 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
6265 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
6266 if (!invoke->GetLocations()->Intrinsified()) {
6267 return location.AsRegister<Register>();
6268 }
6269 // For intrinsics we allow any location, so it may be on the stack.
6270 if (!location.IsRegister()) {
6271 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
6272 return temp;
6273 }
6274 // For register locations, check if the register was saved. If so, get it from the stack.
6275 // Note: There is a chance that the register was saved but not overwritten, so we could
6276 // save one load. However, since this is just an intrinsic slow path we prefer this
6277 // simple and more robust approach rather that trying to determine if that's the case.
6278 SlowPathCode* slow_path = GetCurrentSlowPath();
6279 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
6280 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
6281 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
6282 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
6283 return temp;
6284 }
6285 return location.AsRegister<Register>();
6286}
6287
Nicolas Geoffray38207af2015-06-01 15:46:22 +01006288void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00006289 // For better instruction scheduling we load the direct code pointer before the method pointer.
Vladimir Marko58155012015-08-19 12:49:41 +00006290 switch (invoke->GetCodePtrLocation()) {
Vladimir Marko58155012015-08-19 12:49:41 +00006291 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
6292 // LR = code address from literal pool with link-time patch.
6293 __ LoadLiteral(LR, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
Vladimir Marko58155012015-08-19 12:49:41 +00006294 break;
6295 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
6296 // LR = invoke->GetDirectCodePtr();
6297 __ LoadImmediate(LR, invoke->GetDirectCodePtr());
Vladimir Marko58155012015-08-19 12:49:41 +00006298 break;
6299 default:
6300 break;
6301 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006302
Vladimir Marko58155012015-08-19 12:49:41 +00006303 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
6304 switch (invoke->GetMethodLoadKind()) {
6305 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
6306 // temp = thread->string_init_entrypoint
6307 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, invoke->GetStringInitOffset());
6308 break;
6309 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00006310 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006311 break;
6312 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
6313 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
6314 break;
6315 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
6316 __ LoadLiteral(temp.AsRegister<Register>(),
6317 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
6318 break;
Vladimir Markob4536b72015-11-24 13:45:23 +00006319 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
6320 HArmDexCacheArraysBase* base =
6321 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
6322 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
6323 temp.AsRegister<Register>());
6324 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
6325 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
6326 break;
6327 }
Vladimir Marko58155012015-08-19 12:49:41 +00006328 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00006329 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006330 Register method_reg;
6331 Register reg = temp.AsRegister<Register>();
6332 if (current_method.IsRegister()) {
6333 method_reg = current_method.AsRegister<Register>();
6334 } else {
6335 DCHECK(invoke->GetLocations()->Intrinsified());
6336 DCHECK(!current_method.IsValid());
6337 method_reg = reg;
6338 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
6339 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006340 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
6341 __ LoadFromOffset(kLoadWord,
6342 reg,
6343 method_reg,
6344 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00006345 // temp = temp[index_in_cache]
6346 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
6347 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
6348 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01006349 }
Vladimir Marko58155012015-08-19 12:49:41 +00006350 }
6351
6352 switch (invoke->GetCodePtrLocation()) {
6353 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
6354 __ bl(GetFrameEntryLabel());
6355 break;
6356 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01006357 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006358 __ BindTrackedLabel(&relative_call_patches_.back().label);
Vladimir Markodc151b22015-10-15 18:02:30 +01006359 // Arbitrarily branch to the BL itself, override at link time.
6360 __ bl(&relative_call_patches_.back().label);
6361 break;
Vladimir Marko58155012015-08-19 12:49:41 +00006362 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
6363 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
6364 // LR prepared above for better instruction scheduling.
Vladimir Marko58155012015-08-19 12:49:41 +00006365 // LR()
6366 __ blx(LR);
6367 break;
6368 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
6369 // LR = callee_method->entry_point_from_quick_compiled_code_
6370 __ LoadFromOffset(
6371 kLoadWord, LR, callee_method.AsRegister<Register>(),
6372 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize).Int32Value());
6373 // LR()
6374 __ blx(LR);
6375 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006376 }
6377
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006378 DCHECK(!IsLeafMethod());
6379}
6380
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006381void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
6382 Register temp = temp_location.AsRegister<Register>();
6383 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
6384 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006385
6386 // Use the calling convention instead of the location of the receiver, as
6387 // intrinsics may have put the receiver in a different register. In the intrinsics
6388 // slow path, the arguments have been moved to the right place, so here we are
6389 // guaranteed that the receiver is the first register of the calling convention.
6390 InvokeDexCallingConvention calling_convention;
6391 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006392 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00006393 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006394 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006395 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00006396 // Instead of simply (possibly) unpoisoning `temp` here, we should
6397 // emit a read barrier for the previous class reference load.
6398 // However this is not required in practice, as this is an
6399 // intermediate/temporary reference and because the current
6400 // concurrent copying collector keeps the from-space memory
6401 // intact/accessible until the end of the marking phase (the
6402 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006403 __ MaybeUnpoisonHeapReference(temp);
6404 // temp = temp->GetMethodAt(method_offset);
6405 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
6406 kArmWordSize).Int32Value();
6407 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
6408 // LR = temp->GetEntryPoint();
6409 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
6410 // LR();
6411 __ blx(LR);
6412}
6413
Vladimir Marko58155012015-08-19 12:49:41 +00006414void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
6415 DCHECK(linker_patches->empty());
Vladimir Markob4536b72015-11-24 13:45:23 +00006416 size_t size =
6417 method_patches_.size() +
6418 call_patches_.size() +
6419 relative_call_patches_.size() +
6420 /* MOVW+MOVT for each base */ 2u * dex_cache_arrays_base_labels_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00006421 linker_patches->reserve(size);
6422 for (const auto& entry : method_patches_) {
6423 const MethodReference& target_method = entry.first;
6424 Literal* literal = entry.second;
6425 DCHECK(literal->GetLabel()->IsBound());
6426 uint32_t literal_offset = literal->GetLabel()->Position();
6427 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
6428 target_method.dex_file,
6429 target_method.dex_method_index));
6430 }
6431 for (const auto& entry : call_patches_) {
6432 const MethodReference& target_method = entry.first;
6433 Literal* literal = entry.second;
6434 DCHECK(literal->GetLabel()->IsBound());
6435 uint32_t literal_offset = literal->GetLabel()->Position();
6436 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
6437 target_method.dex_file,
6438 target_method.dex_method_index));
6439 }
6440 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
6441 uint32_t literal_offset = info.label.Position();
6442 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
6443 info.target_method.dex_file,
6444 info.target_method.dex_method_index));
6445 }
Vladimir Markob4536b72015-11-24 13:45:23 +00006446 for (const auto& pair : dex_cache_arrays_base_labels_) {
6447 HArmDexCacheArraysBase* base = pair.first;
6448 const DexCacheArraysBaseLabels* labels = &pair.second;
6449 const DexFile& dex_file = base->GetDexFile();
6450 size_t base_element_offset = base->GetElementOffset();
6451 DCHECK(labels->add_pc_label.IsBound());
6452 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(labels->add_pc_label.Position());
6453 // Add MOVW patch.
6454 DCHECK(labels->movw_label.IsBound());
6455 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(labels->movw_label.Position());
6456 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(movw_offset,
6457 &dex_file,
6458 add_pc_offset,
6459 base_element_offset));
6460 // Add MOVT patch.
6461 DCHECK(labels->movt_label.IsBound());
6462 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(labels->movt_label.Position());
6463 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(movt_offset,
6464 &dex_file,
6465 add_pc_offset,
6466 base_element_offset));
6467 }
Vladimir Marko58155012015-08-19 12:49:41 +00006468}
6469
6470Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
6471 MethodToLiteralMap* map) {
6472 // Look up the literal for target_method.
6473 auto lb = map->lower_bound(target_method);
6474 if (lb != map->end() && !map->key_comp()(target_method, lb->first)) {
6475 return lb->second;
6476 }
6477 // We don't have a literal for this method yet, insert a new one.
6478 Literal* literal = __ NewLiteral<uint32_t>(0u);
6479 map->PutBefore(lb, target_method, literal);
6480 return literal;
6481}
6482
6483Literal* CodeGeneratorARM::DeduplicateMethodAddressLiteral(MethodReference target_method) {
6484 return DeduplicateMethodLiteral(target_method, &method_patches_);
6485}
6486
6487Literal* CodeGeneratorARM::DeduplicateMethodCodeLiteral(MethodReference target_method) {
6488 return DeduplicateMethodLiteral(target_method, &call_patches_);
6489}
6490
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006491void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006492 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006493 LOG(FATAL) << "Unreachable";
6494}
6495
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006496void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006497 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006498 LOG(FATAL) << "Unreachable";
6499}
6500
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01006501void LocationsBuilderARM::VisitFakeString(HFakeString* instruction) {
6502 DCHECK(codegen_->IsBaseline());
6503 LocationSummary* locations =
6504 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6505 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
6506}
6507
6508void InstructionCodeGeneratorARM::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
6509 DCHECK(codegen_->IsBaseline());
6510 // Will be generated at use site.
6511}
6512
Mark Mendellfe57faa2015-09-18 09:26:15 -04006513// Simple implementation of packed switch - generate cascaded compare/jumps.
6514void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6515 LocationSummary* locations =
6516 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6517 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006518 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006519 codegen_->GetAssembler()->IsThumb()) {
6520 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
6521 if (switch_instr->GetStartValue() != 0) {
6522 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
6523 }
6524 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04006525}
6526
6527void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6528 int32_t lower_bound = switch_instr->GetStartValue();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006529 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006530 LocationSummary* locations = switch_instr->GetLocations();
6531 Register value_reg = locations->InAt(0).AsRegister<Register>();
6532 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6533
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006534 if (num_entries <= kPackedSwitchCompareJumpThreshold || !codegen_->GetAssembler()->IsThumb()) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006535 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006536 Register temp_reg = IP;
6537 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
6538 // the immediate, because IP is used as the destination register. For the other
6539 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
6540 // and they can be encoded in the instruction without making use of IP register.
6541 __ AddConstantSetFlags(temp_reg, value_reg, -lower_bound);
6542
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006543 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006544 // Jump to successors[0] if value == lower_bound.
6545 __ b(codegen_->GetLabelOf(successors[0]), EQ);
6546 int32_t last_index = 0;
6547 for (; num_entries - last_index > 2; last_index += 2) {
6548 __ AddConstantSetFlags(temp_reg, temp_reg, -2);
6549 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6550 __ b(codegen_->GetLabelOf(successors[last_index + 1]), LO);
6551 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6552 __ b(codegen_->GetLabelOf(successors[last_index + 2]), EQ);
6553 }
6554 if (num_entries - last_index == 2) {
6555 // The last missing case_value.
6556 GenerateCompareWithImmediate(temp_reg, 1);
6557 __ b(codegen_->GetLabelOf(successors[last_index + 1]), EQ);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006558 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04006559
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006560 // And the default for any other value.
6561 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6562 __ b(codegen_->GetLabelOf(default_block));
6563 }
6564 } else {
6565 // Create a table lookup.
6566 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
6567
6568 // Materialize a pointer to the switch table
6569 std::vector<Label*> labels(num_entries);
6570 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6571 for (uint32_t i = 0; i < num_entries; i++) {
6572 labels[i] = codegen_->GetLabelOf(successors[i]);
6573 }
6574 JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg);
6575
6576 // Remove the bias.
6577 Register key_reg;
6578 if (lower_bound != 0) {
6579 key_reg = locations->GetTemp(1).AsRegister<Register>();
6580 __ AddConstant(key_reg, value_reg, -lower_bound);
6581 } else {
6582 key_reg = value_reg;
6583 }
6584
6585 // Check whether the value is in the table, jump to default block if not.
6586 __ CmpConstant(key_reg, num_entries - 1);
6587 __ b(codegen_->GetLabelOf(default_block), Condition::HI);
6588
6589 // Load the displacement from the table.
6590 __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2));
6591
6592 // Dispatch is a direct add to the PC (for Thumb2).
6593 __ EmitJumpTableDispatch(table, temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006594 }
6595}
6596
Vladimir Markob4536b72015-11-24 13:45:23 +00006597void LocationsBuilderARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
6598 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
6599 locations->SetOut(Location::RequiresRegister());
6600 codegen_->AddDexCacheArraysBase(base);
6601}
6602
6603void InstructionCodeGeneratorARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
6604 Register base_reg = base->GetLocations()->Out().AsRegister<Register>();
6605 CodeGeneratorARM::DexCacheArraysBaseLabels* labels = codegen_->GetDexCacheArraysBaseLabels(base);
6606 __ BindTrackedLabel(&labels->movw_label);
6607 __ movw(base_reg, 0u);
6608 __ BindTrackedLabel(&labels->movt_label);
6609 __ movt(base_reg, 0u);
6610 __ BindTrackedLabel(&labels->add_pc_label);
6611 __ add(base_reg, base_reg, ShifterOperand(PC));
6612}
6613
Andreas Gampe85b62f22015-09-09 13:15:38 -07006614void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
6615 if (!trg.IsValid()) {
Roland Levillainc9285912015-12-18 10:38:42 +00006616 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006617 return;
6618 }
6619
6620 DCHECK_NE(type, Primitive::kPrimVoid);
6621
6622 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
6623 if (return_loc.Equals(trg)) {
6624 return;
6625 }
6626
6627 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
6628 // with the last branch.
6629 if (type == Primitive::kPrimLong) {
6630 HParallelMove parallel_move(GetGraph()->GetArena());
6631 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
6632 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
6633 GetMoveResolver()->EmitNativeCode(&parallel_move);
6634 } else if (type == Primitive::kPrimDouble) {
6635 HParallelMove parallel_move(GetGraph()->GetArena());
6636 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
6637 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
6638 GetMoveResolver()->EmitNativeCode(&parallel_move);
6639 } else {
6640 // Let the parallel move resolver take care of all of this.
6641 HParallelMove parallel_move(GetGraph()->GetArena());
6642 parallel_move.AddMove(return_loc, trg, type, nullptr);
6643 GetMoveResolver()->EmitNativeCode(&parallel_move);
6644 }
6645}
6646
Roland Levillain4d027112015-07-01 15:41:14 +01006647#undef __
6648#undef QUICK_ENTRY_POINT
6649
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006650} // namespace arm
6651} // namespace art