blob: 45520b45bf34a0836c90581afe47e7925ed6aac4 [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:
Aart Bik42249c32016-01-07 15:33:50 -0800353 explicit DeoptimizationSlowPathARM(HDeoptimize* instruction)
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700354 : instruction_(instruction) {}
355
356 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800357 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700358 __ Bind(GetEntryLabel());
359 SaveLiveRegisters(codegen, instruction_->GetLocations());
Aart Bik42249c32016-01-07 15:33:50 -0800360 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
361 instruction_,
362 instruction_->GetDexPc(),
363 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:
Aart Bik42249c32016-01-07 15:33:50 -0800370 HDeoptimize* const instruction_;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700371 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)
Aart Bik42249c32016-01-07 15:33:50 -0800916 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100917 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::GenerateFPJumps(HCondition* cond,
1418 Label* true_label,
1419 Label* false_label) {
1420 __ vmstat(); // transfer FP status register to ARM APSR.
Aart Bike9f37602015-10-09 11:15:55 -07001421 // TODO: merge into a single branch (except "equal or unordered" and "not equal")
Roland Levillain4fa13f62015-07-06 18:11:54 +01001422 if (cond->IsFPConditionTrueIfNaN()) {
1423 __ b(true_label, VS); // VS for unordered.
1424 } else if (cond->IsFPConditionFalseIfNaN()) {
1425 __ b(false_label, VS); // VS for unordered.
1426 }
Aart Bike9f37602015-10-09 11:15:55 -07001427 __ b(true_label, ARMCondition(cond->GetCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001428}
1429
1430void InstructionCodeGeneratorARM::GenerateLongComparesAndJumps(HCondition* cond,
1431 Label* true_label,
1432 Label* false_label) {
1433 LocationSummary* locations = cond->GetLocations();
1434 Location left = locations->InAt(0);
1435 Location right = locations->InAt(1);
1436 IfCondition if_cond = cond->GetCondition();
1437
1438 Register left_high = left.AsRegisterPairHigh<Register>();
1439 Register left_low = left.AsRegisterPairLow<Register>();
1440 IfCondition true_high_cond = if_cond;
1441 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001442 Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
Roland Levillain4fa13f62015-07-06 18:11:54 +01001443
1444 // Set the conditions for the test, remembering that == needs to be
1445 // decided using the low words.
Aart Bike9f37602015-10-09 11:15:55 -07001446 // TODO: consider avoiding jumps with temporary and CMP low+SBC high
Roland Levillain4fa13f62015-07-06 18:11:54 +01001447 switch (if_cond) {
1448 case kCondEQ:
1449 case kCondNE:
1450 // Nothing to do.
1451 break;
1452 case kCondLT:
1453 false_high_cond = kCondGT;
1454 break;
1455 case kCondLE:
1456 true_high_cond = kCondLT;
1457 break;
1458 case kCondGT:
1459 false_high_cond = kCondLT;
1460 break;
1461 case kCondGE:
1462 true_high_cond = kCondGT;
1463 break;
Aart Bike9f37602015-10-09 11:15:55 -07001464 case kCondB:
1465 false_high_cond = kCondA;
1466 break;
1467 case kCondBE:
1468 true_high_cond = kCondB;
1469 break;
1470 case kCondA:
1471 false_high_cond = kCondB;
1472 break;
1473 case kCondAE:
1474 true_high_cond = kCondA;
1475 break;
Roland Levillain4fa13f62015-07-06 18:11:54 +01001476 }
1477 if (right.IsConstant()) {
1478 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1479 int32_t val_low = Low32Bits(value);
1480 int32_t val_high = High32Bits(value);
1481
Vladimir Markoac6ac102015-12-17 12:14:00 +00001482 __ CmpConstant(left_high, val_high);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001483 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001484 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001485 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001486 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001487 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001488 __ b(true_label, ARMCondition(true_high_cond));
1489 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001490 }
1491 // Must be equal high, so compare the lows.
Vladimir Markoac6ac102015-12-17 12:14:00 +00001492 __ CmpConstant(left_low, val_low);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001493 } else {
1494 Register right_high = right.AsRegisterPairHigh<Register>();
1495 Register right_low = right.AsRegisterPairLow<Register>();
1496
1497 __ cmp(left_high, ShifterOperand(right_high));
1498 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001499 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001500 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001501 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001502 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001503 __ b(true_label, ARMCondition(true_high_cond));
1504 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001505 }
1506 // Must be equal high, so compare the lows.
1507 __ cmp(left_low, ShifterOperand(right_low));
1508 }
1509 // The last comparison might be unsigned.
Aart Bike9f37602015-10-09 11:15:55 -07001510 // TODO: optimize cases where this is always true/false
Roland Levillain4fa13f62015-07-06 18:11:54 +01001511 __ b(true_label, final_condition);
1512}
1513
David Brazdil0debae72015-11-12 18:37:00 +00001514void InstructionCodeGeneratorARM::GenerateCompareTestAndBranch(HCondition* condition,
1515 Label* true_target_in,
1516 Label* false_target_in) {
1517 // Generated branching requires both targets to be explicit. If either of the
1518 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1519 Label fallthrough_target;
1520 Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1521 Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1522
Roland Levillain4fa13f62015-07-06 18:11:54 +01001523 LocationSummary* locations = condition->GetLocations();
1524 Location left = locations->InAt(0);
1525 Location right = locations->InAt(1);
1526
Roland Levillain4fa13f62015-07-06 18:11:54 +01001527 Primitive::Type type = condition->InputAt(0)->GetType();
1528 switch (type) {
1529 case Primitive::kPrimLong:
1530 GenerateLongComparesAndJumps(condition, true_target, false_target);
1531 break;
1532 case Primitive::kPrimFloat:
1533 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
1534 GenerateFPJumps(condition, true_target, false_target);
1535 break;
1536 case Primitive::kPrimDouble:
1537 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
1538 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
1539 GenerateFPJumps(condition, true_target, false_target);
1540 break;
1541 default:
1542 LOG(FATAL) << "Unexpected compare type " << type;
1543 }
1544
David Brazdil0debae72015-11-12 18:37:00 +00001545 if (false_target != &fallthrough_target) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001546 __ b(false_target);
1547 }
David Brazdil0debae72015-11-12 18:37:00 +00001548
1549 if (fallthrough_target.IsLinked()) {
1550 __ Bind(&fallthrough_target);
1551 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001552}
1553
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001554void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001555 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001556 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00001557 Label* false_target) {
1558 HInstruction* cond = instruction->InputAt(condition_input_index);
1559
1560 if (true_target == nullptr && false_target == nullptr) {
1561 // Nothing to do. The code always falls through.
1562 return;
1563 } else if (cond->IsIntConstant()) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001564 // Constant condition, statically compared against 1.
David Brazdil0debae72015-11-12 18:37:00 +00001565 if (cond->AsIntConstant()->IsOne()) {
1566 if (true_target != nullptr) {
1567 __ b(true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001568 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001569 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001570 DCHECK(cond->AsIntConstant()->IsZero());
1571 if (false_target != nullptr) {
1572 __ b(false_target);
1573 }
1574 }
1575 return;
1576 }
1577
1578 // The following code generates these patterns:
1579 // (1) true_target == nullptr && false_target != nullptr
1580 // - opposite condition true => branch to false_target
1581 // (2) true_target != nullptr && false_target == nullptr
1582 // - condition true => branch to true_target
1583 // (3) true_target != nullptr && false_target != nullptr
1584 // - condition true => branch to true_target
1585 // - branch to false_target
1586 if (IsBooleanValueOrMaterializedCondition(cond)) {
1587 // Condition has been materialized, compare the output to 0.
1588 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
1589 DCHECK(cond_val.IsRegister());
1590 if (true_target == nullptr) {
1591 __ CompareAndBranchIfZero(cond_val.AsRegister<Register>(), false_target);
1592 } else {
1593 __ CompareAndBranchIfNonZero(cond_val.AsRegister<Register>(), true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001594 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001595 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001596 // Condition has not been materialized. Use its inputs as the comparison and
1597 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001598 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001599
1600 // If this is a long or FP comparison that has been folded into
1601 // the HCondition, generate the comparison directly.
1602 Primitive::Type type = condition->InputAt(0)->GetType();
1603 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1604 GenerateCompareTestAndBranch(condition, true_target, false_target);
1605 return;
1606 }
1607
1608 LocationSummary* locations = cond->GetLocations();
1609 DCHECK(locations->InAt(0).IsRegister());
1610 Register left = locations->InAt(0).AsRegister<Register>();
1611 Location right = locations->InAt(1);
1612 if (right.IsRegister()) {
1613 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001614 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001615 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00001616 __ CmpConstant(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
David Brazdil0debae72015-11-12 18:37:00 +00001617 }
1618 if (true_target == nullptr) {
1619 __ b(false_target, ARMCondition(condition->GetOppositeCondition()));
1620 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001621 __ b(true_target, ARMCondition(condition->GetCondition()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001622 }
Dave Allison20dfc792014-06-16 20:44:29 -07001623 }
David Brazdil0debae72015-11-12 18:37:00 +00001624
1625 // If neither branch falls through (case 3), the conditional branch to `true_target`
1626 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1627 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001628 __ b(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001629 }
1630}
1631
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001632void LocationsBuilderARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001633 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1634 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001635 locations->SetInAt(0, Location::RequiresRegister());
1636 }
1637}
1638
1639void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001640 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1641 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1642 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1643 nullptr : codegen_->GetLabelOf(true_successor);
1644 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1645 nullptr : codegen_->GetLabelOf(false_successor);
1646 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001647}
1648
1649void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
1650 LocationSummary* locations = new (GetGraph()->GetArena())
1651 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001652 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001653 locations->SetInAt(0, Location::RequiresRegister());
1654 }
1655}
1656
1657void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001658 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00001659 GenerateTestAndBranch(deoptimize,
1660 /* condition_input_index */ 0,
1661 slow_path->GetEntryLabel(),
1662 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001663}
Dave Allison20dfc792014-06-16 20:44:29 -07001664
David Srbecky0cf44932015-12-09 14:09:59 +00001665void LocationsBuilderARM::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1666 new (GetGraph()->GetArena()) LocationSummary(info);
1667}
1668
1669void InstructionCodeGeneratorARM::VisitNativeDebugInfo(HNativeDebugInfo* info) {
David Srbeckyb7070a22016-01-08 18:13:53 +00001670 if (codegen_->HasStackMapAtCurrentPc()) {
1671 // Ensure that we do not collide with the stack map of the previous instruction.
1672 __ nop();
1673 }
David Srbecky0cf44932015-12-09 14:09:59 +00001674 codegen_->RecordPcInfo(info, info->GetDexPc());
1675}
1676
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001677void LocationsBuilderARM::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001678 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001679 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001680 // Handle the long/FP comparisons made in instruction simplification.
1681 switch (cond->InputAt(0)->GetType()) {
1682 case Primitive::kPrimLong:
1683 locations->SetInAt(0, Location::RequiresRegister());
1684 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1685 if (cond->NeedsMaterialization()) {
1686 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1687 }
1688 break;
1689
1690 case Primitive::kPrimFloat:
1691 case Primitive::kPrimDouble:
1692 locations->SetInAt(0, Location::RequiresFpuRegister());
1693 locations->SetInAt(1, Location::RequiresFpuRegister());
1694 if (cond->NeedsMaterialization()) {
1695 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1696 }
1697 break;
1698
1699 default:
1700 locations->SetInAt(0, Location::RequiresRegister());
1701 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1702 if (cond->NeedsMaterialization()) {
1703 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1704 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001705 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001706}
1707
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001708void InstructionCodeGeneratorARM::HandleCondition(HCondition* cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001709 if (!cond->NeedsMaterialization()) {
1710 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001711 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001712
1713 LocationSummary* locations = cond->GetLocations();
1714 Location left = locations->InAt(0);
1715 Location right = locations->InAt(1);
1716 Register out = locations->Out().AsRegister<Register>();
1717 Label true_label, false_label;
1718
1719 switch (cond->InputAt(0)->GetType()) {
1720 default: {
1721 // Integer case.
1722 if (right.IsRegister()) {
1723 __ cmp(left.AsRegister<Register>(), ShifterOperand(right.AsRegister<Register>()));
1724 } else {
1725 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00001726 __ CmpConstant(left.AsRegister<Register>(),
1727 CodeGenerator::GetInt32ValueOf(right.GetConstant()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001728 }
Aart Bike9f37602015-10-09 11:15:55 -07001729 __ it(ARMCondition(cond->GetCondition()), kItElse);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001730 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(1),
Aart Bike9f37602015-10-09 11:15:55 -07001731 ARMCondition(cond->GetCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001732 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(0),
Aart Bike9f37602015-10-09 11:15:55 -07001733 ARMCondition(cond->GetOppositeCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001734 return;
1735 }
1736 case Primitive::kPrimLong:
1737 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1738 break;
1739 case Primitive::kPrimFloat:
1740 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
1741 GenerateFPJumps(cond, &true_label, &false_label);
1742 break;
1743 case Primitive::kPrimDouble:
1744 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
1745 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
1746 GenerateFPJumps(cond, &true_label, &false_label);
1747 break;
1748 }
1749
1750 // Convert the jumps into the result.
1751 Label done_label;
1752
1753 // False case: result = 0.
1754 __ Bind(&false_label);
1755 __ LoadImmediate(out, 0);
1756 __ b(&done_label);
1757
1758 // True case: result = 1.
1759 __ Bind(&true_label);
1760 __ LoadImmediate(out, 1);
1761 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001762}
1763
1764void LocationsBuilderARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001765 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001766}
1767
1768void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001769 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001770}
1771
1772void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001773 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001774}
1775
1776void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001777 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001778}
1779
1780void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001781 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001782}
1783
1784void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001785 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001786}
1787
1788void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001789 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001790}
1791
1792void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001793 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001794}
1795
1796void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001797 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001798}
1799
1800void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001801 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001802}
1803
1804void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001805 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001806}
1807
1808void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001809 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001810}
1811
Aart Bike9f37602015-10-09 11:15:55 -07001812void LocationsBuilderARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001813 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001814}
1815
1816void InstructionCodeGeneratorARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001817 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001818}
1819
1820void LocationsBuilderARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001821 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001822}
1823
1824void InstructionCodeGeneratorARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001825 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001826}
1827
1828void LocationsBuilderARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001829 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001830}
1831
1832void InstructionCodeGeneratorARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001833 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001834}
1835
1836void LocationsBuilderARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001837 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001838}
1839
1840void InstructionCodeGeneratorARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001841 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001842}
1843
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001844void LocationsBuilderARM::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001845 local->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001846}
1847
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001848void InstructionCodeGeneratorARM::VisitLocal(HLocal* local) {
1849 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001850}
1851
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001852void LocationsBuilderARM::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001853 load->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001854}
1855
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001856void InstructionCodeGeneratorARM::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001857 // Nothing to do, this is driven by the code generator.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001858}
1859
1860void LocationsBuilderARM::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001861 LocationSummary* locations =
1862 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001863 switch (store->InputAt(1)->GetType()) {
1864 case Primitive::kPrimBoolean:
1865 case Primitive::kPrimByte:
1866 case Primitive::kPrimChar:
1867 case Primitive::kPrimShort:
1868 case Primitive::kPrimInt:
1869 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001870 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001871 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1872 break;
1873
1874 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001875 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001876 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1877 break;
1878
1879 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001880 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001881 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001882}
1883
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001884void InstructionCodeGeneratorARM::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001885}
1886
1887void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001888 LocationSummary* locations =
1889 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001890 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001891}
1892
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001893void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001894 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001895}
1896
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001897void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
1898 LocationSummary* locations =
1899 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1900 locations->SetOut(Location::ConstantLocation(constant));
1901}
1902
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001903void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001904 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001905}
1906
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001907void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001908 LocationSummary* locations =
1909 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001910 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001911}
1912
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001913void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001914 // Will be generated at use site.
1915}
1916
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001917void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
1918 LocationSummary* locations =
1919 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1920 locations->SetOut(Location::ConstantLocation(constant));
1921}
1922
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001923void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001924 // Will be generated at use site.
1925}
1926
1927void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
1928 LocationSummary* locations =
1929 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1930 locations->SetOut(Location::ConstantLocation(constant));
1931}
1932
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001933void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001934 // Will be generated at use site.
1935}
1936
Calin Juravle27df7582015-04-17 19:12:31 +01001937void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1938 memory_barrier->SetLocations(nullptr);
1939}
1940
1941void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00001942 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001943}
1944
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001945void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001946 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001947}
1948
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001949void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001950 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001951}
1952
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001953void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001954 LocationSummary* locations =
1955 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001956 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001957}
1958
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001959void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001960 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001961}
1962
Calin Juravle175dc732015-08-25 15:42:32 +01001963void LocationsBuilderARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1964 // The trampoline uses the same calling convention as dex calling conventions,
1965 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1966 // the method_idx.
1967 HandleInvoke(invoke);
1968}
1969
1970void InstructionCodeGeneratorARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1971 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1972}
1973
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001974void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001975 // When we do not run baseline, explicit clinit checks triggered by static
1976 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1977 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001978
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001979 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001980 codegen_->GetAssembler(),
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001981 codegen_->GetInstructionSetFeatures());
1982 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markob4536b72015-11-24 13:45:23 +00001983 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
1984 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
1985 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001986 return;
1987 }
1988
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001989 HandleInvoke(invoke);
Vladimir Markob4536b72015-11-24 13:45:23 +00001990
1991 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
1992 if (invoke->HasPcRelativeDexCache()) {
1993 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
1994 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001995}
1996
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001997static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
1998 if (invoke->GetLocations()->Intrinsified()) {
1999 IntrinsicCodeGeneratorARM intrinsic(codegen);
2000 intrinsic.Dispatch(invoke);
2001 return true;
2002 }
2003 return false;
2004}
2005
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002006void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002007 // When we do not run baseline, explicit clinit checks triggered by static
2008 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2009 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002010
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002011 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2012 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002013 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002014
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002015 LocationSummary* locations = invoke->GetLocations();
2016 codegen_->GenerateStaticOrDirectCall(
2017 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002018 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002019}
2020
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002021void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002022 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002023 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002024}
2025
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002026void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002027 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01002028 codegen_->GetAssembler(),
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002029 codegen_->GetInstructionSetFeatures());
2030 if (intrinsic.TryDispatch(invoke)) {
2031 return;
2032 }
2033
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002034 HandleInvoke(invoke);
2035}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002036
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002037void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002038 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2039 return;
2040 }
2041
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002042 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002043 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002044 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002045}
2046
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002047void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
2048 HandleInvoke(invoke);
2049 // Add the hidden argument.
2050 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
2051}
2052
2053void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
2054 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain3b359c72015-11-17 19:35:12 +00002055 LocationSummary* locations = invoke->GetLocations();
2056 Register temp = locations->GetTemp(0).AsRegister<Register>();
2057 Register hidden_reg = locations->GetTemp(1).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002058 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2059 invoke->GetImtIndex() % mirror::Class::kImtSize, kArmPointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002060 Location receiver = locations->InAt(0);
2061 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2062
Roland Levillain3b359c72015-11-17 19:35:12 +00002063 // Set the hidden argument. This is safe to do this here, as R12
2064 // won't be modified thereafter, before the `blx` (call) instruction.
2065 DCHECK_EQ(R12, hidden_reg);
2066 __ LoadImmediate(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002067
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002068 if (receiver.IsStackSlot()) {
2069 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
Roland Levillain3b359c72015-11-17 19:35:12 +00002070 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002071 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
2072 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00002073 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002074 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002075 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002076 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00002077 // Instead of simply (possibly) unpoisoning `temp` here, we should
2078 // emit a read barrier for the previous class reference load.
2079 // However this is not required in practice, as this is an
2080 // intermediate/temporary reference and because the current
2081 // concurrent copying collector keeps the from-space memory
2082 // intact/accessible until the end of the marking phase (the
2083 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002084 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002085 // temp = temp->GetImtEntryAt(method_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00002086 uint32_t entry_point =
2087 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002088 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
2089 // LR = temp->GetEntryPoint();
2090 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
2091 // LR();
2092 __ blx(LR);
2093 DCHECK(!codegen_->IsLeafMethod());
2094 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2095}
2096
Roland Levillain88cb1752014-10-20 16:36:47 +01002097void LocationsBuilderARM::VisitNeg(HNeg* neg) {
2098 LocationSummary* locations =
2099 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2100 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002101 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01002102 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002103 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2104 break;
2105 }
2106 case Primitive::kPrimLong: {
2107 locations->SetInAt(0, Location::RequiresRegister());
2108 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01002109 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002110 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002111
Roland Levillain88cb1752014-10-20 16:36:47 +01002112 case Primitive::kPrimFloat:
2113 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002114 locations->SetInAt(0, Location::RequiresFpuRegister());
2115 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01002116 break;
2117
2118 default:
2119 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2120 }
2121}
2122
2123void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
2124 LocationSummary* locations = neg->GetLocations();
2125 Location out = locations->Out();
2126 Location in = locations->InAt(0);
2127 switch (neg->GetResultType()) {
2128 case Primitive::kPrimInt:
2129 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002130 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01002131 break;
2132
2133 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002134 DCHECK(in.IsRegisterPair());
2135 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
2136 __ rsbs(out.AsRegisterPairLow<Register>(),
2137 in.AsRegisterPairLow<Register>(),
2138 ShifterOperand(0));
2139 // We cannot emit an RSC (Reverse Subtract with Carry)
2140 // instruction here, as it does not exist in the Thumb-2
2141 // instruction set. We use the following approach
2142 // using SBC and SUB instead.
2143 //
2144 // out.hi = -C
2145 __ sbc(out.AsRegisterPairHigh<Register>(),
2146 out.AsRegisterPairHigh<Register>(),
2147 ShifterOperand(out.AsRegisterPairHigh<Register>()));
2148 // out.hi = out.hi - in.hi
2149 __ sub(out.AsRegisterPairHigh<Register>(),
2150 out.AsRegisterPairHigh<Register>(),
2151 ShifterOperand(in.AsRegisterPairHigh<Register>()));
2152 break;
2153
Roland Levillain88cb1752014-10-20 16:36:47 +01002154 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002155 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002156 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002157 break;
2158
Roland Levillain88cb1752014-10-20 16:36:47 +01002159 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002160 DCHECK(in.IsFpuRegisterPair());
2161 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2162 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01002163 break;
2164
2165 default:
2166 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2167 }
2168}
2169
Roland Levillaindff1f282014-11-05 14:15:05 +00002170void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002171 Primitive::Type result_type = conversion->GetResultType();
2172 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002173 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002174
Roland Levillain5b3ee562015-04-14 16:02:41 +01002175 // The float-to-long, double-to-long and long-to-float type conversions
2176 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002177 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01002178 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2179 && result_type == Primitive::kPrimLong)
2180 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Roland Levillain624279f2014-12-04 11:54:28 +00002181 ? LocationSummary::kCall
2182 : LocationSummary::kNoCall;
2183 LocationSummary* locations =
2184 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2185
David Brazdilb2bd1c52015-03-25 11:17:37 +00002186 // The Java language does not allow treating boolean as an integral type but
2187 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002188
Roland Levillaindff1f282014-11-05 14:15:05 +00002189 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002190 case Primitive::kPrimByte:
2191 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002192 case Primitive::kPrimBoolean:
2193 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002194 case Primitive::kPrimShort:
2195 case Primitive::kPrimInt:
2196 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002197 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002198 locations->SetInAt(0, Location::RequiresRegister());
2199 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2200 break;
2201
2202 default:
2203 LOG(FATAL) << "Unexpected type conversion from " << input_type
2204 << " to " << result_type;
2205 }
2206 break;
2207
Roland Levillain01a8d712014-11-14 16:27:39 +00002208 case Primitive::kPrimShort:
2209 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002210 case Primitive::kPrimBoolean:
2211 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002212 case Primitive::kPrimByte:
2213 case Primitive::kPrimInt:
2214 case Primitive::kPrimChar:
2215 // Processing a Dex `int-to-short' instruction.
2216 locations->SetInAt(0, Location::RequiresRegister());
2217 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2218 break;
2219
2220 default:
2221 LOG(FATAL) << "Unexpected type conversion from " << input_type
2222 << " to " << result_type;
2223 }
2224 break;
2225
Roland Levillain946e1432014-11-11 17:35:19 +00002226 case Primitive::kPrimInt:
2227 switch (input_type) {
2228 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002229 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002230 locations->SetInAt(0, Location::Any());
2231 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2232 break;
2233
2234 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002235 // Processing a Dex `float-to-int' instruction.
2236 locations->SetInAt(0, Location::RequiresFpuRegister());
2237 locations->SetOut(Location::RequiresRegister());
2238 locations->AddTemp(Location::RequiresFpuRegister());
2239 break;
2240
Roland Levillain946e1432014-11-11 17:35:19 +00002241 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002242 // Processing a Dex `double-to-int' instruction.
2243 locations->SetInAt(0, Location::RequiresFpuRegister());
2244 locations->SetOut(Location::RequiresRegister());
2245 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002246 break;
2247
2248 default:
2249 LOG(FATAL) << "Unexpected type conversion from " << input_type
2250 << " to " << result_type;
2251 }
2252 break;
2253
Roland Levillaindff1f282014-11-05 14:15:05 +00002254 case Primitive::kPrimLong:
2255 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002256 case Primitive::kPrimBoolean:
2257 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002258 case Primitive::kPrimByte:
2259 case Primitive::kPrimShort:
2260 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002261 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002262 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002263 locations->SetInAt(0, Location::RequiresRegister());
2264 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2265 break;
2266
Roland Levillain624279f2014-12-04 11:54:28 +00002267 case Primitive::kPrimFloat: {
2268 // Processing a Dex `float-to-long' instruction.
2269 InvokeRuntimeCallingConvention calling_convention;
2270 locations->SetInAt(0, Location::FpuRegisterLocation(
2271 calling_convention.GetFpuRegisterAt(0)));
2272 locations->SetOut(Location::RegisterPairLocation(R0, R1));
2273 break;
2274 }
2275
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002276 case Primitive::kPrimDouble: {
2277 // Processing a Dex `double-to-long' instruction.
2278 InvokeRuntimeCallingConvention calling_convention;
2279 locations->SetInAt(0, Location::FpuRegisterPairLocation(
2280 calling_convention.GetFpuRegisterAt(0),
2281 calling_convention.GetFpuRegisterAt(1)));
2282 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00002283 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002284 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002285
2286 default:
2287 LOG(FATAL) << "Unexpected type conversion from " << input_type
2288 << " to " << result_type;
2289 }
2290 break;
2291
Roland Levillain981e4542014-11-14 11:47:14 +00002292 case Primitive::kPrimChar:
2293 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002294 case Primitive::kPrimBoolean:
2295 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002296 case Primitive::kPrimByte:
2297 case Primitive::kPrimShort:
2298 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002299 // Processing a Dex `int-to-char' instruction.
2300 locations->SetInAt(0, Location::RequiresRegister());
2301 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2302 break;
2303
2304 default:
2305 LOG(FATAL) << "Unexpected type conversion from " << input_type
2306 << " to " << result_type;
2307 }
2308 break;
2309
Roland Levillaindff1f282014-11-05 14:15:05 +00002310 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002311 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002312 case Primitive::kPrimBoolean:
2313 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002314 case Primitive::kPrimByte:
2315 case Primitive::kPrimShort:
2316 case Primitive::kPrimInt:
2317 case Primitive::kPrimChar:
2318 // Processing a Dex `int-to-float' instruction.
2319 locations->SetInAt(0, Location::RequiresRegister());
2320 locations->SetOut(Location::RequiresFpuRegister());
2321 break;
2322
Roland Levillain5b3ee562015-04-14 16:02:41 +01002323 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00002324 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002325 InvokeRuntimeCallingConvention calling_convention;
2326 locations->SetInAt(0, Location::RegisterPairLocation(
2327 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2328 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00002329 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01002330 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002331
Roland Levillaincff13742014-11-17 14:32:17 +00002332 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002333 // Processing a Dex `double-to-float' instruction.
2334 locations->SetInAt(0, Location::RequiresFpuRegister());
2335 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002336 break;
2337
2338 default:
2339 LOG(FATAL) << "Unexpected type conversion from " << input_type
2340 << " to " << result_type;
2341 };
2342 break;
2343
Roland Levillaindff1f282014-11-05 14:15:05 +00002344 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002345 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002346 case Primitive::kPrimBoolean:
2347 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002348 case Primitive::kPrimByte:
2349 case Primitive::kPrimShort:
2350 case Primitive::kPrimInt:
2351 case Primitive::kPrimChar:
2352 // Processing a Dex `int-to-double' instruction.
2353 locations->SetInAt(0, Location::RequiresRegister());
2354 locations->SetOut(Location::RequiresFpuRegister());
2355 break;
2356
2357 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002358 // Processing a Dex `long-to-double' instruction.
2359 locations->SetInAt(0, Location::RequiresRegister());
2360 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01002361 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002362 locations->AddTemp(Location::RequiresFpuRegister());
2363 break;
2364
Roland Levillaincff13742014-11-17 14:32:17 +00002365 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002366 // Processing a Dex `float-to-double' instruction.
2367 locations->SetInAt(0, Location::RequiresFpuRegister());
2368 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002369 break;
2370
2371 default:
2372 LOG(FATAL) << "Unexpected type conversion from " << input_type
2373 << " to " << result_type;
2374 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002375 break;
2376
2377 default:
2378 LOG(FATAL) << "Unexpected type conversion from " << input_type
2379 << " to " << result_type;
2380 }
2381}
2382
2383void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
2384 LocationSummary* locations = conversion->GetLocations();
2385 Location out = locations->Out();
2386 Location in = locations->InAt(0);
2387 Primitive::Type result_type = conversion->GetResultType();
2388 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002389 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002390 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002391 case Primitive::kPrimByte:
2392 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002393 case Primitive::kPrimBoolean:
2394 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002395 case Primitive::kPrimShort:
2396 case Primitive::kPrimInt:
2397 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002398 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002399 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002400 break;
2401
2402 default:
2403 LOG(FATAL) << "Unexpected type conversion from " << input_type
2404 << " to " << result_type;
2405 }
2406 break;
2407
Roland Levillain01a8d712014-11-14 16:27:39 +00002408 case Primitive::kPrimShort:
2409 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002410 case Primitive::kPrimBoolean:
2411 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002412 case Primitive::kPrimByte:
2413 case Primitive::kPrimInt:
2414 case Primitive::kPrimChar:
2415 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002416 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00002417 break;
2418
2419 default:
2420 LOG(FATAL) << "Unexpected type conversion from " << input_type
2421 << " to " << result_type;
2422 }
2423 break;
2424
Roland Levillain946e1432014-11-11 17:35:19 +00002425 case Primitive::kPrimInt:
2426 switch (input_type) {
2427 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002428 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002429 DCHECK(out.IsRegister());
2430 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002431 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002432 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002433 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00002434 } else {
2435 DCHECK(in.IsConstant());
2436 DCHECK(in.GetConstant()->IsLongConstant());
2437 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002438 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00002439 }
2440 break;
2441
Roland Levillain3f8f9362014-12-02 17:45:01 +00002442 case Primitive::kPrimFloat: {
2443 // Processing a Dex `float-to-int' instruction.
2444 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
2445 __ vmovs(temp, in.AsFpuRegister<SRegister>());
2446 __ vcvtis(temp, temp);
2447 __ vmovrs(out.AsRegister<Register>(), temp);
2448 break;
2449 }
2450
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002451 case Primitive::kPrimDouble: {
2452 // Processing a Dex `double-to-int' instruction.
2453 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
2454 DRegister temp_d = FromLowSToD(temp_s);
2455 __ vmovd(temp_d, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
2456 __ vcvtid(temp_s, temp_d);
2457 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00002458 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002459 }
Roland Levillain946e1432014-11-11 17:35:19 +00002460
2461 default:
2462 LOG(FATAL) << "Unexpected type conversion from " << input_type
2463 << " to " << result_type;
2464 }
2465 break;
2466
Roland Levillaindff1f282014-11-05 14:15:05 +00002467 case Primitive::kPrimLong:
2468 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002469 case Primitive::kPrimBoolean:
2470 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002471 case Primitive::kPrimByte:
2472 case Primitive::kPrimShort:
2473 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002474 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002475 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002476 DCHECK(out.IsRegisterPair());
2477 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002478 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002479 // Sign extension.
2480 __ Asr(out.AsRegisterPairHigh<Register>(),
2481 out.AsRegisterPairLow<Register>(),
2482 31);
2483 break;
2484
2485 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002486 // Processing a Dex `float-to-long' instruction.
Roland Levillain624279f2014-12-04 11:54:28 +00002487 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2488 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002489 conversion->GetDexPc(),
2490 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002491 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002492 break;
2493
Roland Levillaindff1f282014-11-05 14:15:05 +00002494 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002495 // Processing a Dex `double-to-long' instruction.
2496 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2497 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002498 conversion->GetDexPc(),
2499 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002500 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002501 break;
2502
2503 default:
2504 LOG(FATAL) << "Unexpected type conversion from " << input_type
2505 << " to " << result_type;
2506 }
2507 break;
2508
Roland Levillain981e4542014-11-14 11:47:14 +00002509 case Primitive::kPrimChar:
2510 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002511 case Primitive::kPrimBoolean:
2512 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002513 case Primitive::kPrimByte:
2514 case Primitive::kPrimShort:
2515 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002516 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002517 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00002518 break;
2519
2520 default:
2521 LOG(FATAL) << "Unexpected type conversion from " << input_type
2522 << " to " << result_type;
2523 }
2524 break;
2525
Roland Levillaindff1f282014-11-05 14:15:05 +00002526 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002527 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002528 case Primitive::kPrimBoolean:
2529 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002530 case Primitive::kPrimByte:
2531 case Primitive::kPrimShort:
2532 case Primitive::kPrimInt:
2533 case Primitive::kPrimChar: {
2534 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002535 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
2536 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002537 break;
2538 }
2539
Roland Levillain5b3ee562015-04-14 16:02:41 +01002540 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002541 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002542 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pL2f),
2543 conversion,
2544 conversion->GetDexPc(),
2545 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002546 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
Roland Levillain6d0e4832014-11-27 18:31:21 +00002547 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002548
Roland Levillaincff13742014-11-17 14:32:17 +00002549 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002550 // Processing a Dex `double-to-float' instruction.
2551 __ vcvtsd(out.AsFpuRegister<SRegister>(),
2552 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00002553 break;
2554
2555 default:
2556 LOG(FATAL) << "Unexpected type conversion from " << input_type
2557 << " to " << result_type;
2558 };
2559 break;
2560
Roland Levillaindff1f282014-11-05 14:15:05 +00002561 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002562 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002563 case Primitive::kPrimBoolean:
2564 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002565 case Primitive::kPrimByte:
2566 case Primitive::kPrimShort:
2567 case Primitive::kPrimInt:
2568 case Primitive::kPrimChar: {
2569 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002570 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002571 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2572 out.AsFpuRegisterPairLow<SRegister>());
2573 break;
2574 }
2575
Roland Levillain647b9ed2014-11-27 12:06:00 +00002576 case Primitive::kPrimLong: {
2577 // Processing a Dex `long-to-double' instruction.
2578 Register low = in.AsRegisterPairLow<Register>();
2579 Register high = in.AsRegisterPairHigh<Register>();
2580 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
2581 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002582 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00002583 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002584 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
2585 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002586
Roland Levillain682393c2015-04-14 15:57:52 +01002587 // temp_d = int-to-double(high)
2588 __ vmovsr(temp_s, high);
2589 __ vcvtdi(temp_d, temp_s);
2590 // constant_d = k2Pow32EncodingForDouble
2591 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
2592 // out_d = unsigned-to-double(low)
2593 __ vmovsr(out_s, low);
2594 __ vcvtdu(out_d, out_s);
2595 // out_d += temp_d * constant_d
2596 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002597 break;
2598 }
2599
Roland Levillaincff13742014-11-17 14:32:17 +00002600 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002601 // Processing a Dex `float-to-double' instruction.
2602 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2603 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002604 break;
2605
2606 default:
2607 LOG(FATAL) << "Unexpected type conversion from " << input_type
2608 << " to " << result_type;
2609 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002610 break;
2611
2612 default:
2613 LOG(FATAL) << "Unexpected type conversion from " << input_type
2614 << " to " << result_type;
2615 }
2616}
2617
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002618void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002619 LocationSummary* locations =
2620 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002621 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002622 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002623 locations->SetInAt(0, Location::RequiresRegister());
2624 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002625 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2626 break;
2627 }
2628
2629 case Primitive::kPrimLong: {
2630 locations->SetInAt(0, Location::RequiresRegister());
2631 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002632 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002633 break;
2634 }
2635
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002636 case Primitive::kPrimFloat:
2637 case Primitive::kPrimDouble: {
2638 locations->SetInAt(0, Location::RequiresFpuRegister());
2639 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002640 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002641 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002642 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002643
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002644 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002645 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002646 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002647}
2648
2649void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
2650 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002651 Location out = locations->Out();
2652 Location first = locations->InAt(0);
2653 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002654 switch (add->GetResultType()) {
2655 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002656 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002657 __ add(out.AsRegister<Register>(),
2658 first.AsRegister<Register>(),
2659 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002660 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002661 __ AddConstant(out.AsRegister<Register>(),
2662 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002663 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002664 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002665 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002666
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002667 case Primitive::kPrimLong: {
2668 DCHECK(second.IsRegisterPair());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002669 __ adds(out.AsRegisterPairLow<Register>(),
2670 first.AsRegisterPairLow<Register>(),
2671 ShifterOperand(second.AsRegisterPairLow<Register>()));
2672 __ adc(out.AsRegisterPairHigh<Register>(),
2673 first.AsRegisterPairHigh<Register>(),
2674 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002675 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002676 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002677
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002678 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00002679 __ vadds(out.AsFpuRegister<SRegister>(),
2680 first.AsFpuRegister<SRegister>(),
2681 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002682 break;
2683
2684 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002685 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2686 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2687 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002688 break;
2689
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002690 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002691 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002692 }
2693}
2694
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002695void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002696 LocationSummary* locations =
2697 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002698 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002699 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002700 locations->SetInAt(0, Location::RequiresRegister());
2701 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002702 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2703 break;
2704 }
2705
2706 case Primitive::kPrimLong: {
2707 locations->SetInAt(0, Location::RequiresRegister());
2708 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002709 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002710 break;
2711 }
Calin Juravle11351682014-10-23 15:38:15 +01002712 case Primitive::kPrimFloat:
2713 case Primitive::kPrimDouble: {
2714 locations->SetInAt(0, Location::RequiresFpuRegister());
2715 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002716 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002717 break;
Calin Juravle11351682014-10-23 15:38:15 +01002718 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002719 default:
Calin Juravle11351682014-10-23 15:38:15 +01002720 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002721 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002722}
2723
2724void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
2725 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002726 Location out = locations->Out();
2727 Location first = locations->InAt(0);
2728 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002729 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002730 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002731 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002732 __ sub(out.AsRegister<Register>(),
2733 first.AsRegister<Register>(),
2734 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002735 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002736 __ AddConstant(out.AsRegister<Register>(),
2737 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01002738 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002739 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002740 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002741 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002742
Calin Juravle11351682014-10-23 15:38:15 +01002743 case Primitive::kPrimLong: {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002744 DCHECK(second.IsRegisterPair());
Calin Juravle11351682014-10-23 15:38:15 +01002745 __ subs(out.AsRegisterPairLow<Register>(),
2746 first.AsRegisterPairLow<Register>(),
2747 ShifterOperand(second.AsRegisterPairLow<Register>()));
2748 __ sbc(out.AsRegisterPairHigh<Register>(),
2749 first.AsRegisterPairHigh<Register>(),
2750 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002751 break;
Calin Juravle11351682014-10-23 15:38:15 +01002752 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002753
Calin Juravle11351682014-10-23 15:38:15 +01002754 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002755 __ vsubs(out.AsFpuRegister<SRegister>(),
2756 first.AsFpuRegister<SRegister>(),
2757 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002758 break;
Calin Juravle11351682014-10-23 15:38:15 +01002759 }
2760
2761 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002762 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2763 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2764 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01002765 break;
2766 }
2767
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002768
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002769 default:
Calin Juravle11351682014-10-23 15:38:15 +01002770 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002771 }
2772}
2773
Calin Juravle34bacdf2014-10-07 20:23:36 +01002774void LocationsBuilderARM::VisitMul(HMul* mul) {
2775 LocationSummary* locations =
2776 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2777 switch (mul->GetResultType()) {
2778 case Primitive::kPrimInt:
2779 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002780 locations->SetInAt(0, Location::RequiresRegister());
2781 locations->SetInAt(1, Location::RequiresRegister());
2782 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002783 break;
2784 }
2785
Calin Juravleb5bfa962014-10-21 18:02:24 +01002786 case Primitive::kPrimFloat:
2787 case Primitive::kPrimDouble: {
2788 locations->SetInAt(0, Location::RequiresFpuRegister());
2789 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002790 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002791 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002792 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002793
2794 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002795 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002796 }
2797}
2798
2799void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
2800 LocationSummary* locations = mul->GetLocations();
2801 Location out = locations->Out();
2802 Location first = locations->InAt(0);
2803 Location second = locations->InAt(1);
2804 switch (mul->GetResultType()) {
2805 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00002806 __ mul(out.AsRegister<Register>(),
2807 first.AsRegister<Register>(),
2808 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002809 break;
2810 }
2811 case Primitive::kPrimLong: {
2812 Register out_hi = out.AsRegisterPairHigh<Register>();
2813 Register out_lo = out.AsRegisterPairLow<Register>();
2814 Register in1_hi = first.AsRegisterPairHigh<Register>();
2815 Register in1_lo = first.AsRegisterPairLow<Register>();
2816 Register in2_hi = second.AsRegisterPairHigh<Register>();
2817 Register in2_lo = second.AsRegisterPairLow<Register>();
2818
2819 // Extra checks to protect caused by the existence of R1_R2.
2820 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
2821 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
2822 DCHECK_NE(out_hi, in1_lo);
2823 DCHECK_NE(out_hi, in2_lo);
2824
2825 // input: in1 - 64 bits, in2 - 64 bits
2826 // output: out
2827 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2828 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2829 // parts: out.lo = (in1.lo * in2.lo)[31:0]
2830
2831 // IP <- in1.lo * in2.hi
2832 __ mul(IP, in1_lo, in2_hi);
2833 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2834 __ mla(out_hi, in1_hi, in2_lo, IP);
2835 // out.lo <- (in1.lo * in2.lo)[31:0];
2836 __ umull(out_lo, IP, in1_lo, in2_lo);
2837 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2838 __ add(out_hi, out_hi, ShifterOperand(IP));
2839 break;
2840 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002841
2842 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002843 __ vmuls(out.AsFpuRegister<SRegister>(),
2844 first.AsFpuRegister<SRegister>(),
2845 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002846 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002847 }
2848
2849 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002850 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2851 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2852 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01002853 break;
2854 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002855
2856 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002857 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002858 }
2859}
2860
Zheng Xuc6667102015-05-15 16:08:45 +08002861void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2862 DCHECK(instruction->IsDiv() || instruction->IsRem());
2863 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2864
2865 LocationSummary* locations = instruction->GetLocations();
2866 Location second = locations->InAt(1);
2867 DCHECK(second.IsConstant());
2868
2869 Register out = locations->Out().AsRegister<Register>();
2870 Register dividend = locations->InAt(0).AsRegister<Register>();
2871 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2872 DCHECK(imm == 1 || imm == -1);
2873
2874 if (instruction->IsRem()) {
2875 __ LoadImmediate(out, 0);
2876 } else {
2877 if (imm == 1) {
2878 __ Mov(out, dividend);
2879 } else {
2880 __ rsb(out, dividend, ShifterOperand(0));
2881 }
2882 }
2883}
2884
2885void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2886 DCHECK(instruction->IsDiv() || instruction->IsRem());
2887 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2888
2889 LocationSummary* locations = instruction->GetLocations();
2890 Location second = locations->InAt(1);
2891 DCHECK(second.IsConstant());
2892
2893 Register out = locations->Out().AsRegister<Register>();
2894 Register dividend = locations->InAt(0).AsRegister<Register>();
2895 Register temp = locations->GetTemp(0).AsRegister<Register>();
2896 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002897 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002898 int ctz_imm = CTZ(abs_imm);
2899
2900 if (ctz_imm == 1) {
2901 __ Lsr(temp, dividend, 32 - ctz_imm);
2902 } else {
2903 __ Asr(temp, dividend, 31);
2904 __ Lsr(temp, temp, 32 - ctz_imm);
2905 }
2906 __ add(out, temp, ShifterOperand(dividend));
2907
2908 if (instruction->IsDiv()) {
2909 __ Asr(out, out, ctz_imm);
2910 if (imm < 0) {
2911 __ rsb(out, out, ShifterOperand(0));
2912 }
2913 } else {
2914 __ ubfx(out, out, 0, ctz_imm);
2915 __ sub(out, out, ShifterOperand(temp));
2916 }
2917}
2918
2919void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2920 DCHECK(instruction->IsDiv() || instruction->IsRem());
2921 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2922
2923 LocationSummary* locations = instruction->GetLocations();
2924 Location second = locations->InAt(1);
2925 DCHECK(second.IsConstant());
2926
2927 Register out = locations->Out().AsRegister<Register>();
2928 Register dividend = locations->InAt(0).AsRegister<Register>();
2929 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2930 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
2931 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2932
2933 int64_t magic;
2934 int shift;
2935 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2936
2937 __ LoadImmediate(temp1, magic);
2938 __ smull(temp2, temp1, dividend, temp1);
2939
2940 if (imm > 0 && magic < 0) {
2941 __ add(temp1, temp1, ShifterOperand(dividend));
2942 } else if (imm < 0 && magic > 0) {
2943 __ sub(temp1, temp1, ShifterOperand(dividend));
2944 }
2945
2946 if (shift != 0) {
2947 __ Asr(temp1, temp1, shift);
2948 }
2949
2950 if (instruction->IsDiv()) {
2951 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
2952 } else {
2953 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
2954 // TODO: Strength reduction for mls.
2955 __ LoadImmediate(temp2, imm);
2956 __ mls(out, temp1, temp2, dividend);
2957 }
2958}
2959
2960void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
2961 DCHECK(instruction->IsDiv() || instruction->IsRem());
2962 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2963
2964 LocationSummary* locations = instruction->GetLocations();
2965 Location second = locations->InAt(1);
2966 DCHECK(second.IsConstant());
2967
2968 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2969 if (imm == 0) {
2970 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2971 } else if (imm == 1 || imm == -1) {
2972 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002973 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002974 DivRemByPowerOfTwo(instruction);
2975 } else {
2976 DCHECK(imm <= -2 || imm >= 2);
2977 GenerateDivRemWithAnyConstant(instruction);
2978 }
2979}
2980
Calin Juravle7c4954d2014-10-28 16:57:40 +00002981void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002982 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2983 if (div->GetResultType() == Primitive::kPrimLong) {
2984 // pLdiv runtime call.
2985 call_kind = LocationSummary::kCall;
Zheng Xuc6667102015-05-15 16:08:45 +08002986 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
2987 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002988 } else if (div->GetResultType() == Primitive::kPrimInt &&
2989 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
2990 // pIdivmod runtime call.
2991 call_kind = LocationSummary::kCall;
2992 }
2993
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002994 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2995
Calin Juravle7c4954d2014-10-28 16:57:40 +00002996 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002997 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002998 if (div->InputAt(1)->IsConstant()) {
2999 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003000 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08003001 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003002 int32_t value = div->InputAt(1)->AsIntConstant()->GetValue();
3003 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08003004 // No temp register required.
3005 } else {
3006 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003007 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003008 locations->AddTemp(Location::RequiresRegister());
3009 }
3010 }
3011 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003012 locations->SetInAt(0, Location::RequiresRegister());
3013 locations->SetInAt(1, Location::RequiresRegister());
3014 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3015 } else {
3016 InvokeRuntimeCallingConvention calling_convention;
3017 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3018 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3019 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
3020 // we only need the former.
3021 locations->SetOut(Location::RegisterLocation(R0));
3022 }
Calin Juravled0d48522014-11-04 16:40:20 +00003023 break;
3024 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003025 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003026 InvokeRuntimeCallingConvention calling_convention;
3027 locations->SetInAt(0, Location::RegisterPairLocation(
3028 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3029 locations->SetInAt(1, Location::RegisterPairLocation(
3030 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003031 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003032 break;
3033 }
3034 case Primitive::kPrimFloat:
3035 case Primitive::kPrimDouble: {
3036 locations->SetInAt(0, Location::RequiresFpuRegister());
3037 locations->SetInAt(1, Location::RequiresFpuRegister());
3038 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3039 break;
3040 }
3041
3042 default:
3043 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3044 }
3045}
3046
3047void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
3048 LocationSummary* locations = div->GetLocations();
3049 Location out = locations->Out();
3050 Location first = locations->InAt(0);
3051 Location second = locations->InAt(1);
3052
3053 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003054 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003055 if (second.IsConstant()) {
3056 GenerateDivRemConstantIntegral(div);
3057 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003058 __ sdiv(out.AsRegister<Register>(),
3059 first.AsRegister<Register>(),
3060 second.AsRegister<Register>());
3061 } else {
3062 InvokeRuntimeCallingConvention calling_convention;
3063 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3064 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3065 DCHECK_EQ(R0, out.AsRegister<Register>());
3066
3067 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), div, div->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003068 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003069 }
Calin Juravled0d48522014-11-04 16:40:20 +00003070 break;
3071 }
3072
Calin Juravle7c4954d2014-10-28 16:57:40 +00003073 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003074 InvokeRuntimeCallingConvention calling_convention;
3075 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3076 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3077 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3078 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3079 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003080 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003081
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003082 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv), div, div->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003083 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravle7c4954d2014-10-28 16:57:40 +00003084 break;
3085 }
3086
3087 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003088 __ vdivs(out.AsFpuRegister<SRegister>(),
3089 first.AsFpuRegister<SRegister>(),
3090 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003091 break;
3092 }
3093
3094 case Primitive::kPrimDouble: {
3095 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3096 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3097 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
3098 break;
3099 }
3100
3101 default:
3102 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3103 }
3104}
3105
Calin Juravlebacfec32014-11-14 15:54:36 +00003106void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003107 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003108
3109 // Most remainders are implemented in the runtime.
3110 LocationSummary::CallKind call_kind = LocationSummary::kCall;
Zheng Xuc6667102015-05-15 16:08:45 +08003111 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
3112 // sdiv will be replaced by other instruction sequence.
3113 call_kind = LocationSummary::kNoCall;
3114 } else if ((rem->GetResultType() == Primitive::kPrimInt)
3115 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003116 // Have hardware divide instruction for int, do it with three instructions.
3117 call_kind = LocationSummary::kNoCall;
3118 }
3119
Calin Juravlebacfec32014-11-14 15:54:36 +00003120 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3121
Calin Juravled2ec87d2014-12-08 14:24:46 +00003122 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003123 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003124 if (rem->InputAt(1)->IsConstant()) {
3125 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003126 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08003127 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003128 int32_t value = rem->InputAt(1)->AsIntConstant()->GetValue();
3129 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08003130 // No temp register required.
3131 } else {
3132 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003133 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003134 locations->AddTemp(Location::RequiresRegister());
3135 }
3136 }
3137 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003138 locations->SetInAt(0, Location::RequiresRegister());
3139 locations->SetInAt(1, Location::RequiresRegister());
3140 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3141 locations->AddTemp(Location::RequiresRegister());
3142 } else {
3143 InvokeRuntimeCallingConvention calling_convention;
3144 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3145 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3146 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
3147 // we only need the latter.
3148 locations->SetOut(Location::RegisterLocation(R1));
3149 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003150 break;
3151 }
3152 case Primitive::kPrimLong: {
3153 InvokeRuntimeCallingConvention calling_convention;
3154 locations->SetInAt(0, Location::RegisterPairLocation(
3155 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3156 locations->SetInAt(1, Location::RegisterPairLocation(
3157 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3158 // The runtime helper puts the output in R2,R3.
3159 locations->SetOut(Location::RegisterPairLocation(R2, R3));
3160 break;
3161 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00003162 case Primitive::kPrimFloat: {
3163 InvokeRuntimeCallingConvention calling_convention;
3164 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3165 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3166 locations->SetOut(Location::FpuRegisterLocation(S0));
3167 break;
3168 }
3169
Calin Juravlebacfec32014-11-14 15:54:36 +00003170 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003171 InvokeRuntimeCallingConvention calling_convention;
3172 locations->SetInAt(0, Location::FpuRegisterPairLocation(
3173 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
3174 locations->SetInAt(1, Location::FpuRegisterPairLocation(
3175 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
3176 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00003177 break;
3178 }
3179
3180 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003181 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003182 }
3183}
3184
3185void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
3186 LocationSummary* locations = rem->GetLocations();
3187 Location out = locations->Out();
3188 Location first = locations->InAt(0);
3189 Location second = locations->InAt(1);
3190
Calin Juravled2ec87d2014-12-08 14:24:46 +00003191 Primitive::Type type = rem->GetResultType();
3192 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003193 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003194 if (second.IsConstant()) {
3195 GenerateDivRemConstantIntegral(rem);
3196 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003197 Register reg1 = first.AsRegister<Register>();
3198 Register reg2 = second.AsRegister<Register>();
3199 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003200
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003201 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003202 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003203 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003204 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003205 } else {
3206 InvokeRuntimeCallingConvention calling_convention;
3207 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3208 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3209 DCHECK_EQ(R1, out.AsRegister<Register>());
3210
3211 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003212 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003213 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003214 break;
3215 }
3216
3217 case Primitive::kPrimLong: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003218 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003219 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003220 break;
3221 }
3222
Calin Juravled2ec87d2014-12-08 14:24:46 +00003223 case Primitive::kPrimFloat: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003224 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003225 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Calin Juravled2ec87d2014-12-08 14:24:46 +00003226 break;
3227 }
3228
Calin Juravlebacfec32014-11-14 15:54:36 +00003229 case Primitive::kPrimDouble: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003230 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003231 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003232 break;
3233 }
3234
3235 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003236 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003237 }
3238}
3239
Calin Juravled0d48522014-11-04 16:40:20 +00003240void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003241 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3242 ? LocationSummary::kCallOnSlowPath
3243 : LocationSummary::kNoCall;
3244 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003245 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00003246 if (instruction->HasUses()) {
3247 locations->SetOut(Location::SameAsFirstInput());
3248 }
3249}
3250
3251void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003252 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003253 codegen_->AddSlowPath(slow_path);
3254
3255 LocationSummary* locations = instruction->GetLocations();
3256 Location value = locations->InAt(0);
3257
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003258 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003259 case Primitive::kPrimByte:
3260 case Primitive::kPrimChar:
3261 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003262 case Primitive::kPrimInt: {
3263 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003264 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003265 } else {
3266 DCHECK(value.IsConstant()) << value;
3267 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3268 __ b(slow_path->GetEntryLabel());
3269 }
3270 }
3271 break;
3272 }
3273 case Primitive::kPrimLong: {
3274 if (value.IsRegisterPair()) {
3275 __ orrs(IP,
3276 value.AsRegisterPairLow<Register>(),
3277 ShifterOperand(value.AsRegisterPairHigh<Register>()));
3278 __ b(slow_path->GetEntryLabel(), EQ);
3279 } else {
3280 DCHECK(value.IsConstant()) << value;
3281 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3282 __ b(slow_path->GetEntryLabel());
3283 }
3284 }
3285 break;
3286 default:
3287 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3288 }
3289 }
Calin Juravled0d48522014-11-04 16:40:20 +00003290}
3291
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003292void InstructionCodeGeneratorARM::HandleIntegerRotate(LocationSummary* locations) {
3293 Register in = locations->InAt(0).AsRegister<Register>();
3294 Location rhs = locations->InAt(1);
3295 Register out = locations->Out().AsRegister<Register>();
3296
3297 if (rhs.IsConstant()) {
3298 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
3299 // so map all rotations to a +ve. equivalent in that range.
3300 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
3301 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
3302 if (rot) {
3303 // Rotate, mapping left rotations to right equivalents if necessary.
3304 // (e.g. left by 2 bits == right by 30.)
3305 __ Ror(out, in, rot);
3306 } else if (out != in) {
3307 __ Mov(out, in);
3308 }
3309 } else {
3310 __ Ror(out, in, rhs.AsRegister<Register>());
3311 }
3312}
3313
3314// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
3315// rotates by swapping input regs (effectively rotating by the first 32-bits of
3316// a larger rotation) or flipping direction (thus treating larger right/left
3317// rotations as sub-word sized rotations in the other direction) as appropriate.
3318void InstructionCodeGeneratorARM::HandleLongRotate(LocationSummary* locations) {
3319 Register in_reg_lo = locations->InAt(0).AsRegisterPairLow<Register>();
3320 Register in_reg_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
3321 Location rhs = locations->InAt(1);
3322 Register out_reg_lo = locations->Out().AsRegisterPairLow<Register>();
3323 Register out_reg_hi = locations->Out().AsRegisterPairHigh<Register>();
3324
3325 if (rhs.IsConstant()) {
3326 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
3327 // Map all rotations to +ve. equivalents on the interval [0,63].
3328 rot &= kMaxLongShiftValue;
3329 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
3330 // logic below to a simple pair of binary orr.
3331 // (e.g. 34 bits == in_reg swap + 2 bits right.)
3332 if (rot >= kArmBitsPerWord) {
3333 rot -= kArmBitsPerWord;
3334 std::swap(in_reg_hi, in_reg_lo);
3335 }
3336 // Rotate, or mov to out for zero or word size rotations.
3337 if (rot != 0u) {
3338 __ Lsr(out_reg_hi, in_reg_hi, rot);
3339 __ orr(out_reg_hi, out_reg_hi, ShifterOperand(in_reg_lo, arm::LSL, kArmBitsPerWord - rot));
3340 __ Lsr(out_reg_lo, in_reg_lo, rot);
3341 __ orr(out_reg_lo, out_reg_lo, ShifterOperand(in_reg_hi, arm::LSL, kArmBitsPerWord - rot));
3342 } else {
3343 __ Mov(out_reg_lo, in_reg_lo);
3344 __ Mov(out_reg_hi, in_reg_hi);
3345 }
3346 } else {
3347 Register shift_right = locations->GetTemp(0).AsRegister<Register>();
3348 Register shift_left = locations->GetTemp(1).AsRegister<Register>();
3349 Label end;
3350 Label shift_by_32_plus_shift_right;
3351
3352 __ and_(shift_right, rhs.AsRegister<Register>(), ShifterOperand(0x1F));
3353 __ Lsrs(shift_left, rhs.AsRegister<Register>(), 6);
3354 __ rsb(shift_left, shift_right, ShifterOperand(kArmBitsPerWord), AL, kCcKeep);
3355 __ b(&shift_by_32_plus_shift_right, CC);
3356
3357 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
3358 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
3359 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
3360 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3361 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3362 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3363 __ Lsr(shift_left, in_reg_hi, shift_right);
3364 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_left));
3365 __ b(&end);
3366
3367 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
3368 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
3369 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
3370 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
3371 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3372 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3373 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3374 __ Lsl(shift_right, in_reg_hi, shift_left);
3375 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_right));
3376
3377 __ Bind(&end);
3378 }
3379}
3380void LocationsBuilderARM::HandleRotate(HRor* ror) {
3381 LocationSummary* locations =
3382 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3383 switch (ror->GetResultType()) {
3384 case Primitive::kPrimInt: {
3385 locations->SetInAt(0, Location::RequiresRegister());
3386 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
3387 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3388 break;
3389 }
3390 case Primitive::kPrimLong: {
3391 locations->SetInAt(0, Location::RequiresRegister());
3392 if (ror->InputAt(1)->IsConstant()) {
3393 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
3394 } else {
3395 locations->SetInAt(1, Location::RequiresRegister());
3396 locations->AddTemp(Location::RequiresRegister());
3397 locations->AddTemp(Location::RequiresRegister());
3398 }
3399 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3400 break;
3401 }
3402 default:
3403 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3404 }
3405}
3406
3407void InstructionCodeGeneratorARM::HandleRotate(HRor* ror) {
3408 LocationSummary* locations = ror->GetLocations();
3409 Primitive::Type type = ror->GetResultType();
3410 switch (type) {
3411 case Primitive::kPrimInt: {
3412 HandleIntegerRotate(locations);
3413 break;
3414 }
3415 case Primitive::kPrimLong: {
3416 HandleLongRotate(locations);
3417 break;
3418 }
3419 default:
3420 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00003421 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003422 }
3423}
3424
3425void LocationsBuilderARM::VisitRor(HRor* op) {
Vladimir Marko351dddf2015-12-11 16:34:46 +00003426 HandleRotate(op);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003427}
3428
3429void InstructionCodeGeneratorARM::VisitRor(HRor* op) {
Vladimir Marko351dddf2015-12-11 16:34:46 +00003430 HandleRotate(op);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003431}
3432
Calin Juravle9aec02f2014-11-18 23:06:35 +00003433void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
3434 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3435
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003436 LocationSummary* locations =
3437 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003438
3439 switch (op->GetResultType()) {
3440 case Primitive::kPrimInt: {
3441 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003442 if (op->InputAt(1)->IsConstant()) {
3443 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3444 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3445 } else {
3446 locations->SetInAt(1, Location::RequiresRegister());
3447 // Make the output overlap, as it will be used to hold the masked
3448 // second input.
3449 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3450 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003451 break;
3452 }
3453 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003454 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003455 if (op->InputAt(1)->IsConstant()) {
3456 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3457 // For simplicity, use kOutputOverlap even though we only require that low registers
3458 // don't clash with high registers which the register allocator currently guarantees.
3459 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3460 } else {
3461 locations->SetInAt(1, Location::RequiresRegister());
3462 locations->AddTemp(Location::RequiresRegister());
3463 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3464 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003465 break;
3466 }
3467 default:
3468 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3469 }
3470}
3471
3472void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
3473 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3474
3475 LocationSummary* locations = op->GetLocations();
3476 Location out = locations->Out();
3477 Location first = locations->InAt(0);
3478 Location second = locations->InAt(1);
3479
3480 Primitive::Type type = op->GetResultType();
3481 switch (type) {
3482 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003483 Register out_reg = out.AsRegister<Register>();
3484 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003485 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003486 Register second_reg = second.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +00003487 // ARM doesn't mask the shift count so we need to do it ourselves.
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003488 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftValue));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003489 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003490 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003491 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003492 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003493 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003494 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003495 }
3496 } else {
3497 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
3498 uint32_t shift_value = static_cast<uint32_t>(cst & kMaxIntShiftValue);
Roland Levillainc9285912015-12-18 10:38:42 +00003499 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
Calin Juravle9aec02f2014-11-18 23:06:35 +00003500 __ Mov(out_reg, first_reg);
3501 } else if (op->IsShl()) {
3502 __ Lsl(out_reg, first_reg, shift_value);
3503 } else if (op->IsShr()) {
3504 __ Asr(out_reg, first_reg, shift_value);
3505 } else {
3506 __ Lsr(out_reg, first_reg, shift_value);
3507 }
3508 }
3509 break;
3510 }
3511 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003512 Register o_h = out.AsRegisterPairHigh<Register>();
3513 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003514
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003515 Register high = first.AsRegisterPairHigh<Register>();
3516 Register low = first.AsRegisterPairLow<Register>();
3517
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003518 if (second.IsRegister()) {
3519 Register temp = locations->GetTemp(0).AsRegister<Register>();
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003520
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003521 Register second_reg = second.AsRegister<Register>();
3522
3523 if (op->IsShl()) {
3524 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftValue));
3525 // Shift the high part
3526 __ Lsl(o_h, high, o_l);
3527 // Shift the low part and `or` what overflew on the high part
3528 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
3529 __ Lsr(temp, low, temp);
3530 __ orr(o_h, o_h, ShifterOperand(temp));
3531 // If the shift is > 32 bits, override the high part
3532 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
3533 __ it(PL);
3534 __ Lsl(o_h, low, temp, PL);
3535 // Shift the low part
3536 __ Lsl(o_l, low, o_l);
3537 } else if (op->IsShr()) {
3538 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftValue));
3539 // Shift the low part
3540 __ Lsr(o_l, low, o_h);
3541 // Shift the high part and `or` what underflew on the low part
3542 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3543 __ Lsl(temp, high, temp);
3544 __ orr(o_l, o_l, ShifterOperand(temp));
3545 // If the shift is > 32 bits, override the low part
3546 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3547 __ it(PL);
3548 __ Asr(o_l, high, temp, PL);
3549 // Shift the high part
3550 __ Asr(o_h, high, o_h);
3551 } else {
3552 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftValue));
3553 // same as Shr except we use `Lsr`s and not `Asr`s
3554 __ Lsr(o_l, low, o_h);
3555 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3556 __ Lsl(temp, high, temp);
3557 __ orr(o_l, o_l, ShifterOperand(temp));
3558 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3559 __ it(PL);
3560 __ Lsr(o_l, high, temp, PL);
3561 __ Lsr(o_h, high, o_h);
3562 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003563 } else {
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003564 // Register allocator doesn't create partial overlap.
3565 DCHECK_NE(o_l, high);
3566 DCHECK_NE(o_h, low);
3567 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
3568 uint32_t shift_value = static_cast<uint32_t>(cst & kMaxLongShiftValue);
3569 if (shift_value > 32) {
3570 if (op->IsShl()) {
3571 __ Lsl(o_h, low, shift_value - 32);
3572 __ LoadImmediate(o_l, 0);
3573 } else if (op->IsShr()) {
3574 __ Asr(o_l, high, shift_value - 32);
3575 __ Asr(o_h, high, 31);
3576 } else {
3577 __ Lsr(o_l, high, shift_value - 32);
3578 __ LoadImmediate(o_h, 0);
3579 }
3580 } else if (shift_value == 32) {
3581 if (op->IsShl()) {
3582 __ mov(o_h, ShifterOperand(low));
3583 __ LoadImmediate(o_l, 0);
3584 } else if (op->IsShr()) {
3585 __ mov(o_l, ShifterOperand(high));
3586 __ Asr(o_h, high, 31);
3587 } else {
3588 __ mov(o_l, ShifterOperand(high));
3589 __ LoadImmediate(o_h, 0);
3590 }
Vladimir Markof9d741e2015-11-20 15:08:11 +00003591 } else if (shift_value == 1) {
3592 if (op->IsShl()) {
3593 __ Lsls(o_l, low, 1);
3594 __ adc(o_h, high, ShifterOperand(high));
3595 } else if (op->IsShr()) {
3596 __ Asrs(o_h, high, 1);
3597 __ Rrx(o_l, low);
3598 } else {
3599 __ Lsrs(o_h, high, 1);
3600 __ Rrx(o_l, low);
3601 }
3602 } else {
3603 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003604 if (op->IsShl()) {
3605 __ Lsl(o_h, high, shift_value);
3606 __ orr(o_h, o_h, ShifterOperand(low, LSR, 32 - shift_value));
3607 __ Lsl(o_l, low, shift_value);
3608 } else if (op->IsShr()) {
3609 __ Lsr(o_l, low, shift_value);
3610 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3611 __ Asr(o_h, high, shift_value);
3612 } else {
3613 __ Lsr(o_l, low, shift_value);
3614 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3615 __ Lsr(o_h, high, shift_value);
3616 }
3617 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003618 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003619 break;
3620 }
3621 default:
3622 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003623 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003624 }
3625}
3626
3627void LocationsBuilderARM::VisitShl(HShl* shl) {
3628 HandleShift(shl);
3629}
3630
3631void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
3632 HandleShift(shl);
3633}
3634
3635void LocationsBuilderARM::VisitShr(HShr* shr) {
3636 HandleShift(shr);
3637}
3638
3639void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
3640 HandleShift(shr);
3641}
3642
3643void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
3644 HandleShift(ushr);
3645}
3646
3647void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
3648 HandleShift(ushr);
3649}
3650
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003651void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003652 LocationSummary* locations =
3653 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003654 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003655 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3656 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003657 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003658}
3659
3660void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003661 // Note: if heap poisoning is enabled, the entry point takes cares
3662 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003663 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003664 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003665 instruction->GetDexPc(),
3666 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003667 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003668}
3669
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003670void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
3671 LocationSummary* locations =
3672 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3673 InvokeRuntimeCallingConvention calling_convention;
3674 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003675 locations->SetOut(Location::RegisterLocation(R0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003676 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003677 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003678}
3679
3680void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
3681 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003682 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003683 // Note: if heap poisoning is enabled, the entry point takes cares
3684 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003685 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003686 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003687 instruction->GetDexPc(),
3688 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003689 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003690}
3691
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003692void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003693 LocationSummary* locations =
3694 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003695 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3696 if (location.IsStackSlot()) {
3697 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3698 } else if (location.IsDoubleStackSlot()) {
3699 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003700 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003701 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003702}
3703
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003704void InstructionCodeGeneratorARM::VisitParameterValue(
3705 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003706 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003707}
3708
3709void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
3710 LocationSummary* locations =
3711 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3712 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3713}
3714
3715void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3716 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003717}
3718
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003719void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003720 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003721 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003722 locations->SetInAt(0, Location::RequiresRegister());
3723 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003724}
3725
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003726void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
3727 LocationSummary* locations = not_->GetLocations();
3728 Location out = locations->Out();
3729 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003730 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003731 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003732 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003733 break;
3734
3735 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003736 __ mvn(out.AsRegisterPairLow<Register>(),
3737 ShifterOperand(in.AsRegisterPairLow<Register>()));
3738 __ mvn(out.AsRegisterPairHigh<Register>(),
3739 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003740 break;
3741
3742 default:
3743 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3744 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003745}
3746
David Brazdil66d126e2015-04-03 16:02:44 +01003747void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
3748 LocationSummary* locations =
3749 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3750 locations->SetInAt(0, Location::RequiresRegister());
3751 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3752}
3753
3754void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003755 LocationSummary* locations = bool_not->GetLocations();
3756 Location out = locations->Out();
3757 Location in = locations->InAt(0);
3758 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
3759}
3760
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003761void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003762 LocationSummary* locations =
3763 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003764 switch (compare->InputAt(0)->GetType()) {
3765 case Primitive::kPrimLong: {
3766 locations->SetInAt(0, Location::RequiresRegister());
3767 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003768 // Output overlaps because it is written before doing the low comparison.
3769 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00003770 break;
3771 }
3772 case Primitive::kPrimFloat:
3773 case Primitive::kPrimDouble: {
3774 locations->SetInAt(0, Location::RequiresFpuRegister());
3775 locations->SetInAt(1, Location::RequiresFpuRegister());
3776 locations->SetOut(Location::RequiresRegister());
3777 break;
3778 }
3779 default:
3780 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3781 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003782}
3783
3784void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003785 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003786 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003787 Location left = locations->InAt(0);
3788 Location right = locations->InAt(1);
3789
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003790 Label less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00003791 Primitive::Type type = compare->InputAt(0)->GetType();
3792 switch (type) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003793 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003794 __ cmp(left.AsRegisterPairHigh<Register>(),
3795 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003796 __ b(&less, LT);
3797 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003798 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00003799 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003800 __ cmp(left.AsRegisterPairLow<Register>(),
3801 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Calin Juravleddb7df22014-11-25 20:56:51 +00003802 break;
3803 }
3804 case Primitive::kPrimFloat:
3805 case Primitive::kPrimDouble: {
3806 __ LoadImmediate(out, 0);
3807 if (type == Primitive::kPrimFloat) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003808 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003809 } else {
3810 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
3811 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
3812 }
3813 __ vmstat(); // transfer FP status register to ARM APSR.
3814 __ b(compare->IsGtBias() ? &greater : &less, VS); // VS for unordered.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003815 break;
3816 }
3817 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003818 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003819 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003820 __ b(&done, EQ);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003821 __ b(&less, LO); // LO is for both: unsigned compare for longs and 'less than' for floats.
Calin Juravleddb7df22014-11-25 20:56:51 +00003822
3823 __ Bind(&greater);
3824 __ LoadImmediate(out, 1);
3825 __ b(&done);
3826
3827 __ Bind(&less);
3828 __ LoadImmediate(out, -1);
3829
3830 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003831}
3832
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003833void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003834 LocationSummary* locations =
3835 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003836 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3837 locations->SetInAt(i, Location::Any());
3838 }
3839 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003840}
3841
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003842void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003843 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003844}
3845
Roland Levillainc9285912015-12-18 10:38:42 +00003846void CodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
3847 // TODO (ported from quick): revisit ARM barrier kinds.
3848 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
Calin Juravle52c48962014-12-16 17:02:57 +00003849 switch (kind) {
3850 case MemBarrierKind::kAnyStore:
3851 case MemBarrierKind::kLoadAny:
3852 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003853 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00003854 break;
3855 }
3856 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003857 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00003858 break;
3859 }
3860 default:
3861 LOG(FATAL) << "Unexpected memory barrier " << kind;
3862 }
Kenny Root1d8199d2015-06-02 11:01:10 -07003863 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00003864}
3865
3866void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
3867 uint32_t offset,
3868 Register out_lo,
3869 Register out_hi) {
3870 if (offset != 0) {
Roland Levillain3b359c72015-11-17 19:35:12 +00003871 // Ensure `out_lo` is different from `addr`, so that loading
3872 // `offset` into `out_lo` does not clutter `addr`.
3873 DCHECK_NE(out_lo, addr);
Calin Juravle52c48962014-12-16 17:02:57 +00003874 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003875 __ add(IP, addr, ShifterOperand(out_lo));
3876 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003877 }
3878 __ ldrexd(out_lo, out_hi, addr);
3879}
3880
3881void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
3882 uint32_t offset,
3883 Register value_lo,
3884 Register value_hi,
3885 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00003886 Register temp2,
3887 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003888 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00003889 if (offset != 0) {
3890 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003891 __ add(IP, addr, ShifterOperand(temp1));
3892 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003893 }
3894 __ Bind(&fail);
3895 // We need a load followed by store. (The address used in a STREX instruction must
3896 // be the same as the address in the most recently executed LDREX instruction.)
3897 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00003898 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003899 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003900 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00003901}
3902
3903void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3904 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3905
Nicolas Geoffray39468442014-09-02 15:17:15 +01003906 LocationSummary* locations =
3907 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003908 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003909
Calin Juravle52c48962014-12-16 17:02:57 +00003910 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003911 if (Primitive::IsFloatingPointType(field_type)) {
3912 locations->SetInAt(1, Location::RequiresFpuRegister());
3913 } else {
3914 locations->SetInAt(1, Location::RequiresRegister());
3915 }
3916
Calin Juravle52c48962014-12-16 17:02:57 +00003917 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00003918 bool generate_volatile = field_info.IsVolatile()
3919 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003920 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01003921 bool needs_write_barrier =
3922 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003923 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00003924 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01003925 if (needs_write_barrier) {
3926 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003927 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003928 } else if (generate_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00003929 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00003930 // - registers need to be consecutive
3931 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00003932 // We don't test for ARM yet, and the assertion makes sure that we
3933 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00003934 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3935
3936 locations->AddTemp(Location::RequiresRegister());
3937 locations->AddTemp(Location::RequiresRegister());
3938 if (field_type == Primitive::kPrimDouble) {
3939 // For doubles we need two more registers to copy the value.
3940 locations->AddTemp(Location::RegisterLocation(R2));
3941 locations->AddTemp(Location::RegisterLocation(R3));
3942 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003943 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003944}
3945
Calin Juravle52c48962014-12-16 17:02:57 +00003946void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003947 const FieldInfo& field_info,
3948 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003949 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3950
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003951 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003952 Register base = locations->InAt(0).AsRegister<Register>();
3953 Location value = locations->InAt(1);
3954
3955 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003956 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00003957 Primitive::Type field_type = field_info.GetFieldType();
3958 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01003959 bool needs_write_barrier =
3960 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003961
3962 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00003963 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00003964 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003965
3966 switch (field_type) {
3967 case Primitive::kPrimBoolean:
3968 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003969 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003970 break;
3971 }
3972
3973 case Primitive::kPrimShort:
3974 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003975 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003976 break;
3977 }
3978
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003979 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003980 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01003981 if (kPoisonHeapReferences && needs_write_barrier) {
3982 // Note that in the case where `value` is a null reference,
3983 // we do not enter this block, as a null reference does not
3984 // need poisoning.
3985 DCHECK_EQ(field_type, Primitive::kPrimNot);
3986 Register temp = locations->GetTemp(0).AsRegister<Register>();
3987 __ Mov(temp, value.AsRegister<Register>());
3988 __ PoisonHeapReference(temp);
3989 __ StoreToOffset(kStoreWord, temp, base, offset);
3990 } else {
3991 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
3992 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003993 break;
3994 }
3995
3996 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00003997 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003998 GenerateWideAtomicStore(base, offset,
3999 value.AsRegisterPairLow<Register>(),
4000 value.AsRegisterPairHigh<Register>(),
4001 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00004002 locations->GetTemp(1).AsRegister<Register>(),
4003 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004004 } else {
4005 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004006 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004007 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004008 break;
4009 }
4010
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004011 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004012 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004013 break;
4014 }
4015
4016 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004017 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004018 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004019 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
4020 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
4021
4022 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
4023
4024 GenerateWideAtomicStore(base, offset,
4025 value_reg_lo,
4026 value_reg_hi,
4027 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00004028 locations->GetTemp(3).AsRegister<Register>(),
4029 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004030 } else {
4031 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004032 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004033 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004034 break;
4035 }
4036
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004037 case Primitive::kPrimVoid:
4038 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004039 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004040 }
Calin Juravle52c48962014-12-16 17:02:57 +00004041
Calin Juravle77520bc2015-01-12 18:45:46 +00004042 // Longs and doubles are handled in the switch.
4043 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
4044 codegen_->MaybeRecordImplicitNullCheck(instruction);
4045 }
4046
4047 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4048 Register temp = locations->GetTemp(0).AsRegister<Register>();
4049 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004050 codegen_->MarkGCCard(
4051 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004052 }
4053
Calin Juravle52c48962014-12-16 17:02:57 +00004054 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004055 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004056 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004057}
4058
Calin Juravle52c48962014-12-16 17:02:57 +00004059void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4060 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain3b359c72015-11-17 19:35:12 +00004061
4062 bool object_field_get_with_read_barrier =
4063 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004064 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004065 new (GetGraph()->GetArena()) LocationSummary(instruction,
4066 object_field_get_with_read_barrier ?
4067 LocationSummary::kCallOnSlowPath :
4068 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004069 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004070
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004071 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00004072 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004073 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain3b359c72015-11-17 19:35:12 +00004074 // The output overlaps in case of volatile long: we don't want the
4075 // code generated by GenerateWideAtomicLoad to overwrite the
4076 // object's location. Likewise, in the case of an object field get
4077 // with read barriers enabled, we do not want the load to overwrite
4078 // the object's location, as we need it to emit the read barrier.
4079 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
4080 object_field_get_with_read_barrier;
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01004081
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004082 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4083 locations->SetOut(Location::RequiresFpuRegister());
4084 } else {
4085 locations->SetOut(Location::RequiresRegister(),
4086 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
4087 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004088 if (volatile_for_double) {
Roland Levillainc9285912015-12-18 10:38:42 +00004089 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00004090 // - registers need to be consecutive
4091 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00004092 // We don't test for ARM yet, and the assertion makes sure that we
4093 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00004094 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4095 locations->AddTemp(Location::RequiresRegister());
4096 locations->AddTemp(Location::RequiresRegister());
Roland Levillainc9285912015-12-18 10:38:42 +00004097 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4098 // We need a temporary register for the read barrier marking slow
4099 // path in CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier.
4100 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004101 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004102}
4103
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004104Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
4105 Opcode opcode) {
4106 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
4107 if (constant->IsConstant() &&
4108 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
4109 return Location::ConstantLocation(constant->AsConstant());
4110 }
4111 return Location::RequiresRegister();
4112}
4113
4114bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
4115 Opcode opcode) {
4116 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
4117 if (Primitive::Is64BitType(input_cst->GetType())) {
4118 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode) &&
4119 CanEncodeConstantAsImmediate(High32Bits(value), opcode);
4120 } else {
4121 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
4122 }
4123}
4124
4125bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value, Opcode opcode) {
4126 ShifterOperand so;
4127 ArmAssembler* assembler = codegen_->GetAssembler();
4128 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, &so)) {
4129 return true;
4130 }
4131 Opcode neg_opcode = kNoOperand;
4132 switch (opcode) {
4133 case AND:
4134 neg_opcode = BIC;
4135 break;
4136 case ORR:
4137 neg_opcode = ORN;
4138 break;
4139 default:
4140 return false;
4141 }
4142 return assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, neg_opcode, ~value, &so);
4143}
4144
Calin Juravle52c48962014-12-16 17:02:57 +00004145void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
4146 const FieldInfo& field_info) {
4147 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004148
Calin Juravle52c48962014-12-16 17:02:57 +00004149 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004150 Location base_loc = locations->InAt(0);
4151 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004152 Location out = locations->Out();
4153 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004154 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004155 Primitive::Type field_type = field_info.GetFieldType();
4156 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4157
4158 switch (field_type) {
Roland Levillainc9285912015-12-18 10:38:42 +00004159 case Primitive::kPrimBoolean:
Calin Juravle52c48962014-12-16 17:02:57 +00004160 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004161 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004162
Roland Levillainc9285912015-12-18 10:38:42 +00004163 case Primitive::kPrimByte:
Calin Juravle52c48962014-12-16 17:02:57 +00004164 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004165 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004166
Roland Levillainc9285912015-12-18 10:38:42 +00004167 case Primitive::kPrimShort:
Calin Juravle52c48962014-12-16 17:02:57 +00004168 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004169 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004170
Roland Levillainc9285912015-12-18 10:38:42 +00004171 case Primitive::kPrimChar:
Calin Juravle52c48962014-12-16 17:02:57 +00004172 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004173 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004174
4175 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004176 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004177 break;
Roland Levillainc9285912015-12-18 10:38:42 +00004178
4179 case Primitive::kPrimNot: {
4180 // /* HeapReference<Object> */ out = *(base + offset)
4181 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4182 Location temp_loc = locations->GetTemp(0);
4183 // Note that a potential implicit null check is handled in this
4184 // CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier call.
4185 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4186 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4187 if (is_volatile) {
4188 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4189 }
4190 } else {
4191 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
4192 codegen_->MaybeRecordImplicitNullCheck(instruction);
4193 if (is_volatile) {
4194 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4195 }
4196 // If read barriers are enabled, emit read barriers other than
4197 // Baker's using a slow path (and also unpoison the loaded
4198 // reference, if heap poisoning is enabled).
4199 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4200 }
4201 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004202 }
4203
Roland Levillainc9285912015-12-18 10:38:42 +00004204 case Primitive::kPrimLong:
Calin Juravle34166012014-12-19 17:22:29 +00004205 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004206 GenerateWideAtomicLoad(base, offset,
4207 out.AsRegisterPairLow<Register>(),
4208 out.AsRegisterPairHigh<Register>());
4209 } else {
4210 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
4211 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004212 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004213
Roland Levillainc9285912015-12-18 10:38:42 +00004214 case Primitive::kPrimFloat:
Calin Juravle52c48962014-12-16 17:02:57 +00004215 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004216 break;
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004217
4218 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004219 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004220 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004221 Register lo = locations->GetTemp(0).AsRegister<Register>();
4222 Register hi = locations->GetTemp(1).AsRegister<Register>();
4223 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00004224 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004225 __ vmovdrr(out_reg, lo, hi);
4226 } else {
4227 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004228 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004229 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004230 break;
4231 }
4232
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004233 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004234 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004235 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004236 }
Calin Juravle52c48962014-12-16 17:02:57 +00004237
Roland Levillainc9285912015-12-18 10:38:42 +00004238 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
4239 // Potential implicit null checks, in the case of reference or
4240 // double fields, are handled in the previous switch statement.
4241 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004242 codegen_->MaybeRecordImplicitNullCheck(instruction);
4243 }
4244
Calin Juravle52c48962014-12-16 17:02:57 +00004245 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004246 if (field_type == Primitive::kPrimNot) {
4247 // Memory barriers, in the case of references, are also handled
4248 // in the previous switch statement.
4249 } else {
4250 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4251 }
Roland Levillain4d027112015-07-01 15:41:14 +01004252 }
Calin Juravle52c48962014-12-16 17:02:57 +00004253}
4254
4255void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4256 HandleFieldSet(instruction, instruction->GetFieldInfo());
4257}
4258
4259void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004260 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004261}
4262
4263void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4264 HandleFieldGet(instruction, instruction->GetFieldInfo());
4265}
4266
4267void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4268 HandleFieldGet(instruction, instruction->GetFieldInfo());
4269}
4270
4271void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4272 HandleFieldGet(instruction, instruction->GetFieldInfo());
4273}
4274
4275void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4276 HandleFieldGet(instruction, instruction->GetFieldInfo());
4277}
4278
4279void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4280 HandleFieldSet(instruction, instruction->GetFieldInfo());
4281}
4282
4283void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004284 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004285}
4286
Calin Juravlee460d1d2015-09-29 04:52:17 +01004287void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
4288 HUnresolvedInstanceFieldGet* instruction) {
4289 FieldAccessCallingConventionARM calling_convention;
4290 codegen_->CreateUnresolvedFieldLocationSummary(
4291 instruction, instruction->GetFieldType(), calling_convention);
4292}
4293
4294void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
4295 HUnresolvedInstanceFieldGet* instruction) {
4296 FieldAccessCallingConventionARM calling_convention;
4297 codegen_->GenerateUnresolvedFieldAccess(instruction,
4298 instruction->GetFieldType(),
4299 instruction->GetFieldIndex(),
4300 instruction->GetDexPc(),
4301 calling_convention);
4302}
4303
4304void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
4305 HUnresolvedInstanceFieldSet* instruction) {
4306 FieldAccessCallingConventionARM calling_convention;
4307 codegen_->CreateUnresolvedFieldLocationSummary(
4308 instruction, instruction->GetFieldType(), calling_convention);
4309}
4310
4311void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
4312 HUnresolvedInstanceFieldSet* instruction) {
4313 FieldAccessCallingConventionARM calling_convention;
4314 codegen_->GenerateUnresolvedFieldAccess(instruction,
4315 instruction->GetFieldType(),
4316 instruction->GetFieldIndex(),
4317 instruction->GetDexPc(),
4318 calling_convention);
4319}
4320
4321void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
4322 HUnresolvedStaticFieldGet* instruction) {
4323 FieldAccessCallingConventionARM calling_convention;
4324 codegen_->CreateUnresolvedFieldLocationSummary(
4325 instruction, instruction->GetFieldType(), calling_convention);
4326}
4327
4328void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
4329 HUnresolvedStaticFieldGet* instruction) {
4330 FieldAccessCallingConventionARM calling_convention;
4331 codegen_->GenerateUnresolvedFieldAccess(instruction,
4332 instruction->GetFieldType(),
4333 instruction->GetFieldIndex(),
4334 instruction->GetDexPc(),
4335 calling_convention);
4336}
4337
4338void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
4339 HUnresolvedStaticFieldSet* instruction) {
4340 FieldAccessCallingConventionARM calling_convention;
4341 codegen_->CreateUnresolvedFieldLocationSummary(
4342 instruction, instruction->GetFieldType(), calling_convention);
4343}
4344
4345void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
4346 HUnresolvedStaticFieldSet* instruction) {
4347 FieldAccessCallingConventionARM calling_convention;
4348 codegen_->GenerateUnresolvedFieldAccess(instruction,
4349 instruction->GetFieldType(),
4350 instruction->GetFieldIndex(),
4351 instruction->GetDexPc(),
4352 calling_convention);
4353}
4354
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004355void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004356 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4357 ? LocationSummary::kCallOnSlowPath
4358 : LocationSummary::kNoCall;
4359 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravle77520bc2015-01-12 18:45:46 +00004360 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004361 if (instruction->HasUses()) {
4362 locations->SetOut(Location::SameAsFirstInput());
4363 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004364}
4365
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004366void InstructionCodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004367 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4368 return;
4369 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004370 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004371
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004372 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
4373 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4374}
4375
4376void InstructionCodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004377 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004378 codegen_->AddSlowPath(slow_path);
4379
4380 LocationSummary* locations = instruction->GetLocations();
4381 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004382
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004383 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004384}
4385
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004386void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004387 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004388 GenerateImplicitNullCheck(instruction);
4389 } else {
4390 GenerateExplicitNullCheck(instruction);
4391 }
4392}
4393
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004394void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004395 bool object_array_get_with_read_barrier =
4396 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004397 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004398 new (GetGraph()->GetArena()) LocationSummary(instruction,
4399 object_array_get_with_read_barrier ?
4400 LocationSummary::kCallOnSlowPath :
4401 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004402 locations->SetInAt(0, Location::RequiresRegister());
4403 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004404 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4405 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4406 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004407 // The output overlaps in the case of an object array get with
4408 // read barriers enabled: we do not want the move to overwrite the
4409 // array's location, as we need it to emit the read barrier.
4410 locations->SetOut(
4411 Location::RequiresRegister(),
4412 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004413 }
Roland Levillainc9285912015-12-18 10:38:42 +00004414 // We need a temporary register for the read barrier marking slow
4415 // path in CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier.
4416 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4417 locations->AddTemp(Location::RequiresRegister());
4418 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004419}
4420
4421void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
4422 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004423 Location obj_loc = locations->InAt(0);
4424 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004425 Location index = locations->InAt(1);
Roland Levillainc9285912015-12-18 10:38:42 +00004426 Location out_loc = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004427
Roland Levillainc9285912015-12-18 10:38:42 +00004428 Primitive::Type type = instruction->GetType();
Roland Levillain4d027112015-07-01 15:41:14 +01004429 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004430 case Primitive::kPrimBoolean: {
4431 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004432 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004433 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004434 size_t offset =
4435 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004436 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
4437 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004438 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004439 __ LoadFromOffset(kLoadUnsignedByte, out, IP, data_offset);
4440 }
4441 break;
4442 }
4443
4444 case Primitive::kPrimByte: {
4445 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004446 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004447 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004448 size_t offset =
4449 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004450 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
4451 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004452 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004453 __ LoadFromOffset(kLoadSignedByte, out, IP, data_offset);
4454 }
4455 break;
4456 }
4457
4458 case Primitive::kPrimShort: {
4459 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004460 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004461 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004462 size_t offset =
4463 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004464 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
4465 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004466 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004467 __ LoadFromOffset(kLoadSignedHalfword, out, IP, data_offset);
4468 }
4469 break;
4470 }
4471
4472 case Primitive::kPrimChar: {
4473 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004474 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004475 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004476 size_t offset =
4477 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004478 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
4479 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004480 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004481 __ LoadFromOffset(kLoadUnsignedHalfword, out, IP, data_offset);
4482 }
4483 break;
4484 }
4485
Roland Levillainc9285912015-12-18 10:38:42 +00004486 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004487 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004488 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004489 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004490 size_t offset =
4491 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004492 __ LoadFromOffset(kLoadWord, out, obj, offset);
4493 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004494 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004495 __ LoadFromOffset(kLoadWord, out, IP, data_offset);
4496 }
4497 break;
4498 }
4499
Roland Levillainc9285912015-12-18 10:38:42 +00004500 case Primitive::kPrimNot: {
4501 static_assert(
4502 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4503 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
4504 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4505 // /* HeapReference<Object> */ out =
4506 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4507 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4508 Location temp = locations->GetTemp(0);
4509 // Note that a potential implicit null check is handled in this
4510 // CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier call.
4511 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4512 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4513 } else {
4514 Register out = out_loc.AsRegister<Register>();
4515 if (index.IsConstant()) {
4516 size_t offset =
4517 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4518 __ LoadFromOffset(kLoadWord, out, obj, offset);
4519 codegen_->MaybeRecordImplicitNullCheck(instruction);
4520 // If read barriers are enabled, emit read barriers other than
4521 // Baker's using a slow path (and also unpoison the loaded
4522 // reference, if heap poisoning is enabled).
4523 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4524 } else {
4525 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4526 __ LoadFromOffset(kLoadWord, out, IP, data_offset);
4527 codegen_->MaybeRecordImplicitNullCheck(instruction);
4528 // If read barriers are enabled, emit read barriers other than
4529 // Baker's using a slow path (and also unpoison the loaded
4530 // reference, if heap poisoning is enabled).
4531 codegen_->MaybeGenerateReadBarrierSlow(
4532 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4533 }
4534 }
4535 break;
4536 }
4537
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004538 case Primitive::kPrimLong: {
4539 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004540 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004541 size_t offset =
4542 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004543 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004544 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004545 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00004546 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004547 }
4548 break;
4549 }
4550
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004551 case Primitive::kPrimFloat: {
4552 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004553 SRegister out = out_loc.AsFpuRegister<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004554 if (index.IsConstant()) {
4555 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004556 __ LoadSFromOffset(out, obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004557 } else {
4558 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillainc9285912015-12-18 10:38:42 +00004559 __ LoadSFromOffset(out, IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004560 }
4561 break;
4562 }
4563
4564 case Primitive::kPrimDouble: {
4565 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004566 SRegister out = out_loc.AsFpuRegisterPairLow<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004567 if (index.IsConstant()) {
4568 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004569 __ LoadDFromOffset(FromLowSToD(out), obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004570 } else {
4571 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00004572 __ LoadDFromOffset(FromLowSToD(out), IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004573 }
4574 break;
4575 }
4576
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004577 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004578 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004579 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004580 }
Roland Levillain4d027112015-07-01 15:41:14 +01004581
4582 if (type == Primitive::kPrimNot) {
Roland Levillainc9285912015-12-18 10:38:42 +00004583 // Potential implicit null checks, in the case of reference
4584 // arrays, are handled in the previous switch statement.
4585 } else {
4586 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004587 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004588}
4589
4590void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004591 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004592
4593 bool needs_write_barrier =
4594 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain3b359c72015-11-17 19:35:12 +00004595 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
4596 bool object_array_set_with_read_barrier =
4597 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004598
Nicolas Geoffray39468442014-09-02 15:17:15 +01004599 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004600 instruction,
Roland Levillain3b359c72015-11-17 19:35:12 +00004601 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
4602 LocationSummary::kCallOnSlowPath :
4603 LocationSummary::kNoCall);
4604
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004605 locations->SetInAt(0, Location::RequiresRegister());
4606 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4607 if (Primitive::IsFloatingPointType(value_type)) {
4608 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004609 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004610 locations->SetInAt(2, Location::RequiresRegister());
4611 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004612 if (needs_write_barrier) {
4613 // Temporary registers for the write barrier.
4614 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00004615 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004616 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004617}
4618
4619void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
4620 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004621 Location array_loc = locations->InAt(0);
4622 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004623 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004624 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain3b359c72015-11-17 19:35:12 +00004625 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004626 bool needs_write_barrier =
4627 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004628
4629 switch (value_type) {
4630 case Primitive::kPrimBoolean:
4631 case Primitive::kPrimByte: {
4632 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004633 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004634 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004635 size_t offset =
4636 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004637 __ StoreToOffset(kStoreByte, value, array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004638 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004639 __ add(IP, array, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004640 __ StoreToOffset(kStoreByte, value, IP, data_offset);
4641 }
4642 break;
4643 }
4644
4645 case Primitive::kPrimShort:
4646 case Primitive::kPrimChar: {
4647 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004648 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004649 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004650 size_t offset =
4651 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004652 __ StoreToOffset(kStoreHalfword, value, array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004653 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004654 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004655 __ StoreToOffset(kStoreHalfword, value, IP, data_offset);
4656 }
4657 break;
4658 }
4659
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004660 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004661 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00004662 Location value_loc = locations->InAt(2);
4663 Register value = value_loc.AsRegister<Register>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004664 Register source = value;
4665
4666 if (instruction->InputAt(2)->IsNullConstant()) {
4667 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004668 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004669 size_t offset =
4670 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004671 __ StoreToOffset(kStoreWord, source, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004672 } else {
4673 DCHECK(index.IsRegister()) << index;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004674 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillain4d027112015-07-01 15:41:14 +01004675 __ StoreToOffset(kStoreWord, source, IP, data_offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004676 }
Roland Levillain1407ee72016-01-08 15:56:19 +00004677 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain3b359c72015-11-17 19:35:12 +00004678 DCHECK(!needs_write_barrier);
4679 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004680 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004681 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004682
4683 DCHECK(needs_write_barrier);
4684 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
4685 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
4686 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4687 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4688 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4689 Label done;
4690 SlowPathCode* slow_path = nullptr;
4691
Roland Levillain3b359c72015-11-17 19:35:12 +00004692 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004693 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
4694 codegen_->AddSlowPath(slow_path);
4695 if (instruction->GetValueCanBeNull()) {
4696 Label non_zero;
4697 __ CompareAndBranchIfNonZero(value, &non_zero);
4698 if (index.IsConstant()) {
4699 size_t offset =
4700 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4701 __ StoreToOffset(kStoreWord, value, array, offset);
4702 } else {
4703 DCHECK(index.IsRegister()) << index;
4704 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4705 __ StoreToOffset(kStoreWord, value, IP, data_offset);
4706 }
4707 codegen_->MaybeRecordImplicitNullCheck(instruction);
4708 __ b(&done);
4709 __ Bind(&non_zero);
4710 }
4711
Roland Levillain3b359c72015-11-17 19:35:12 +00004712 if (kEmitCompilerReadBarrier) {
4713 // When read barriers are enabled, the type checking
4714 // instrumentation requires two read barriers:
4715 //
4716 // __ Mov(temp2, temp1);
4717 // // /* HeapReference<Class> */ temp1 = temp1->component_type_
4718 // __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
Roland Levillainc9285912015-12-18 10:38:42 +00004719 // codegen_->GenerateReadBarrierSlow(
Roland Levillain3b359c72015-11-17 19:35:12 +00004720 // instruction, temp1_loc, temp1_loc, temp2_loc, component_offset);
4721 //
4722 // // /* HeapReference<Class> */ temp2 = value->klass_
4723 // __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
Roland Levillainc9285912015-12-18 10:38:42 +00004724 // codegen_->GenerateReadBarrierSlow(
Roland Levillain3b359c72015-11-17 19:35:12 +00004725 // instruction, temp2_loc, temp2_loc, value_loc, class_offset, temp1_loc);
4726 //
4727 // __ cmp(temp1, ShifterOperand(temp2));
4728 //
4729 // However, the second read barrier may trash `temp`, as it
4730 // is a temporary register, and as such would not be saved
4731 // along with live registers before calling the runtime (nor
4732 // restored afterwards). So in this case, we bail out and
4733 // delegate the work to the array set slow path.
4734 //
4735 // TODO: Extend the register allocator to support a new
4736 // "(locally) live temp" location so as to avoid always
4737 // going into the slow path when read barriers are enabled.
4738 __ b(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004739 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004740 // /* HeapReference<Class> */ temp1 = array->klass_
4741 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
4742 codegen_->MaybeRecordImplicitNullCheck(instruction);
4743 __ MaybeUnpoisonHeapReference(temp1);
4744
4745 // /* HeapReference<Class> */ temp1 = temp1->component_type_
4746 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
4747 // /* HeapReference<Class> */ temp2 = value->klass_
4748 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
4749 // If heap poisoning is enabled, no need to unpoison `temp1`
4750 // nor `temp2`, as we are comparing two poisoned references.
4751 __ cmp(temp1, ShifterOperand(temp2));
4752
4753 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4754 Label do_put;
4755 __ b(&do_put, EQ);
4756 // If heap poisoning is enabled, the `temp1` reference has
4757 // not been unpoisoned yet; unpoison it now.
4758 __ MaybeUnpoisonHeapReference(temp1);
4759
4760 // /* HeapReference<Class> */ temp1 = temp1->super_class_
4761 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
4762 // If heap poisoning is enabled, no need to unpoison
4763 // `temp1`, as we are comparing against null below.
4764 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
4765 __ Bind(&do_put);
4766 } else {
4767 __ b(slow_path->GetEntryLabel(), NE);
4768 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004769 }
4770 }
4771
4772 if (kPoisonHeapReferences) {
4773 // Note that in the case where `value` is a null reference,
4774 // we do not enter this block, as a null reference does not
4775 // need poisoning.
4776 DCHECK_EQ(value_type, Primitive::kPrimNot);
4777 __ Mov(temp1, value);
4778 __ PoisonHeapReference(temp1);
4779 source = temp1;
4780 }
4781
4782 if (index.IsConstant()) {
4783 size_t offset =
4784 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4785 __ StoreToOffset(kStoreWord, source, array, offset);
4786 } else {
4787 DCHECK(index.IsRegister()) << index;
4788 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4789 __ StoreToOffset(kStoreWord, source, IP, data_offset);
4790 }
4791
Roland Levillain3b359c72015-11-17 19:35:12 +00004792 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004793 codegen_->MaybeRecordImplicitNullCheck(instruction);
4794 }
4795
4796 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
4797
4798 if (done.IsLinked()) {
4799 __ Bind(&done);
4800 }
4801
4802 if (slow_path != nullptr) {
4803 __ Bind(slow_path->GetExitLabel());
4804 }
4805
4806 break;
4807 }
4808
4809 case Primitive::kPrimInt: {
4810 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4811 Register value = locations->InAt(2).AsRegister<Register>();
4812 if (index.IsConstant()) {
4813 size_t offset =
4814 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4815 __ StoreToOffset(kStoreWord, value, array, offset);
4816 } else {
4817 DCHECK(index.IsRegister()) << index;
4818 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4819 __ StoreToOffset(kStoreWord, value, IP, data_offset);
4820 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004821 break;
4822 }
4823
4824 case Primitive::kPrimLong: {
4825 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004826 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004827 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004828 size_t offset =
4829 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004830 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004831 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004832 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004833 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004834 }
4835 break;
4836 }
4837
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004838 case Primitive::kPrimFloat: {
4839 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4840 Location value = locations->InAt(2);
4841 DCHECK(value.IsFpuRegister());
4842 if (index.IsConstant()) {
4843 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004844 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004845 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004846 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004847 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
4848 }
4849 break;
4850 }
4851
4852 case Primitive::kPrimDouble: {
4853 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4854 Location value = locations->InAt(2);
4855 DCHECK(value.IsFpuRegisterPair());
4856 if (index.IsConstant()) {
4857 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004858 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004859 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004860 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004861 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
4862 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004863
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004864 break;
4865 }
4866
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004867 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004868 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004869 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004870 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004871
Roland Levillain80e67092016-01-08 16:04:55 +00004872 // Objects are handled in the switch.
4873 if (value_type != Primitive::kPrimNot) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004874 codegen_->MaybeRecordImplicitNullCheck(instruction);
4875 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004876}
4877
4878void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004879 LocationSummary* locations =
4880 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004881 locations->SetInAt(0, Location::RequiresRegister());
4882 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004883}
4884
4885void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
4886 LocationSummary* locations = instruction->GetLocations();
4887 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004888 Register obj = locations->InAt(0).AsRegister<Register>();
4889 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004890 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004891 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004892}
4893
4894void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004895 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4896 ? LocationSummary::kCallOnSlowPath
4897 : LocationSummary::kNoCall;
4898 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004899 locations->SetInAt(0, Location::RequiresRegister());
4900 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004901 if (instruction->HasUses()) {
4902 locations->SetOut(Location::SameAsFirstInput());
4903 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004904}
4905
4906void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
4907 LocationSummary* locations = instruction->GetLocations();
Andreas Gampe85b62f22015-09-09 13:15:38 -07004908 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004909 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004910 codegen_->AddSlowPath(slow_path);
4911
Roland Levillain271ab9c2014-11-27 15:23:57 +00004912 Register index = locations->InAt(0).AsRegister<Register>();
4913 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004914
4915 __ cmp(index, ShifterOperand(length));
Roland Levillain4fa13f62015-07-06 18:11:54 +01004916 __ b(slow_path->GetEntryLabel(), HS);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004917}
4918
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004919void CodeGeneratorARM::MarkGCCard(Register temp,
4920 Register card,
4921 Register object,
4922 Register value,
4923 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004924 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004925 if (can_be_null) {
4926 __ CompareAndBranchIfZero(value, &is_null);
4927 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004928 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmWordSize>().Int32Value());
4929 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
4930 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004931 if (can_be_null) {
4932 __ Bind(&is_null);
4933 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004934}
4935
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004936void LocationsBuilderARM::VisitTemporary(HTemporary* temp) {
4937 temp->SetLocations(nullptr);
4938}
4939
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004940void InstructionCodeGeneratorARM::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004941 // Nothing to do, this is driven by the code generator.
4942}
4943
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004944void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004945 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004946}
4947
4948void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004949 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4950}
4951
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004952void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
4953 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4954}
4955
4956void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004957 HBasicBlock* block = instruction->GetBlock();
4958 if (block->GetLoopInformation() != nullptr) {
4959 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4960 // The back edge will generate the suspend check.
4961 return;
4962 }
4963 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4964 // The goto will generate the suspend check.
4965 return;
4966 }
4967 GenerateSuspendCheck(instruction, nullptr);
4968}
4969
4970void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
4971 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004972 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004973 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
4974 if (slow_path == nullptr) {
4975 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
4976 instruction->SetSlowPath(slow_path);
4977 codegen_->AddSlowPath(slow_path);
4978 if (successor != nullptr) {
4979 DCHECK(successor->IsLoopHeader());
4980 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4981 }
4982 } else {
4983 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4984 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004985
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00004986 __ LoadFromOffset(
4987 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmWordSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004988 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004989 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004990 __ Bind(slow_path->GetReturnLabel());
4991 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004992 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004993 __ b(slow_path->GetEntryLabel());
4994 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004995}
4996
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004997ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
4998 return codegen_->GetAssembler();
4999}
5000
5001void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005002 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005003 Location source = move->GetSource();
5004 Location destination = move->GetDestination();
5005
5006 if (source.IsRegister()) {
5007 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005008 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005009 } else {
5010 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005011 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005012 SP, destination.GetStackIndex());
5013 }
5014 } else if (source.IsStackSlot()) {
5015 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005016 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005017 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005018 } else if (destination.IsFpuRegister()) {
5019 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005020 } else {
5021 DCHECK(destination.IsStackSlot());
5022 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
5023 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5024 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005025 } else if (source.IsFpuRegister()) {
5026 if (destination.IsFpuRegister()) {
5027 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005028 } else {
5029 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005030 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
5031 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005032 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005033 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005034 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
5035 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005036 } else if (destination.IsRegisterPair()) {
5037 DCHECK(ExpectedPairLayout(destination));
5038 __ LoadFromOffset(
5039 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
5040 } else {
5041 DCHECK(destination.IsFpuRegisterPair()) << destination;
5042 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5043 SP,
5044 source.GetStackIndex());
5045 }
5046 } else if (source.IsRegisterPair()) {
5047 if (destination.IsRegisterPair()) {
5048 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
5049 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
5050 } else {
5051 DCHECK(destination.IsDoubleStackSlot()) << destination;
5052 DCHECK(ExpectedPairLayout(source));
5053 __ StoreToOffset(
5054 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
5055 }
5056 } else if (source.IsFpuRegisterPair()) {
5057 if (destination.IsFpuRegisterPair()) {
5058 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5059 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5060 } else {
5061 DCHECK(destination.IsDoubleStackSlot()) << destination;
5062 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
5063 SP,
5064 destination.GetStackIndex());
5065 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005066 } else {
5067 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005068 HConstant* constant = source.GetConstant();
5069 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5070 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005071 if (destination.IsRegister()) {
5072 __ LoadImmediate(destination.AsRegister<Register>(), value);
5073 } else {
5074 DCHECK(destination.IsStackSlot());
5075 __ LoadImmediate(IP, value);
5076 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5077 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005078 } else if (constant->IsLongConstant()) {
5079 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005080 if (destination.IsRegisterPair()) {
5081 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
5082 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005083 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005084 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005085 __ LoadImmediate(IP, Low32Bits(value));
5086 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5087 __ LoadImmediate(IP, High32Bits(value));
5088 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5089 }
5090 } else if (constant->IsDoubleConstant()) {
5091 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005092 if (destination.IsFpuRegisterPair()) {
5093 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005094 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005095 DCHECK(destination.IsDoubleStackSlot()) << destination;
5096 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005097 __ LoadImmediate(IP, Low32Bits(int_value));
5098 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5099 __ LoadImmediate(IP, High32Bits(int_value));
5100 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5101 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005102 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005103 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005104 float value = constant->AsFloatConstant()->GetValue();
5105 if (destination.IsFpuRegister()) {
5106 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
5107 } else {
5108 DCHECK(destination.IsStackSlot());
5109 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
5110 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5111 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005112 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005113 }
5114}
5115
5116void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
5117 __ Mov(IP, reg);
5118 __ LoadFromOffset(kLoadWord, reg, SP, mem);
5119 __ StoreToOffset(kStoreWord, IP, SP, mem);
5120}
5121
5122void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
5123 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
5124 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
5125 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
5126 SP, mem1 + stack_offset);
5127 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
5128 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
5129 SP, mem2 + stack_offset);
5130 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
5131}
5132
5133void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005134 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005135 Location source = move->GetSource();
5136 Location destination = move->GetDestination();
5137
5138 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005139 DCHECK_NE(source.AsRegister<Register>(), IP);
5140 DCHECK_NE(destination.AsRegister<Register>(), IP);
5141 __ Mov(IP, source.AsRegister<Register>());
5142 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
5143 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005144 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005145 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005146 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005147 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005148 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5149 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005150 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005151 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005152 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005153 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005154 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005155 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005156 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005157 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005158 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5159 destination.AsRegisterPairHigh<Register>(),
5160 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005161 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005162 Register low_reg = source.IsRegisterPair()
5163 ? source.AsRegisterPairLow<Register>()
5164 : destination.AsRegisterPairLow<Register>();
5165 int mem = source.IsRegisterPair()
5166 ? destination.GetStackIndex()
5167 : source.GetStackIndex();
5168 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005169 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005170 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005171 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005172 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005173 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
5174 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005175 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005176 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005177 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005178 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
5179 DRegister reg = source.IsFpuRegisterPair()
5180 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
5181 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
5182 int mem = source.IsFpuRegisterPair()
5183 ? destination.GetStackIndex()
5184 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005185 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005186 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005187 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005188 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
5189 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
5190 : destination.AsFpuRegister<SRegister>();
5191 int mem = source.IsFpuRegister()
5192 ? destination.GetStackIndex()
5193 : source.GetStackIndex();
5194
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005195 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005196 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005197 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005198 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005199 Exchange(source.GetStackIndex(), destination.GetStackIndex());
5200 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005201 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005202 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005203 }
5204}
5205
5206void ParallelMoveResolverARM::SpillScratch(int reg) {
5207 __ Push(static_cast<Register>(reg));
5208}
5209
5210void ParallelMoveResolverARM::RestoreScratch(int reg) {
5211 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005212}
5213
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005214void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01005215 InvokeRuntimeCallingConvention calling_convention;
5216 CodeGenerator::CreateLoadClassLocationSummary(
5217 cls,
5218 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Roland Levillain3b359c72015-11-17 19:35:12 +00005219 Location::RegisterLocation(R0),
5220 /* code_generator_supports_read_barrier */ true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005221}
5222
5223void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005224 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005225 if (cls->NeedsAccessCheck()) {
5226 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5227 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5228 cls,
5229 cls->GetDexPc(),
5230 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005231 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005232 return;
5233 }
5234
Roland Levillain3b359c72015-11-17 19:35:12 +00005235 Location out_loc = locations->Out();
5236 Register out = out_loc.AsRegister<Register>();
Calin Juravle580b6092015-10-06 17:35:58 +01005237 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005238
Calin Juravle580b6092015-10-06 17:35:58 +01005239 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005240 DCHECK(!cls->CanCallRuntime());
5241 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillainc9285912015-12-18 10:38:42 +00005242 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5243 GenerateGcRootFieldLoad(
5244 cls, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005245 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00005246 // /* GcRoot<mirror::Class>[] */ out =
5247 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005248 __ LoadFromOffset(kLoadWord,
5249 out,
5250 current_method,
Vladimir Marko05792b92015-08-03 11:56:49 +01005251 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
Roland Levillainc9285912015-12-18 10:38:42 +00005252 // /* GcRoot<mirror::Class> */ out = out[type_index]
5253 GenerateGcRootFieldLoad(cls, out_loc, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005254
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005255 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
5256 DCHECK(cls->CanCallRuntime());
5257 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
5258 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5259 codegen_->AddSlowPath(slow_path);
5260 if (!cls->IsInDexCache()) {
5261 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5262 }
5263 if (cls->MustGenerateClinitCheck()) {
5264 GenerateClassInitializationCheck(slow_path, out);
5265 } else {
5266 __ Bind(slow_path->GetExitLabel());
5267 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005268 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005269 }
5270}
5271
5272void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
5273 LocationSummary* locations =
5274 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5275 locations->SetInAt(0, Location::RequiresRegister());
5276 if (check->HasUses()) {
5277 locations->SetOut(Location::SameAsFirstInput());
5278 }
5279}
5280
5281void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005282 // We assume the class is not null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005283 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005284 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005285 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005286 GenerateClassInitializationCheck(slow_path,
5287 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005288}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005289
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005290void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005291 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005292 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
5293 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
5294 __ b(slow_path->GetEntryLabel(), LT);
5295 // Even if the initialized flag is set, we may be in a situation where caches are not synced
5296 // properly. Therefore, we do a memory fence.
5297 __ dmb(ISH);
5298 __ Bind(slow_path->GetExitLabel());
5299}
5300
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005301void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005302 LocationSummary::CallKind call_kind = (!load->IsInDexCache() || kEmitCompilerReadBarrier)
5303 ? LocationSummary::kCallOnSlowPath
5304 : LocationSummary::kNoCall;
5305 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005306 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005307 locations->SetOut(Location::RequiresRegister());
5308}
5309
5310void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005311 LocationSummary* locations = load->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005312 Location out_loc = locations->Out();
5313 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005314 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005315
Roland Levillainc9285912015-12-18 10:38:42 +00005316 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5317 GenerateGcRootFieldLoad(
5318 load, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Roland Levillain3b359c72015-11-17 19:35:12 +00005319 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
Mathieu Chartiereace4582014-11-24 18:29:54 -08005320 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
Roland Levillainc9285912015-12-18 10:38:42 +00005321 // /* GcRoot<mirror::String> */ out = out[string_index]
5322 GenerateGcRootFieldLoad(
5323 load, out_loc, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Roland Levillain3b359c72015-11-17 19:35:12 +00005324
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005325 if (!load->IsInDexCache()) {
5326 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
5327 codegen_->AddSlowPath(slow_path);
5328 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5329 __ Bind(slow_path->GetExitLabel());
5330 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005331}
5332
David Brazdilcb1c0552015-08-04 16:22:25 +01005333static int32_t GetExceptionTlsOffset() {
5334 return Thread::ExceptionOffset<kArmWordSize>().Int32Value();
5335}
5336
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005337void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
5338 LocationSummary* locations =
5339 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5340 locations->SetOut(Location::RequiresRegister());
5341}
5342
5343void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005344 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01005345 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
5346}
5347
5348void LocationsBuilderARM::VisitClearException(HClearException* clear) {
5349 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5350}
5351
5352void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005353 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01005354 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005355}
5356
5357void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
5358 LocationSummary* locations =
5359 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5360 InvokeRuntimeCallingConvention calling_convention;
5361 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5362}
5363
5364void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
5365 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00005366 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005367 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005368}
5369
Roland Levillainc9285912015-12-18 10:38:42 +00005370static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5371 return kEmitCompilerReadBarrier &&
5372 (kUseBakerReadBarrier ||
5373 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5374 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5375 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5376}
5377
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005378void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005379 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain3b359c72015-11-17 19:35:12 +00005380 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5381 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005382 case TypeCheckKind::kExactCheck:
5383 case TypeCheckKind::kAbstractClassCheck:
5384 case TypeCheckKind::kClassHierarchyCheck:
5385 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005386 call_kind =
5387 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005388 break;
5389 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005390 case TypeCheckKind::kUnresolvedCheck:
5391 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005392 call_kind = LocationSummary::kCallOnSlowPath;
5393 break;
5394 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005395
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005396 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain3b359c72015-11-17 19:35:12 +00005397 locations->SetInAt(0, Location::RequiresRegister());
5398 locations->SetInAt(1, Location::RequiresRegister());
5399 // The "out" register is used as a temporary, so it overlaps with the inputs.
5400 // Note that TypeCheckSlowPathARM uses this register too.
5401 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5402 // When read barriers are enabled, we need a temporary register for
5403 // some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00005404 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain3b359c72015-11-17 19:35:12 +00005405 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005406 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005407}
5408
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005409void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00005410 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005411 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005412 Location obj_loc = locations->InAt(0);
5413 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005414 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005415 Location out_loc = locations->Out();
5416 Register out = out_loc.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +00005417 Location temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
5418 locations->GetTemp(0) :
5419 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005420 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005421 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5422 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5423 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005424 Label done, zero;
Andreas Gampe85b62f22015-09-09 13:15:38 -07005425 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005426
5427 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005428 // avoid null check if we know obj is not null.
5429 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00005430 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005431 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005432
Roland Levillain3b359c72015-11-17 19:35:12 +00005433 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillainc9285912015-12-18 10:38:42 +00005434 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005435
Roland Levillainc9285912015-12-18 10:38:42 +00005436 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005437 case TypeCheckKind::kExactCheck: {
5438 __ cmp(out, ShifterOperand(cls));
5439 // Classes must be equal for the instanceof to succeed.
5440 __ b(&zero, NE);
5441 __ LoadImmediate(out, 1);
5442 __ b(&done);
5443 break;
5444 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005445
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005446 case TypeCheckKind::kAbstractClassCheck: {
5447 // If the class is abstract, we eagerly fetch the super class of the
5448 // object to avoid doing a comparison we know will fail.
5449 Label loop;
5450 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005451 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillainc9285912015-12-18 10:38:42 +00005452 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005453 // If `out` is null, we use it for the result, and jump to `done`.
5454 __ CompareAndBranchIfZero(out, &done);
5455 __ cmp(out, ShifterOperand(cls));
5456 __ b(&loop, NE);
5457 __ LoadImmediate(out, 1);
5458 if (zero.IsLinked()) {
5459 __ b(&done);
5460 }
5461 break;
5462 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005463
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005464 case TypeCheckKind::kClassHierarchyCheck: {
5465 // Walk over the class hierarchy to find a match.
5466 Label loop, success;
5467 __ Bind(&loop);
5468 __ cmp(out, ShifterOperand(cls));
5469 __ b(&success, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005470 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillainc9285912015-12-18 10:38:42 +00005471 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005472 __ CompareAndBranchIfNonZero(out, &loop);
5473 // If `out` is null, we use it for the result, and jump to `done`.
5474 __ b(&done);
5475 __ Bind(&success);
5476 __ LoadImmediate(out, 1);
5477 if (zero.IsLinked()) {
5478 __ b(&done);
5479 }
5480 break;
5481 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005482
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005483 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005484 // Do an exact check.
5485 Label exact_check;
5486 __ cmp(out, ShifterOperand(cls));
5487 __ b(&exact_check, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005488 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00005489 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillainc9285912015-12-18 10:38:42 +00005490 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005491 // If `out` is null, we use it for the result, and jump to `done`.
5492 __ CompareAndBranchIfZero(out, &done);
5493 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
5494 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
5495 __ CompareAndBranchIfNonZero(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005496 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005497 __ LoadImmediate(out, 1);
5498 __ b(&done);
5499 break;
5500 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005501
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005502 case TypeCheckKind::kArrayCheck: {
5503 __ cmp(out, ShifterOperand(cls));
5504 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain3b359c72015-11-17 19:35:12 +00005505 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5506 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005507 codegen_->AddSlowPath(slow_path);
5508 __ b(slow_path->GetEntryLabel(), NE);
5509 __ LoadImmediate(out, 1);
5510 if (zero.IsLinked()) {
5511 __ b(&done);
5512 }
5513 break;
5514 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005515
Calin Juravle98893e12015-10-02 21:05:03 +01005516 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005517 case TypeCheckKind::kInterfaceCheck: {
5518 // Note that we indeed only call on slow path, but we always go
5519 // into the slow path for the unresolved & interface check
5520 // cases.
5521 //
5522 // We cannot directly call the InstanceofNonTrivial runtime
5523 // entry point without resorting to a type checking slow path
5524 // here (i.e. by calling InvokeRuntime directly), as it would
5525 // require to assign fixed registers for the inputs of this
5526 // HInstanceOf instruction (following the runtime calling
5527 // convention), which might be cluttered by the potential first
5528 // read barrier emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00005529 //
5530 // TODO: Introduce a new runtime entry point taking the object
5531 // to test (instead of its class) as argument, and let it deal
5532 // with the read barrier issues. This will let us refactor this
5533 // case of the `switch` code as it was previously (with a direct
5534 // call to the runtime not using a type checking slow path).
5535 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00005536 DCHECK(locations->OnlyCallsOnSlowPath());
5537 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5538 /* is_fatal */ false);
5539 codegen_->AddSlowPath(slow_path);
5540 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005541 if (zero.IsLinked()) {
5542 __ b(&done);
5543 }
5544 break;
5545 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005546 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005547
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005548 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005549 __ Bind(&zero);
5550 __ LoadImmediate(out, 0);
5551 }
5552
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005553 if (done.IsLinked()) {
5554 __ Bind(&done);
5555 }
5556
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005557 if (slow_path != nullptr) {
5558 __ Bind(slow_path->GetExitLabel());
5559 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005560}
5561
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005562void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005563 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5564 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
5565
Roland Levillain3b359c72015-11-17 19:35:12 +00005566 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5567 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005568 case TypeCheckKind::kExactCheck:
5569 case TypeCheckKind::kAbstractClassCheck:
5570 case TypeCheckKind::kClassHierarchyCheck:
5571 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005572 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5573 LocationSummary::kCallOnSlowPath :
5574 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005575 break;
5576 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005577 case TypeCheckKind::kUnresolvedCheck:
5578 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005579 call_kind = LocationSummary::kCallOnSlowPath;
5580 break;
5581 }
5582
Roland Levillain3b359c72015-11-17 19:35:12 +00005583 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5584 locations->SetInAt(0, Location::RequiresRegister());
5585 locations->SetInAt(1, Location::RequiresRegister());
5586 // Note that TypeCheckSlowPathARM uses this "temp" register too.
5587 locations->AddTemp(Location::RequiresRegister());
5588 // When read barriers are enabled, we need an additional temporary
5589 // register for some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00005590 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005591 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005592 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005593}
5594
5595void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00005596 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005597 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005598 Location obj_loc = locations->InAt(0);
5599 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005600 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005601 Location temp_loc = locations->GetTemp(0);
5602 Register temp = temp_loc.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +00005603 Location temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
5604 locations->GetTemp(1) :
5605 Location::NoLocation();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005606 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005607 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5608 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5609 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005610
Roland Levillain3b359c72015-11-17 19:35:12 +00005611 bool is_type_check_slow_path_fatal =
5612 (type_check_kind == TypeCheckKind::kExactCheck ||
5613 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5614 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5615 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5616 !instruction->CanThrowIntoCatchBlock();
5617 SlowPathCode* type_check_slow_path =
5618 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5619 is_type_check_slow_path_fatal);
5620 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005621
5622 Label done;
5623 // Avoid null check if we know obj is not null.
5624 if (instruction->MustDoNullCheck()) {
5625 __ CompareAndBranchIfZero(obj, &done);
5626 }
5627
Roland Levillain3b359c72015-11-17 19:35:12 +00005628 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillainc9285912015-12-18 10:38:42 +00005629 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005630
Roland Levillain3b359c72015-11-17 19:35:12 +00005631 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005632 case TypeCheckKind::kExactCheck:
5633 case TypeCheckKind::kArrayCheck: {
5634 __ cmp(temp, ShifterOperand(cls));
5635 // Jump to slow path for throwing the exception or doing a
5636 // more involved array check.
Roland Levillain3b359c72015-11-17 19:35:12 +00005637 __ b(type_check_slow_path->GetEntryLabel(), NE);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005638 break;
5639 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005640
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005641 case TypeCheckKind::kAbstractClassCheck: {
5642 // If the class is abstract, we eagerly fetch the super class of the
5643 // object to avoid doing a comparison we know will fail.
Roland Levillain3b359c72015-11-17 19:35:12 +00005644 Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005645 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005646 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillainc9285912015-12-18 10:38:42 +00005647 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005648
5649 // If the class reference currently in `temp` is not null, jump
5650 // to the `compare_classes` label to compare it with the checked
5651 // class.
5652 __ CompareAndBranchIfNonZero(temp, &compare_classes);
5653 // Otherwise, jump to the slow path to throw the exception.
5654 //
5655 // But before, move back the object's class into `temp` before
5656 // going into the slow path, as it has been overwritten in the
5657 // meantime.
5658 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillainc9285912015-12-18 10:38:42 +00005659 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005660 __ b(type_check_slow_path->GetEntryLabel());
5661
5662 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005663 __ cmp(temp, ShifterOperand(cls));
5664 __ b(&loop, NE);
5665 break;
5666 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005667
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005668 case TypeCheckKind::kClassHierarchyCheck: {
5669 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005670 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005671 __ Bind(&loop);
5672 __ cmp(temp, ShifterOperand(cls));
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005673 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005674
Roland Levillain3b359c72015-11-17 19:35:12 +00005675 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillainc9285912015-12-18 10:38:42 +00005676 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005677
5678 // If the class reference currently in `temp` is not null, jump
5679 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005680 __ CompareAndBranchIfNonZero(temp, &loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005681 // Otherwise, jump to the slow path to throw the exception.
5682 //
5683 // But before, move back the object's class into `temp` before
5684 // going into the slow path, as it has been overwritten in the
5685 // meantime.
5686 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillainc9285912015-12-18 10:38:42 +00005687 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005688 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005689 break;
5690 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005691
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005692 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005693 // Do an exact check.
Roland Levillain3b359c72015-11-17 19:35:12 +00005694 Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005695 __ cmp(temp, ShifterOperand(cls));
5696 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005697
5698 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00005699 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillainc9285912015-12-18 10:38:42 +00005700 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005701
5702 // If the component type is not null (i.e. the object is indeed
5703 // an array), jump to label `check_non_primitive_component_type`
5704 // to further check that this component type is not a primitive
5705 // type.
5706 __ CompareAndBranchIfNonZero(temp, &check_non_primitive_component_type);
5707 // Otherwise, jump to the slow path to throw the exception.
5708 //
5709 // But before, move back the object's class into `temp` before
5710 // going into the slow path, as it has been overwritten in the
5711 // meantime.
5712 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillainc9285912015-12-18 10:38:42 +00005713 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005714 __ b(type_check_slow_path->GetEntryLabel());
5715
5716 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005717 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00005718 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
5719 __ CompareAndBranchIfZero(temp, &done);
5720 // Same comment as above regarding `temp` and the slow path.
5721 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillainc9285912015-12-18 10:38:42 +00005722 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005723 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005724 break;
5725 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005726
Calin Juravle98893e12015-10-02 21:05:03 +01005727 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005728 case TypeCheckKind::kInterfaceCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005729 // We always go into the type check slow path for the unresolved &
5730 // interface check cases.
5731 //
5732 // We cannot directly call the CheckCast runtime entry point
5733 // without resorting to a type checking slow path here (i.e. by
5734 // calling InvokeRuntime directly), as it would require to
5735 // assign fixed registers for the inputs of this HInstanceOf
5736 // instruction (following the runtime calling convention), which
5737 // might be cluttered by the potential first read barrier
5738 // emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00005739 //
5740 // TODO: Introduce a new runtime entry point taking the object
5741 // to test (instead of its class) as argument, and let it deal
5742 // with the read barrier issues. This will let us refactor this
5743 // case of the `switch` code as it was previously (with a direct
5744 // call to the runtime not using a type checking slow path).
5745 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00005746 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005747 break;
5748 }
5749 __ Bind(&done);
5750
Roland Levillain3b359c72015-11-17 19:35:12 +00005751 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005752}
5753
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005754void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
5755 LocationSummary* locations =
5756 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5757 InvokeRuntimeCallingConvention calling_convention;
5758 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5759}
5760
5761void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
5762 codegen_->InvokeRuntime(instruction->IsEnter()
5763 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
5764 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00005765 instruction->GetDexPc(),
5766 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005767 if (instruction->IsEnter()) {
5768 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5769 } else {
5770 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5771 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005772}
5773
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005774void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
5775void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
5776void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005777
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005778void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005779 LocationSummary* locations =
5780 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5781 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
5782 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005783 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005784 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005785 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00005786 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005787}
5788
5789void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
5790 HandleBitwiseOperation(instruction);
5791}
5792
5793void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
5794 HandleBitwiseOperation(instruction);
5795}
5796
5797void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
5798 HandleBitwiseOperation(instruction);
5799}
5800
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005801void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
5802 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
5803 if (value == 0xffffffffu) {
5804 if (out != first) {
5805 __ mov(out, ShifterOperand(first));
5806 }
5807 return;
5808 }
5809 if (value == 0u) {
5810 __ mov(out, ShifterOperand(0));
5811 return;
5812 }
5813 ShifterOperand so;
5814 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
5815 __ and_(out, first, so);
5816 } else {
5817 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so));
5818 __ bic(out, first, ShifterOperand(~value));
5819 }
5820}
5821
5822void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
5823 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
5824 if (value == 0u) {
5825 if (out != first) {
5826 __ mov(out, ShifterOperand(first));
5827 }
5828 return;
5829 }
5830 if (value == 0xffffffffu) {
5831 __ mvn(out, ShifterOperand(0));
5832 return;
5833 }
5834 ShifterOperand so;
5835 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
5836 __ orr(out, first, so);
5837 } else {
5838 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
5839 __ orn(out, first, ShifterOperand(~value));
5840 }
5841}
5842
5843void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
5844 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
5845 if (value == 0u) {
5846 if (out != first) {
5847 __ mov(out, ShifterOperand(first));
5848 }
5849 return;
5850 }
5851 __ eor(out, first, ShifterOperand(value));
5852}
5853
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005854void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
5855 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005856 Location first = locations->InAt(0);
5857 Location second = locations->InAt(1);
5858 Location out = locations->Out();
5859
5860 if (second.IsConstant()) {
5861 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
5862 uint32_t value_low = Low32Bits(value);
5863 if (instruction->GetResultType() == Primitive::kPrimInt) {
5864 Register first_reg = first.AsRegister<Register>();
5865 Register out_reg = out.AsRegister<Register>();
5866 if (instruction->IsAnd()) {
5867 GenerateAndConst(out_reg, first_reg, value_low);
5868 } else if (instruction->IsOr()) {
5869 GenerateOrrConst(out_reg, first_reg, value_low);
5870 } else {
5871 DCHECK(instruction->IsXor());
5872 GenerateEorConst(out_reg, first_reg, value_low);
5873 }
5874 } else {
5875 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
5876 uint32_t value_high = High32Bits(value);
5877 Register first_low = first.AsRegisterPairLow<Register>();
5878 Register first_high = first.AsRegisterPairHigh<Register>();
5879 Register out_low = out.AsRegisterPairLow<Register>();
5880 Register out_high = out.AsRegisterPairHigh<Register>();
5881 if (instruction->IsAnd()) {
5882 GenerateAndConst(out_low, first_low, value_low);
5883 GenerateAndConst(out_high, first_high, value_high);
5884 } else if (instruction->IsOr()) {
5885 GenerateOrrConst(out_low, first_low, value_low);
5886 GenerateOrrConst(out_high, first_high, value_high);
5887 } else {
5888 DCHECK(instruction->IsXor());
5889 GenerateEorConst(out_low, first_low, value_low);
5890 GenerateEorConst(out_high, first_high, value_high);
5891 }
5892 }
5893 return;
5894 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005895
5896 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005897 Register first_reg = first.AsRegister<Register>();
5898 ShifterOperand second_reg(second.AsRegister<Register>());
5899 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005900 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005901 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005902 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005903 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005904 } else {
5905 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005906 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005907 }
5908 } else {
5909 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005910 Register first_low = first.AsRegisterPairLow<Register>();
5911 Register first_high = first.AsRegisterPairHigh<Register>();
5912 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
5913 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
5914 Register out_low = out.AsRegisterPairLow<Register>();
5915 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005916 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005917 __ and_(out_low, first_low, second_low);
5918 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005919 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005920 __ orr(out_low, first_low, second_low);
5921 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005922 } else {
5923 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005924 __ eor(out_low, first_low, second_low);
5925 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005926 }
5927 }
5928}
5929
Roland Levillainc9285912015-12-18 10:38:42 +00005930void InstructionCodeGeneratorARM::GenerateReferenceLoadOneRegister(HInstruction* instruction,
5931 Location out,
5932 uint32_t offset,
5933 Location temp) {
5934 Register out_reg = out.AsRegister<Register>();
5935 if (kEmitCompilerReadBarrier) {
5936 if (kUseBakerReadBarrier) {
5937 // Load with fast path based Baker's read barrier.
5938 // /* HeapReference<Object> */ out = *(out + offset)
5939 codegen_->GenerateFieldLoadWithBakerReadBarrier(
5940 instruction, out, out_reg, offset, temp, /* needs_null_check */ false);
5941 } else {
5942 // Load with slow path based read barrier.
5943 // Save the value of `out` into `temp` before overwriting it
5944 // in the following move operation, as we will need it for the
5945 // read barrier below.
5946 __ Mov(temp.AsRegister<Register>(), out_reg);
5947 // /* HeapReference<Object> */ out = *(out + offset)
5948 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
5949 codegen_->GenerateReadBarrierSlow(instruction, out, out, temp, offset);
5950 }
5951 } else {
5952 // Plain load with no read barrier.
5953 // /* HeapReference<Object> */ out = *(out + offset)
5954 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
5955 __ MaybeUnpoisonHeapReference(out_reg);
5956 }
5957}
5958
5959void InstructionCodeGeneratorARM::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
5960 Location out,
5961 Location obj,
5962 uint32_t offset,
5963 Location temp) {
5964 Register out_reg = out.AsRegister<Register>();
5965 Register obj_reg = obj.AsRegister<Register>();
5966 if (kEmitCompilerReadBarrier) {
5967 if (kUseBakerReadBarrier) {
5968 // Load with fast path based Baker's read barrier.
5969 // /* HeapReference<Object> */ out = *(obj + offset)
5970 codegen_->GenerateFieldLoadWithBakerReadBarrier(
5971 instruction, out, obj_reg, offset, temp, /* needs_null_check */ false);
5972 } else {
5973 // Load with slow path based read barrier.
5974 // /* HeapReference<Object> */ out = *(obj + offset)
5975 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
5976 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5977 }
5978 } else {
5979 // Plain load with no read barrier.
5980 // /* HeapReference<Object> */ out = *(obj + offset)
5981 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
5982 __ MaybeUnpoisonHeapReference(out_reg);
5983 }
5984}
5985
5986void InstructionCodeGeneratorARM::GenerateGcRootFieldLoad(HInstruction* instruction,
5987 Location root,
5988 Register obj,
5989 uint32_t offset) {
5990 Register root_reg = root.AsRegister<Register>();
5991 if (kEmitCompilerReadBarrier) {
5992 if (kUseBakerReadBarrier) {
5993 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
5994 // Baker's read barrier are used:
5995 //
5996 // root = obj.field;
5997 // if (Thread::Current()->GetIsGcMarking()) {
5998 // root = ReadBarrier::Mark(root)
5999 // }
6000
6001 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6002 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6003 static_assert(
6004 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6005 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6006 "have different sizes.");
6007 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6008 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6009 "have different sizes.");
6010
6011 // Slow path used to mark the GC root `root`.
6012 SlowPathCode* slow_path =
6013 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, root, root);
6014 codegen_->AddSlowPath(slow_path);
6015
6016 __ LoadFromOffset(
6017 kLoadWord, IP, TR, Thread::IsGcMarkingOffset<kArmWordSize>().Int32Value());
6018 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
6019 __ Bind(slow_path->GetExitLabel());
6020 } else {
6021 // GC root loaded through a slow path for read barriers other
6022 // than Baker's.
6023 // /* GcRoot<mirror::Object>* */ root = obj + offset
6024 __ AddConstant(root_reg, obj, offset);
6025 // /* mirror::Object* */ root = root->Read()
6026 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6027 }
6028 } else {
6029 // Plain GC root load with no read barrier.
6030 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6031 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6032 // Note that GC roots are not affected by heap poisoning, thus we
6033 // do not have to unpoison `root_reg` here.
6034 }
6035}
6036
6037void CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6038 Location ref,
6039 Register obj,
6040 uint32_t offset,
6041 Location temp,
6042 bool needs_null_check) {
6043 DCHECK(kEmitCompilerReadBarrier);
6044 DCHECK(kUseBakerReadBarrier);
6045
6046 // /* HeapReference<Object> */ ref = *(obj + offset)
6047 Location no_index = Location::NoLocation();
6048 GenerateReferenceLoadWithBakerReadBarrier(
6049 instruction, ref, obj, offset, no_index, temp, needs_null_check);
6050}
6051
6052void CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6053 Location ref,
6054 Register obj,
6055 uint32_t data_offset,
6056 Location index,
6057 Location temp,
6058 bool needs_null_check) {
6059 DCHECK(kEmitCompilerReadBarrier);
6060 DCHECK(kUseBakerReadBarrier);
6061
6062 // /* HeapReference<Object> */ ref =
6063 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6064 GenerateReferenceLoadWithBakerReadBarrier(
6065 instruction, ref, obj, data_offset, index, temp, needs_null_check);
6066}
6067
6068void CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6069 Location ref,
6070 Register obj,
6071 uint32_t offset,
6072 Location index,
6073 Location temp,
6074 bool needs_null_check) {
6075 DCHECK(kEmitCompilerReadBarrier);
6076 DCHECK(kUseBakerReadBarrier);
6077
6078 // In slow path based read barriers, the read barrier call is
6079 // inserted after the original load. However, in fast path based
6080 // Baker's read barriers, we need to perform the load of
6081 // mirror::Object::monitor_ *before* the original reference load.
6082 // This load-load ordering is required by the read barrier.
6083 // The fast path/slow path (for Baker's algorithm) should look like:
6084 //
6085 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6086 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6087 // HeapReference<Object> ref = *src; // Original reference load.
6088 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6089 // if (is_gray) {
6090 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6091 // }
6092 //
6093 // Note: the original implementation in ReadBarrier::Barrier is
6094 // slightly more complex as:
6095 // - it implements the load-load fence using a data dependency on
6096 // the high-bits of rb_state, which are expected to be all zeroes;
6097 // - it performs additional checks that we do not do here for
6098 // performance reasons.
6099
6100 Register ref_reg = ref.AsRegister<Register>();
6101 Register temp_reg = temp.AsRegister<Register>();
6102 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6103
6104 // /* int32_t */ monitor = obj->monitor_
6105 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
6106 if (needs_null_check) {
6107 MaybeRecordImplicitNullCheck(instruction);
6108 }
6109 // /* LockWord */ lock_word = LockWord(monitor)
6110 static_assert(sizeof(LockWord) == sizeof(int32_t),
6111 "art::LockWord and int32_t have different sizes.");
6112 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
6113 __ Lsr(temp_reg, temp_reg, LockWord::kReadBarrierStateShift);
6114 __ and_(temp_reg, temp_reg, ShifterOperand(LockWord::kReadBarrierStateMask));
6115 static_assert(
6116 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
6117 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
6118
6119 // Introduce a dependency on the high bits of rb_state, which shall
6120 // be all zeroes, to prevent load-load reordering, and without using
6121 // a memory barrier (which would be more expensive).
6122 // IP = rb_state & ~LockWord::kReadBarrierStateMask = 0
6123 __ bic(IP, temp_reg, ShifterOperand(LockWord::kReadBarrierStateMask));
6124 // obj is unchanged by this operation, but its value now depends on
6125 // IP, which depends on temp_reg.
6126 __ add(obj, obj, ShifterOperand(IP));
6127
6128 // The actual reference load.
6129 if (index.IsValid()) {
6130 static_assert(
6131 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6132 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
6133 // /* HeapReference<Object> */ ref =
6134 // *(obj + offset + index * sizeof(HeapReference<Object>))
6135 if (index.IsConstant()) {
6136 size_t computed_offset =
6137 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset;
6138 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
6139 } else {
6140 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
6141 __ LoadFromOffset(kLoadWord, ref_reg, IP, offset);
6142 }
6143 } else {
6144 // /* HeapReference<Object> */ ref = *(obj + offset)
6145 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
6146 }
6147
6148 // Object* ref = ref_addr->AsMirrorPtr()
6149 __ MaybeUnpoisonHeapReference(ref_reg);
6150
6151 // Slow path used to mark the object `ref` when it is gray.
6152 SlowPathCode* slow_path =
6153 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, ref, ref);
6154 AddSlowPath(slow_path);
6155
6156 // if (rb_state == ReadBarrier::gray_ptr_)
6157 // ref = ReadBarrier::Mark(ref);
6158 __ cmp(temp_reg, ShifterOperand(ReadBarrier::gray_ptr_));
6159 __ b(slow_path->GetEntryLabel(), EQ);
6160 __ Bind(slow_path->GetExitLabel());
6161}
6162
6163void CodeGeneratorARM::GenerateReadBarrierSlow(HInstruction* instruction,
6164 Location out,
6165 Location ref,
6166 Location obj,
6167 uint32_t offset,
6168 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006169 DCHECK(kEmitCompilerReadBarrier);
6170
Roland Levillainc9285912015-12-18 10:38:42 +00006171 // Insert a slow path based read barrier *after* the reference load.
6172 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006173 // If heap poisoning is enabled, the unpoisoning of the loaded
6174 // reference will be carried out by the runtime within the slow
6175 // path.
6176 //
6177 // Note that `ref` currently does not get unpoisoned (when heap
6178 // poisoning is enabled), which is alright as the `ref` argument is
6179 // not used by the artReadBarrierSlow entry point.
6180 //
6181 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6182 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6183 ReadBarrierForHeapReferenceSlowPathARM(instruction, out, ref, obj, offset, index);
6184 AddSlowPath(slow_path);
6185
Roland Levillain3b359c72015-11-17 19:35:12 +00006186 __ b(slow_path->GetEntryLabel());
6187 __ Bind(slow_path->GetExitLabel());
6188}
6189
Roland Levillainc9285912015-12-18 10:38:42 +00006190void CodeGeneratorARM::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6191 Location out,
6192 Location ref,
6193 Location obj,
6194 uint32_t offset,
6195 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006196 if (kEmitCompilerReadBarrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00006197 // Baker's read barriers shall be handled by the fast path
6198 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
6199 DCHECK(!kUseBakerReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00006200 // If heap poisoning is enabled, unpoisoning will be taken care of
6201 // by the runtime within the slow path.
Roland Levillainc9285912015-12-18 10:38:42 +00006202 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain3b359c72015-11-17 19:35:12 +00006203 } else if (kPoisonHeapReferences) {
6204 __ UnpoisonHeapReference(out.AsRegister<Register>());
6205 }
6206}
6207
Roland Levillainc9285912015-12-18 10:38:42 +00006208void CodeGeneratorARM::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6209 Location out,
6210 Location root) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006211 DCHECK(kEmitCompilerReadBarrier);
6212
Roland Levillainc9285912015-12-18 10:38:42 +00006213 // Insert a slow path based read barrier *after* the GC root load.
6214 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006215 // Note that GC roots are not affected by heap poisoning, so we do
6216 // not need to do anything special for this here.
6217 SlowPathCode* slow_path =
6218 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM(instruction, out, root);
6219 AddSlowPath(slow_path);
6220
Roland Levillain3b359c72015-11-17 19:35:12 +00006221 __ b(slow_path->GetEntryLabel());
6222 __ Bind(slow_path->GetExitLabel());
6223}
6224
Vladimir Markodc151b22015-10-15 18:02:30 +01006225HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch(
6226 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
6227 MethodReference target_method) {
Vladimir Markodc151b22015-10-15 18:02:30 +01006228 if (desired_dispatch_info.code_ptr_location ==
6229 HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative) {
6230 const DexFile& outer_dex_file = GetGraph()->GetDexFile();
6231 if (&outer_dex_file != target_method.dex_file) {
6232 // Calls across dex files are more likely to exceed the available BL range,
6233 // so use absolute patch with fixup if available and kCallArtMethod otherwise.
6234 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location =
6235 (desired_dispatch_info.method_load_kind ==
6236 HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup)
6237 ? HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup
6238 : HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
6239 return HInvokeStaticOrDirect::DispatchInfo {
6240 desired_dispatch_info.method_load_kind,
6241 code_ptr_location,
6242 desired_dispatch_info.method_load_data,
6243 0u
6244 };
6245 }
6246 }
6247 return desired_dispatch_info;
6248}
6249
Vladimir Markob4536b72015-11-24 13:45:23 +00006250Register CodeGeneratorARM::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
6251 Register temp) {
6252 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
6253 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
6254 if (!invoke->GetLocations()->Intrinsified()) {
6255 return location.AsRegister<Register>();
6256 }
6257 // For intrinsics we allow any location, so it may be on the stack.
6258 if (!location.IsRegister()) {
6259 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
6260 return temp;
6261 }
6262 // For register locations, check if the register was saved. If so, get it from the stack.
6263 // Note: There is a chance that the register was saved but not overwritten, so we could
6264 // save one load. However, since this is just an intrinsic slow path we prefer this
6265 // simple and more robust approach rather that trying to determine if that's the case.
6266 SlowPathCode* slow_path = GetCurrentSlowPath();
6267 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
6268 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
6269 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
6270 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
6271 return temp;
6272 }
6273 return location.AsRegister<Register>();
6274}
6275
Nicolas Geoffray38207af2015-06-01 15:46:22 +01006276void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00006277 // For better instruction scheduling we load the direct code pointer before the method pointer.
Vladimir Marko58155012015-08-19 12:49:41 +00006278 switch (invoke->GetCodePtrLocation()) {
Vladimir Marko58155012015-08-19 12:49:41 +00006279 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
6280 // LR = code address from literal pool with link-time patch.
6281 __ LoadLiteral(LR, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
Vladimir Marko58155012015-08-19 12:49:41 +00006282 break;
6283 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
6284 // LR = invoke->GetDirectCodePtr();
6285 __ LoadImmediate(LR, invoke->GetDirectCodePtr());
Vladimir Marko58155012015-08-19 12:49:41 +00006286 break;
6287 default:
6288 break;
6289 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006290
Vladimir Marko58155012015-08-19 12:49:41 +00006291 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
6292 switch (invoke->GetMethodLoadKind()) {
6293 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
6294 // temp = thread->string_init_entrypoint
6295 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, invoke->GetStringInitOffset());
6296 break;
6297 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00006298 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006299 break;
6300 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
6301 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
6302 break;
6303 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
6304 __ LoadLiteral(temp.AsRegister<Register>(),
6305 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
6306 break;
Vladimir Markob4536b72015-11-24 13:45:23 +00006307 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
6308 HArmDexCacheArraysBase* base =
6309 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
6310 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
6311 temp.AsRegister<Register>());
6312 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
6313 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
6314 break;
6315 }
Vladimir Marko58155012015-08-19 12:49:41 +00006316 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00006317 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006318 Register method_reg;
6319 Register reg = temp.AsRegister<Register>();
6320 if (current_method.IsRegister()) {
6321 method_reg = current_method.AsRegister<Register>();
6322 } else {
6323 DCHECK(invoke->GetLocations()->Intrinsified());
6324 DCHECK(!current_method.IsValid());
6325 method_reg = reg;
6326 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
6327 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006328 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
6329 __ LoadFromOffset(kLoadWord,
6330 reg,
6331 method_reg,
6332 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00006333 // temp = temp[index_in_cache]
6334 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
6335 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
6336 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01006337 }
Vladimir Marko58155012015-08-19 12:49:41 +00006338 }
6339
6340 switch (invoke->GetCodePtrLocation()) {
6341 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
6342 __ bl(GetFrameEntryLabel());
6343 break;
6344 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01006345 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006346 __ BindTrackedLabel(&relative_call_patches_.back().label);
Vladimir Markodc151b22015-10-15 18:02:30 +01006347 // Arbitrarily branch to the BL itself, override at link time.
6348 __ bl(&relative_call_patches_.back().label);
6349 break;
Vladimir Marko58155012015-08-19 12:49:41 +00006350 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
6351 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
6352 // LR prepared above for better instruction scheduling.
Vladimir Marko58155012015-08-19 12:49:41 +00006353 // LR()
6354 __ blx(LR);
6355 break;
6356 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
6357 // LR = callee_method->entry_point_from_quick_compiled_code_
6358 __ LoadFromOffset(
6359 kLoadWord, LR, callee_method.AsRegister<Register>(),
6360 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize).Int32Value());
6361 // LR()
6362 __ blx(LR);
6363 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006364 }
6365
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006366 DCHECK(!IsLeafMethod());
6367}
6368
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006369void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
6370 Register temp = temp_location.AsRegister<Register>();
6371 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
6372 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006373
6374 // Use the calling convention instead of the location of the receiver, as
6375 // intrinsics may have put the receiver in a different register. In the intrinsics
6376 // slow path, the arguments have been moved to the right place, so here we are
6377 // guaranteed that the receiver is the first register of the calling convention.
6378 InvokeDexCallingConvention calling_convention;
6379 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006380 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00006381 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006382 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006383 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00006384 // Instead of simply (possibly) unpoisoning `temp` here, we should
6385 // emit a read barrier for the previous class reference load.
6386 // However this is not required in practice, as this is an
6387 // intermediate/temporary reference and because the current
6388 // concurrent copying collector keeps the from-space memory
6389 // intact/accessible until the end of the marking phase (the
6390 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006391 __ MaybeUnpoisonHeapReference(temp);
6392 // temp = temp->GetMethodAt(method_offset);
6393 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
6394 kArmWordSize).Int32Value();
6395 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
6396 // LR = temp->GetEntryPoint();
6397 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
6398 // LR();
6399 __ blx(LR);
6400}
6401
Vladimir Marko58155012015-08-19 12:49:41 +00006402void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
6403 DCHECK(linker_patches->empty());
Vladimir Markob4536b72015-11-24 13:45:23 +00006404 size_t size =
6405 method_patches_.size() +
6406 call_patches_.size() +
6407 relative_call_patches_.size() +
6408 /* MOVW+MOVT for each base */ 2u * dex_cache_arrays_base_labels_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00006409 linker_patches->reserve(size);
6410 for (const auto& entry : method_patches_) {
6411 const MethodReference& target_method = entry.first;
6412 Literal* literal = entry.second;
6413 DCHECK(literal->GetLabel()->IsBound());
6414 uint32_t literal_offset = literal->GetLabel()->Position();
6415 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
6416 target_method.dex_file,
6417 target_method.dex_method_index));
6418 }
6419 for (const auto& entry : call_patches_) {
6420 const MethodReference& target_method = entry.first;
6421 Literal* literal = entry.second;
6422 DCHECK(literal->GetLabel()->IsBound());
6423 uint32_t literal_offset = literal->GetLabel()->Position();
6424 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
6425 target_method.dex_file,
6426 target_method.dex_method_index));
6427 }
6428 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
6429 uint32_t literal_offset = info.label.Position();
6430 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
6431 info.target_method.dex_file,
6432 info.target_method.dex_method_index));
6433 }
Vladimir Markob4536b72015-11-24 13:45:23 +00006434 for (const auto& pair : dex_cache_arrays_base_labels_) {
6435 HArmDexCacheArraysBase* base = pair.first;
6436 const DexCacheArraysBaseLabels* labels = &pair.second;
6437 const DexFile& dex_file = base->GetDexFile();
6438 size_t base_element_offset = base->GetElementOffset();
6439 DCHECK(labels->add_pc_label.IsBound());
6440 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(labels->add_pc_label.Position());
6441 // Add MOVW patch.
6442 DCHECK(labels->movw_label.IsBound());
6443 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(labels->movw_label.Position());
6444 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(movw_offset,
6445 &dex_file,
6446 add_pc_offset,
6447 base_element_offset));
6448 // Add MOVT patch.
6449 DCHECK(labels->movt_label.IsBound());
6450 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(labels->movt_label.Position());
6451 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(movt_offset,
6452 &dex_file,
6453 add_pc_offset,
6454 base_element_offset));
6455 }
Vladimir Marko58155012015-08-19 12:49:41 +00006456}
6457
6458Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
6459 MethodToLiteralMap* map) {
6460 // Look up the literal for target_method.
6461 auto lb = map->lower_bound(target_method);
6462 if (lb != map->end() && !map->key_comp()(target_method, lb->first)) {
6463 return lb->second;
6464 }
6465 // We don't have a literal for this method yet, insert a new one.
6466 Literal* literal = __ NewLiteral<uint32_t>(0u);
6467 map->PutBefore(lb, target_method, literal);
6468 return literal;
6469}
6470
6471Literal* CodeGeneratorARM::DeduplicateMethodAddressLiteral(MethodReference target_method) {
6472 return DeduplicateMethodLiteral(target_method, &method_patches_);
6473}
6474
6475Literal* CodeGeneratorARM::DeduplicateMethodCodeLiteral(MethodReference target_method) {
6476 return DeduplicateMethodLiteral(target_method, &call_patches_);
6477}
6478
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006479void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006480 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006481 LOG(FATAL) << "Unreachable";
6482}
6483
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006484void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006485 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006486 LOG(FATAL) << "Unreachable";
6487}
6488
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01006489void LocationsBuilderARM::VisitFakeString(HFakeString* instruction) {
6490 DCHECK(codegen_->IsBaseline());
6491 LocationSummary* locations =
6492 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6493 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
6494}
6495
6496void InstructionCodeGeneratorARM::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
6497 DCHECK(codegen_->IsBaseline());
6498 // Will be generated at use site.
6499}
6500
Mark Mendellfe57faa2015-09-18 09:26:15 -04006501// Simple implementation of packed switch - generate cascaded compare/jumps.
6502void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6503 LocationSummary* locations =
6504 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6505 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006506 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006507 codegen_->GetAssembler()->IsThumb()) {
6508 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
6509 if (switch_instr->GetStartValue() != 0) {
6510 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
6511 }
6512 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04006513}
6514
6515void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6516 int32_t lower_bound = switch_instr->GetStartValue();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006517 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006518 LocationSummary* locations = switch_instr->GetLocations();
6519 Register value_reg = locations->InAt(0).AsRegister<Register>();
6520 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6521
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006522 if (num_entries <= kPackedSwitchCompareJumpThreshold || !codegen_->GetAssembler()->IsThumb()) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006523 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006524 Register temp_reg = IP;
6525 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
6526 // the immediate, because IP is used as the destination register. For the other
6527 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
6528 // and they can be encoded in the instruction without making use of IP register.
6529 __ AddConstantSetFlags(temp_reg, value_reg, -lower_bound);
6530
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006531 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006532 // Jump to successors[0] if value == lower_bound.
6533 __ b(codegen_->GetLabelOf(successors[0]), EQ);
6534 int32_t last_index = 0;
6535 for (; num_entries - last_index > 2; last_index += 2) {
6536 __ AddConstantSetFlags(temp_reg, temp_reg, -2);
6537 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6538 __ b(codegen_->GetLabelOf(successors[last_index + 1]), LO);
6539 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6540 __ b(codegen_->GetLabelOf(successors[last_index + 2]), EQ);
6541 }
6542 if (num_entries - last_index == 2) {
6543 // The last missing case_value.
Vladimir Markoac6ac102015-12-17 12:14:00 +00006544 __ CmpConstant(temp_reg, 1);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006545 __ b(codegen_->GetLabelOf(successors[last_index + 1]), EQ);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006546 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04006547
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006548 // And the default for any other value.
6549 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6550 __ b(codegen_->GetLabelOf(default_block));
6551 }
6552 } else {
6553 // Create a table lookup.
6554 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
6555
6556 // Materialize a pointer to the switch table
6557 std::vector<Label*> labels(num_entries);
6558 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6559 for (uint32_t i = 0; i < num_entries; i++) {
6560 labels[i] = codegen_->GetLabelOf(successors[i]);
6561 }
6562 JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg);
6563
6564 // Remove the bias.
6565 Register key_reg;
6566 if (lower_bound != 0) {
6567 key_reg = locations->GetTemp(1).AsRegister<Register>();
6568 __ AddConstant(key_reg, value_reg, -lower_bound);
6569 } else {
6570 key_reg = value_reg;
6571 }
6572
6573 // Check whether the value is in the table, jump to default block if not.
6574 __ CmpConstant(key_reg, num_entries - 1);
6575 __ b(codegen_->GetLabelOf(default_block), Condition::HI);
6576
6577 // Load the displacement from the table.
6578 __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2));
6579
6580 // Dispatch is a direct add to the PC (for Thumb2).
6581 __ EmitJumpTableDispatch(table, temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006582 }
6583}
6584
Vladimir Markob4536b72015-11-24 13:45:23 +00006585void LocationsBuilderARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
6586 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
6587 locations->SetOut(Location::RequiresRegister());
6588 codegen_->AddDexCacheArraysBase(base);
6589}
6590
6591void InstructionCodeGeneratorARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
6592 Register base_reg = base->GetLocations()->Out().AsRegister<Register>();
6593 CodeGeneratorARM::DexCacheArraysBaseLabels* labels = codegen_->GetDexCacheArraysBaseLabels(base);
6594 __ BindTrackedLabel(&labels->movw_label);
6595 __ movw(base_reg, 0u);
6596 __ BindTrackedLabel(&labels->movt_label);
6597 __ movt(base_reg, 0u);
6598 __ BindTrackedLabel(&labels->add_pc_label);
6599 __ add(base_reg, base_reg, ShifterOperand(PC));
6600}
6601
Andreas Gampe85b62f22015-09-09 13:15:38 -07006602void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
6603 if (!trg.IsValid()) {
Roland Levillainc9285912015-12-18 10:38:42 +00006604 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006605 return;
6606 }
6607
6608 DCHECK_NE(type, Primitive::kPrimVoid);
6609
6610 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
6611 if (return_loc.Equals(trg)) {
6612 return;
6613 }
6614
6615 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
6616 // with the last branch.
6617 if (type == Primitive::kPrimLong) {
6618 HParallelMove parallel_move(GetGraph()->GetArena());
6619 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
6620 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
6621 GetMoveResolver()->EmitNativeCode(&parallel_move);
6622 } else if (type == Primitive::kPrimDouble) {
6623 HParallelMove parallel_move(GetGraph()->GetArena());
6624 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
6625 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
6626 GetMoveResolver()->EmitNativeCode(&parallel_move);
6627 } else {
6628 // Let the parallel move resolver take care of all of this.
6629 HParallelMove parallel_move(GetGraph()->GetArena());
6630 parallel_move.AddMove(return_loc, trg, type, nullptr);
6631 GetMoveResolver()->EmitNativeCode(&parallel_move);
6632 }
6633}
6634
Roland Levillain4d027112015-07-01 15:41:14 +01006635#undef __
6636#undef QUICK_ENTRY_POINT
6637
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006638} // namespace arm
6639} // namespace art