blob: 731ee3918156673c5a09c8d9b5e882a10d549c0c [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000018
Calin Juravle34166012014-12-19 17:22:29 +000019#include "arch/arm/instruction_set_features_arm.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "art_method.h"
Zheng Xuc6667102015-05-15 16:08:45 +080021#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000022#include "compiled_method.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070023#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010024#include "gc/accounting/card_table.h"
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -080025#include "intrinsics.h"
26#include "intrinsics_arm.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070027#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070028#include "mirror/class-inl.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070029#include "thread.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010030#include "utils/arm/assembler_arm.h"
31#include "utils/arm/managed_register_arm.h"
Roland Levillain946e1432014-11-11 17:35:19 +000032#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010033#include "utils/stack_checks.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000034
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000035namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010036
Roland Levillain3b359c72015-11-17 19:35:12 +000037template<class MirrorType>
38class GcRoot;
39
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000040namespace arm {
41
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +000042static bool ExpectedPairLayout(Location location) {
43 // We expected this for both core and fpu register pairs.
44 return ((location.low() & 1) == 0) && (location.low() + 1 == location.high());
45}
46
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010047static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010048static constexpr Register kMethodRegisterArgument = R0;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010049
David Brazdil58282f42016-01-14 12:45:10 +000050static constexpr Register kCoreAlwaysSpillRegister = R5;
Nicolas Geoffray4dee6362015-01-23 18:23:14 +000051static constexpr Register kCoreCalleeSaves[] =
Andreas Gampe501fd632015-09-10 16:11:06 -070052 { R5, R6, R7, R8, R10, R11, LR };
Nicolas Geoffray4dee6362015-01-23 18:23:14 +000053static constexpr SRegister kFpuCalleeSaves[] =
54 { S16, S17, S18, S19, S20, S21, S22, S23, S24, S25, S26, S27, S28, S29, S30, S31 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010055
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +000056// D31 cannot be split into two S registers, and the register allocator only works on
57// S registers. Therefore there is no need to block it.
58static constexpr DRegister DTMP = D31;
59
Vladimir Markof3e0ee22015-12-17 15:23:13 +000060static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Andreas Gampe7cffc3b2015-10-19 21:31:53 -070061
Roland Levillain62a46b22015-06-01 18:24:13 +010062#define __ down_cast<ArmAssembler*>(codegen->GetAssembler())->
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010063#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmWordSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010064
Andreas Gampe85b62f22015-09-09 13:15:38 -070065class NullCheckSlowPathARM : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010066 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000067 explicit NullCheckSlowPathARM(HNullCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010068
Alexandre Rames67555f72014-11-18 10:55:16 +000069 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010070 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010071 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000072 if (instruction_->CanThrowIntoCatchBlock()) {
73 // Live registers will be restored in the catch block if caught.
74 SaveLiveRegisters(codegen, instruction_->GetLocations());
75 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010076 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +000077 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +000078 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +010079 }
80
Alexandre Rames8158f282015-08-07 10:26:17 +010081 bool IsFatal() const OVERRIDE { return true; }
82
Alexandre Rames9931f312015-06-19 14:47:01 +010083 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM"; }
84
Nicolas Geoffraye5038322014-07-04 09:41:32 +010085 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +010086 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM);
87};
88
Andreas Gampe85b62f22015-09-09 13:15:38 -070089class DivZeroCheckSlowPathARM : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000090 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000091 explicit DivZeroCheckSlowPathARM(HDivZeroCheck* instruction) : SlowPathCode(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +000092
Alexandre Rames67555f72014-11-18 10:55:16 +000093 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000094 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
95 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000096 if (instruction_->CanThrowIntoCatchBlock()) {
97 // Live registers will be restored in the catch block if caught.
98 SaveLiveRegisters(codegen, instruction_->GetLocations());
99 }
Calin Juravled0d48522014-11-04 16:40:20 +0000100 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000101 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000102 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +0000103 }
104
Alexandre Rames8158f282015-08-07 10:26:17 +0100105 bool IsFatal() const OVERRIDE { return true; }
106
Alexandre Rames9931f312015-06-19 14:47:01 +0100107 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM"; }
108
Calin Juravled0d48522014-11-04 16:40:20 +0000109 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000110 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM);
111};
112
Andreas Gampe85b62f22015-09-09 13:15:38 -0700113class SuspendCheckSlowPathARM : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000114 public:
Alexandre Rames67555f72014-11-18 10:55:16 +0000115 SuspendCheckSlowPathARM(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000116 : SlowPathCode(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000117
Alexandre Rames67555f72014-11-18 10:55:16 +0000118 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100119 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000120 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000121 SaveLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100122 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000123 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000124 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000125 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100126 if (successor_ == nullptr) {
127 __ b(GetReturnLabel());
128 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100129 __ b(arm_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100130 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000131 }
132
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100133 Label* GetReturnLabel() {
134 DCHECK(successor_ == nullptr);
135 return &return_label_;
136 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000137
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100138 HBasicBlock* GetSuccessor() const {
139 return successor_;
140 }
141
Alexandre Rames9931f312015-06-19 14:47:01 +0100142 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM"; }
143
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000144 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100145 // If not null, the block to branch to after the suspend check.
146 HBasicBlock* const successor_;
147
148 // If `successor_` is null, the label to branch to after the suspend check.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000149 Label return_label_;
150
151 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM);
152};
153
Andreas Gampe85b62f22015-09-09 13:15:38 -0700154class BoundsCheckSlowPathARM : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100155 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100156 explicit BoundsCheckSlowPathARM(HBoundsCheck* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000157 : SlowPathCode(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100158
Alexandre Rames67555f72014-11-18 10:55:16 +0000159 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100160 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100161 LocationSummary* locations = instruction_->GetLocations();
162
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100163 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000164 if (instruction_->CanThrowIntoCatchBlock()) {
165 // Live registers will be restored in the catch block if caught.
166 SaveLiveRegisters(codegen, instruction_->GetLocations());
167 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000168 // We're moving two locations to locations that could overlap, so we need a parallel
169 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100170 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000171 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100172 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000173 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100174 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100175 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100176 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
177 Primitive::kPrimInt);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100178 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000179 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000180 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100181 }
182
Alexandre Rames8158f282015-08-07 10:26:17 +0100183 bool IsFatal() const OVERRIDE { return true; }
184
Alexandre Rames9931f312015-06-19 14:47:01 +0100185 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM"; }
186
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100187 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100188 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM);
189};
190
Andreas Gampe85b62f22015-09-09 13:15:38 -0700191class LoadClassSlowPathARM : public SlowPathCode {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100192 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000193 LoadClassSlowPathARM(HLoadClass* cls,
194 HInstruction* at,
195 uint32_t dex_pc,
196 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000197 : SlowPathCode(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000198 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
199 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100200
Alexandre Rames67555f72014-11-18 10:55:16 +0000201 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000202 LocationSummary* locations = at_->GetLocations();
203
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100204 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
205 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000206 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100207
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100208 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000209 __ LoadImmediate(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000210 int32_t entry_point_offset = do_clinit_
211 ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
212 : QUICK_ENTRY_POINT(pInitializeType);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000213 arm_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000214 if (do_clinit_) {
215 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
216 } else {
217 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
218 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000219
220 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000221 Location out = locations->Out();
222 if (out.IsValid()) {
223 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000224 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
225 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000226 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100227 __ b(GetExitLabel());
228 }
229
Alexandre Rames9931f312015-06-19 14:47:01 +0100230 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM"; }
231
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100232 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000233 // The class this slow path will load.
234 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100235
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000236 // The instruction where this slow path is happening.
237 // (Might be the load class or an initialization check).
238 HInstruction* const at_;
239
240 // The dex PC of `at_`.
241 const uint32_t dex_pc_;
242
243 // Whether to initialize the class.
244 const bool do_clinit_;
245
246 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100247};
248
Andreas Gampe85b62f22015-09-09 13:15:38 -0700249class LoadStringSlowPathARM : public SlowPathCode {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000250 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000251 explicit LoadStringSlowPathARM(HLoadString* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000252
Alexandre Rames67555f72014-11-18 10:55:16 +0000253 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000254 LocationSummary* locations = instruction_->GetLocations();
255 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
256
257 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
258 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000259 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000260
261 InvokeRuntimeCallingConvention calling_convention;
David Srbecky9cd6d372016-02-09 15:24:47 +0000262 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
263 __ LoadImmediate(calling_convention.GetRegisterAt(0), string_index);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000264 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000265 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000266 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000267 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
268
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000269 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000270 __ b(GetExitLabel());
271 }
272
Alexandre Rames9931f312015-06-19 14:47:01 +0100273 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM"; }
274
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000275 private:
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000276 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM);
277};
278
Andreas Gampe85b62f22015-09-09 13:15:38 -0700279class TypeCheckSlowPathARM : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000280 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000281 TypeCheckSlowPathARM(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000282 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000283
Alexandre Rames67555f72014-11-18 10:55:16 +0000284 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000285 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100286 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
287 : locations->Out();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000288 DCHECK(instruction_->IsCheckCast()
289 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000290
291 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
292 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000293
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000294 if (!is_fatal_) {
295 SaveLiveRegisters(codegen, locations);
296 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000297
298 // We're moving two locations to locations that could overlap, so we need a parallel
299 // move resolver.
300 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000301 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100302 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000303 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100304 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100305 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100306 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
307 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000308
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000309 if (instruction_->IsInstanceOf()) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100310 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
311 instruction_,
312 instruction_->GetDexPc(),
313 this);
Roland Levillain3b359c72015-11-17 19:35:12 +0000314 CheckEntrypointTypes<
315 kQuickInstanceofNonTrivial, uint32_t, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000316 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
317 } else {
318 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100319 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
320 instruction_,
321 instruction_->GetDexPc(),
322 this);
Roland Levillain3b359c72015-11-17 19:35:12 +0000323 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000324 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000325
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000326 if (!is_fatal_) {
327 RestoreLiveRegisters(codegen, locations);
328 __ b(GetExitLabel());
329 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000330 }
331
Alexandre Rames9931f312015-06-19 14:47:01 +0100332 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM"; }
333
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000334 bool IsFatal() const OVERRIDE { return is_fatal_; }
335
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000336 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000337 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000338
339 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM);
340};
341
Andreas Gampe85b62f22015-09-09 13:15:38 -0700342class DeoptimizationSlowPathARM : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700343 public:
Aart Bik42249c32016-01-07 15:33:50 -0800344 explicit DeoptimizationSlowPathARM(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000345 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700346
347 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800348 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700349 __ Bind(GetEntryLabel());
350 SaveLiveRegisters(codegen, instruction_->GetLocations());
Aart Bik42249c32016-01-07 15:33:50 -0800351 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
352 instruction_,
353 instruction_->GetDexPc(),
354 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000355 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700356 }
357
Alexandre Rames9931f312015-06-19 14:47:01 +0100358 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM"; }
359
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700360 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700361 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM);
362};
363
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100364class ArraySetSlowPathARM : public SlowPathCode {
365 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000366 explicit ArraySetSlowPathARM(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100367
368 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
369 LocationSummary* locations = instruction_->GetLocations();
370 __ Bind(GetEntryLabel());
371 SaveLiveRegisters(codegen, locations);
372
373 InvokeRuntimeCallingConvention calling_convention;
374 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
375 parallel_move.AddMove(
376 locations->InAt(0),
377 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
378 Primitive::kPrimNot,
379 nullptr);
380 parallel_move.AddMove(
381 locations->InAt(1),
382 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
383 Primitive::kPrimInt,
384 nullptr);
385 parallel_move.AddMove(
386 locations->InAt(2),
387 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
388 Primitive::kPrimNot,
389 nullptr);
390 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
391
392 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
393 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
394 instruction_,
395 instruction_->GetDexPc(),
396 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000397 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100398 RestoreLiveRegisters(codegen, locations);
399 __ b(GetExitLabel());
400 }
401
402 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM"; }
403
404 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100405 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM);
406};
407
Roland Levillainc9285912015-12-18 10:38:42 +0000408// Slow path marking an object during a read barrier.
409class ReadBarrierMarkSlowPathARM : public SlowPathCode {
410 public:
411 ReadBarrierMarkSlowPathARM(HInstruction* instruction, Location out, Location obj)
David Srbecky9cd6d372016-02-09 15:24:47 +0000412 : SlowPathCode(instruction), out_(out), obj_(obj) {
Roland Levillainc9285912015-12-18 10:38:42 +0000413 DCHECK(kEmitCompilerReadBarrier);
414 }
415
416 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM"; }
417
418 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
419 LocationSummary* locations = instruction_->GetLocations();
420 Register reg_out = out_.AsRegister<Register>();
421 DCHECK(locations->CanCall());
422 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
423 DCHECK(instruction_->IsInstanceFieldGet() ||
424 instruction_->IsStaticFieldGet() ||
425 instruction_->IsArrayGet() ||
426 instruction_->IsLoadClass() ||
427 instruction_->IsLoadString() ||
428 instruction_->IsInstanceOf() ||
Roland Levillainc0cdb0b2016-06-23 13:53:42 +0100429 instruction_->IsCheckCast() ||
430 ((instruction_->IsInvokeStaticOrDirect() || instruction_->IsInvokeVirtual()) &&
431 instruction_->GetLocations()->Intrinsified()))
Roland Levillainc9285912015-12-18 10:38:42 +0000432 << "Unexpected instruction in read barrier marking slow path: "
433 << instruction_->DebugName();
434
435 __ Bind(GetEntryLabel());
436 SaveLiveRegisters(codegen, locations);
437
438 InvokeRuntimeCallingConvention calling_convention;
439 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
440 arm_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), obj_);
441 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierMark),
442 instruction_,
443 instruction_->GetDexPc(),
444 this);
445 CheckEntrypointTypes<kQuickReadBarrierMark, mirror::Object*, mirror::Object*>();
446 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
447
448 RestoreLiveRegisters(codegen, locations);
449 __ b(GetExitLabel());
450 }
451
452 private:
Roland Levillainc9285912015-12-18 10:38:42 +0000453 const Location out_;
454 const Location obj_;
455
456 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM);
457};
458
Roland Levillain3b359c72015-11-17 19:35:12 +0000459// Slow path generating a read barrier for a heap reference.
460class ReadBarrierForHeapReferenceSlowPathARM : public SlowPathCode {
461 public:
462 ReadBarrierForHeapReferenceSlowPathARM(HInstruction* instruction,
463 Location out,
464 Location ref,
465 Location obj,
466 uint32_t offset,
467 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000468 : SlowPathCode(instruction),
Roland Levillain3b359c72015-11-17 19:35:12 +0000469 out_(out),
470 ref_(ref),
471 obj_(obj),
472 offset_(offset),
473 index_(index) {
474 DCHECK(kEmitCompilerReadBarrier);
475 // If `obj` is equal to `out` or `ref`, it means the initial object
476 // has been overwritten by (or after) the heap object reference load
477 // to be instrumented, e.g.:
478 //
479 // __ LoadFromOffset(kLoadWord, out, out, offset);
Roland Levillainc9285912015-12-18 10:38:42 +0000480 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain3b359c72015-11-17 19:35:12 +0000481 //
482 // In that case, we have lost the information about the original
483 // object, and the emitted read barrier cannot work properly.
484 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
485 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
486 }
487
488 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
489 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
490 LocationSummary* locations = instruction_->GetLocations();
491 Register reg_out = out_.AsRegister<Register>();
492 DCHECK(locations->CanCall());
493 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillainc0cdb0b2016-06-23 13:53:42 +0100494 DCHECK(instruction_->IsInstanceFieldGet() ||
495 instruction_->IsStaticFieldGet() ||
496 instruction_->IsArrayGet() ||
497 instruction_->IsInstanceOf() ||
498 instruction_->IsCheckCast() ||
499 ((instruction_->IsInvokeStaticOrDirect() || instruction_->IsInvokeVirtual()) &&
Roland Levillainc9285912015-12-18 10:38:42 +0000500 instruction_->GetLocations()->Intrinsified()))
501 << "Unexpected instruction in read barrier for heap reference slow path: "
502 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000503
504 __ Bind(GetEntryLabel());
505 SaveLiveRegisters(codegen, locations);
506
507 // We may have to change the index's value, but as `index_` is a
508 // constant member (like other "inputs" of this slow path),
509 // introduce a copy of it, `index`.
510 Location index = index_;
511 if (index_.IsValid()) {
Roland Levillainc0cdb0b2016-06-23 13:53:42 +0100512 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain3b359c72015-11-17 19:35:12 +0000513 if (instruction_->IsArrayGet()) {
514 // Compute the actual memory offset and store it in `index`.
515 Register index_reg = index_.AsRegister<Register>();
516 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
517 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
518 // We are about to change the value of `index_reg` (see the
519 // calls to art::arm::Thumb2Assembler::Lsl and
520 // art::arm::Thumb2Assembler::AddConstant below), but it has
521 // not been saved by the previous call to
522 // art::SlowPathCode::SaveLiveRegisters, as it is a
523 // callee-save register --
524 // art::SlowPathCode::SaveLiveRegisters does not consider
525 // callee-save registers, as it has been designed with the
526 // assumption that callee-save registers are supposed to be
527 // handled by the called function. So, as a callee-save
528 // register, `index_reg` _would_ eventually be saved onto
529 // the stack, but it would be too late: we would have
530 // changed its value earlier. Therefore, we manually save
531 // it here into another freely available register,
532 // `free_reg`, chosen of course among the caller-save
533 // registers (as a callee-save `free_reg` register would
534 // exhibit the same problem).
535 //
536 // Note we could have requested a temporary register from
537 // the register allocator instead; but we prefer not to, as
538 // this is a slow path, and we know we can find a
539 // caller-save register that is available.
540 Register free_reg = FindAvailableCallerSaveRegister(codegen);
541 __ Mov(free_reg, index_reg);
542 index_reg = free_reg;
543 index = Location::RegisterLocation(index_reg);
544 } else {
545 // The initial register stored in `index_` has already been
546 // saved in the call to art::SlowPathCode::SaveLiveRegisters
547 // (as it is not a callee-save register), so we can freely
548 // use it.
549 }
550 // Shifting the index value contained in `index_reg` by the scale
551 // factor (2) cannot overflow in practice, as the runtime is
552 // unable to allocate object arrays with a size larger than
553 // 2^26 - 1 (that is, 2^28 - 4 bytes).
554 __ Lsl(index_reg, index_reg, TIMES_4);
555 static_assert(
556 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
557 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
558 __ AddConstant(index_reg, index_reg, offset_);
559 } else {
Roland Levillainc0cdb0b2016-06-23 13:53:42 +0100560 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
561 // intrinsics, `index_` is not shifted by a scale factor of 2
562 // (as in the case of ArrayGet), as it is actually an offset
563 // to an object field within an object.
564 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000565 DCHECK(instruction_->GetLocations()->Intrinsified());
566 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
567 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
568 << instruction_->AsInvoke()->GetIntrinsic();
569 DCHECK_EQ(offset_, 0U);
570 DCHECK(index_.IsRegisterPair());
571 // UnsafeGet's offset location is a register pair, the low
572 // part contains the correct offset.
573 index = index_.ToLow();
574 }
575 }
576
577 // We're moving two or three locations to locations that could
578 // overlap, so we need a parallel move resolver.
579 InvokeRuntimeCallingConvention calling_convention;
580 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
581 parallel_move.AddMove(ref_,
582 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
583 Primitive::kPrimNot,
584 nullptr);
585 parallel_move.AddMove(obj_,
586 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
587 Primitive::kPrimNot,
588 nullptr);
589 if (index.IsValid()) {
590 parallel_move.AddMove(index,
591 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
592 Primitive::kPrimInt,
593 nullptr);
594 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
595 } else {
596 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
597 __ LoadImmediate(calling_convention.GetRegisterAt(2), offset_);
598 }
599 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
600 instruction_,
601 instruction_->GetDexPc(),
602 this);
603 CheckEntrypointTypes<
604 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
605 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
606
607 RestoreLiveRegisters(codegen, locations);
608 __ b(GetExitLabel());
609 }
610
611 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM"; }
612
613 private:
614 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
615 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
616 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
617 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
618 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
619 return static_cast<Register>(i);
620 }
621 }
622 // We shall never fail to find a free caller-save register, as
623 // there are more than two core caller-save registers on ARM
624 // (meaning it is possible to find one which is different from
625 // `ref` and `obj`).
626 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
627 LOG(FATAL) << "Could not find a free caller-save register";
628 UNREACHABLE();
629 }
630
Roland Levillain3b359c72015-11-17 19:35:12 +0000631 const Location out_;
632 const Location ref_;
633 const Location obj_;
634 const uint32_t offset_;
635 // An additional location containing an index to an array.
636 // Only used for HArrayGet and the UnsafeGetObject &
637 // UnsafeGetObjectVolatile intrinsics.
638 const Location index_;
639
640 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM);
641};
642
643// Slow path generating a read barrier for a GC root.
644class ReadBarrierForRootSlowPathARM : public SlowPathCode {
645 public:
646 ReadBarrierForRootSlowPathARM(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000647 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillainc9285912015-12-18 10:38:42 +0000648 DCHECK(kEmitCompilerReadBarrier);
649 }
Roland Levillain3b359c72015-11-17 19:35:12 +0000650
651 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
652 LocationSummary* locations = instruction_->GetLocations();
653 Register reg_out = out_.AsRegister<Register>();
654 DCHECK(locations->CanCall());
655 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillainc9285912015-12-18 10:38:42 +0000656 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
657 << "Unexpected instruction in read barrier for GC root slow path: "
658 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000659
660 __ Bind(GetEntryLabel());
661 SaveLiveRegisters(codegen, locations);
662
663 InvokeRuntimeCallingConvention calling_convention;
664 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
665 arm_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
666 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
667 instruction_,
668 instruction_->GetDexPc(),
669 this);
670 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
671 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
672
673 RestoreLiveRegisters(codegen, locations);
674 __ b(GetExitLabel());
675 }
676
677 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM"; }
678
679 private:
Roland Levillain3b359c72015-11-17 19:35:12 +0000680 const Location out_;
681 const Location root_;
682
683 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM);
684};
685
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000686#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100687#define __ down_cast<ArmAssembler*>(GetAssembler())->
Dave Allison20dfc792014-06-16 20:44:29 -0700688
Aart Bike9f37602015-10-09 11:15:55 -0700689inline Condition ARMCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700690 switch (cond) {
691 case kCondEQ: return EQ;
692 case kCondNE: return NE;
693 case kCondLT: return LT;
694 case kCondLE: return LE;
695 case kCondGT: return GT;
696 case kCondGE: return GE;
Aart Bike9f37602015-10-09 11:15:55 -0700697 case kCondB: return LO;
698 case kCondBE: return LS;
699 case kCondA: return HI;
700 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -0700701 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100702 LOG(FATAL) << "Unreachable";
703 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700704}
705
Aart Bike9f37602015-10-09 11:15:55 -0700706// Maps signed condition to unsigned condition.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100707inline Condition ARMUnsignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700708 switch (cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100709 case kCondEQ: return EQ;
710 case kCondNE: return NE;
Aart Bike9f37602015-10-09 11:15:55 -0700711 // Signed to unsigned.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100712 case kCondLT: return LO;
713 case kCondLE: return LS;
714 case kCondGT: return HI;
715 case kCondGE: return HS;
Aart Bike9f37602015-10-09 11:15:55 -0700716 // Unsigned remain unchanged.
717 case kCondB: return LO;
718 case kCondBE: return LS;
719 case kCondA: return HI;
720 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -0700721 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100722 LOG(FATAL) << "Unreachable";
723 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700724}
725
Vladimir Markod6e069b2016-01-18 11:11:01 +0000726inline Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
727 // The ARM condition codes can express all the necessary branches, see the
728 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
729 // There is no dex instruction or HIR that would need the missing conditions
730 // "equal or unordered" or "not equal".
731 switch (cond) {
732 case kCondEQ: return EQ;
733 case kCondNE: return NE /* unordered */;
734 case kCondLT: return gt_bias ? CC : LT /* unordered */;
735 case kCondLE: return gt_bias ? LS : LE /* unordered */;
736 case kCondGT: return gt_bias ? HI /* unordered */ : GT;
737 case kCondGE: return gt_bias ? CS /* unordered */ : GE;
738 default:
739 LOG(FATAL) << "UNREACHABLE";
740 UNREACHABLE();
741 }
742}
743
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100744void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100745 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100746}
747
748void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100749 stream << SRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100750}
751
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100752size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
753 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
754 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100755}
756
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100757size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
758 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
759 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100760}
761
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000762size_t CodeGeneratorARM::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
763 __ StoreSToOffset(static_cast<SRegister>(reg_id), SP, stack_index);
764 return kArmWordSize;
765}
766
767size_t CodeGeneratorARM::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
768 __ LoadSFromOffset(static_cast<SRegister>(reg_id), SP, stack_index);
769 return kArmWordSize;
770}
771
Calin Juravle34166012014-12-19 17:22:29 +0000772CodeGeneratorARM::CodeGeneratorARM(HGraph* graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000773 const ArmInstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100774 const CompilerOptions& compiler_options,
775 OptimizingCompilerStats* stats)
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000776 : CodeGenerator(graph,
777 kNumberOfCoreRegisters,
778 kNumberOfSRegisters,
779 kNumberOfRegisterPairs,
780 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
781 arraysize(kCoreCalleeSaves)),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000782 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
783 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100784 compiler_options,
785 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100786 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100787 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100788 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100789 move_resolver_(graph->GetArena(), this),
Vladimir Markod1ee8092016-04-13 11:59:46 +0100790 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +0000791 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000792 uint32_literals_(std::less<uint32_t>(),
793 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +0100794 method_patches_(MethodReferenceComparator(),
795 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
796 call_patches_(MethodReferenceComparator(),
797 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markob4536b72015-11-24 13:45:23 +0000798 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000799 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
800 boot_image_string_patches_(StringReferenceValueComparator(),
801 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
802 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
803 boot_image_address_patches_(std::less<uint32_t>(),
804 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Andreas Gampe501fd632015-09-10 16:11:06 -0700805 // Always save the LR register to mimic Quick.
806 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100807}
808
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000809void CodeGeneratorARM::Finalize(CodeAllocator* allocator) {
810 // Ensure that we fix up branches and literal loads and emit the literal pool.
811 __ FinalizeCode();
812
813 // Adjust native pc offsets in stack maps.
814 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
815 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
816 uint32_t new_position = __ GetAdjustedPosition(old_position);
817 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
818 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100819 // Adjust pc offsets for the disassembly information.
820 if (disasm_info_ != nullptr) {
821 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
822 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
823 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
824 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
825 it.second.start = __ GetAdjustedPosition(it.second.start);
826 it.second.end = __ GetAdjustedPosition(it.second.end);
827 }
828 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
829 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
830 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
831 }
832 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000833
834 CodeGenerator::Finalize(allocator);
835}
836
David Brazdil58282f42016-01-14 12:45:10 +0000837void CodeGeneratorARM::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100838 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100839 blocked_register_pairs_[R1_R2] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100840
841 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100842 blocked_core_registers_[SP] = true;
843 blocked_core_registers_[LR] = true;
844 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100845
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100846 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100847 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100848
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100849 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100850 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100851
David Brazdil58282f42016-01-14 12:45:10 +0000852 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +0100853 // Stubs do not save callee-save floating point registers. If the graph
854 // is debuggable, we need to deal with these registers differently. For
855 // now, just block them.
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000856 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
857 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
858 }
859 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100860
861 UpdateBlockedPairRegisters();
862}
863
864void CodeGeneratorARM::UpdateBlockedPairRegisters() const {
865 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
866 ArmManagedRegister current =
867 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
868 if (blocked_core_registers_[current.AsRegisterPairLow()]
869 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
870 blocked_register_pairs_[i] = true;
871 }
872 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100873}
874
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100875InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
Aart Bik42249c32016-01-07 15:33:50 -0800876 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100877 assembler_(codegen->GetAssembler()),
878 codegen_(codegen) {}
879
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000880void CodeGeneratorARM::ComputeSpillMask() {
881 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
882 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
David Brazdil58282f42016-01-14 12:45:10 +0000883 // There is no easy instruction to restore just the PC on thumb2. We spill and
884 // restore another arbitrary register.
885 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000886 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
887 // We use vpush and vpop for saving and restoring floating point registers, which take
888 // a SRegister and the number of registers to save/restore after that SRegister. We
889 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
890 // but in the range.
891 if (fpu_spill_mask_ != 0) {
892 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
893 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
894 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
895 fpu_spill_mask_ |= (1 << i);
896 }
897 }
898}
899
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100900static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100901 return dwarf::Reg::ArmCore(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100902}
903
904static dwarf::Reg DWARFReg(SRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100905 return dwarf::Reg::ArmFp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100906}
907
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000908void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +0000909 bool skip_overflow_check =
910 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000911 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000912 __ Bind(&frame_entry_label_);
913
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000914 if (HasEmptyFrame()) {
915 return;
916 }
917
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100918 if (!skip_overflow_check) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000919 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
920 __ LoadFromOffset(kLoadWord, IP, IP, 0);
921 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100922 }
923
Andreas Gampe501fd632015-09-10 16:11:06 -0700924 __ PushList(core_spill_mask_);
925 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
926 __ cfi().RelOffsetForMany(DWARFReg(kMethodRegisterArgument), 0, core_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000927 if (fpu_spill_mask_ != 0) {
928 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
929 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100930 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
David Srbecky9d8606d2015-04-12 09:35:32 +0100931 __ cfi().RelOffsetForMany(DWARFReg(S0), 0, fpu_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000932 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100933 int adjust = GetFrameSize() - FrameEntrySpillSize();
934 __ AddConstant(SP, -adjust);
935 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100936 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, 0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000937}
938
939void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000940 if (HasEmptyFrame()) {
941 __ bx(LR);
942 return;
943 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100944 __ cfi().RememberState();
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100945 int adjust = GetFrameSize() - FrameEntrySpillSize();
946 __ AddConstant(SP, adjust);
947 __ cfi().AdjustCFAOffset(-adjust);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000948 if (fpu_spill_mask_ != 0) {
949 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
950 __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100951 __ cfi().AdjustCFAOffset(-kArmPointerSize * POPCOUNT(fpu_spill_mask_));
952 __ cfi().RestoreMany(DWARFReg(SRegister(0)), fpu_spill_mask_);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000953 }
Andreas Gampe501fd632015-09-10 16:11:06 -0700954 // Pop LR into PC to return.
955 DCHECK_NE(core_spill_mask_ & (1 << LR), 0U);
956 uint32_t pop_mask = (core_spill_mask_ & (~(1 << LR))) | 1 << PC;
957 __ PopList(pop_mask);
David Srbeckyc34dc932015-04-12 09:27:43 +0100958 __ cfi().RestoreState();
959 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000960}
961
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100962void CodeGeneratorARM::Bind(HBasicBlock* block) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -0700963 Label* label = GetLabelOf(block);
964 __ BindTrackedLabel(label);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000965}
966
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100967Location InvokeDexCallingConventionVisitorARM::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100968 switch (type) {
969 case Primitive::kPrimBoolean:
970 case Primitive::kPrimByte:
971 case Primitive::kPrimChar:
972 case Primitive::kPrimShort:
973 case Primitive::kPrimInt:
974 case Primitive::kPrimNot: {
975 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000976 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100977 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100978 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100979 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000980 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100981 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100982 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100983
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000984 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100985 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000986 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100987 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000988 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100989 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000990 if (calling_convention.GetRegisterAt(index) == R1) {
991 // Skip R1, and use R2_R3 instead.
992 gp_index_++;
993 index++;
994 }
995 }
996 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
997 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +0000998 calling_convention.GetRegisterAt(index + 1));
Calin Juravle175dc732015-08-25 15:42:32 +0100999
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001000 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001001 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001002 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001003 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
1004 }
1005 }
1006
1007 case Primitive::kPrimFloat: {
1008 uint32_t stack_index = stack_index_++;
1009 if (float_index_ % 2 == 0) {
1010 float_index_ = std::max(double_index_, float_index_);
1011 }
1012 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
1013 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
1014 } else {
1015 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
1016 }
1017 }
1018
1019 case Primitive::kPrimDouble: {
1020 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
1021 uint32_t stack_index = stack_index_;
1022 stack_index_ += 2;
1023 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
1024 uint32_t index = double_index_;
1025 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001026 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001027 calling_convention.GetFpuRegisterAt(index),
1028 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001029 DCHECK(ExpectedPairLayout(result));
1030 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001031 } else {
1032 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001033 }
1034 }
1035
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001036 case Primitive::kPrimVoid:
1037 LOG(FATAL) << "Unexpected parameter type " << type;
1038 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001039 }
Roland Levillain3b359c72015-11-17 19:35:12 +00001040 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001041}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001042
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001043Location InvokeDexCallingConventionVisitorARM::GetReturnLocation(Primitive::Type type) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001044 switch (type) {
1045 case Primitive::kPrimBoolean:
1046 case Primitive::kPrimByte:
1047 case Primitive::kPrimChar:
1048 case Primitive::kPrimShort:
1049 case Primitive::kPrimInt:
1050 case Primitive::kPrimNot: {
1051 return Location::RegisterLocation(R0);
1052 }
1053
1054 case Primitive::kPrimFloat: {
1055 return Location::FpuRegisterLocation(S0);
1056 }
1057
1058 case Primitive::kPrimLong: {
1059 return Location::RegisterPairLocation(R0, R1);
1060 }
1061
1062 case Primitive::kPrimDouble: {
1063 return Location::FpuRegisterPairLocation(S0, S1);
1064 }
1065
1066 case Primitive::kPrimVoid:
Roland Levillain3b359c72015-11-17 19:35:12 +00001067 return Location::NoLocation();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001068 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001069
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001070 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001071}
1072
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001073Location InvokeDexCallingConventionVisitorARM::GetMethodLocation() const {
1074 return Location::RegisterLocation(kMethodRegisterArgument);
1075}
1076
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001077void CodeGeneratorARM::Move32(Location destination, Location source) {
1078 if (source.Equals(destination)) {
1079 return;
1080 }
1081 if (destination.IsRegister()) {
1082 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001083 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001084 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001085 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001086 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001087 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001088 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001089 } else if (destination.IsFpuRegister()) {
1090 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001091 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001092 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001093 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001094 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001095 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001096 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001097 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001098 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001099 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001100 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001101 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001102 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001103 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001104 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001105 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
1106 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001107 }
1108 }
1109}
1110
1111void CodeGeneratorARM::Move64(Location destination, Location source) {
1112 if (source.Equals(destination)) {
1113 return;
1114 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001115 if (destination.IsRegisterPair()) {
1116 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001117 EmitParallelMoves(
1118 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1119 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001120 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001121 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001122 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1123 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001124 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001125 UNIMPLEMENTED(FATAL);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001126 } else if (source.IsFpuRegisterPair()) {
1127 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
1128 destination.AsRegisterPairHigh<Register>(),
1129 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001130 } else {
1131 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001132 DCHECK(ExpectedPairLayout(destination));
1133 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
1134 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001135 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001136 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001137 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001138 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1139 SP,
1140 source.GetStackIndex());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001141 } else if (source.IsRegisterPair()) {
1142 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1143 source.AsRegisterPairLow<Register>(),
1144 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001145 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001146 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001147 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001148 } else {
1149 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001150 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001151 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001152 if (source.AsRegisterPairLow<Register>() == R1) {
1153 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001154 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
1155 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001156 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001157 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001158 SP, destination.GetStackIndex());
1159 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001160 } else if (source.IsFpuRegisterPair()) {
1161 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
1162 SP,
1163 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001164 } else {
1165 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001166 EmitParallelMoves(
1167 Location::StackSlot(source.GetStackIndex()),
1168 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001169 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001170 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001171 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)),
1172 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001173 }
1174 }
1175}
1176
Calin Juravle175dc732015-08-25 15:42:32 +01001177void CodeGeneratorARM::MoveConstant(Location location, int32_t value) {
1178 DCHECK(location.IsRegister());
1179 __ LoadImmediate(location.AsRegister<Register>(), value);
1180}
1181
Calin Juravlee460d1d2015-09-29 04:52:17 +01001182void CodeGeneratorARM::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001183 HParallelMove move(GetGraph()->GetArena());
1184 move.AddMove(src, dst, dst_type, nullptr);
1185 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001186}
1187
1188void CodeGeneratorARM::AddLocationAsTemp(Location location, LocationSummary* locations) {
1189 if (location.IsRegister()) {
1190 locations->AddTemp(location);
1191 } else if (location.IsRegisterPair()) {
1192 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1193 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1194 } else {
1195 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1196 }
1197}
1198
Calin Juravle175dc732015-08-25 15:42:32 +01001199void CodeGeneratorARM::InvokeRuntime(QuickEntrypointEnum entrypoint,
1200 HInstruction* instruction,
1201 uint32_t dex_pc,
1202 SlowPathCode* slow_path) {
1203 InvokeRuntime(GetThreadOffset<kArmWordSize>(entrypoint).Int32Value(),
1204 instruction,
1205 dex_pc,
1206 slow_path);
1207}
1208
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001209void CodeGeneratorARM::InvokeRuntime(int32_t entry_point_offset,
1210 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001211 uint32_t dex_pc,
1212 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001213 ValidateInvokeRuntime(instruction, slow_path);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001214 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
1215 __ blx(LR);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001216 RecordPcInfo(instruction, dex_pc, slow_path);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001217}
1218
David Brazdilfc6a86a2015-06-26 10:33:45 +00001219void InstructionCodeGeneratorARM::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001220 DCHECK(!successor->IsExitBlock());
1221
1222 HBasicBlock* block = got->GetBlock();
1223 HInstruction* previous = got->GetPrevious();
1224
1225 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001226 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001227 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1228 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1229 return;
1230 }
1231
1232 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1233 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1234 }
1235 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001236 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001237 }
1238}
1239
David Brazdilfc6a86a2015-06-26 10:33:45 +00001240void LocationsBuilderARM::VisitGoto(HGoto* got) {
1241 got->SetLocations(nullptr);
1242}
1243
1244void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
1245 HandleGoto(got, got->GetSuccessor());
1246}
1247
1248void LocationsBuilderARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1249 try_boundary->SetLocations(nullptr);
1250}
1251
1252void InstructionCodeGeneratorARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1253 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1254 if (!successor->IsExitBlock()) {
1255 HandleGoto(try_boundary, successor);
1256 }
1257}
1258
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001259void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001260 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001261}
1262
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001263void InstructionCodeGeneratorARM::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001264}
1265
Vladimir Marko2887aa22016-08-01 17:41:45 +01001266void InstructionCodeGeneratorARM::GenerateVcmp(HInstruction* instruction) {
1267 Primitive::Type type = instruction->InputAt(0)->GetType();
1268 Location lhs_loc = instruction->GetLocations()->InAt(0);
1269 Location rhs_loc = instruction->GetLocations()->InAt(1);
1270 if (rhs_loc.IsConstant()) {
1271 // 0.0 is the only immediate that can be encoded directly in
1272 // a VCMP instruction.
1273 //
1274 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1275 // specify that in a floating-point comparison, positive zero
1276 // and negative zero are considered equal, so we can use the
1277 // literal 0.0 for both cases here.
1278 //
1279 // Note however that some methods (Float.equal, Float.compare,
1280 // Float.compareTo, Double.equal, Double.compare,
1281 // Double.compareTo, Math.max, Math.min, StrictMath.max,
1282 // StrictMath.min) consider 0.0 to be (strictly) greater than
1283 // -0.0. So if we ever translate calls to these methods into a
1284 // HCompare instruction, we must handle the -0.0 case with
1285 // care here.
1286 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1287 if (type == Primitive::kPrimFloat) {
1288 __ vcmpsz(lhs_loc.AsFpuRegister<SRegister>());
1289 } else {
1290 DCHECK_EQ(type, Primitive::kPrimDouble);
1291 __ vcmpdz(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()));
1292 }
1293 } else {
1294 if (type == Primitive::kPrimFloat) {
1295 __ vcmps(lhs_loc.AsFpuRegister<SRegister>(), rhs_loc.AsFpuRegister<SRegister>());
1296 } else {
1297 DCHECK_EQ(type, Primitive::kPrimDouble);
1298 __ vcmpd(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()),
1299 FromLowSToD(rhs_loc.AsFpuRegisterPairLow<SRegister>()));
1300 }
1301 }
1302}
1303
Roland Levillain4fa13f62015-07-06 18:11:54 +01001304void InstructionCodeGeneratorARM::GenerateFPJumps(HCondition* cond,
1305 Label* true_label,
Vladimir Markod6e069b2016-01-18 11:11:01 +00001306 Label* false_label ATTRIBUTE_UNUSED) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001307 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00001308 __ b(true_label, ARMFPCondition(cond->GetCondition(), cond->IsGtBias()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001309}
1310
1311void InstructionCodeGeneratorARM::GenerateLongComparesAndJumps(HCondition* cond,
1312 Label* true_label,
1313 Label* false_label) {
1314 LocationSummary* locations = cond->GetLocations();
1315 Location left = locations->InAt(0);
1316 Location right = locations->InAt(1);
1317 IfCondition if_cond = cond->GetCondition();
1318
1319 Register left_high = left.AsRegisterPairHigh<Register>();
1320 Register left_low = left.AsRegisterPairLow<Register>();
1321 IfCondition true_high_cond = if_cond;
1322 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001323 Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
Roland Levillain4fa13f62015-07-06 18:11:54 +01001324
1325 // Set the conditions for the test, remembering that == needs to be
1326 // decided using the low words.
Aart Bike9f37602015-10-09 11:15:55 -07001327 // TODO: consider avoiding jumps with temporary and CMP low+SBC high
Roland Levillain4fa13f62015-07-06 18:11:54 +01001328 switch (if_cond) {
1329 case kCondEQ:
1330 case kCondNE:
1331 // Nothing to do.
1332 break;
1333 case kCondLT:
1334 false_high_cond = kCondGT;
1335 break;
1336 case kCondLE:
1337 true_high_cond = kCondLT;
1338 break;
1339 case kCondGT:
1340 false_high_cond = kCondLT;
1341 break;
1342 case kCondGE:
1343 true_high_cond = kCondGT;
1344 break;
Aart Bike9f37602015-10-09 11:15:55 -07001345 case kCondB:
1346 false_high_cond = kCondA;
1347 break;
1348 case kCondBE:
1349 true_high_cond = kCondB;
1350 break;
1351 case kCondA:
1352 false_high_cond = kCondB;
1353 break;
1354 case kCondAE:
1355 true_high_cond = kCondA;
1356 break;
Roland Levillain4fa13f62015-07-06 18:11:54 +01001357 }
1358 if (right.IsConstant()) {
1359 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1360 int32_t val_low = Low32Bits(value);
1361 int32_t val_high = High32Bits(value);
1362
Vladimir Markoac6ac102015-12-17 12:14:00 +00001363 __ CmpConstant(left_high, val_high);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001364 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001365 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001366 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001367 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001368 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001369 __ b(true_label, ARMCondition(true_high_cond));
1370 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001371 }
1372 // Must be equal high, so compare the lows.
Vladimir Markoac6ac102015-12-17 12:14:00 +00001373 __ CmpConstant(left_low, val_low);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001374 } else {
1375 Register right_high = right.AsRegisterPairHigh<Register>();
1376 Register right_low = right.AsRegisterPairLow<Register>();
1377
1378 __ cmp(left_high, ShifterOperand(right_high));
1379 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001380 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001381 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001382 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001383 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001384 __ b(true_label, ARMCondition(true_high_cond));
1385 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001386 }
1387 // Must be equal high, so compare the lows.
1388 __ cmp(left_low, ShifterOperand(right_low));
1389 }
1390 // The last comparison might be unsigned.
Aart Bike9f37602015-10-09 11:15:55 -07001391 // TODO: optimize cases where this is always true/false
Roland Levillain4fa13f62015-07-06 18:11:54 +01001392 __ b(true_label, final_condition);
1393}
1394
David Brazdil0debae72015-11-12 18:37:00 +00001395void InstructionCodeGeneratorARM::GenerateCompareTestAndBranch(HCondition* condition,
1396 Label* true_target_in,
1397 Label* false_target_in) {
1398 // Generated branching requires both targets to be explicit. If either of the
1399 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1400 Label fallthrough_target;
1401 Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1402 Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1403
Roland Levillain4fa13f62015-07-06 18:11:54 +01001404 Primitive::Type type = condition->InputAt(0)->GetType();
1405 switch (type) {
1406 case Primitive::kPrimLong:
1407 GenerateLongComparesAndJumps(condition, true_target, false_target);
1408 break;
1409 case Primitive::kPrimFloat:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001410 case Primitive::kPrimDouble:
Vladimir Marko2887aa22016-08-01 17:41:45 +01001411 GenerateVcmp(condition);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001412 GenerateFPJumps(condition, true_target, false_target);
1413 break;
1414 default:
1415 LOG(FATAL) << "Unexpected compare type " << type;
1416 }
1417
David Brazdil0debae72015-11-12 18:37:00 +00001418 if (false_target != &fallthrough_target) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001419 __ b(false_target);
1420 }
David Brazdil0debae72015-11-12 18:37:00 +00001421
1422 if (fallthrough_target.IsLinked()) {
1423 __ Bind(&fallthrough_target);
1424 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001425}
1426
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001427void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001428 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001429 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00001430 Label* false_target) {
1431 HInstruction* cond = instruction->InputAt(condition_input_index);
1432
1433 if (true_target == nullptr && false_target == nullptr) {
1434 // Nothing to do. The code always falls through.
1435 return;
1436 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001437 // Constant condition, statically compared against "true" (integer value 1).
1438 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001439 if (true_target != nullptr) {
1440 __ b(true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001441 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001442 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001443 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001444 if (false_target != nullptr) {
1445 __ b(false_target);
1446 }
1447 }
1448 return;
1449 }
1450
1451 // The following code generates these patterns:
1452 // (1) true_target == nullptr && false_target != nullptr
1453 // - opposite condition true => branch to false_target
1454 // (2) true_target != nullptr && false_target == nullptr
1455 // - condition true => branch to true_target
1456 // (3) true_target != nullptr && false_target != nullptr
1457 // - condition true => branch to true_target
1458 // - branch to false_target
1459 if (IsBooleanValueOrMaterializedCondition(cond)) {
1460 // Condition has been materialized, compare the output to 0.
1461 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
1462 DCHECK(cond_val.IsRegister());
1463 if (true_target == nullptr) {
1464 __ CompareAndBranchIfZero(cond_val.AsRegister<Register>(), false_target);
1465 } else {
1466 __ CompareAndBranchIfNonZero(cond_val.AsRegister<Register>(), true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001467 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001468 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001469 // Condition has not been materialized. Use its inputs as the comparison and
1470 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001471 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001472
1473 // If this is a long or FP comparison that has been folded into
1474 // the HCondition, generate the comparison directly.
1475 Primitive::Type type = condition->InputAt(0)->GetType();
1476 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1477 GenerateCompareTestAndBranch(condition, true_target, false_target);
1478 return;
1479 }
1480
1481 LocationSummary* locations = cond->GetLocations();
1482 DCHECK(locations->InAt(0).IsRegister());
1483 Register left = locations->InAt(0).AsRegister<Register>();
1484 Location right = locations->InAt(1);
1485 if (right.IsRegister()) {
1486 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001487 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001488 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00001489 __ CmpConstant(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
David Brazdil0debae72015-11-12 18:37:00 +00001490 }
1491 if (true_target == nullptr) {
1492 __ b(false_target, ARMCondition(condition->GetOppositeCondition()));
1493 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001494 __ b(true_target, ARMCondition(condition->GetCondition()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001495 }
Dave Allison20dfc792014-06-16 20:44:29 -07001496 }
David Brazdil0debae72015-11-12 18:37:00 +00001497
1498 // If neither branch falls through (case 3), the conditional branch to `true_target`
1499 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1500 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001501 __ b(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001502 }
1503}
1504
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001505void LocationsBuilderARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001506 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1507 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001508 locations->SetInAt(0, Location::RequiresRegister());
1509 }
1510}
1511
1512void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001513 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1514 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1515 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1516 nullptr : codegen_->GetLabelOf(true_successor);
1517 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1518 nullptr : codegen_->GetLabelOf(false_successor);
1519 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001520}
1521
1522void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
1523 LocationSummary* locations = new (GetGraph()->GetArena())
1524 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001525 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001526 locations->SetInAt(0, Location::RequiresRegister());
1527 }
1528}
1529
1530void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001531 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00001532 GenerateTestAndBranch(deoptimize,
1533 /* condition_input_index */ 0,
1534 slow_path->GetEntryLabel(),
1535 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001536}
Dave Allison20dfc792014-06-16 20:44:29 -07001537
David Brazdil74eb1b22015-12-14 11:44:01 +00001538void LocationsBuilderARM::VisitSelect(HSelect* select) {
1539 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1540 if (Primitive::IsFloatingPointType(select->GetType())) {
1541 locations->SetInAt(0, Location::RequiresFpuRegister());
1542 locations->SetInAt(1, Location::RequiresFpuRegister());
1543 } else {
1544 locations->SetInAt(0, Location::RequiresRegister());
1545 locations->SetInAt(1, Location::RequiresRegister());
1546 }
1547 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1548 locations->SetInAt(2, Location::RequiresRegister());
1549 }
1550 locations->SetOut(Location::SameAsFirstInput());
1551}
1552
1553void InstructionCodeGeneratorARM::VisitSelect(HSelect* select) {
1554 LocationSummary* locations = select->GetLocations();
1555 Label false_target;
1556 GenerateTestAndBranch(select,
1557 /* condition_input_index */ 2,
1558 /* true_target */ nullptr,
1559 &false_target);
1560 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1561 __ Bind(&false_target);
1562}
1563
David Srbecky0cf44932015-12-09 14:09:59 +00001564void LocationsBuilderARM::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1565 new (GetGraph()->GetArena()) LocationSummary(info);
1566}
1567
David Srbeckyd28f4a02016-03-14 17:14:24 +00001568void InstructionCodeGeneratorARM::VisitNativeDebugInfo(HNativeDebugInfo*) {
1569 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001570}
1571
1572void CodeGeneratorARM::GenerateNop() {
1573 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001574}
1575
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001576void LocationsBuilderARM::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001577 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001578 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001579 // Handle the long/FP comparisons made in instruction simplification.
1580 switch (cond->InputAt(0)->GetType()) {
1581 case Primitive::kPrimLong:
1582 locations->SetInAt(0, Location::RequiresRegister());
1583 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001584 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001585 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1586 }
1587 break;
1588
1589 case Primitive::kPrimFloat:
1590 case Primitive::kPrimDouble:
1591 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko2887aa22016-08-01 17:41:45 +01001592 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001593 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001594 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1595 }
1596 break;
1597
1598 default:
1599 locations->SetInAt(0, Location::RequiresRegister());
1600 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001601 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001602 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1603 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001604 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001605}
1606
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001607void InstructionCodeGeneratorARM::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001608 if (cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001609 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001610 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001611
1612 LocationSummary* locations = cond->GetLocations();
1613 Location left = locations->InAt(0);
1614 Location right = locations->InAt(1);
1615 Register out = locations->Out().AsRegister<Register>();
1616 Label true_label, false_label;
1617
1618 switch (cond->InputAt(0)->GetType()) {
1619 default: {
1620 // Integer case.
1621 if (right.IsRegister()) {
1622 __ cmp(left.AsRegister<Register>(), ShifterOperand(right.AsRegister<Register>()));
1623 } else {
1624 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00001625 __ CmpConstant(left.AsRegister<Register>(),
1626 CodeGenerator::GetInt32ValueOf(right.GetConstant()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001627 }
Aart Bike9f37602015-10-09 11:15:55 -07001628 __ it(ARMCondition(cond->GetCondition()), kItElse);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001629 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(1),
Aart Bike9f37602015-10-09 11:15:55 -07001630 ARMCondition(cond->GetCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001631 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(0),
Aart Bike9f37602015-10-09 11:15:55 -07001632 ARMCondition(cond->GetOppositeCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001633 return;
1634 }
1635 case Primitive::kPrimLong:
1636 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1637 break;
1638 case Primitive::kPrimFloat:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001639 case Primitive::kPrimDouble:
Vladimir Marko2887aa22016-08-01 17:41:45 +01001640 GenerateVcmp(cond);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001641 GenerateFPJumps(cond, &true_label, &false_label);
1642 break;
1643 }
1644
1645 // Convert the jumps into the result.
1646 Label done_label;
1647
1648 // False case: result = 0.
1649 __ Bind(&false_label);
1650 __ LoadImmediate(out, 0);
1651 __ b(&done_label);
1652
1653 // True case: result = 1.
1654 __ Bind(&true_label);
1655 __ LoadImmediate(out, 1);
1656 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001657}
1658
1659void LocationsBuilderARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001660 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001661}
1662
1663void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001664 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001665}
1666
1667void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001668 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001669}
1670
1671void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001672 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001673}
1674
1675void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001676 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001677}
1678
1679void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001680 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001681}
1682
1683void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001684 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001685}
1686
1687void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001688 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001689}
1690
1691void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001692 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001693}
1694
1695void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001696 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001697}
1698
1699void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001700 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001701}
1702
1703void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001704 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001705}
1706
Aart Bike9f37602015-10-09 11:15:55 -07001707void LocationsBuilderARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001708 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001709}
1710
1711void InstructionCodeGeneratorARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001712 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001713}
1714
1715void LocationsBuilderARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001716 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001717}
1718
1719void InstructionCodeGeneratorARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001720 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001721}
1722
1723void LocationsBuilderARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001724 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001725}
1726
1727void InstructionCodeGeneratorARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001728 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001729}
1730
1731void LocationsBuilderARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001732 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001733}
1734
1735void InstructionCodeGeneratorARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001736 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001737}
1738
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001739void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001740 LocationSummary* locations =
1741 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001742 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001743}
1744
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001745void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001746 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001747}
1748
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001749void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
1750 LocationSummary* locations =
1751 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1752 locations->SetOut(Location::ConstantLocation(constant));
1753}
1754
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001755void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001756 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001757}
1758
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001759void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001760 LocationSummary* locations =
1761 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001762 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001763}
1764
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001765void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001766 // Will be generated at use site.
1767}
1768
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001769void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
1770 LocationSummary* locations =
1771 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1772 locations->SetOut(Location::ConstantLocation(constant));
1773}
1774
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001775void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001776 // Will be generated at use site.
1777}
1778
1779void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
1780 LocationSummary* locations =
1781 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1782 locations->SetOut(Location::ConstantLocation(constant));
1783}
1784
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001785void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001786 // Will be generated at use site.
1787}
1788
Calin Juravle27df7582015-04-17 19:12:31 +01001789void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1790 memory_barrier->SetLocations(nullptr);
1791}
1792
1793void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00001794 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001795}
1796
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001797void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001798 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001799}
1800
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001801void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001802 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001803}
1804
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001805void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001806 LocationSummary* locations =
1807 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001808 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001809}
1810
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001811void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001812 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001813}
1814
Calin Juravle175dc732015-08-25 15:42:32 +01001815void LocationsBuilderARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1816 // The trampoline uses the same calling convention as dex calling conventions,
1817 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1818 // the method_idx.
1819 HandleInvoke(invoke);
1820}
1821
1822void InstructionCodeGeneratorARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1823 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1824}
1825
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001826void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001827 // Explicit clinit checks triggered by static invokes must have been pruned by
1828 // art::PrepareForRegisterAllocation.
1829 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001830
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001831 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001832 codegen_->GetAssembler(),
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001833 codegen_->GetInstructionSetFeatures());
1834 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markob4536b72015-11-24 13:45:23 +00001835 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
1836 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
1837 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001838 return;
1839 }
1840
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001841 HandleInvoke(invoke);
Vladimir Markob4536b72015-11-24 13:45:23 +00001842
1843 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
1844 if (invoke->HasPcRelativeDexCache()) {
1845 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
1846 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001847}
1848
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001849static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
1850 if (invoke->GetLocations()->Intrinsified()) {
1851 IntrinsicCodeGeneratorARM intrinsic(codegen);
1852 intrinsic.Dispatch(invoke);
1853 return true;
1854 }
1855 return false;
1856}
1857
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001858void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001859 // Explicit clinit checks triggered by static invokes must have been pruned by
1860 // art::PrepareForRegisterAllocation.
1861 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001862
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001863 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1864 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001865 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001866
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001867 LocationSummary* locations = invoke->GetLocations();
1868 codegen_->GenerateStaticOrDirectCall(
1869 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001870 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001871}
1872
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001873void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001874 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001875 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001876}
1877
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001878void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001879 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001880 codegen_->GetAssembler(),
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001881 codegen_->GetInstructionSetFeatures());
1882 if (intrinsic.TryDispatch(invoke)) {
1883 return;
1884 }
1885
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001886 HandleInvoke(invoke);
1887}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001888
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001889void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001890 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1891 return;
1892 }
1893
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001894 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001895 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001896 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001897}
1898
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001899void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1900 HandleInvoke(invoke);
1901 // Add the hidden argument.
1902 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
1903}
1904
1905void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1906 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain3b359c72015-11-17 19:35:12 +00001907 LocationSummary* locations = invoke->GetLocations();
1908 Register temp = locations->GetTemp(0).AsRegister<Register>();
1909 Register hidden_reg = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001910 Location receiver = locations->InAt(0);
1911 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1912
Roland Levillain3b359c72015-11-17 19:35:12 +00001913 // Set the hidden argument. This is safe to do this here, as R12
1914 // won't be modified thereafter, before the `blx` (call) instruction.
1915 DCHECK_EQ(R12, hidden_reg);
1916 __ LoadImmediate(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001917
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001918 if (receiver.IsStackSlot()) {
1919 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
Roland Levillain3b359c72015-11-17 19:35:12 +00001920 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001921 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
1922 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00001923 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00001924 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001925 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001926 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00001927 // Instead of simply (possibly) unpoisoning `temp` here, we should
1928 // emit a read barrier for the previous class reference load.
1929 // However this is not required in practice, as this is an
1930 // intermediate/temporary reference and because the current
1931 // concurrent copying collector keeps the from-space memory
1932 // intact/accessible until the end of the marking phase (the
1933 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01001934 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkodf2d4f22016-06-30 09:18:25 +00001935 __ LoadFromOffset(kLoadWord, temp, temp,
1936 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
1937 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
1938 invoke->GetImtIndex() % ImTable::kSize, kArmPointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001939 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkodf2d4f22016-06-30 09:18:25 +00001940 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00001941 uint32_t entry_point =
1942 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001943 // LR = temp->GetEntryPoint();
1944 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
1945 // LR();
1946 __ blx(LR);
1947 DCHECK(!codegen_->IsLeafMethod());
1948 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1949}
1950
Roland Levillain88cb1752014-10-20 16:36:47 +01001951void LocationsBuilderARM::VisitNeg(HNeg* neg) {
1952 LocationSummary* locations =
1953 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1954 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001955 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01001956 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001957 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1958 break;
1959 }
1960 case Primitive::kPrimLong: {
1961 locations->SetInAt(0, Location::RequiresRegister());
1962 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01001963 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001964 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001965
Roland Levillain88cb1752014-10-20 16:36:47 +01001966 case Primitive::kPrimFloat:
1967 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001968 locations->SetInAt(0, Location::RequiresFpuRegister());
1969 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01001970 break;
1971
1972 default:
1973 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1974 }
1975}
1976
1977void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
1978 LocationSummary* locations = neg->GetLocations();
1979 Location out = locations->Out();
1980 Location in = locations->InAt(0);
1981 switch (neg->GetResultType()) {
1982 case Primitive::kPrimInt:
1983 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001984 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01001985 break;
1986
1987 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001988 DCHECK(in.IsRegisterPair());
1989 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
1990 __ rsbs(out.AsRegisterPairLow<Register>(),
1991 in.AsRegisterPairLow<Register>(),
1992 ShifterOperand(0));
1993 // We cannot emit an RSC (Reverse Subtract with Carry)
1994 // instruction here, as it does not exist in the Thumb-2
1995 // instruction set. We use the following approach
1996 // using SBC and SUB instead.
1997 //
1998 // out.hi = -C
1999 __ sbc(out.AsRegisterPairHigh<Register>(),
2000 out.AsRegisterPairHigh<Register>(),
2001 ShifterOperand(out.AsRegisterPairHigh<Register>()));
2002 // out.hi = out.hi - in.hi
2003 __ sub(out.AsRegisterPairHigh<Register>(),
2004 out.AsRegisterPairHigh<Register>(),
2005 ShifterOperand(in.AsRegisterPairHigh<Register>()));
2006 break;
2007
Roland Levillain88cb1752014-10-20 16:36:47 +01002008 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002009 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002010 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002011 break;
2012
Roland Levillain88cb1752014-10-20 16:36:47 +01002013 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002014 DCHECK(in.IsFpuRegisterPair());
2015 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2016 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01002017 break;
2018
2019 default:
2020 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2021 }
2022}
2023
Roland Levillaindff1f282014-11-05 14:15:05 +00002024void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002025 Primitive::Type result_type = conversion->GetResultType();
2026 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002027 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002028
Roland Levillain5b3ee562015-04-14 16:02:41 +01002029 // The float-to-long, double-to-long and long-to-float type conversions
2030 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002031 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01002032 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2033 && result_type == Primitive::kPrimLong)
2034 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Roland Levillain624279f2014-12-04 11:54:28 +00002035 ? LocationSummary::kCall
2036 : LocationSummary::kNoCall;
2037 LocationSummary* locations =
2038 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2039
David Brazdilb2bd1c52015-03-25 11:17:37 +00002040 // The Java language does not allow treating boolean as an integral type but
2041 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002042
Roland Levillaindff1f282014-11-05 14:15:05 +00002043 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002044 case Primitive::kPrimByte:
2045 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002046 case Primitive::kPrimLong:
2047 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002048 case Primitive::kPrimBoolean:
2049 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002050 case Primitive::kPrimShort:
2051 case Primitive::kPrimInt:
2052 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002053 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002054 locations->SetInAt(0, Location::RequiresRegister());
2055 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2056 break;
2057
2058 default:
2059 LOG(FATAL) << "Unexpected type conversion from " << input_type
2060 << " to " << result_type;
2061 }
2062 break;
2063
Roland Levillain01a8d712014-11-14 16:27:39 +00002064 case Primitive::kPrimShort:
2065 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002066 case Primitive::kPrimLong:
2067 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002068 case Primitive::kPrimBoolean:
2069 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002070 case Primitive::kPrimByte:
2071 case Primitive::kPrimInt:
2072 case Primitive::kPrimChar:
2073 // Processing a Dex `int-to-short' instruction.
2074 locations->SetInAt(0, Location::RequiresRegister());
2075 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2076 break;
2077
2078 default:
2079 LOG(FATAL) << "Unexpected type conversion from " << input_type
2080 << " to " << result_type;
2081 }
2082 break;
2083
Roland Levillain946e1432014-11-11 17:35:19 +00002084 case Primitive::kPrimInt:
2085 switch (input_type) {
2086 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002087 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002088 locations->SetInAt(0, Location::Any());
2089 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2090 break;
2091
2092 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002093 // Processing a Dex `float-to-int' instruction.
2094 locations->SetInAt(0, Location::RequiresFpuRegister());
2095 locations->SetOut(Location::RequiresRegister());
2096 locations->AddTemp(Location::RequiresFpuRegister());
2097 break;
2098
Roland Levillain946e1432014-11-11 17:35:19 +00002099 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002100 // Processing a Dex `double-to-int' instruction.
2101 locations->SetInAt(0, Location::RequiresFpuRegister());
2102 locations->SetOut(Location::RequiresRegister());
2103 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002104 break;
2105
2106 default:
2107 LOG(FATAL) << "Unexpected type conversion from " << input_type
2108 << " to " << result_type;
2109 }
2110 break;
2111
Roland Levillaindff1f282014-11-05 14:15:05 +00002112 case Primitive::kPrimLong:
2113 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002114 case Primitive::kPrimBoolean:
2115 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002116 case Primitive::kPrimByte:
2117 case Primitive::kPrimShort:
2118 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002119 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002120 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002121 locations->SetInAt(0, Location::RequiresRegister());
2122 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2123 break;
2124
Roland Levillain624279f2014-12-04 11:54:28 +00002125 case Primitive::kPrimFloat: {
2126 // Processing a Dex `float-to-long' instruction.
2127 InvokeRuntimeCallingConvention calling_convention;
2128 locations->SetInAt(0, Location::FpuRegisterLocation(
2129 calling_convention.GetFpuRegisterAt(0)));
2130 locations->SetOut(Location::RegisterPairLocation(R0, R1));
2131 break;
2132 }
2133
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002134 case Primitive::kPrimDouble: {
2135 // Processing a Dex `double-to-long' instruction.
2136 InvokeRuntimeCallingConvention calling_convention;
2137 locations->SetInAt(0, Location::FpuRegisterPairLocation(
2138 calling_convention.GetFpuRegisterAt(0),
2139 calling_convention.GetFpuRegisterAt(1)));
2140 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00002141 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002142 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002143
2144 default:
2145 LOG(FATAL) << "Unexpected type conversion from " << input_type
2146 << " to " << result_type;
2147 }
2148 break;
2149
Roland Levillain981e4542014-11-14 11:47:14 +00002150 case Primitive::kPrimChar:
2151 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002152 case Primitive::kPrimLong:
2153 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002154 case Primitive::kPrimBoolean:
2155 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002156 case Primitive::kPrimByte:
2157 case Primitive::kPrimShort:
2158 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002159 // Processing a Dex `int-to-char' instruction.
2160 locations->SetInAt(0, Location::RequiresRegister());
2161 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2162 break;
2163
2164 default:
2165 LOG(FATAL) << "Unexpected type conversion from " << input_type
2166 << " to " << result_type;
2167 }
2168 break;
2169
Roland Levillaindff1f282014-11-05 14:15:05 +00002170 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002171 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002172 case Primitive::kPrimBoolean:
2173 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002174 case Primitive::kPrimByte:
2175 case Primitive::kPrimShort:
2176 case Primitive::kPrimInt:
2177 case Primitive::kPrimChar:
2178 // Processing a Dex `int-to-float' instruction.
2179 locations->SetInAt(0, Location::RequiresRegister());
2180 locations->SetOut(Location::RequiresFpuRegister());
2181 break;
2182
Roland Levillain5b3ee562015-04-14 16:02:41 +01002183 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00002184 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002185 InvokeRuntimeCallingConvention calling_convention;
2186 locations->SetInAt(0, Location::RegisterPairLocation(
2187 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2188 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00002189 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01002190 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002191
Roland Levillaincff13742014-11-17 14:32:17 +00002192 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002193 // Processing a Dex `double-to-float' instruction.
2194 locations->SetInAt(0, Location::RequiresFpuRegister());
2195 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002196 break;
2197
2198 default:
2199 LOG(FATAL) << "Unexpected type conversion from " << input_type
2200 << " to " << result_type;
2201 };
2202 break;
2203
Roland Levillaindff1f282014-11-05 14:15:05 +00002204 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002205 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002206 case Primitive::kPrimBoolean:
2207 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002208 case Primitive::kPrimByte:
2209 case Primitive::kPrimShort:
2210 case Primitive::kPrimInt:
2211 case Primitive::kPrimChar:
2212 // Processing a Dex `int-to-double' instruction.
2213 locations->SetInAt(0, Location::RequiresRegister());
2214 locations->SetOut(Location::RequiresFpuRegister());
2215 break;
2216
2217 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002218 // Processing a Dex `long-to-double' instruction.
2219 locations->SetInAt(0, Location::RequiresRegister());
2220 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01002221 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002222 locations->AddTemp(Location::RequiresFpuRegister());
2223 break;
2224
Roland Levillaincff13742014-11-17 14:32:17 +00002225 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002226 // Processing a Dex `float-to-double' instruction.
2227 locations->SetInAt(0, Location::RequiresFpuRegister());
2228 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002229 break;
2230
2231 default:
2232 LOG(FATAL) << "Unexpected type conversion from " << input_type
2233 << " to " << result_type;
2234 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002235 break;
2236
2237 default:
2238 LOG(FATAL) << "Unexpected type conversion from " << input_type
2239 << " to " << result_type;
2240 }
2241}
2242
2243void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
2244 LocationSummary* locations = conversion->GetLocations();
2245 Location out = locations->Out();
2246 Location in = locations->InAt(0);
2247 Primitive::Type result_type = conversion->GetResultType();
2248 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002249 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002250 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002251 case Primitive::kPrimByte:
2252 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002253 case Primitive::kPrimLong:
2254 // Type conversion from long to byte is a result of code transformations.
2255 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 8);
2256 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002257 case Primitive::kPrimBoolean:
2258 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002259 case Primitive::kPrimShort:
2260 case Primitive::kPrimInt:
2261 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002262 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002263 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002264 break;
2265
2266 default:
2267 LOG(FATAL) << "Unexpected type conversion from " << input_type
2268 << " to " << result_type;
2269 }
2270 break;
2271
Roland Levillain01a8d712014-11-14 16:27:39 +00002272 case Primitive::kPrimShort:
2273 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002274 case Primitive::kPrimLong:
2275 // Type conversion from long to short is a result of code transformations.
2276 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2277 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002278 case Primitive::kPrimBoolean:
2279 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002280 case Primitive::kPrimByte:
2281 case Primitive::kPrimInt:
2282 case Primitive::kPrimChar:
2283 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002284 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00002285 break;
2286
2287 default:
2288 LOG(FATAL) << "Unexpected type conversion from " << input_type
2289 << " to " << result_type;
2290 }
2291 break;
2292
Roland Levillain946e1432014-11-11 17:35:19 +00002293 case Primitive::kPrimInt:
2294 switch (input_type) {
2295 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002296 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002297 DCHECK(out.IsRegister());
2298 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002299 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002300 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002301 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00002302 } else {
2303 DCHECK(in.IsConstant());
2304 DCHECK(in.GetConstant()->IsLongConstant());
2305 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002306 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00002307 }
2308 break;
2309
Roland Levillain3f8f9362014-12-02 17:45:01 +00002310 case Primitive::kPrimFloat: {
2311 // Processing a Dex `float-to-int' instruction.
2312 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko9cfdcf42016-07-07 12:07:44 +01002313 __ vcvtis(temp, in.AsFpuRegister<SRegister>());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002314 __ vmovrs(out.AsRegister<Register>(), temp);
2315 break;
2316 }
2317
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002318 case Primitive::kPrimDouble: {
2319 // Processing a Dex `double-to-int' instruction.
2320 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko9cfdcf42016-07-07 12:07:44 +01002321 __ vcvtid(temp_s, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002322 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00002323 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002324 }
Roland Levillain946e1432014-11-11 17:35:19 +00002325
2326 default:
2327 LOG(FATAL) << "Unexpected type conversion from " << input_type
2328 << " to " << result_type;
2329 }
2330 break;
2331
Roland Levillaindff1f282014-11-05 14:15:05 +00002332 case Primitive::kPrimLong:
2333 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002334 case Primitive::kPrimBoolean:
2335 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002336 case Primitive::kPrimByte:
2337 case Primitive::kPrimShort:
2338 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002339 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002340 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002341 DCHECK(out.IsRegisterPair());
2342 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002343 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002344 // Sign extension.
2345 __ Asr(out.AsRegisterPairHigh<Register>(),
2346 out.AsRegisterPairLow<Register>(),
2347 31);
2348 break;
2349
2350 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002351 // Processing a Dex `float-to-long' instruction.
Roland Levillain624279f2014-12-04 11:54:28 +00002352 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2353 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002354 conversion->GetDexPc(),
2355 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002356 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002357 break;
2358
Roland Levillaindff1f282014-11-05 14:15:05 +00002359 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002360 // Processing a Dex `double-to-long' instruction.
2361 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2362 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002363 conversion->GetDexPc(),
2364 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002365 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002366 break;
2367
2368 default:
2369 LOG(FATAL) << "Unexpected type conversion from " << input_type
2370 << " to " << result_type;
2371 }
2372 break;
2373
Roland Levillain981e4542014-11-14 11:47:14 +00002374 case Primitive::kPrimChar:
2375 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002376 case Primitive::kPrimLong:
2377 // Type conversion from long to char is a result of code transformations.
2378 __ ubfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2379 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002380 case Primitive::kPrimBoolean:
2381 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002382 case Primitive::kPrimByte:
2383 case Primitive::kPrimShort:
2384 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002385 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002386 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00002387 break;
2388
2389 default:
2390 LOG(FATAL) << "Unexpected type conversion from " << input_type
2391 << " to " << result_type;
2392 }
2393 break;
2394
Roland Levillaindff1f282014-11-05 14:15:05 +00002395 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002396 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002397 case Primitive::kPrimBoolean:
2398 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002399 case Primitive::kPrimByte:
2400 case Primitive::kPrimShort:
2401 case Primitive::kPrimInt:
2402 case Primitive::kPrimChar: {
2403 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002404 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
2405 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002406 break;
2407 }
2408
Roland Levillain5b3ee562015-04-14 16:02:41 +01002409 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002410 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002411 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pL2f),
2412 conversion,
2413 conversion->GetDexPc(),
2414 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002415 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
Roland Levillain6d0e4832014-11-27 18:31:21 +00002416 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002417
Roland Levillaincff13742014-11-17 14:32:17 +00002418 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002419 // Processing a Dex `double-to-float' instruction.
2420 __ vcvtsd(out.AsFpuRegister<SRegister>(),
2421 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00002422 break;
2423
2424 default:
2425 LOG(FATAL) << "Unexpected type conversion from " << input_type
2426 << " to " << result_type;
2427 };
2428 break;
2429
Roland Levillaindff1f282014-11-05 14:15:05 +00002430 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002431 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002432 case Primitive::kPrimBoolean:
2433 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002434 case Primitive::kPrimByte:
2435 case Primitive::kPrimShort:
2436 case Primitive::kPrimInt:
2437 case Primitive::kPrimChar: {
2438 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002439 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002440 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2441 out.AsFpuRegisterPairLow<SRegister>());
2442 break;
2443 }
2444
Roland Levillain647b9ed2014-11-27 12:06:00 +00002445 case Primitive::kPrimLong: {
2446 // Processing a Dex `long-to-double' instruction.
2447 Register low = in.AsRegisterPairLow<Register>();
2448 Register high = in.AsRegisterPairHigh<Register>();
2449 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
2450 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002451 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00002452 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002453 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
2454 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002455
Roland Levillain682393c2015-04-14 15:57:52 +01002456 // temp_d = int-to-double(high)
2457 __ vmovsr(temp_s, high);
2458 __ vcvtdi(temp_d, temp_s);
2459 // constant_d = k2Pow32EncodingForDouble
2460 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
2461 // out_d = unsigned-to-double(low)
2462 __ vmovsr(out_s, low);
2463 __ vcvtdu(out_d, out_s);
2464 // out_d += temp_d * constant_d
2465 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002466 break;
2467 }
2468
Roland Levillaincff13742014-11-17 14:32:17 +00002469 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002470 // Processing a Dex `float-to-double' instruction.
2471 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2472 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002473 break;
2474
2475 default:
2476 LOG(FATAL) << "Unexpected type conversion from " << input_type
2477 << " to " << result_type;
2478 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002479 break;
2480
2481 default:
2482 LOG(FATAL) << "Unexpected type conversion from " << input_type
2483 << " to " << result_type;
2484 }
2485}
2486
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002487void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002488 LocationSummary* locations =
2489 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002490 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002491 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002492 locations->SetInAt(0, Location::RequiresRegister());
2493 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002494 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2495 break;
2496 }
2497
2498 case Primitive::kPrimLong: {
2499 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko966901f2016-08-05 14:37:27 +01002500 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002501 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002502 break;
2503 }
2504
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002505 case Primitive::kPrimFloat:
2506 case Primitive::kPrimDouble: {
2507 locations->SetInAt(0, Location::RequiresFpuRegister());
2508 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002509 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002510 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002511 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002512
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002513 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002514 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002515 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002516}
2517
2518void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
2519 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002520 Location out = locations->Out();
2521 Location first = locations->InAt(0);
2522 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002523 switch (add->GetResultType()) {
2524 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002525 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002526 __ add(out.AsRegister<Register>(),
2527 first.AsRegister<Register>(),
2528 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002529 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002530 __ AddConstant(out.AsRegister<Register>(),
2531 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002532 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002533 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002534 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002535
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002536 case Primitive::kPrimLong: {
Vladimir Marko966901f2016-08-05 14:37:27 +01002537 if (second.IsConstant()) {
2538 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
2539 GenerateAddLongConst(out, first, value);
2540 } else {
2541 DCHECK(second.IsRegisterPair());
2542 __ adds(out.AsRegisterPairLow<Register>(),
2543 first.AsRegisterPairLow<Register>(),
2544 ShifterOperand(second.AsRegisterPairLow<Register>()));
2545 __ adc(out.AsRegisterPairHigh<Register>(),
2546 first.AsRegisterPairHigh<Register>(),
2547 ShifterOperand(second.AsRegisterPairHigh<Register>()));
2548 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002549 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002550 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002551
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002552 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00002553 __ vadds(out.AsFpuRegister<SRegister>(),
2554 first.AsFpuRegister<SRegister>(),
2555 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002556 break;
2557
2558 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002559 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2560 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2561 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002562 break;
2563
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002564 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002565 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002566 }
2567}
2568
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002569void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002570 LocationSummary* locations =
2571 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002572 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002573 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002574 locations->SetInAt(0, Location::RequiresRegister());
2575 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002576 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2577 break;
2578 }
2579
2580 case Primitive::kPrimLong: {
2581 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko966901f2016-08-05 14:37:27 +01002582 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002583 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002584 break;
2585 }
Calin Juravle11351682014-10-23 15:38:15 +01002586 case Primitive::kPrimFloat:
2587 case Primitive::kPrimDouble: {
2588 locations->SetInAt(0, Location::RequiresFpuRegister());
2589 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002590 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002591 break;
Calin Juravle11351682014-10-23 15:38:15 +01002592 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002593 default:
Calin Juravle11351682014-10-23 15:38:15 +01002594 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002595 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002596}
2597
2598void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
2599 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002600 Location out = locations->Out();
2601 Location first = locations->InAt(0);
2602 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002603 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002604 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002605 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002606 __ sub(out.AsRegister<Register>(),
2607 first.AsRegister<Register>(),
2608 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002609 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002610 __ AddConstant(out.AsRegister<Register>(),
2611 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01002612 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002613 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002614 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002615 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002616
Calin Juravle11351682014-10-23 15:38:15 +01002617 case Primitive::kPrimLong: {
Vladimir Marko966901f2016-08-05 14:37:27 +01002618 if (second.IsConstant()) {
2619 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
2620 GenerateAddLongConst(out, first, -value);
2621 } else {
2622 DCHECK(second.IsRegisterPair());
2623 __ subs(out.AsRegisterPairLow<Register>(),
2624 first.AsRegisterPairLow<Register>(),
2625 ShifterOperand(second.AsRegisterPairLow<Register>()));
2626 __ sbc(out.AsRegisterPairHigh<Register>(),
2627 first.AsRegisterPairHigh<Register>(),
2628 ShifterOperand(second.AsRegisterPairHigh<Register>()));
2629 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002630 break;
Calin Juravle11351682014-10-23 15:38:15 +01002631 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002632
Calin Juravle11351682014-10-23 15:38:15 +01002633 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002634 __ vsubs(out.AsFpuRegister<SRegister>(),
2635 first.AsFpuRegister<SRegister>(),
2636 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002637 break;
Calin Juravle11351682014-10-23 15:38:15 +01002638 }
2639
2640 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002641 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2642 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2643 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01002644 break;
2645 }
2646
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002647
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002648 default:
Calin Juravle11351682014-10-23 15:38:15 +01002649 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002650 }
2651}
2652
Calin Juravle34bacdf2014-10-07 20:23:36 +01002653void LocationsBuilderARM::VisitMul(HMul* mul) {
2654 LocationSummary* locations =
2655 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2656 switch (mul->GetResultType()) {
2657 case Primitive::kPrimInt:
2658 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002659 locations->SetInAt(0, Location::RequiresRegister());
2660 locations->SetInAt(1, Location::RequiresRegister());
2661 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002662 break;
2663 }
2664
Calin Juravleb5bfa962014-10-21 18:02:24 +01002665 case Primitive::kPrimFloat:
2666 case Primitive::kPrimDouble: {
2667 locations->SetInAt(0, Location::RequiresFpuRegister());
2668 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002669 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002670 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002671 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002672
2673 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002674 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002675 }
2676}
2677
2678void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
2679 LocationSummary* locations = mul->GetLocations();
2680 Location out = locations->Out();
2681 Location first = locations->InAt(0);
2682 Location second = locations->InAt(1);
2683 switch (mul->GetResultType()) {
2684 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00002685 __ mul(out.AsRegister<Register>(),
2686 first.AsRegister<Register>(),
2687 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002688 break;
2689 }
2690 case Primitive::kPrimLong: {
2691 Register out_hi = out.AsRegisterPairHigh<Register>();
2692 Register out_lo = out.AsRegisterPairLow<Register>();
2693 Register in1_hi = first.AsRegisterPairHigh<Register>();
2694 Register in1_lo = first.AsRegisterPairLow<Register>();
2695 Register in2_hi = second.AsRegisterPairHigh<Register>();
2696 Register in2_lo = second.AsRegisterPairLow<Register>();
2697
2698 // Extra checks to protect caused by the existence of R1_R2.
2699 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
2700 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
2701 DCHECK_NE(out_hi, in1_lo);
2702 DCHECK_NE(out_hi, in2_lo);
2703
2704 // input: in1 - 64 bits, in2 - 64 bits
2705 // output: out
2706 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2707 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2708 // parts: out.lo = (in1.lo * in2.lo)[31:0]
2709
2710 // IP <- in1.lo * in2.hi
2711 __ mul(IP, in1_lo, in2_hi);
2712 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2713 __ mla(out_hi, in1_hi, in2_lo, IP);
2714 // out.lo <- (in1.lo * in2.lo)[31:0];
2715 __ umull(out_lo, IP, in1_lo, in2_lo);
2716 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2717 __ add(out_hi, out_hi, ShifterOperand(IP));
2718 break;
2719 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002720
2721 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002722 __ vmuls(out.AsFpuRegister<SRegister>(),
2723 first.AsFpuRegister<SRegister>(),
2724 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002725 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002726 }
2727
2728 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002729 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2730 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2731 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01002732 break;
2733 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002734
2735 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002736 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002737 }
2738}
2739
Zheng Xuc6667102015-05-15 16:08:45 +08002740void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2741 DCHECK(instruction->IsDiv() || instruction->IsRem());
2742 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2743
2744 LocationSummary* locations = instruction->GetLocations();
2745 Location second = locations->InAt(1);
2746 DCHECK(second.IsConstant());
2747
2748 Register out = locations->Out().AsRegister<Register>();
2749 Register dividend = locations->InAt(0).AsRegister<Register>();
2750 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2751 DCHECK(imm == 1 || imm == -1);
2752
2753 if (instruction->IsRem()) {
2754 __ LoadImmediate(out, 0);
2755 } else {
2756 if (imm == 1) {
2757 __ Mov(out, dividend);
2758 } else {
2759 __ rsb(out, dividend, ShifterOperand(0));
2760 }
2761 }
2762}
2763
2764void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2765 DCHECK(instruction->IsDiv() || instruction->IsRem());
2766 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2767
2768 LocationSummary* locations = instruction->GetLocations();
2769 Location second = locations->InAt(1);
2770 DCHECK(second.IsConstant());
2771
2772 Register out = locations->Out().AsRegister<Register>();
2773 Register dividend = locations->InAt(0).AsRegister<Register>();
2774 Register temp = locations->GetTemp(0).AsRegister<Register>();
2775 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002776 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002777 int ctz_imm = CTZ(abs_imm);
2778
2779 if (ctz_imm == 1) {
2780 __ Lsr(temp, dividend, 32 - ctz_imm);
2781 } else {
2782 __ Asr(temp, dividend, 31);
2783 __ Lsr(temp, temp, 32 - ctz_imm);
2784 }
2785 __ add(out, temp, ShifterOperand(dividend));
2786
2787 if (instruction->IsDiv()) {
2788 __ Asr(out, out, ctz_imm);
2789 if (imm < 0) {
2790 __ rsb(out, out, ShifterOperand(0));
2791 }
2792 } else {
2793 __ ubfx(out, out, 0, ctz_imm);
2794 __ sub(out, out, ShifterOperand(temp));
2795 }
2796}
2797
2798void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2799 DCHECK(instruction->IsDiv() || instruction->IsRem());
2800 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2801
2802 LocationSummary* locations = instruction->GetLocations();
2803 Location second = locations->InAt(1);
2804 DCHECK(second.IsConstant());
2805
2806 Register out = locations->Out().AsRegister<Register>();
2807 Register dividend = locations->InAt(0).AsRegister<Register>();
2808 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2809 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
2810 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2811
2812 int64_t magic;
2813 int shift;
2814 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2815
2816 __ LoadImmediate(temp1, magic);
2817 __ smull(temp2, temp1, dividend, temp1);
2818
2819 if (imm > 0 && magic < 0) {
2820 __ add(temp1, temp1, ShifterOperand(dividend));
2821 } else if (imm < 0 && magic > 0) {
2822 __ sub(temp1, temp1, ShifterOperand(dividend));
2823 }
2824
2825 if (shift != 0) {
2826 __ Asr(temp1, temp1, shift);
2827 }
2828
2829 if (instruction->IsDiv()) {
2830 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
2831 } else {
2832 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
2833 // TODO: Strength reduction for mls.
2834 __ LoadImmediate(temp2, imm);
2835 __ mls(out, temp1, temp2, dividend);
2836 }
2837}
2838
2839void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
2840 DCHECK(instruction->IsDiv() || instruction->IsRem());
2841 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2842
2843 LocationSummary* locations = instruction->GetLocations();
2844 Location second = locations->InAt(1);
2845 DCHECK(second.IsConstant());
2846
2847 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2848 if (imm == 0) {
2849 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2850 } else if (imm == 1 || imm == -1) {
2851 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002852 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002853 DivRemByPowerOfTwo(instruction);
2854 } else {
2855 DCHECK(imm <= -2 || imm >= 2);
2856 GenerateDivRemWithAnyConstant(instruction);
2857 }
2858}
2859
Calin Juravle7c4954d2014-10-28 16:57:40 +00002860void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002861 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2862 if (div->GetResultType() == Primitive::kPrimLong) {
2863 // pLdiv runtime call.
2864 call_kind = LocationSummary::kCall;
Zheng Xuc6667102015-05-15 16:08:45 +08002865 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
2866 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002867 } else if (div->GetResultType() == Primitive::kPrimInt &&
2868 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
2869 // pIdivmod runtime call.
2870 call_kind = LocationSummary::kCall;
2871 }
2872
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002873 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2874
Calin Juravle7c4954d2014-10-28 16:57:40 +00002875 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002876 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002877 if (div->InputAt(1)->IsConstant()) {
2878 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00002879 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08002880 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002881 int32_t value = div->InputAt(1)->AsIntConstant()->GetValue();
2882 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08002883 // No temp register required.
2884 } else {
2885 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002886 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002887 locations->AddTemp(Location::RequiresRegister());
2888 }
2889 }
2890 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002891 locations->SetInAt(0, Location::RequiresRegister());
2892 locations->SetInAt(1, Location::RequiresRegister());
2893 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2894 } else {
2895 InvokeRuntimeCallingConvention calling_convention;
2896 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2897 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2898 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
2899 // we only need the former.
2900 locations->SetOut(Location::RegisterLocation(R0));
2901 }
Calin Juravled0d48522014-11-04 16:40:20 +00002902 break;
2903 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002904 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002905 InvokeRuntimeCallingConvention calling_convention;
2906 locations->SetInAt(0, Location::RegisterPairLocation(
2907 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2908 locations->SetInAt(1, Location::RegisterPairLocation(
2909 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002910 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002911 break;
2912 }
2913 case Primitive::kPrimFloat:
2914 case Primitive::kPrimDouble: {
2915 locations->SetInAt(0, Location::RequiresFpuRegister());
2916 locations->SetInAt(1, Location::RequiresFpuRegister());
2917 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2918 break;
2919 }
2920
2921 default:
2922 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2923 }
2924}
2925
2926void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
2927 LocationSummary* locations = div->GetLocations();
2928 Location out = locations->Out();
2929 Location first = locations->InAt(0);
2930 Location second = locations->InAt(1);
2931
2932 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002933 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002934 if (second.IsConstant()) {
2935 GenerateDivRemConstantIntegral(div);
2936 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002937 __ sdiv(out.AsRegister<Register>(),
2938 first.AsRegister<Register>(),
2939 second.AsRegister<Register>());
2940 } else {
2941 InvokeRuntimeCallingConvention calling_convention;
2942 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
2943 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
2944 DCHECK_EQ(R0, out.AsRegister<Register>());
2945
2946 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), div, div->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002947 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002948 }
Calin Juravled0d48522014-11-04 16:40:20 +00002949 break;
2950 }
2951
Calin Juravle7c4954d2014-10-28 16:57:40 +00002952 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002953 InvokeRuntimeCallingConvention calling_convention;
2954 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2955 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2956 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2957 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2958 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002959 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002960
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002961 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv), div, div->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002962 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002963 break;
2964 }
2965
2966 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002967 __ vdivs(out.AsFpuRegister<SRegister>(),
2968 first.AsFpuRegister<SRegister>(),
2969 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002970 break;
2971 }
2972
2973 case Primitive::kPrimDouble: {
2974 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2975 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2976 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
2977 break;
2978 }
2979
2980 default:
2981 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2982 }
2983}
2984
Calin Juravlebacfec32014-11-14 15:54:36 +00002985void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002986 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002987
2988 // Most remainders are implemented in the runtime.
2989 LocationSummary::CallKind call_kind = LocationSummary::kCall;
Zheng Xuc6667102015-05-15 16:08:45 +08002990 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
2991 // sdiv will be replaced by other instruction sequence.
2992 call_kind = LocationSummary::kNoCall;
2993 } else if ((rem->GetResultType() == Primitive::kPrimInt)
2994 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002995 // Have hardware divide instruction for int, do it with three instructions.
2996 call_kind = LocationSummary::kNoCall;
2997 }
2998
Calin Juravlebacfec32014-11-14 15:54:36 +00002999 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3000
Calin Juravled2ec87d2014-12-08 14:24:46 +00003001 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003002 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003003 if (rem->InputAt(1)->IsConstant()) {
3004 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003005 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08003006 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003007 int32_t value = rem->InputAt(1)->AsIntConstant()->GetValue();
3008 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08003009 // No temp register required.
3010 } else {
3011 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003012 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003013 locations->AddTemp(Location::RequiresRegister());
3014 }
3015 }
3016 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003017 locations->SetInAt(0, Location::RequiresRegister());
3018 locations->SetInAt(1, Location::RequiresRegister());
3019 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3020 locations->AddTemp(Location::RequiresRegister());
3021 } else {
3022 InvokeRuntimeCallingConvention calling_convention;
3023 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3024 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3025 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
3026 // we only need the latter.
3027 locations->SetOut(Location::RegisterLocation(R1));
3028 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003029 break;
3030 }
3031 case Primitive::kPrimLong: {
3032 InvokeRuntimeCallingConvention calling_convention;
3033 locations->SetInAt(0, Location::RegisterPairLocation(
3034 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3035 locations->SetInAt(1, Location::RegisterPairLocation(
3036 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3037 // The runtime helper puts the output in R2,R3.
3038 locations->SetOut(Location::RegisterPairLocation(R2, R3));
3039 break;
3040 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00003041 case Primitive::kPrimFloat: {
3042 InvokeRuntimeCallingConvention calling_convention;
3043 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3044 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3045 locations->SetOut(Location::FpuRegisterLocation(S0));
3046 break;
3047 }
3048
Calin Juravlebacfec32014-11-14 15:54:36 +00003049 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003050 InvokeRuntimeCallingConvention calling_convention;
3051 locations->SetInAt(0, Location::FpuRegisterPairLocation(
3052 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
3053 locations->SetInAt(1, Location::FpuRegisterPairLocation(
3054 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
3055 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00003056 break;
3057 }
3058
3059 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003060 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003061 }
3062}
3063
3064void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
3065 LocationSummary* locations = rem->GetLocations();
3066 Location out = locations->Out();
3067 Location first = locations->InAt(0);
3068 Location second = locations->InAt(1);
3069
Calin Juravled2ec87d2014-12-08 14:24:46 +00003070 Primitive::Type type = rem->GetResultType();
3071 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003072 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003073 if (second.IsConstant()) {
3074 GenerateDivRemConstantIntegral(rem);
3075 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003076 Register reg1 = first.AsRegister<Register>();
3077 Register reg2 = second.AsRegister<Register>();
3078 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003079
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003080 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003081 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003082 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003083 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003084 } else {
3085 InvokeRuntimeCallingConvention calling_convention;
3086 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3087 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3088 DCHECK_EQ(R1, out.AsRegister<Register>());
3089
3090 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003091 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003092 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003093 break;
3094 }
3095
3096 case Primitive::kPrimLong: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003097 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003098 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003099 break;
3100 }
3101
Calin Juravled2ec87d2014-12-08 14:24:46 +00003102 case Primitive::kPrimFloat: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003103 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003104 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Calin Juravled2ec87d2014-12-08 14:24:46 +00003105 break;
3106 }
3107
Calin Juravlebacfec32014-11-14 15:54:36 +00003108 case Primitive::kPrimDouble: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003109 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003110 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003111 break;
3112 }
3113
3114 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003115 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003116 }
3117}
3118
Calin Juravled0d48522014-11-04 16:40:20 +00003119void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003120 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3121 ? LocationSummary::kCallOnSlowPath
3122 : LocationSummary::kNoCall;
3123 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003124 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00003125 if (instruction->HasUses()) {
3126 locations->SetOut(Location::SameAsFirstInput());
3127 }
3128}
3129
3130void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003131 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003132 codegen_->AddSlowPath(slow_path);
3133
3134 LocationSummary* locations = instruction->GetLocations();
3135 Location value = locations->InAt(0);
3136
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003137 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003138 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003139 case Primitive::kPrimByte:
3140 case Primitive::kPrimChar:
3141 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003142 case Primitive::kPrimInt: {
3143 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003144 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003145 } else {
3146 DCHECK(value.IsConstant()) << value;
3147 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3148 __ b(slow_path->GetEntryLabel());
3149 }
3150 }
3151 break;
3152 }
3153 case Primitive::kPrimLong: {
3154 if (value.IsRegisterPair()) {
3155 __ orrs(IP,
3156 value.AsRegisterPairLow<Register>(),
3157 ShifterOperand(value.AsRegisterPairHigh<Register>()));
3158 __ b(slow_path->GetEntryLabel(), EQ);
3159 } else {
3160 DCHECK(value.IsConstant()) << value;
3161 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3162 __ b(slow_path->GetEntryLabel());
3163 }
3164 }
3165 break;
3166 default:
3167 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3168 }
3169 }
Calin Juravled0d48522014-11-04 16:40:20 +00003170}
3171
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003172void InstructionCodeGeneratorARM::HandleIntegerRotate(LocationSummary* locations) {
3173 Register in = locations->InAt(0).AsRegister<Register>();
3174 Location rhs = locations->InAt(1);
3175 Register out = locations->Out().AsRegister<Register>();
3176
3177 if (rhs.IsConstant()) {
3178 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
3179 // so map all rotations to a +ve. equivalent in that range.
3180 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
3181 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
3182 if (rot) {
3183 // Rotate, mapping left rotations to right equivalents if necessary.
3184 // (e.g. left by 2 bits == right by 30.)
3185 __ Ror(out, in, rot);
3186 } else if (out != in) {
3187 __ Mov(out, in);
3188 }
3189 } else {
3190 __ Ror(out, in, rhs.AsRegister<Register>());
3191 }
3192}
3193
3194// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
3195// rotates by swapping input regs (effectively rotating by the first 32-bits of
3196// a larger rotation) or flipping direction (thus treating larger right/left
3197// rotations as sub-word sized rotations in the other direction) as appropriate.
3198void InstructionCodeGeneratorARM::HandleLongRotate(LocationSummary* locations) {
3199 Register in_reg_lo = locations->InAt(0).AsRegisterPairLow<Register>();
3200 Register in_reg_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
3201 Location rhs = locations->InAt(1);
3202 Register out_reg_lo = locations->Out().AsRegisterPairLow<Register>();
3203 Register out_reg_hi = locations->Out().AsRegisterPairHigh<Register>();
3204
3205 if (rhs.IsConstant()) {
3206 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
3207 // Map all rotations to +ve. equivalents on the interval [0,63].
Roland Levillain5b5b9312016-03-22 14:57:31 +00003208 rot &= kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003209 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
3210 // logic below to a simple pair of binary orr.
3211 // (e.g. 34 bits == in_reg swap + 2 bits right.)
3212 if (rot >= kArmBitsPerWord) {
3213 rot -= kArmBitsPerWord;
3214 std::swap(in_reg_hi, in_reg_lo);
3215 }
3216 // Rotate, or mov to out for zero or word size rotations.
3217 if (rot != 0u) {
3218 __ Lsr(out_reg_hi, in_reg_hi, rot);
3219 __ orr(out_reg_hi, out_reg_hi, ShifterOperand(in_reg_lo, arm::LSL, kArmBitsPerWord - rot));
3220 __ Lsr(out_reg_lo, in_reg_lo, rot);
3221 __ orr(out_reg_lo, out_reg_lo, ShifterOperand(in_reg_hi, arm::LSL, kArmBitsPerWord - rot));
3222 } else {
3223 __ Mov(out_reg_lo, in_reg_lo);
3224 __ Mov(out_reg_hi, in_reg_hi);
3225 }
3226 } else {
3227 Register shift_right = locations->GetTemp(0).AsRegister<Register>();
3228 Register shift_left = locations->GetTemp(1).AsRegister<Register>();
3229 Label end;
3230 Label shift_by_32_plus_shift_right;
3231
3232 __ and_(shift_right, rhs.AsRegister<Register>(), ShifterOperand(0x1F));
3233 __ Lsrs(shift_left, rhs.AsRegister<Register>(), 6);
3234 __ rsb(shift_left, shift_right, ShifterOperand(kArmBitsPerWord), AL, kCcKeep);
3235 __ b(&shift_by_32_plus_shift_right, CC);
3236
3237 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
3238 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
3239 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
3240 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3241 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3242 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3243 __ Lsr(shift_left, in_reg_hi, shift_right);
3244 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_left));
3245 __ b(&end);
3246
3247 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
3248 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
3249 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
3250 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
3251 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3252 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3253 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3254 __ Lsl(shift_right, in_reg_hi, shift_left);
3255 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_right));
3256
3257 __ Bind(&end);
3258 }
3259}
Roland Levillain22c49222016-03-18 14:04:28 +00003260
3261void LocationsBuilderARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003262 LocationSummary* locations =
3263 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3264 switch (ror->GetResultType()) {
3265 case Primitive::kPrimInt: {
3266 locations->SetInAt(0, Location::RequiresRegister());
3267 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
3268 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3269 break;
3270 }
3271 case Primitive::kPrimLong: {
3272 locations->SetInAt(0, Location::RequiresRegister());
3273 if (ror->InputAt(1)->IsConstant()) {
3274 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
3275 } else {
3276 locations->SetInAt(1, Location::RequiresRegister());
3277 locations->AddTemp(Location::RequiresRegister());
3278 locations->AddTemp(Location::RequiresRegister());
3279 }
3280 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3281 break;
3282 }
3283 default:
3284 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3285 }
3286}
3287
Roland Levillain22c49222016-03-18 14:04:28 +00003288void InstructionCodeGeneratorARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003289 LocationSummary* locations = ror->GetLocations();
3290 Primitive::Type type = ror->GetResultType();
3291 switch (type) {
3292 case Primitive::kPrimInt: {
3293 HandleIntegerRotate(locations);
3294 break;
3295 }
3296 case Primitive::kPrimLong: {
3297 HandleLongRotate(locations);
3298 break;
3299 }
3300 default:
3301 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00003302 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003303 }
3304}
3305
Calin Juravle9aec02f2014-11-18 23:06:35 +00003306void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
3307 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3308
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003309 LocationSummary* locations =
3310 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003311
3312 switch (op->GetResultType()) {
3313 case Primitive::kPrimInt: {
3314 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003315 if (op->InputAt(1)->IsConstant()) {
3316 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3317 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3318 } else {
3319 locations->SetInAt(1, Location::RequiresRegister());
3320 // Make the output overlap, as it will be used to hold the masked
3321 // second input.
3322 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3323 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003324 break;
3325 }
3326 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003327 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003328 if (op->InputAt(1)->IsConstant()) {
3329 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3330 // For simplicity, use kOutputOverlap even though we only require that low registers
3331 // don't clash with high registers which the register allocator currently guarantees.
3332 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3333 } else {
3334 locations->SetInAt(1, Location::RequiresRegister());
3335 locations->AddTemp(Location::RequiresRegister());
3336 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3337 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003338 break;
3339 }
3340 default:
3341 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3342 }
3343}
3344
3345void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
3346 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3347
3348 LocationSummary* locations = op->GetLocations();
3349 Location out = locations->Out();
3350 Location first = locations->InAt(0);
3351 Location second = locations->InAt(1);
3352
3353 Primitive::Type type = op->GetResultType();
3354 switch (type) {
3355 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003356 Register out_reg = out.AsRegister<Register>();
3357 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003358 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003359 Register second_reg = second.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +00003360 // ARM doesn't mask the shift count so we need to do it ourselves.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003361 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftDistance));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003362 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003363 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003364 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003365 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003366 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003367 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003368 }
3369 } else {
3370 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00003371 uint32_t shift_value = cst & kMaxIntShiftDistance;
Roland Levillainc9285912015-12-18 10:38:42 +00003372 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
Calin Juravle9aec02f2014-11-18 23:06:35 +00003373 __ Mov(out_reg, first_reg);
3374 } else if (op->IsShl()) {
3375 __ Lsl(out_reg, first_reg, shift_value);
3376 } else if (op->IsShr()) {
3377 __ Asr(out_reg, first_reg, shift_value);
3378 } else {
3379 __ Lsr(out_reg, first_reg, shift_value);
3380 }
3381 }
3382 break;
3383 }
3384 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003385 Register o_h = out.AsRegisterPairHigh<Register>();
3386 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003387
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003388 Register high = first.AsRegisterPairHigh<Register>();
3389 Register low = first.AsRegisterPairLow<Register>();
3390
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003391 if (second.IsRegister()) {
3392 Register temp = locations->GetTemp(0).AsRegister<Register>();
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003393
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003394 Register second_reg = second.AsRegister<Register>();
3395
3396 if (op->IsShl()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003397 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003398 // Shift the high part
3399 __ Lsl(o_h, high, o_l);
3400 // Shift the low part and `or` what overflew on the high part
3401 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
3402 __ Lsr(temp, low, temp);
3403 __ orr(o_h, o_h, ShifterOperand(temp));
3404 // If the shift is > 32 bits, override the high part
3405 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
3406 __ it(PL);
3407 __ Lsl(o_h, low, temp, PL);
3408 // Shift the low part
3409 __ Lsl(o_l, low, o_l);
3410 } else if (op->IsShr()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003411 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003412 // Shift the low part
3413 __ Lsr(o_l, low, o_h);
3414 // Shift the high part and `or` what underflew on the low part
3415 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3416 __ Lsl(temp, high, temp);
3417 __ orr(o_l, o_l, ShifterOperand(temp));
3418 // If the shift is > 32 bits, override the low part
3419 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3420 __ it(PL);
3421 __ Asr(o_l, high, temp, PL);
3422 // Shift the high part
3423 __ Asr(o_h, high, o_h);
3424 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003425 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003426 // same as Shr except we use `Lsr`s and not `Asr`s
3427 __ Lsr(o_l, low, o_h);
3428 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3429 __ Lsl(temp, high, temp);
3430 __ orr(o_l, o_l, ShifterOperand(temp));
3431 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3432 __ it(PL);
3433 __ Lsr(o_l, high, temp, PL);
3434 __ Lsr(o_h, high, o_h);
3435 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003436 } else {
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003437 // Register allocator doesn't create partial overlap.
3438 DCHECK_NE(o_l, high);
3439 DCHECK_NE(o_h, low);
3440 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00003441 uint32_t shift_value = cst & kMaxLongShiftDistance;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003442 if (shift_value > 32) {
3443 if (op->IsShl()) {
3444 __ Lsl(o_h, low, shift_value - 32);
3445 __ LoadImmediate(o_l, 0);
3446 } else if (op->IsShr()) {
3447 __ Asr(o_l, high, shift_value - 32);
3448 __ Asr(o_h, high, 31);
3449 } else {
3450 __ Lsr(o_l, high, shift_value - 32);
3451 __ LoadImmediate(o_h, 0);
3452 }
3453 } else if (shift_value == 32) {
3454 if (op->IsShl()) {
3455 __ mov(o_h, ShifterOperand(low));
3456 __ LoadImmediate(o_l, 0);
3457 } else if (op->IsShr()) {
3458 __ mov(o_l, ShifterOperand(high));
3459 __ Asr(o_h, high, 31);
3460 } else {
3461 __ mov(o_l, ShifterOperand(high));
3462 __ LoadImmediate(o_h, 0);
3463 }
Vladimir Markof9d741e2015-11-20 15:08:11 +00003464 } else if (shift_value == 1) {
3465 if (op->IsShl()) {
3466 __ Lsls(o_l, low, 1);
3467 __ adc(o_h, high, ShifterOperand(high));
3468 } else if (op->IsShr()) {
3469 __ Asrs(o_h, high, 1);
3470 __ Rrx(o_l, low);
3471 } else {
3472 __ Lsrs(o_h, high, 1);
3473 __ Rrx(o_l, low);
3474 }
3475 } else {
3476 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003477 if (op->IsShl()) {
3478 __ Lsl(o_h, high, shift_value);
3479 __ orr(o_h, o_h, ShifterOperand(low, LSR, 32 - shift_value));
3480 __ Lsl(o_l, low, shift_value);
3481 } else if (op->IsShr()) {
3482 __ Lsr(o_l, low, shift_value);
3483 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3484 __ Asr(o_h, high, shift_value);
3485 } else {
3486 __ Lsr(o_l, low, shift_value);
3487 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3488 __ Lsr(o_h, high, shift_value);
3489 }
3490 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003491 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003492 break;
3493 }
3494 default:
3495 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003496 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003497 }
3498}
3499
3500void LocationsBuilderARM::VisitShl(HShl* shl) {
3501 HandleShift(shl);
3502}
3503
3504void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
3505 HandleShift(shl);
3506}
3507
3508void LocationsBuilderARM::VisitShr(HShr* shr) {
3509 HandleShift(shr);
3510}
3511
3512void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
3513 HandleShift(shr);
3514}
3515
3516void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
3517 HandleShift(ushr);
3518}
3519
3520void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
3521 HandleShift(ushr);
3522}
3523
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003524void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003525 LocationSummary* locations =
3526 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
David Brazdil6de19382016-01-08 17:37:10 +00003527 if (instruction->IsStringAlloc()) {
3528 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3529 } else {
3530 InvokeRuntimeCallingConvention calling_convention;
3531 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3532 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3533 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003534 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003535}
3536
3537void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003538 // Note: if heap poisoning is enabled, the entry point takes cares
3539 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003540 if (instruction->IsStringAlloc()) {
3541 // String is allocated through StringFactory. Call NewEmptyString entry point.
3542 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
3543 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize);
3544 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
3545 __ LoadFromOffset(kLoadWord, LR, temp, code_offset.Int32Value());
3546 __ blx(LR);
3547 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3548 } else {
3549 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3550 instruction,
3551 instruction->GetDexPc(),
3552 nullptr);
3553 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3554 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003555}
3556
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003557void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
3558 LocationSummary* locations =
3559 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3560 InvokeRuntimeCallingConvention calling_convention;
3561 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003562 locations->SetOut(Location::RegisterLocation(R0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003563 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003564 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003565}
3566
3567void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
3568 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003569 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003570 // Note: if heap poisoning is enabled, the entry point takes cares
3571 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003572 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003573 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003574 instruction->GetDexPc(),
3575 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003576 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003577}
3578
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003579void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003580 LocationSummary* locations =
3581 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003582 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3583 if (location.IsStackSlot()) {
3584 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3585 } else if (location.IsDoubleStackSlot()) {
3586 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003587 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003588 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003589}
3590
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003591void InstructionCodeGeneratorARM::VisitParameterValue(
3592 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003593 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003594}
3595
3596void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
3597 LocationSummary* locations =
3598 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3599 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3600}
3601
3602void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3603 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003604}
3605
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003606void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003607 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003608 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003609 locations->SetInAt(0, Location::RequiresRegister());
3610 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003611}
3612
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003613void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
3614 LocationSummary* locations = not_->GetLocations();
3615 Location out = locations->Out();
3616 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003617 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003618 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003619 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003620 break;
3621
3622 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003623 __ mvn(out.AsRegisterPairLow<Register>(),
3624 ShifterOperand(in.AsRegisterPairLow<Register>()));
3625 __ mvn(out.AsRegisterPairHigh<Register>(),
3626 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003627 break;
3628
3629 default:
3630 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3631 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003632}
3633
David Brazdil66d126e2015-04-03 16:02:44 +01003634void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
3635 LocationSummary* locations =
3636 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3637 locations->SetInAt(0, Location::RequiresRegister());
3638 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3639}
3640
3641void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003642 LocationSummary* locations = bool_not->GetLocations();
3643 Location out = locations->Out();
3644 Location in = locations->InAt(0);
3645 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
3646}
3647
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003648void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003649 LocationSummary* locations =
3650 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003651 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003652 case Primitive::kPrimBoolean:
3653 case Primitive::kPrimByte:
3654 case Primitive::kPrimShort:
3655 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003656 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00003657 case Primitive::kPrimLong: {
3658 locations->SetInAt(0, Location::RequiresRegister());
3659 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003660 // Output overlaps because it is written before doing the low comparison.
3661 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00003662 break;
3663 }
3664 case Primitive::kPrimFloat:
3665 case Primitive::kPrimDouble: {
3666 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko2887aa22016-08-01 17:41:45 +01003667 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
Calin Juravleddb7df22014-11-25 20:56:51 +00003668 locations->SetOut(Location::RequiresRegister());
3669 break;
3670 }
3671 default:
3672 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3673 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003674}
3675
3676void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003677 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003678 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003679 Location left = locations->InAt(0);
3680 Location right = locations->InAt(1);
3681
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003682 Label less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00003683 Primitive::Type type = compare->InputAt(0)->GetType();
Vladimir Markod6e069b2016-01-18 11:11:01 +00003684 Condition less_cond;
Calin Juravleddb7df22014-11-25 20:56:51 +00003685 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003686 case Primitive::kPrimBoolean:
3687 case Primitive::kPrimByte:
3688 case Primitive::kPrimShort:
3689 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003690 case Primitive::kPrimInt: {
3691 __ LoadImmediate(out, 0);
3692 __ cmp(left.AsRegister<Register>(),
3693 ShifterOperand(right.AsRegister<Register>())); // Signed compare.
3694 less_cond = LT;
3695 break;
3696 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003697 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003698 __ cmp(left.AsRegisterPairHigh<Register>(),
3699 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003700 __ b(&less, LT);
3701 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003702 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00003703 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003704 __ cmp(left.AsRegisterPairLow<Register>(),
3705 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Vladimir Markod6e069b2016-01-18 11:11:01 +00003706 less_cond = LO;
Calin Juravleddb7df22014-11-25 20:56:51 +00003707 break;
3708 }
3709 case Primitive::kPrimFloat:
3710 case Primitive::kPrimDouble: {
3711 __ LoadImmediate(out, 0);
Vladimir Marko2887aa22016-08-01 17:41:45 +01003712 GenerateVcmp(compare);
Calin Juravleddb7df22014-11-25 20:56:51 +00003713 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00003714 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003715 break;
3716 }
3717 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003718 LOG(FATAL) << "Unexpected compare type " << type;
Vladimir Markod6e069b2016-01-18 11:11:01 +00003719 UNREACHABLE();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003720 }
Aart Bika19616e2016-02-01 18:57:58 -08003721
Calin Juravleddb7df22014-11-25 20:56:51 +00003722 __ b(&done, EQ);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003723 __ b(&less, less_cond);
Calin Juravleddb7df22014-11-25 20:56:51 +00003724
3725 __ Bind(&greater);
3726 __ LoadImmediate(out, 1);
3727 __ b(&done);
3728
3729 __ Bind(&less);
3730 __ LoadImmediate(out, -1);
3731
3732 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003733}
3734
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003735void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003736 LocationSummary* locations =
3737 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko3925c6e2016-05-17 16:30:10 +01003738 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003739 locations->SetInAt(i, Location::Any());
3740 }
3741 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003742}
3743
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003744void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003745 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003746}
3747
Roland Levillainc9285912015-12-18 10:38:42 +00003748void CodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
3749 // TODO (ported from quick): revisit ARM barrier kinds.
3750 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
Calin Juravle52c48962014-12-16 17:02:57 +00003751 switch (kind) {
3752 case MemBarrierKind::kAnyStore:
3753 case MemBarrierKind::kLoadAny:
3754 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003755 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00003756 break;
3757 }
3758 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003759 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00003760 break;
3761 }
3762 default:
3763 LOG(FATAL) << "Unexpected memory barrier " << kind;
3764 }
Kenny Root1d8199d2015-06-02 11:01:10 -07003765 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00003766}
3767
3768void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
3769 uint32_t offset,
3770 Register out_lo,
3771 Register out_hi) {
3772 if (offset != 0) {
Roland Levillain3b359c72015-11-17 19:35:12 +00003773 // Ensure `out_lo` is different from `addr`, so that loading
3774 // `offset` into `out_lo` does not clutter `addr`.
3775 DCHECK_NE(out_lo, addr);
Calin Juravle52c48962014-12-16 17:02:57 +00003776 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003777 __ add(IP, addr, ShifterOperand(out_lo));
3778 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003779 }
3780 __ ldrexd(out_lo, out_hi, addr);
3781}
3782
3783void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
3784 uint32_t offset,
3785 Register value_lo,
3786 Register value_hi,
3787 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00003788 Register temp2,
3789 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003790 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00003791 if (offset != 0) {
3792 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003793 __ add(IP, addr, ShifterOperand(temp1));
3794 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003795 }
3796 __ Bind(&fail);
3797 // We need a load followed by store. (The address used in a STREX instruction must
3798 // be the same as the address in the most recently executed LDREX instruction.)
3799 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00003800 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003801 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003802 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00003803}
3804
3805void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3806 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3807
Nicolas Geoffray39468442014-09-02 15:17:15 +01003808 LocationSummary* locations =
3809 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003810 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003811
Calin Juravle52c48962014-12-16 17:02:57 +00003812 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003813 if (Primitive::IsFloatingPointType(field_type)) {
3814 locations->SetInAt(1, Location::RequiresFpuRegister());
3815 } else {
3816 locations->SetInAt(1, Location::RequiresRegister());
3817 }
3818
Calin Juravle52c48962014-12-16 17:02:57 +00003819 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00003820 bool generate_volatile = field_info.IsVolatile()
3821 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003822 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01003823 bool needs_write_barrier =
3824 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003825 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00003826 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01003827 if (needs_write_barrier) {
3828 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003829 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003830 } else if (generate_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00003831 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00003832 // - registers need to be consecutive
3833 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00003834 // We don't test for ARM yet, and the assertion makes sure that we
3835 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00003836 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3837
3838 locations->AddTemp(Location::RequiresRegister());
3839 locations->AddTemp(Location::RequiresRegister());
3840 if (field_type == Primitive::kPrimDouble) {
3841 // For doubles we need two more registers to copy the value.
3842 locations->AddTemp(Location::RegisterLocation(R2));
3843 locations->AddTemp(Location::RegisterLocation(R3));
3844 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003845 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003846}
3847
Calin Juravle52c48962014-12-16 17:02:57 +00003848void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003849 const FieldInfo& field_info,
3850 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003851 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3852
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003853 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003854 Register base = locations->InAt(0).AsRegister<Register>();
3855 Location value = locations->InAt(1);
3856
3857 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003858 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00003859 Primitive::Type field_type = field_info.GetFieldType();
3860 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01003861 bool needs_write_barrier =
3862 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003863
3864 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00003865 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00003866 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003867
3868 switch (field_type) {
3869 case Primitive::kPrimBoolean:
3870 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003871 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003872 break;
3873 }
3874
3875 case Primitive::kPrimShort:
3876 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003877 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003878 break;
3879 }
3880
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003881 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003882 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01003883 if (kPoisonHeapReferences && needs_write_barrier) {
3884 // Note that in the case where `value` is a null reference,
3885 // we do not enter this block, as a null reference does not
3886 // need poisoning.
3887 DCHECK_EQ(field_type, Primitive::kPrimNot);
3888 Register temp = locations->GetTemp(0).AsRegister<Register>();
3889 __ Mov(temp, value.AsRegister<Register>());
3890 __ PoisonHeapReference(temp);
3891 __ StoreToOffset(kStoreWord, temp, base, offset);
3892 } else {
3893 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
3894 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003895 break;
3896 }
3897
3898 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00003899 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003900 GenerateWideAtomicStore(base, offset,
3901 value.AsRegisterPairLow<Register>(),
3902 value.AsRegisterPairHigh<Register>(),
3903 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003904 locations->GetTemp(1).AsRegister<Register>(),
3905 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003906 } else {
3907 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003908 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003909 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003910 break;
3911 }
3912
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003913 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003914 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003915 break;
3916 }
3917
3918 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003919 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00003920 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003921 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
3922 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
3923
3924 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
3925
3926 GenerateWideAtomicStore(base, offset,
3927 value_reg_lo,
3928 value_reg_hi,
3929 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003930 locations->GetTemp(3).AsRegister<Register>(),
3931 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003932 } else {
3933 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003934 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003935 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003936 break;
3937 }
3938
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003939 case Primitive::kPrimVoid:
3940 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003941 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003942 }
Calin Juravle52c48962014-12-16 17:02:57 +00003943
Calin Juravle77520bc2015-01-12 18:45:46 +00003944 // Longs and doubles are handled in the switch.
3945 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
3946 codegen_->MaybeRecordImplicitNullCheck(instruction);
3947 }
3948
3949 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3950 Register temp = locations->GetTemp(0).AsRegister<Register>();
3951 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003952 codegen_->MarkGCCard(
3953 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003954 }
3955
Calin Juravle52c48962014-12-16 17:02:57 +00003956 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00003957 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00003958 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003959}
3960
Calin Juravle52c48962014-12-16 17:02:57 +00003961void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3962 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain3b359c72015-11-17 19:35:12 +00003963
3964 bool object_field_get_with_read_barrier =
3965 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003966 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00003967 new (GetGraph()->GetArena()) LocationSummary(instruction,
3968 object_field_get_with_read_barrier ?
3969 LocationSummary::kCallOnSlowPath :
3970 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003971 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003972
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003973 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00003974 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003975 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain3b359c72015-11-17 19:35:12 +00003976 // The output overlaps in case of volatile long: we don't want the
3977 // code generated by GenerateWideAtomicLoad to overwrite the
3978 // object's location. Likewise, in the case of an object field get
3979 // with read barriers enabled, we do not want the load to overwrite
3980 // the object's location, as we need it to emit the read barrier.
3981 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
3982 object_field_get_with_read_barrier;
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01003983
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003984 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3985 locations->SetOut(Location::RequiresFpuRegister());
3986 } else {
3987 locations->SetOut(Location::RequiresRegister(),
3988 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
3989 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003990 if (volatile_for_double) {
Roland Levillainc9285912015-12-18 10:38:42 +00003991 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00003992 // - registers need to be consecutive
3993 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00003994 // We don't test for ARM yet, and the assertion makes sure that we
3995 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00003996 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3997 locations->AddTemp(Location::RequiresRegister());
3998 locations->AddTemp(Location::RequiresRegister());
Roland Levillainc9285912015-12-18 10:38:42 +00003999 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4000 // We need a temporary register for the read barrier marking slow
4001 // path in CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier.
4002 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004003 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004004}
4005
Vladimir Marko2887aa22016-08-01 17:41:45 +01004006Location LocationsBuilderARM::ArithmeticZeroOrFpuRegister(HInstruction* input) {
4007 DCHECK(input->GetType() == Primitive::kPrimDouble || input->GetType() == Primitive::kPrimFloat)
4008 << input->GetType();
4009 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
4010 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
4011 return Location::ConstantLocation(input->AsConstant());
4012 } else {
4013 return Location::RequiresFpuRegister();
4014 }
4015}
4016
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004017Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
4018 Opcode opcode) {
4019 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
4020 if (constant->IsConstant() &&
4021 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
4022 return Location::ConstantLocation(constant->AsConstant());
4023 }
4024 return Location::RequiresRegister();
4025}
4026
4027bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
4028 Opcode opcode) {
4029 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
4030 if (Primitive::Is64BitType(input_cst->GetType())) {
Vladimir Marko966901f2016-08-05 14:37:27 +01004031 Opcode high_opcode = opcode;
4032 SetCc low_set_cc = kCcDontCare;
4033 switch (opcode) {
4034 case SUB:
4035 // Flip the operation to an ADD.
4036 value = -value;
4037 opcode = ADD;
4038 FALLTHROUGH_INTENDED;
4039 case ADD:
4040 if (Low32Bits(value) == 0u) {
4041 return CanEncodeConstantAsImmediate(High32Bits(value), opcode, kCcDontCare);
4042 }
4043 high_opcode = ADC;
4044 low_set_cc = kCcSet;
4045 break;
4046 default:
4047 break;
4048 }
4049 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode, low_set_cc) &&
4050 CanEncodeConstantAsImmediate(High32Bits(value), high_opcode, kCcDontCare);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004051 } else {
4052 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
4053 }
4054}
4055
Vladimir Marko966901f2016-08-05 14:37:27 +01004056bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value,
4057 Opcode opcode,
4058 SetCc set_cc) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004059 ShifterOperand so;
4060 ArmAssembler* assembler = codegen_->GetAssembler();
Vladimir Marko966901f2016-08-05 14:37:27 +01004061 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, set_cc, &so)) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004062 return true;
4063 }
4064 Opcode neg_opcode = kNoOperand;
4065 switch (opcode) {
Vladimir Marko966901f2016-08-05 14:37:27 +01004066 case AND: neg_opcode = BIC; value = ~value; break;
4067 case ORR: neg_opcode = ORN; value = ~value; break;
4068 case ADD: neg_opcode = SUB; value = -value; break;
4069 case ADC: neg_opcode = SBC; value = ~value; break;
4070 case SUB: neg_opcode = ADD; value = -value; break;
4071 case SBC: neg_opcode = ADC; value = ~value; break;
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004072 default:
4073 return false;
4074 }
Vladimir Marko966901f2016-08-05 14:37:27 +01004075 return assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, neg_opcode, value, set_cc, &so);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004076}
4077
Calin Juravle52c48962014-12-16 17:02:57 +00004078void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
4079 const FieldInfo& field_info) {
4080 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004081
Calin Juravle52c48962014-12-16 17:02:57 +00004082 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004083 Location base_loc = locations->InAt(0);
4084 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004085 Location out = locations->Out();
4086 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004087 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004088 Primitive::Type field_type = field_info.GetFieldType();
4089 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4090
4091 switch (field_type) {
Roland Levillainc9285912015-12-18 10:38:42 +00004092 case Primitive::kPrimBoolean:
Calin Juravle52c48962014-12-16 17:02:57 +00004093 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004094 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004095
Roland Levillainc9285912015-12-18 10:38:42 +00004096 case Primitive::kPrimByte:
Calin Juravle52c48962014-12-16 17:02:57 +00004097 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004098 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004099
Roland Levillainc9285912015-12-18 10:38:42 +00004100 case Primitive::kPrimShort:
Calin Juravle52c48962014-12-16 17:02:57 +00004101 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004102 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004103
Roland Levillainc9285912015-12-18 10:38:42 +00004104 case Primitive::kPrimChar:
Calin Juravle52c48962014-12-16 17:02:57 +00004105 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004106 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004107
4108 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004109 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004110 break;
Roland Levillainc9285912015-12-18 10:38:42 +00004111
4112 case Primitive::kPrimNot: {
4113 // /* HeapReference<Object> */ out = *(base + offset)
4114 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4115 Location temp_loc = locations->GetTemp(0);
4116 // Note that a potential implicit null check is handled in this
4117 // CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier call.
4118 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4119 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4120 if (is_volatile) {
4121 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4122 }
4123 } else {
4124 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
4125 codegen_->MaybeRecordImplicitNullCheck(instruction);
4126 if (is_volatile) {
4127 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4128 }
4129 // If read barriers are enabled, emit read barriers other than
4130 // Baker's using a slow path (and also unpoison the loaded
4131 // reference, if heap poisoning is enabled).
4132 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4133 }
4134 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004135 }
4136
Roland Levillainc9285912015-12-18 10:38:42 +00004137 case Primitive::kPrimLong:
Calin Juravle34166012014-12-19 17:22:29 +00004138 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004139 GenerateWideAtomicLoad(base, offset,
4140 out.AsRegisterPairLow<Register>(),
4141 out.AsRegisterPairHigh<Register>());
4142 } else {
4143 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
4144 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004145 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004146
Roland Levillainc9285912015-12-18 10:38:42 +00004147 case Primitive::kPrimFloat:
Calin Juravle52c48962014-12-16 17:02:57 +00004148 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004149 break;
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004150
4151 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004152 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004153 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004154 Register lo = locations->GetTemp(0).AsRegister<Register>();
4155 Register hi = locations->GetTemp(1).AsRegister<Register>();
4156 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00004157 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004158 __ vmovdrr(out_reg, lo, hi);
4159 } else {
4160 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004161 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004162 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004163 break;
4164 }
4165
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004166 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004167 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004168 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004169 }
Calin Juravle52c48962014-12-16 17:02:57 +00004170
Roland Levillainc9285912015-12-18 10:38:42 +00004171 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
4172 // Potential implicit null checks, in the case of reference or
4173 // double fields, are handled in the previous switch statement.
4174 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004175 codegen_->MaybeRecordImplicitNullCheck(instruction);
4176 }
4177
Calin Juravle52c48962014-12-16 17:02:57 +00004178 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004179 if (field_type == Primitive::kPrimNot) {
4180 // Memory barriers, in the case of references, are also handled
4181 // in the previous switch statement.
4182 } else {
4183 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4184 }
Roland Levillain4d027112015-07-01 15:41:14 +01004185 }
Calin Juravle52c48962014-12-16 17:02:57 +00004186}
4187
4188void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4189 HandleFieldSet(instruction, instruction->GetFieldInfo());
4190}
4191
4192void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004193 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004194}
4195
4196void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4197 HandleFieldGet(instruction, instruction->GetFieldInfo());
4198}
4199
4200void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4201 HandleFieldGet(instruction, instruction->GetFieldInfo());
4202}
4203
4204void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4205 HandleFieldGet(instruction, instruction->GetFieldInfo());
4206}
4207
4208void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4209 HandleFieldGet(instruction, instruction->GetFieldInfo());
4210}
4211
4212void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4213 HandleFieldSet(instruction, instruction->GetFieldInfo());
4214}
4215
4216void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004217 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004218}
4219
Calin Juravlee460d1d2015-09-29 04:52:17 +01004220void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
4221 HUnresolvedInstanceFieldGet* instruction) {
4222 FieldAccessCallingConventionARM calling_convention;
4223 codegen_->CreateUnresolvedFieldLocationSummary(
4224 instruction, instruction->GetFieldType(), calling_convention);
4225}
4226
4227void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
4228 HUnresolvedInstanceFieldGet* instruction) {
4229 FieldAccessCallingConventionARM calling_convention;
4230 codegen_->GenerateUnresolvedFieldAccess(instruction,
4231 instruction->GetFieldType(),
4232 instruction->GetFieldIndex(),
4233 instruction->GetDexPc(),
4234 calling_convention);
4235}
4236
4237void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
4238 HUnresolvedInstanceFieldSet* instruction) {
4239 FieldAccessCallingConventionARM calling_convention;
4240 codegen_->CreateUnresolvedFieldLocationSummary(
4241 instruction, instruction->GetFieldType(), calling_convention);
4242}
4243
4244void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
4245 HUnresolvedInstanceFieldSet* instruction) {
4246 FieldAccessCallingConventionARM calling_convention;
4247 codegen_->GenerateUnresolvedFieldAccess(instruction,
4248 instruction->GetFieldType(),
4249 instruction->GetFieldIndex(),
4250 instruction->GetDexPc(),
4251 calling_convention);
4252}
4253
4254void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
4255 HUnresolvedStaticFieldGet* instruction) {
4256 FieldAccessCallingConventionARM calling_convention;
4257 codegen_->CreateUnresolvedFieldLocationSummary(
4258 instruction, instruction->GetFieldType(), calling_convention);
4259}
4260
4261void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
4262 HUnresolvedStaticFieldGet* instruction) {
4263 FieldAccessCallingConventionARM calling_convention;
4264 codegen_->GenerateUnresolvedFieldAccess(instruction,
4265 instruction->GetFieldType(),
4266 instruction->GetFieldIndex(),
4267 instruction->GetDexPc(),
4268 calling_convention);
4269}
4270
4271void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
4272 HUnresolvedStaticFieldSet* instruction) {
4273 FieldAccessCallingConventionARM calling_convention;
4274 codegen_->CreateUnresolvedFieldLocationSummary(
4275 instruction, instruction->GetFieldType(), calling_convention);
4276}
4277
4278void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
4279 HUnresolvedStaticFieldSet* instruction) {
4280 FieldAccessCallingConventionARM calling_convention;
4281 codegen_->GenerateUnresolvedFieldAccess(instruction,
4282 instruction->GetFieldType(),
4283 instruction->GetFieldIndex(),
4284 instruction->GetDexPc(),
4285 calling_convention);
4286}
4287
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004288void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004289 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4290 ? LocationSummary::kCallOnSlowPath
4291 : LocationSummary::kNoCall;
4292 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravle77520bc2015-01-12 18:45:46 +00004293 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004294 if (instruction->HasUses()) {
4295 locations->SetOut(Location::SameAsFirstInput());
4296 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004297}
4298
Calin Juravle2ae48182016-03-16 14:05:09 +00004299void CodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
4300 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004301 return;
4302 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004303 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004304
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004305 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00004306 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004307}
4308
Calin Juravle2ae48182016-03-16 14:05:09 +00004309void CodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004310 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004311 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004312
4313 LocationSummary* locations = instruction->GetLocations();
4314 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004315
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004316 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004317}
4318
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004319void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004320 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004321}
4322
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004323void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004324 bool object_array_get_with_read_barrier =
4325 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004326 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004327 new (GetGraph()->GetArena()) LocationSummary(instruction,
4328 object_array_get_with_read_barrier ?
4329 LocationSummary::kCallOnSlowPath :
4330 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004331 locations->SetInAt(0, Location::RequiresRegister());
4332 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004333 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4334 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4335 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004336 // The output overlaps in the case of an object array get with
4337 // read barriers enabled: we do not want the move to overwrite the
4338 // array's location, as we need it to emit the read barrier.
4339 locations->SetOut(
4340 Location::RequiresRegister(),
4341 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004342 }
Roland Levillainc9285912015-12-18 10:38:42 +00004343 // We need a temporary register for the read barrier marking slow
4344 // path in CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier.
4345 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4346 locations->AddTemp(Location::RequiresRegister());
4347 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004348}
4349
4350void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
4351 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004352 Location obj_loc = locations->InAt(0);
4353 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004354 Location index = locations->InAt(1);
Roland Levillainc9285912015-12-18 10:38:42 +00004355 Location out_loc = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004356
Roland Levillainc9285912015-12-18 10:38:42 +00004357 Primitive::Type type = instruction->GetType();
Roland Levillain4d027112015-07-01 15:41:14 +01004358 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004359 case Primitive::kPrimBoolean: {
4360 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004361 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004362 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004363 size_t offset =
4364 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004365 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
4366 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004367 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004368 __ LoadFromOffset(kLoadUnsignedByte, out, IP, data_offset);
4369 }
4370 break;
4371 }
4372
4373 case Primitive::kPrimByte: {
4374 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004375 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004376 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004377 size_t offset =
4378 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004379 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
4380 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004381 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004382 __ LoadFromOffset(kLoadSignedByte, out, IP, data_offset);
4383 }
4384 break;
4385 }
4386
4387 case Primitive::kPrimShort: {
4388 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004389 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004390 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004391 size_t offset =
4392 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004393 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
4394 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004395 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004396 __ LoadFromOffset(kLoadSignedHalfword, out, IP, data_offset);
4397 }
4398 break;
4399 }
4400
4401 case Primitive::kPrimChar: {
4402 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004403 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004404 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004405 size_t offset =
4406 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004407 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
4408 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004409 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004410 __ LoadFromOffset(kLoadUnsignedHalfword, out, IP, data_offset);
4411 }
4412 break;
4413 }
4414
Roland Levillainc9285912015-12-18 10:38:42 +00004415 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004416 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004417 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004418 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004419 size_t offset =
4420 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004421 __ LoadFromOffset(kLoadWord, out, obj, offset);
4422 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004423 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004424 __ LoadFromOffset(kLoadWord, out, IP, data_offset);
4425 }
4426 break;
4427 }
4428
Roland Levillainc9285912015-12-18 10:38:42 +00004429 case Primitive::kPrimNot: {
4430 static_assert(
4431 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4432 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
4433 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4434 // /* HeapReference<Object> */ out =
4435 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4436 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4437 Location temp = locations->GetTemp(0);
4438 // Note that a potential implicit null check is handled in this
4439 // CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier call.
4440 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4441 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4442 } else {
4443 Register out = out_loc.AsRegister<Register>();
4444 if (index.IsConstant()) {
4445 size_t offset =
4446 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4447 __ LoadFromOffset(kLoadWord, out, obj, offset);
4448 codegen_->MaybeRecordImplicitNullCheck(instruction);
4449 // If read barriers are enabled, emit read barriers other than
4450 // Baker's using a slow path (and also unpoison the loaded
4451 // reference, if heap poisoning is enabled).
4452 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4453 } else {
4454 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4455 __ LoadFromOffset(kLoadWord, out, IP, data_offset);
4456 codegen_->MaybeRecordImplicitNullCheck(instruction);
4457 // If read barriers are enabled, emit read barriers other than
4458 // Baker's using a slow path (and also unpoison the loaded
4459 // reference, if heap poisoning is enabled).
4460 codegen_->MaybeGenerateReadBarrierSlow(
4461 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4462 }
4463 }
4464 break;
4465 }
4466
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004467 case Primitive::kPrimLong: {
4468 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004469 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004470 size_t offset =
4471 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004472 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004473 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004474 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00004475 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004476 }
4477 break;
4478 }
4479
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004480 case Primitive::kPrimFloat: {
4481 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004482 SRegister out = out_loc.AsFpuRegister<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004483 if (index.IsConstant()) {
4484 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004485 __ LoadSFromOffset(out, obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004486 } else {
4487 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillainc9285912015-12-18 10:38:42 +00004488 __ LoadSFromOffset(out, IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004489 }
4490 break;
4491 }
4492
4493 case Primitive::kPrimDouble: {
4494 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004495 SRegister out = out_loc.AsFpuRegisterPairLow<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004496 if (index.IsConstant()) {
4497 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004498 __ LoadDFromOffset(FromLowSToD(out), obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004499 } else {
4500 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00004501 __ LoadDFromOffset(FromLowSToD(out), IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004502 }
4503 break;
4504 }
4505
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004506 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004507 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004508 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004509 }
Roland Levillain4d027112015-07-01 15:41:14 +01004510
4511 if (type == Primitive::kPrimNot) {
Roland Levillainc9285912015-12-18 10:38:42 +00004512 // Potential implicit null checks, in the case of reference
4513 // arrays, are handled in the previous switch statement.
4514 } else {
4515 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004516 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004517}
4518
4519void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004520 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004521
4522 bool needs_write_barrier =
4523 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain3b359c72015-11-17 19:35:12 +00004524 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004525
Nicolas Geoffray39468442014-09-02 15:17:15 +01004526 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004527 instruction,
Vladimir Markoefafb122016-08-25 15:20:47 +01004528 may_need_runtime_call_for_type_check ?
Roland Levillain3b359c72015-11-17 19:35:12 +00004529 LocationSummary::kCallOnSlowPath :
4530 LocationSummary::kNoCall);
4531
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004532 locations->SetInAt(0, Location::RequiresRegister());
4533 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4534 if (Primitive::IsFloatingPointType(value_type)) {
4535 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004536 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004537 locations->SetInAt(2, Location::RequiresRegister());
4538 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004539 if (needs_write_barrier) {
4540 // Temporary registers for the write barrier.
4541 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00004542 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004543 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004544}
4545
4546void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
4547 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004548 Location array_loc = locations->InAt(0);
4549 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004550 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004551 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain3b359c72015-11-17 19:35:12 +00004552 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004553 bool needs_write_barrier =
4554 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004555
4556 switch (value_type) {
4557 case Primitive::kPrimBoolean:
4558 case Primitive::kPrimByte: {
4559 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004560 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004561 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004562 size_t offset =
4563 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004564 __ StoreToOffset(kStoreByte, value, array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004565 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004566 __ add(IP, array, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004567 __ StoreToOffset(kStoreByte, value, IP, data_offset);
4568 }
4569 break;
4570 }
4571
4572 case Primitive::kPrimShort:
4573 case Primitive::kPrimChar: {
4574 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004575 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004576 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004577 size_t offset =
4578 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004579 __ StoreToOffset(kStoreHalfword, value, array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004580 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004581 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004582 __ StoreToOffset(kStoreHalfword, value, IP, data_offset);
4583 }
4584 break;
4585 }
4586
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004587 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004588 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00004589 Location value_loc = locations->InAt(2);
4590 Register value = value_loc.AsRegister<Register>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004591 Register source = value;
4592
4593 if (instruction->InputAt(2)->IsNullConstant()) {
4594 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004595 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004596 size_t offset =
4597 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004598 __ StoreToOffset(kStoreWord, source, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004599 } else {
4600 DCHECK(index.IsRegister()) << index;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004601 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillain4d027112015-07-01 15:41:14 +01004602 __ StoreToOffset(kStoreWord, source, IP, data_offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004603 }
Roland Levillain1407ee72016-01-08 15:56:19 +00004604 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain3b359c72015-11-17 19:35:12 +00004605 DCHECK(!needs_write_barrier);
4606 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004607 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004608 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004609
4610 DCHECK(needs_write_barrier);
4611 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
4612 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
4613 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4614 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4615 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4616 Label done;
4617 SlowPathCode* slow_path = nullptr;
4618
Roland Levillain3b359c72015-11-17 19:35:12 +00004619 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004620 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
4621 codegen_->AddSlowPath(slow_path);
4622 if (instruction->GetValueCanBeNull()) {
4623 Label non_zero;
4624 __ CompareAndBranchIfNonZero(value, &non_zero);
4625 if (index.IsConstant()) {
4626 size_t offset =
4627 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4628 __ StoreToOffset(kStoreWord, value, array, offset);
4629 } else {
4630 DCHECK(index.IsRegister()) << index;
4631 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4632 __ StoreToOffset(kStoreWord, value, IP, data_offset);
4633 }
4634 codegen_->MaybeRecordImplicitNullCheck(instruction);
4635 __ b(&done);
4636 __ Bind(&non_zero);
4637 }
4638
Roland Levillain3b359c72015-11-17 19:35:12 +00004639 if (kEmitCompilerReadBarrier) {
4640 // When read barriers are enabled, the type checking
4641 // instrumentation requires two read barriers:
4642 //
4643 // __ Mov(temp2, temp1);
4644 // // /* HeapReference<Class> */ temp1 = temp1->component_type_
4645 // __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
Roland Levillainc9285912015-12-18 10:38:42 +00004646 // codegen_->GenerateReadBarrierSlow(
Roland Levillain3b359c72015-11-17 19:35:12 +00004647 // instruction, temp1_loc, temp1_loc, temp2_loc, component_offset);
4648 //
4649 // // /* HeapReference<Class> */ temp2 = value->klass_
4650 // __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
Roland Levillainc9285912015-12-18 10:38:42 +00004651 // codegen_->GenerateReadBarrierSlow(
Roland Levillain3b359c72015-11-17 19:35:12 +00004652 // instruction, temp2_loc, temp2_loc, value_loc, class_offset, temp1_loc);
4653 //
4654 // __ cmp(temp1, ShifterOperand(temp2));
4655 //
4656 // However, the second read barrier may trash `temp`, as it
4657 // is a temporary register, and as such would not be saved
4658 // along with live registers before calling the runtime (nor
4659 // restored afterwards). So in this case, we bail out and
4660 // delegate the work to the array set slow path.
4661 //
4662 // TODO: Extend the register allocator to support a new
4663 // "(locally) live temp" location so as to avoid always
4664 // going into the slow path when read barriers are enabled.
4665 __ b(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004666 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004667 // /* HeapReference<Class> */ temp1 = array->klass_
4668 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
4669 codegen_->MaybeRecordImplicitNullCheck(instruction);
4670 __ MaybeUnpoisonHeapReference(temp1);
4671
4672 // /* HeapReference<Class> */ temp1 = temp1->component_type_
4673 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
4674 // /* HeapReference<Class> */ temp2 = value->klass_
4675 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
4676 // If heap poisoning is enabled, no need to unpoison `temp1`
4677 // nor `temp2`, as we are comparing two poisoned references.
4678 __ cmp(temp1, ShifterOperand(temp2));
4679
4680 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4681 Label do_put;
4682 __ b(&do_put, EQ);
4683 // If heap poisoning is enabled, the `temp1` reference has
4684 // not been unpoisoned yet; unpoison it now.
4685 __ MaybeUnpoisonHeapReference(temp1);
4686
4687 // /* HeapReference<Class> */ temp1 = temp1->super_class_
4688 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
4689 // If heap poisoning is enabled, no need to unpoison
4690 // `temp1`, as we are comparing against null below.
4691 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
4692 __ Bind(&do_put);
4693 } else {
4694 __ b(slow_path->GetEntryLabel(), NE);
4695 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004696 }
4697 }
4698
4699 if (kPoisonHeapReferences) {
4700 // Note that in the case where `value` is a null reference,
4701 // we do not enter this block, as a null reference does not
4702 // need poisoning.
4703 DCHECK_EQ(value_type, Primitive::kPrimNot);
4704 __ Mov(temp1, value);
4705 __ PoisonHeapReference(temp1);
4706 source = temp1;
4707 }
4708
4709 if (index.IsConstant()) {
4710 size_t offset =
4711 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4712 __ StoreToOffset(kStoreWord, source, array, offset);
4713 } else {
4714 DCHECK(index.IsRegister()) << index;
4715 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4716 __ StoreToOffset(kStoreWord, source, IP, data_offset);
4717 }
4718
Roland Levillain3b359c72015-11-17 19:35:12 +00004719 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004720 codegen_->MaybeRecordImplicitNullCheck(instruction);
4721 }
4722
4723 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
4724
4725 if (done.IsLinked()) {
4726 __ Bind(&done);
4727 }
4728
4729 if (slow_path != nullptr) {
4730 __ Bind(slow_path->GetExitLabel());
4731 }
4732
4733 break;
4734 }
4735
4736 case Primitive::kPrimInt: {
4737 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4738 Register value = locations->InAt(2).AsRegister<Register>();
4739 if (index.IsConstant()) {
4740 size_t offset =
4741 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4742 __ StoreToOffset(kStoreWord, value, array, offset);
4743 } else {
4744 DCHECK(index.IsRegister()) << index;
4745 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4746 __ StoreToOffset(kStoreWord, value, IP, data_offset);
4747 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004748 break;
4749 }
4750
4751 case Primitive::kPrimLong: {
4752 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004753 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004754 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004755 size_t offset =
4756 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004757 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004758 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004759 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004760 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004761 }
4762 break;
4763 }
4764
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004765 case Primitive::kPrimFloat: {
4766 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4767 Location value = locations->InAt(2);
4768 DCHECK(value.IsFpuRegister());
4769 if (index.IsConstant()) {
4770 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004771 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004772 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004773 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004774 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
4775 }
4776 break;
4777 }
4778
4779 case Primitive::kPrimDouble: {
4780 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4781 Location value = locations->InAt(2);
4782 DCHECK(value.IsFpuRegisterPair());
4783 if (index.IsConstant()) {
4784 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004785 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004786 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004787 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004788 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
4789 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004790
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004791 break;
4792 }
4793
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004794 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004795 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004796 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004797 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004798
Roland Levillain80e67092016-01-08 16:04:55 +00004799 // Objects are handled in the switch.
4800 if (value_type != Primitive::kPrimNot) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004801 codegen_->MaybeRecordImplicitNullCheck(instruction);
4802 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004803}
4804
4805void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004806 LocationSummary* locations =
4807 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004808 locations->SetInAt(0, Location::RequiresRegister());
4809 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004810}
4811
4812void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
4813 LocationSummary* locations = instruction->GetLocations();
Vladimir Marko51c103e2016-04-28 13:10:02 +01004814 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004815 Register obj = locations->InAt(0).AsRegister<Register>();
4816 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004817 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004818 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004819}
4820
4821void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004822 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4823 ? LocationSummary::kCallOnSlowPath
4824 : LocationSummary::kNoCall;
4825 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004826 locations->SetInAt(0, Location::RequiresRegister());
4827 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004828 if (instruction->HasUses()) {
4829 locations->SetOut(Location::SameAsFirstInput());
4830 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004831}
4832
4833void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
4834 LocationSummary* locations = instruction->GetLocations();
Andreas Gampe85b62f22015-09-09 13:15:38 -07004835 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004836 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004837 codegen_->AddSlowPath(slow_path);
4838
Roland Levillain271ab9c2014-11-27 15:23:57 +00004839 Register index = locations->InAt(0).AsRegister<Register>();
4840 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004841
4842 __ cmp(index, ShifterOperand(length));
Roland Levillain4fa13f62015-07-06 18:11:54 +01004843 __ b(slow_path->GetEntryLabel(), HS);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004844}
4845
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004846void CodeGeneratorARM::MarkGCCard(Register temp,
4847 Register card,
4848 Register object,
4849 Register value,
4850 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004851 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004852 if (can_be_null) {
4853 __ CompareAndBranchIfZero(value, &is_null);
4854 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004855 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmWordSize>().Int32Value());
4856 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
4857 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004858 if (can_be_null) {
4859 __ Bind(&is_null);
4860 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004861}
4862
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004863void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004864 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004865}
4866
4867void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004868 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4869}
4870
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004871void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
4872 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4873}
4874
4875void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004876 HBasicBlock* block = instruction->GetBlock();
4877 if (block->GetLoopInformation() != nullptr) {
4878 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4879 // The back edge will generate the suspend check.
4880 return;
4881 }
4882 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4883 // The goto will generate the suspend check.
4884 return;
4885 }
4886 GenerateSuspendCheck(instruction, nullptr);
4887}
4888
4889void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
4890 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004891 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004892 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
4893 if (slow_path == nullptr) {
4894 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
4895 instruction->SetSlowPath(slow_path);
4896 codegen_->AddSlowPath(slow_path);
4897 if (successor != nullptr) {
4898 DCHECK(successor->IsLoopHeader());
4899 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4900 }
4901 } else {
4902 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4903 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004904
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00004905 __ LoadFromOffset(
4906 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmWordSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004907 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004908 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004909 __ Bind(slow_path->GetReturnLabel());
4910 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004911 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004912 __ b(slow_path->GetEntryLabel());
4913 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004914}
4915
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004916ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
4917 return codegen_->GetAssembler();
4918}
4919
4920void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004921 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004922 Location source = move->GetSource();
4923 Location destination = move->GetDestination();
4924
4925 if (source.IsRegister()) {
4926 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004927 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00004928 } else if (destination.IsFpuRegister()) {
4929 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004930 } else {
4931 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004932 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004933 SP, destination.GetStackIndex());
4934 }
4935 } else if (source.IsStackSlot()) {
4936 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004937 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004938 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004939 } else if (destination.IsFpuRegister()) {
4940 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004941 } else {
4942 DCHECK(destination.IsStackSlot());
4943 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
4944 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4945 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004946 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00004947 if (destination.IsRegister()) {
4948 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
4949 } else if (destination.IsFpuRegister()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004950 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004951 } else {
4952 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004953 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
4954 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004955 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004956 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004957 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
4958 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004959 } else if (destination.IsRegisterPair()) {
4960 DCHECK(ExpectedPairLayout(destination));
4961 __ LoadFromOffset(
4962 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
4963 } else {
4964 DCHECK(destination.IsFpuRegisterPair()) << destination;
4965 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
4966 SP,
4967 source.GetStackIndex());
4968 }
4969 } else if (source.IsRegisterPair()) {
4970 if (destination.IsRegisterPair()) {
4971 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
4972 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00004973 } else if (destination.IsFpuRegisterPair()) {
4974 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
4975 source.AsRegisterPairLow<Register>(),
4976 source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004977 } else {
4978 DCHECK(destination.IsDoubleStackSlot()) << destination;
4979 DCHECK(ExpectedPairLayout(source));
4980 __ StoreToOffset(
4981 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
4982 }
4983 } else if (source.IsFpuRegisterPair()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00004984 if (destination.IsRegisterPair()) {
4985 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
4986 destination.AsRegisterPairHigh<Register>(),
4987 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
4988 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004989 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
4990 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
4991 } else {
4992 DCHECK(destination.IsDoubleStackSlot()) << destination;
4993 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
4994 SP,
4995 destination.GetStackIndex());
4996 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004997 } else {
4998 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004999 HConstant* constant = source.GetConstant();
5000 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5001 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005002 if (destination.IsRegister()) {
5003 __ LoadImmediate(destination.AsRegister<Register>(), value);
5004 } else {
5005 DCHECK(destination.IsStackSlot());
5006 __ LoadImmediate(IP, value);
5007 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5008 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005009 } else if (constant->IsLongConstant()) {
5010 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005011 if (destination.IsRegisterPair()) {
5012 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
5013 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005014 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005015 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005016 __ LoadImmediate(IP, Low32Bits(value));
5017 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5018 __ LoadImmediate(IP, High32Bits(value));
5019 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5020 }
5021 } else if (constant->IsDoubleConstant()) {
5022 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005023 if (destination.IsFpuRegisterPair()) {
5024 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005025 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005026 DCHECK(destination.IsDoubleStackSlot()) << destination;
5027 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005028 __ LoadImmediate(IP, Low32Bits(int_value));
5029 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5030 __ LoadImmediate(IP, High32Bits(int_value));
5031 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5032 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005033 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005034 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005035 float value = constant->AsFloatConstant()->GetValue();
5036 if (destination.IsFpuRegister()) {
5037 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
5038 } else {
5039 DCHECK(destination.IsStackSlot());
5040 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
5041 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5042 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005043 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005044 }
5045}
5046
5047void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
5048 __ Mov(IP, reg);
5049 __ LoadFromOffset(kLoadWord, reg, SP, mem);
5050 __ StoreToOffset(kStoreWord, IP, SP, mem);
5051}
5052
5053void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
5054 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
5055 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
5056 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
5057 SP, mem1 + stack_offset);
5058 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
5059 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
5060 SP, mem2 + stack_offset);
5061 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
5062}
5063
5064void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005065 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005066 Location source = move->GetSource();
5067 Location destination = move->GetDestination();
5068
5069 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005070 DCHECK_NE(source.AsRegister<Register>(), IP);
5071 DCHECK_NE(destination.AsRegister<Register>(), IP);
5072 __ Mov(IP, source.AsRegister<Register>());
5073 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
5074 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005075 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005076 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005077 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005078 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005079 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5080 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005081 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005082 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005083 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005084 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005085 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005086 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005087 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005088 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005089 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5090 destination.AsRegisterPairHigh<Register>(),
5091 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005092 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005093 Register low_reg = source.IsRegisterPair()
5094 ? source.AsRegisterPairLow<Register>()
5095 : destination.AsRegisterPairLow<Register>();
5096 int mem = source.IsRegisterPair()
5097 ? destination.GetStackIndex()
5098 : source.GetStackIndex();
5099 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005100 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005101 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005102 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005103 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005104 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
5105 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005106 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005107 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005108 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005109 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
5110 DRegister reg = source.IsFpuRegisterPair()
5111 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
5112 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
5113 int mem = source.IsFpuRegisterPair()
5114 ? destination.GetStackIndex()
5115 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005116 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005117 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005118 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005119 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
5120 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
5121 : destination.AsFpuRegister<SRegister>();
5122 int mem = source.IsFpuRegister()
5123 ? destination.GetStackIndex()
5124 : source.GetStackIndex();
5125
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005126 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005127 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005128 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005129 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005130 Exchange(source.GetStackIndex(), destination.GetStackIndex());
5131 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005132 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005133 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005134 }
5135}
5136
5137void ParallelMoveResolverARM::SpillScratch(int reg) {
5138 __ Push(static_cast<Register>(reg));
5139}
5140
5141void ParallelMoveResolverARM::RestoreScratch(int reg) {
5142 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005143}
5144
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005145void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01005146 InvokeRuntimeCallingConvention calling_convention;
5147 CodeGenerator::CreateLoadClassLocationSummary(
5148 cls,
5149 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Roland Levillain3b359c72015-11-17 19:35:12 +00005150 Location::RegisterLocation(R0),
5151 /* code_generator_supports_read_barrier */ true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005152}
5153
5154void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005155 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005156 if (cls->NeedsAccessCheck()) {
5157 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5158 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5159 cls,
5160 cls->GetDexPc(),
5161 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005162 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005163 return;
5164 }
5165
Roland Levillain3b359c72015-11-17 19:35:12 +00005166 Location out_loc = locations->Out();
5167 Register out = out_loc.AsRegister<Register>();
Calin Juravle580b6092015-10-06 17:35:58 +01005168 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005169
Calin Juravle580b6092015-10-06 17:35:58 +01005170 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005171 DCHECK(!cls->CanCallRuntime());
5172 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillainc9285912015-12-18 10:38:42 +00005173 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5174 GenerateGcRootFieldLoad(
5175 cls, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005176 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00005177 // /* GcRoot<mirror::Class>[] */ out =
5178 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005179 __ LoadFromOffset(kLoadWord,
5180 out,
5181 current_method,
Vladimir Marko05792b92015-08-03 11:56:49 +01005182 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
Roland Levillainc9285912015-12-18 10:38:42 +00005183 // /* GcRoot<mirror::Class> */ out = out[type_index]
5184 GenerateGcRootFieldLoad(cls, out_loc, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005185
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005186 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
5187 DCHECK(cls->CanCallRuntime());
5188 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
5189 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5190 codegen_->AddSlowPath(slow_path);
5191 if (!cls->IsInDexCache()) {
5192 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5193 }
5194 if (cls->MustGenerateClinitCheck()) {
5195 GenerateClassInitializationCheck(slow_path, out);
5196 } else {
5197 __ Bind(slow_path->GetExitLabel());
5198 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005199 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005200 }
5201}
5202
5203void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
5204 LocationSummary* locations =
5205 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5206 locations->SetInAt(0, Location::RequiresRegister());
5207 if (check->HasUses()) {
5208 locations->SetOut(Location::SameAsFirstInput());
5209 }
5210}
5211
5212void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005213 // We assume the class is not null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005214 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005215 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005216 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005217 GenerateClassInitializationCheck(slow_path,
5218 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005219}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005220
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005221void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005222 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005223 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
5224 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
5225 __ b(slow_path->GetEntryLabel(), LT);
5226 // Even if the initialized flag is set, we may be in a situation where caches are not synced
5227 // properly. Therefore, we do a memory fence.
5228 __ dmb(ISH);
5229 __ Bind(slow_path->GetExitLabel());
5230}
5231
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005232HLoadString::LoadKind CodeGeneratorARM::GetSupportedLoadStringKind(
5233 HLoadString::LoadKind desired_string_load_kind) {
5234 if (kEmitCompilerReadBarrier) {
5235 switch (desired_string_load_kind) {
5236 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5237 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5238 case HLoadString::LoadKind::kBootImageAddress:
5239 // TODO: Implement for read barrier.
5240 return HLoadString::LoadKind::kDexCacheViaMethod;
5241 default:
5242 break;
5243 }
5244 }
5245 switch (desired_string_load_kind) {
5246 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5247 DCHECK(!GetCompilerOptions().GetCompilePic());
5248 break;
5249 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5250 DCHECK(GetCompilerOptions().GetCompilePic());
5251 break;
5252 case HLoadString::LoadKind::kBootImageAddress:
5253 break;
5254 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravlee5de54c2016-04-20 14:22:09 +01005255 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005256 break;
5257 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravlee5de54c2016-04-20 14:22:09 +01005258 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005259 // We disable pc-relative load when there is an irreducible loop, as the optimization
5260 // is incompatible with it.
5261 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
5262 // with irreducible loops.
5263 if (GetGraph()->HasIrreducibleLoops()) {
5264 return HLoadString::LoadKind::kDexCacheViaMethod;
5265 }
5266 break;
5267 case HLoadString::LoadKind::kDexCacheViaMethod:
5268 break;
5269 }
5270 return desired_string_load_kind;
5271}
5272
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005273void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005274 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005275 ? LocationSummary::kCallOnSlowPath
5276 : LocationSummary::kNoCall;
5277 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005278 HLoadString::LoadKind load_kind = load->GetLoadKind();
5279 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod ||
5280 load_kind == HLoadString::LoadKind::kDexCachePcRelative) {
5281 locations->SetInAt(0, Location::RequiresRegister());
5282 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005283 locations->SetOut(Location::RequiresRegister());
5284}
5285
5286void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005287 LocationSummary* locations = load->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005288 Location out_loc = locations->Out();
5289 Register out = out_loc.AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005290
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005291 switch (load->GetLoadKind()) {
5292 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
5293 DCHECK(!kEmitCompilerReadBarrier);
5294 __ LoadLiteral(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
5295 load->GetStringIndex()));
5296 return; // No dex cache slow path.
5297 }
5298 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
5299 DCHECK(!kEmitCompilerReadBarrier);
5300 CodeGeneratorARM::PcRelativePatchInfo* labels =
5301 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
5302 __ BindTrackedLabel(&labels->movw_label);
5303 __ movw(out, /* placeholder */ 0u);
5304 __ BindTrackedLabel(&labels->movt_label);
5305 __ movt(out, /* placeholder */ 0u);
5306 __ BindTrackedLabel(&labels->add_pc_label);
5307 __ add(out, out, ShifterOperand(PC));
5308 return; // No dex cache slow path.
5309 }
5310 case HLoadString::LoadKind::kBootImageAddress: {
5311 DCHECK(!kEmitCompilerReadBarrier);
5312 DCHECK_NE(load->GetAddress(), 0u);
5313 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5314 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
5315 return; // No dex cache slow path.
5316 }
5317 case HLoadString::LoadKind::kDexCacheAddress: {
5318 DCHECK_NE(load->GetAddress(), 0u);
5319 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5320 // 16-bit LDR immediate has a 5-bit offset multiplied by the size and that gives
5321 // a 128B range. To try and reduce the number of literals if we load multiple strings,
5322 // simply split the dex cache address to a 128B aligned base loaded from a literal
5323 // and the remaining offset embedded in the load.
5324 static_assert(sizeof(GcRoot<mirror::String>) == 4u, "Expected GC root to be 4 bytes.");
5325 DCHECK_ALIGNED(load->GetAddress(), 4u);
5326 constexpr size_t offset_bits = /* encoded bits */ 5 + /* scale */ 2;
5327 uint32_t base_address = address & ~MaxInt<uint32_t>(offset_bits);
5328 uint32_t offset = address & MaxInt<uint32_t>(offset_bits);
5329 __ LoadLiteral(out, codegen_->DeduplicateDexCacheAddressLiteral(base_address));
5330 GenerateGcRootFieldLoad(load, out_loc, out, offset);
5331 break;
5332 }
5333 case HLoadString::LoadKind::kDexCachePcRelative: {
5334 Register base_reg = locations->InAt(0).AsRegister<Register>();
5335 HArmDexCacheArraysBase* base = load->InputAt(0)->AsArmDexCacheArraysBase();
5336 int32_t offset = load->GetDexCacheElementOffset() - base->GetElementOffset();
5337 GenerateGcRootFieldLoad(load, out_loc, base_reg, offset);
5338 break;
5339 }
5340 case HLoadString::LoadKind::kDexCacheViaMethod: {
5341 Register current_method = locations->InAt(0).AsRegister<Register>();
5342
5343 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5344 GenerateGcRootFieldLoad(
5345 load, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
5346 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
5347 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
5348 // /* GcRoot<mirror::String> */ out = out[string_index]
5349 GenerateGcRootFieldLoad(
5350 load, out_loc, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
5351 break;
5352 }
5353 default:
5354 LOG(FATAL) << "Unexpected load kind: " << load->GetLoadKind();
5355 UNREACHABLE();
5356 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005357
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005358 if (!load->IsInDexCache()) {
5359 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
5360 codegen_->AddSlowPath(slow_path);
5361 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5362 __ Bind(slow_path->GetExitLabel());
5363 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005364}
5365
David Brazdilcb1c0552015-08-04 16:22:25 +01005366static int32_t GetExceptionTlsOffset() {
5367 return Thread::ExceptionOffset<kArmWordSize>().Int32Value();
5368}
5369
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005370void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
5371 LocationSummary* locations =
5372 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5373 locations->SetOut(Location::RequiresRegister());
5374}
5375
5376void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005377 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01005378 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
5379}
5380
5381void LocationsBuilderARM::VisitClearException(HClearException* clear) {
5382 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5383}
5384
5385void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005386 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01005387 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005388}
5389
5390void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
5391 LocationSummary* locations =
5392 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5393 InvokeRuntimeCallingConvention calling_convention;
5394 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5395}
5396
5397void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
5398 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00005399 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005400 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005401}
5402
Roland Levillainc9285912015-12-18 10:38:42 +00005403static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5404 return kEmitCompilerReadBarrier &&
5405 (kUseBakerReadBarrier ||
5406 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5407 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5408 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5409}
5410
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005411void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005412 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain3b359c72015-11-17 19:35:12 +00005413 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5414 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005415 case TypeCheckKind::kExactCheck:
5416 case TypeCheckKind::kAbstractClassCheck:
5417 case TypeCheckKind::kClassHierarchyCheck:
5418 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005419 call_kind =
5420 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005421 break;
5422 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005423 case TypeCheckKind::kUnresolvedCheck:
5424 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005425 call_kind = LocationSummary::kCallOnSlowPath;
5426 break;
5427 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005428
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005429 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain3b359c72015-11-17 19:35:12 +00005430 locations->SetInAt(0, Location::RequiresRegister());
5431 locations->SetInAt(1, Location::RequiresRegister());
5432 // The "out" register is used as a temporary, so it overlaps with the inputs.
5433 // Note that TypeCheckSlowPathARM uses this register too.
5434 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5435 // When read barriers are enabled, we need a temporary register for
5436 // some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00005437 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain3b359c72015-11-17 19:35:12 +00005438 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005439 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005440}
5441
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005442void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00005443 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005444 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005445 Location obj_loc = locations->InAt(0);
5446 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005447 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005448 Location out_loc = locations->Out();
5449 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005450 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillainc9285912015-12-18 10:38:42 +00005451 locations->GetTemp(0) :
5452 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005453 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005454 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5455 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5456 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005457 Label done, zero;
Andreas Gampe85b62f22015-09-09 13:15:38 -07005458 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005459
5460 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005461 // avoid null check if we know obj is not null.
5462 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00005463 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005464 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005465
Roland Levillain3b359c72015-11-17 19:35:12 +00005466 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005467 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005468
Roland Levillainc9285912015-12-18 10:38:42 +00005469 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005470 case TypeCheckKind::kExactCheck: {
5471 __ cmp(out, ShifterOperand(cls));
5472 // Classes must be equal for the instanceof to succeed.
5473 __ b(&zero, NE);
5474 __ LoadImmediate(out, 1);
5475 __ b(&done);
5476 break;
5477 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005478
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005479 case TypeCheckKind::kAbstractClassCheck: {
5480 // If the class is abstract, we eagerly fetch the super class of the
5481 // object to avoid doing a comparison we know will fail.
5482 Label loop;
5483 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005484 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005485 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005486 // If `out` is null, we use it for the result, and jump to `done`.
5487 __ CompareAndBranchIfZero(out, &done);
5488 __ cmp(out, ShifterOperand(cls));
5489 __ b(&loop, NE);
5490 __ LoadImmediate(out, 1);
5491 if (zero.IsLinked()) {
5492 __ b(&done);
5493 }
5494 break;
5495 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005496
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005497 case TypeCheckKind::kClassHierarchyCheck: {
5498 // Walk over the class hierarchy to find a match.
5499 Label loop, success;
5500 __ Bind(&loop);
5501 __ cmp(out, ShifterOperand(cls));
5502 __ b(&success, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005503 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005504 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005505 __ CompareAndBranchIfNonZero(out, &loop);
5506 // If `out` is null, we use it for the result, and jump to `done`.
5507 __ b(&done);
5508 __ Bind(&success);
5509 __ LoadImmediate(out, 1);
5510 if (zero.IsLinked()) {
5511 __ b(&done);
5512 }
5513 break;
5514 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005515
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005516 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005517 // Do an exact check.
5518 Label exact_check;
5519 __ cmp(out, ShifterOperand(cls));
5520 __ b(&exact_check, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005521 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00005522 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005523 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005524 // If `out` is null, we use it for the result, and jump to `done`.
5525 __ CompareAndBranchIfZero(out, &done);
5526 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
5527 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
5528 __ CompareAndBranchIfNonZero(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005529 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005530 __ LoadImmediate(out, 1);
5531 __ b(&done);
5532 break;
5533 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005534
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005535 case TypeCheckKind::kArrayCheck: {
5536 __ cmp(out, ShifterOperand(cls));
5537 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain3b359c72015-11-17 19:35:12 +00005538 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5539 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005540 codegen_->AddSlowPath(slow_path);
5541 __ b(slow_path->GetEntryLabel(), NE);
5542 __ LoadImmediate(out, 1);
5543 if (zero.IsLinked()) {
5544 __ b(&done);
5545 }
5546 break;
5547 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005548
Calin Juravle98893e12015-10-02 21:05:03 +01005549 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005550 case TypeCheckKind::kInterfaceCheck: {
5551 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005552 // into the slow path for the unresolved and interface check
Roland Levillain3b359c72015-11-17 19:35:12 +00005553 // cases.
5554 //
5555 // We cannot directly call the InstanceofNonTrivial runtime
5556 // entry point without resorting to a type checking slow path
5557 // here (i.e. by calling InvokeRuntime directly), as it would
5558 // require to assign fixed registers for the inputs of this
5559 // HInstanceOf instruction (following the runtime calling
5560 // convention), which might be cluttered by the potential first
5561 // read barrier emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00005562 //
5563 // TODO: Introduce a new runtime entry point taking the object
5564 // to test (instead of its class) as argument, and let it deal
5565 // with the read barrier issues. This will let us refactor this
5566 // case of the `switch` code as it was previously (with a direct
5567 // call to the runtime not using a type checking slow path).
5568 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00005569 DCHECK(locations->OnlyCallsOnSlowPath());
5570 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5571 /* is_fatal */ false);
5572 codegen_->AddSlowPath(slow_path);
5573 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005574 if (zero.IsLinked()) {
5575 __ b(&done);
5576 }
5577 break;
5578 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005579 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005580
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005581 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005582 __ Bind(&zero);
5583 __ LoadImmediate(out, 0);
5584 }
5585
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005586 if (done.IsLinked()) {
5587 __ Bind(&done);
5588 }
5589
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005590 if (slow_path != nullptr) {
5591 __ Bind(slow_path->GetExitLabel());
5592 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005593}
5594
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005595void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005596 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5597 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
5598
Roland Levillain3b359c72015-11-17 19:35:12 +00005599 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5600 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005601 case TypeCheckKind::kExactCheck:
5602 case TypeCheckKind::kAbstractClassCheck:
5603 case TypeCheckKind::kClassHierarchyCheck:
5604 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005605 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5606 LocationSummary::kCallOnSlowPath :
5607 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005608 break;
5609 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005610 case TypeCheckKind::kUnresolvedCheck:
5611 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005612 call_kind = LocationSummary::kCallOnSlowPath;
5613 break;
5614 }
5615
Roland Levillain3b359c72015-11-17 19:35:12 +00005616 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5617 locations->SetInAt(0, Location::RequiresRegister());
5618 locations->SetInAt(1, Location::RequiresRegister());
5619 // Note that TypeCheckSlowPathARM uses this "temp" register too.
5620 locations->AddTemp(Location::RequiresRegister());
5621 // When read barriers are enabled, we need an additional temporary
5622 // register for some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00005623 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005624 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005625 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005626}
5627
5628void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00005629 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005630 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005631 Location obj_loc = locations->InAt(0);
5632 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005633 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005634 Location temp_loc = locations->GetTemp(0);
5635 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005636 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillainc9285912015-12-18 10:38:42 +00005637 locations->GetTemp(1) :
5638 Location::NoLocation();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005639 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005640 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5641 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5642 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005643
Roland Levillain3b359c72015-11-17 19:35:12 +00005644 bool is_type_check_slow_path_fatal =
5645 (type_check_kind == TypeCheckKind::kExactCheck ||
5646 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5647 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5648 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5649 !instruction->CanThrowIntoCatchBlock();
5650 SlowPathCode* type_check_slow_path =
5651 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5652 is_type_check_slow_path_fatal);
5653 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005654
5655 Label done;
5656 // Avoid null check if we know obj is not null.
5657 if (instruction->MustDoNullCheck()) {
5658 __ CompareAndBranchIfZero(obj, &done);
5659 }
5660
Roland Levillain3b359c72015-11-17 19:35:12 +00005661 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005662 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005663
Roland Levillain3b359c72015-11-17 19:35:12 +00005664 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005665 case TypeCheckKind::kExactCheck:
5666 case TypeCheckKind::kArrayCheck: {
5667 __ cmp(temp, ShifterOperand(cls));
5668 // Jump to slow path for throwing the exception or doing a
5669 // more involved array check.
Roland Levillain3b359c72015-11-17 19:35:12 +00005670 __ b(type_check_slow_path->GetEntryLabel(), NE);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005671 break;
5672 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005673
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005674 case TypeCheckKind::kAbstractClassCheck: {
5675 // If the class is abstract, we eagerly fetch the super class of the
5676 // object to avoid doing a comparison we know will fail.
Roland Levillain3b359c72015-11-17 19:35:12 +00005677 Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005678 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005679 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005680 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005681
5682 // If the class reference currently in `temp` is not null, jump
5683 // to the `compare_classes` label to compare it with the checked
5684 // class.
5685 __ CompareAndBranchIfNonZero(temp, &compare_classes);
5686 // Otherwise, jump to the slow path to throw the exception.
5687 //
5688 // But before, move back the object's class into `temp` before
5689 // going into the slow path, as it has been overwritten in the
5690 // meantime.
5691 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005692 GenerateReferenceLoadTwoRegisters(
5693 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005694 __ b(type_check_slow_path->GetEntryLabel());
5695
5696 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005697 __ cmp(temp, ShifterOperand(cls));
5698 __ b(&loop, NE);
5699 break;
5700 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005701
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005702 case TypeCheckKind::kClassHierarchyCheck: {
5703 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005704 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005705 __ Bind(&loop);
5706 __ cmp(temp, ShifterOperand(cls));
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005707 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005708
Roland Levillain3b359c72015-11-17 19:35:12 +00005709 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005710 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005711
5712 // If the class reference currently in `temp` is not null, jump
5713 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005714 __ CompareAndBranchIfNonZero(temp, &loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005715 // Otherwise, jump to the slow path to throw the exception.
5716 //
5717 // But before, move back the object's class into `temp` before
5718 // going into the slow path, as it has been overwritten in the
5719 // meantime.
5720 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005721 GenerateReferenceLoadTwoRegisters(
5722 instruction, temp_loc, obj_loc, class_offset, maybe_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
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005727 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005728 // Do an exact check.
Roland Levillain3b359c72015-11-17 19:35:12 +00005729 Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005730 __ cmp(temp, ShifterOperand(cls));
5731 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005732
5733 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00005734 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005735 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005736
5737 // If the component type is not null (i.e. the object is indeed
5738 // an array), jump to label `check_non_primitive_component_type`
5739 // to further check that this component type is not a primitive
5740 // type.
5741 __ CompareAndBranchIfNonZero(temp, &check_non_primitive_component_type);
5742 // Otherwise, jump to the slow path to throw the exception.
5743 //
5744 // But before, move back the object's class into `temp` before
5745 // going into the slow path, as it has been overwritten in the
5746 // meantime.
5747 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005748 GenerateReferenceLoadTwoRegisters(
5749 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005750 __ b(type_check_slow_path->GetEntryLabel());
5751
5752 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005753 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00005754 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
5755 __ CompareAndBranchIfZero(temp, &done);
5756 // Same comment as above regarding `temp` and the slow path.
5757 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005758 GenerateReferenceLoadTwoRegisters(
5759 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005760 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005761 break;
5762 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005763
Calin Juravle98893e12015-10-02 21:05:03 +01005764 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005765 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005766 // We always go into the type check slow path for the unresolved
5767 // and interface check cases.
Roland Levillain3b359c72015-11-17 19:35:12 +00005768 //
5769 // We cannot directly call the CheckCast runtime entry point
5770 // without resorting to a type checking slow path here (i.e. by
5771 // calling InvokeRuntime directly), as it would require to
5772 // assign fixed registers for the inputs of this HInstanceOf
5773 // instruction (following the runtime calling convention), which
5774 // might be cluttered by the potential first read barrier
5775 // emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00005776 //
5777 // TODO: Introduce a new runtime entry point taking the object
5778 // to test (instead of its class) as argument, and let it deal
5779 // with the read barrier issues. This will let us refactor this
5780 // case of the `switch` code as it was previously (with a direct
5781 // call to the runtime not using a type checking slow path).
5782 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00005783 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005784 break;
5785 }
5786 __ Bind(&done);
5787
Roland Levillain3b359c72015-11-17 19:35:12 +00005788 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005789}
5790
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005791void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
5792 LocationSummary* locations =
5793 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5794 InvokeRuntimeCallingConvention calling_convention;
5795 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5796}
5797
5798void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
5799 codegen_->InvokeRuntime(instruction->IsEnter()
5800 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
5801 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00005802 instruction->GetDexPc(),
5803 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005804 if (instruction->IsEnter()) {
5805 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5806 } else {
5807 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5808 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005809}
5810
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005811void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
5812void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
5813void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005814
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005815void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005816 LocationSummary* locations =
5817 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5818 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
5819 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005820 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005821 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005822 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00005823 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005824}
5825
5826void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
5827 HandleBitwiseOperation(instruction);
5828}
5829
5830void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
5831 HandleBitwiseOperation(instruction);
5832}
5833
5834void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
5835 HandleBitwiseOperation(instruction);
5836}
5837
Artem Serov7fc63502016-02-09 17:15:29 +00005838
5839void LocationsBuilderARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
5840 LocationSummary* locations =
5841 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5842 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
5843 || instruction->GetResultType() == Primitive::kPrimLong);
5844
5845 locations->SetInAt(0, Location::RequiresRegister());
5846 locations->SetInAt(1, Location::RequiresRegister());
5847 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5848}
5849
5850void InstructionCodeGeneratorARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
5851 LocationSummary* locations = instruction->GetLocations();
5852 Location first = locations->InAt(0);
5853 Location second = locations->InAt(1);
5854 Location out = locations->Out();
5855
5856 if (instruction->GetResultType() == Primitive::kPrimInt) {
5857 Register first_reg = first.AsRegister<Register>();
5858 ShifterOperand second_reg(second.AsRegister<Register>());
5859 Register out_reg = out.AsRegister<Register>();
5860
5861 switch (instruction->GetOpKind()) {
5862 case HInstruction::kAnd:
5863 __ bic(out_reg, first_reg, second_reg);
5864 break;
5865 case HInstruction::kOr:
5866 __ orn(out_reg, first_reg, second_reg);
5867 break;
5868 // There is no EON on arm.
5869 case HInstruction::kXor:
5870 default:
5871 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
5872 UNREACHABLE();
5873 }
5874 return;
5875
5876 } else {
5877 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
5878 Register first_low = first.AsRegisterPairLow<Register>();
5879 Register first_high = first.AsRegisterPairHigh<Register>();
5880 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
5881 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
5882 Register out_low = out.AsRegisterPairLow<Register>();
5883 Register out_high = out.AsRegisterPairHigh<Register>();
5884
5885 switch (instruction->GetOpKind()) {
5886 case HInstruction::kAnd:
5887 __ bic(out_low, first_low, second_low);
5888 __ bic(out_high, first_high, second_high);
5889 break;
5890 case HInstruction::kOr:
5891 __ orn(out_low, first_low, second_low);
5892 __ orn(out_high, first_high, second_high);
5893 break;
5894 // There is no EON on arm.
5895 case HInstruction::kXor:
5896 default:
5897 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
5898 UNREACHABLE();
5899 }
5900 }
5901}
5902
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005903void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
5904 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
5905 if (value == 0xffffffffu) {
5906 if (out != first) {
5907 __ mov(out, ShifterOperand(first));
5908 }
5909 return;
5910 }
5911 if (value == 0u) {
5912 __ mov(out, ShifterOperand(0));
5913 return;
5914 }
5915 ShifterOperand so;
5916 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
5917 __ and_(out, first, so);
5918 } else {
5919 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so));
5920 __ bic(out, first, ShifterOperand(~value));
5921 }
5922}
5923
5924void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
5925 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
5926 if (value == 0u) {
5927 if (out != first) {
5928 __ mov(out, ShifterOperand(first));
5929 }
5930 return;
5931 }
5932 if (value == 0xffffffffu) {
5933 __ mvn(out, ShifterOperand(0));
5934 return;
5935 }
5936 ShifterOperand so;
5937 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
5938 __ orr(out, first, so);
5939 } else {
5940 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
5941 __ orn(out, first, ShifterOperand(~value));
5942 }
5943}
5944
5945void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
5946 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
5947 if (value == 0u) {
5948 if (out != first) {
5949 __ mov(out, ShifterOperand(first));
5950 }
5951 return;
5952 }
5953 __ eor(out, first, ShifterOperand(value));
5954}
5955
Vladimir Marko966901f2016-08-05 14:37:27 +01005956void InstructionCodeGeneratorARM::GenerateAddLongConst(Location out,
5957 Location first,
5958 uint64_t value) {
5959 Register out_low = out.AsRegisterPairLow<Register>();
5960 Register out_high = out.AsRegisterPairHigh<Register>();
5961 Register first_low = first.AsRegisterPairLow<Register>();
5962 Register first_high = first.AsRegisterPairHigh<Register>();
5963 uint32_t value_low = Low32Bits(value);
5964 uint32_t value_high = High32Bits(value);
5965 if (value_low == 0u) {
5966 if (out_low != first_low) {
5967 __ mov(out_low, ShifterOperand(first_low));
5968 }
5969 __ AddConstant(out_high, first_high, value_high);
5970 return;
5971 }
5972 __ AddConstantSetFlags(out_low, first_low, value_low);
5973 ShifterOperand so;
5974 if (__ ShifterOperandCanHold(out_high, first_high, ADC, value_high, kCcDontCare, &so)) {
5975 __ adc(out_high, first_high, so);
5976 } else if (__ ShifterOperandCanHold(out_low, first_low, SBC, ~value_high, kCcDontCare, &so)) {
5977 __ sbc(out_high, first_high, so);
5978 } else {
5979 LOG(FATAL) << "Unexpected constant " << value_high;
5980 UNREACHABLE();
5981 }
5982}
5983
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005984void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
5985 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005986 Location first = locations->InAt(0);
5987 Location second = locations->InAt(1);
5988 Location out = locations->Out();
5989
5990 if (second.IsConstant()) {
5991 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
5992 uint32_t value_low = Low32Bits(value);
5993 if (instruction->GetResultType() == Primitive::kPrimInt) {
5994 Register first_reg = first.AsRegister<Register>();
5995 Register out_reg = out.AsRegister<Register>();
5996 if (instruction->IsAnd()) {
5997 GenerateAndConst(out_reg, first_reg, value_low);
5998 } else if (instruction->IsOr()) {
5999 GenerateOrrConst(out_reg, first_reg, value_low);
6000 } else {
6001 DCHECK(instruction->IsXor());
6002 GenerateEorConst(out_reg, first_reg, value_low);
6003 }
6004 } else {
6005 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6006 uint32_t value_high = High32Bits(value);
6007 Register first_low = first.AsRegisterPairLow<Register>();
6008 Register first_high = first.AsRegisterPairHigh<Register>();
6009 Register out_low = out.AsRegisterPairLow<Register>();
6010 Register out_high = out.AsRegisterPairHigh<Register>();
6011 if (instruction->IsAnd()) {
6012 GenerateAndConst(out_low, first_low, value_low);
6013 GenerateAndConst(out_high, first_high, value_high);
6014 } else if (instruction->IsOr()) {
6015 GenerateOrrConst(out_low, first_low, value_low);
6016 GenerateOrrConst(out_high, first_high, value_high);
6017 } else {
6018 DCHECK(instruction->IsXor());
6019 GenerateEorConst(out_low, first_low, value_low);
6020 GenerateEorConst(out_high, first_high, value_high);
6021 }
6022 }
6023 return;
6024 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006025
6026 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006027 Register first_reg = first.AsRegister<Register>();
6028 ShifterOperand second_reg(second.AsRegister<Register>());
6029 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006030 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006031 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006032 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006033 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006034 } else {
6035 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006036 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006037 }
6038 } else {
6039 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006040 Register first_low = first.AsRegisterPairLow<Register>();
6041 Register first_high = first.AsRegisterPairHigh<Register>();
6042 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6043 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6044 Register out_low = out.AsRegisterPairLow<Register>();
6045 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006046 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006047 __ and_(out_low, first_low, second_low);
6048 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006049 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006050 __ orr(out_low, first_low, second_low);
6051 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006052 } else {
6053 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006054 __ eor(out_low, first_low, second_low);
6055 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006056 }
6057 }
6058}
6059
Roland Levillainc9285912015-12-18 10:38:42 +00006060void InstructionCodeGeneratorARM::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6061 Location out,
6062 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006063 Location maybe_temp) {
Roland Levillainc9285912015-12-18 10:38:42 +00006064 Register out_reg = out.AsRegister<Register>();
6065 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006066 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006067 if (kUseBakerReadBarrier) {
6068 // Load with fast path based Baker's read barrier.
6069 // /* HeapReference<Object> */ out = *(out + offset)
6070 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006071 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006072 } else {
6073 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006074 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillainc9285912015-12-18 10:38:42 +00006075 // in the following move operation, as we will need it for the
6076 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006077 __ Mov(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillainc9285912015-12-18 10:38:42 +00006078 // /* HeapReference<Object> */ out = *(out + offset)
6079 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006080 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillainc9285912015-12-18 10:38:42 +00006081 }
6082 } else {
6083 // Plain load with no read barrier.
6084 // /* HeapReference<Object> */ out = *(out + offset)
6085 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6086 __ MaybeUnpoisonHeapReference(out_reg);
6087 }
6088}
6089
6090void InstructionCodeGeneratorARM::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6091 Location out,
6092 Location obj,
6093 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006094 Location maybe_temp) {
Roland Levillainc9285912015-12-18 10:38:42 +00006095 Register out_reg = out.AsRegister<Register>();
6096 Register obj_reg = obj.AsRegister<Register>();
6097 if (kEmitCompilerReadBarrier) {
6098 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006099 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006100 // Load with fast path based Baker's read barrier.
6101 // /* HeapReference<Object> */ out = *(obj + offset)
6102 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006103 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006104 } else {
6105 // Load with slow path based read barrier.
6106 // /* HeapReference<Object> */ out = *(obj + offset)
6107 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6108 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6109 }
6110 } else {
6111 // Plain load with no read barrier.
6112 // /* HeapReference<Object> */ out = *(obj + offset)
6113 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6114 __ MaybeUnpoisonHeapReference(out_reg);
6115 }
6116}
6117
6118void InstructionCodeGeneratorARM::GenerateGcRootFieldLoad(HInstruction* instruction,
6119 Location root,
6120 Register obj,
6121 uint32_t offset) {
6122 Register root_reg = root.AsRegister<Register>();
6123 if (kEmitCompilerReadBarrier) {
6124 if (kUseBakerReadBarrier) {
6125 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6126 // Baker's read barrier are used:
6127 //
6128 // root = obj.field;
6129 // if (Thread::Current()->GetIsGcMarking()) {
6130 // root = ReadBarrier::Mark(root)
6131 // }
6132
6133 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6134 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6135 static_assert(
6136 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6137 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6138 "have different sizes.");
6139 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6140 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6141 "have different sizes.");
6142
6143 // Slow path used to mark the GC root `root`.
6144 SlowPathCode* slow_path =
6145 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, root, root);
6146 codegen_->AddSlowPath(slow_path);
6147
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006148 // IP = Thread::Current()->GetIsGcMarking()
Roland Levillainc9285912015-12-18 10:38:42 +00006149 __ LoadFromOffset(
6150 kLoadWord, IP, TR, Thread::IsGcMarkingOffset<kArmWordSize>().Int32Value());
6151 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
6152 __ Bind(slow_path->GetExitLabel());
6153 } else {
6154 // GC root loaded through a slow path for read barriers other
6155 // than Baker's.
6156 // /* GcRoot<mirror::Object>* */ root = obj + offset
6157 __ AddConstant(root_reg, obj, offset);
6158 // /* mirror::Object* */ root = root->Read()
6159 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6160 }
6161 } else {
6162 // Plain GC root load with no read barrier.
6163 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6164 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6165 // Note that GC roots are not affected by heap poisoning, thus we
6166 // do not have to unpoison `root_reg` here.
6167 }
6168}
6169
6170void CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6171 Location ref,
6172 Register obj,
6173 uint32_t offset,
6174 Location temp,
6175 bool needs_null_check) {
6176 DCHECK(kEmitCompilerReadBarrier);
6177 DCHECK(kUseBakerReadBarrier);
6178
6179 // /* HeapReference<Object> */ ref = *(obj + offset)
6180 Location no_index = Location::NoLocation();
Roland Levillainb6a94412016-06-23 13:48:47 +01006181 ScaleFactor no_scale_factor = TIMES_1;
Roland Levillainc9285912015-12-18 10:38:42 +00006182 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainb6a94412016-06-23 13:48:47 +01006183 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00006184}
6185
6186void CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6187 Location ref,
6188 Register obj,
6189 uint32_t data_offset,
6190 Location index,
6191 Location temp,
6192 bool needs_null_check) {
6193 DCHECK(kEmitCompilerReadBarrier);
6194 DCHECK(kUseBakerReadBarrier);
6195
Roland Levillainb6a94412016-06-23 13:48:47 +01006196 static_assert(
6197 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6198 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00006199 // /* HeapReference<Object> */ ref =
6200 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainb6a94412016-06-23 13:48:47 +01006201 ScaleFactor scale_factor = TIMES_4;
Roland Levillainc9285912015-12-18 10:38:42 +00006202 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainb6a94412016-06-23 13:48:47 +01006203 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00006204}
6205
6206void CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6207 Location ref,
6208 Register obj,
6209 uint32_t offset,
6210 Location index,
Roland Levillainb6a94412016-06-23 13:48:47 +01006211 ScaleFactor scale_factor,
Roland Levillainc9285912015-12-18 10:38:42 +00006212 Location temp,
6213 bool needs_null_check) {
6214 DCHECK(kEmitCompilerReadBarrier);
6215 DCHECK(kUseBakerReadBarrier);
6216
6217 // In slow path based read barriers, the read barrier call is
6218 // inserted after the original load. However, in fast path based
6219 // Baker's read barriers, we need to perform the load of
6220 // mirror::Object::monitor_ *before* the original reference load.
6221 // This load-load ordering is required by the read barrier.
6222 // The fast path/slow path (for Baker's algorithm) should look like:
6223 //
6224 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6225 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6226 // HeapReference<Object> ref = *src; // Original reference load.
6227 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6228 // if (is_gray) {
6229 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6230 // }
6231 //
6232 // Note: the original implementation in ReadBarrier::Barrier is
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006233 // slightly more complex as it performs additional checks that we do
6234 // not do here for performance reasons.
Roland Levillainc9285912015-12-18 10:38:42 +00006235
6236 Register ref_reg = ref.AsRegister<Register>();
6237 Register temp_reg = temp.AsRegister<Register>();
6238 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6239
6240 // /* int32_t */ monitor = obj->monitor_
6241 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
6242 if (needs_null_check) {
6243 MaybeRecordImplicitNullCheck(instruction);
6244 }
6245 // /* LockWord */ lock_word = LockWord(monitor)
6246 static_assert(sizeof(LockWord) == sizeof(int32_t),
6247 "art::LockWord and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00006248
Vladimir Markob7d10aa2016-07-11 15:52:00 +01006249 // Introduce a dependency on the lock_word including the rb_state,
6250 // which shall prevent load-load reordering without using
Roland Levillainc9285912015-12-18 10:38:42 +00006251 // a memory barrier (which would be more expensive).
Vladimir Markob7d10aa2016-07-11 15:52:00 +01006252 // obj is unchanged by this operation, but its value now depends on temp_reg.
6253 __ add(obj, obj, ShifterOperand(temp_reg, LSR, 32));
Roland Levillainc9285912015-12-18 10:38:42 +00006254
6255 // The actual reference load.
6256 if (index.IsValid()) {
Roland Levillainb6a94412016-06-23 13:48:47 +01006257 // Load types involving an "index": ArrayGet and
6258 // UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
6259 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainc9285912015-12-18 10:38:42 +00006260 if (index.IsConstant()) {
6261 size_t computed_offset =
Roland Levillainb6a94412016-06-23 13:48:47 +01006262 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
Roland Levillainc9285912015-12-18 10:38:42 +00006263 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
6264 } else {
Roland Levillainb6a94412016-06-23 13:48:47 +01006265 // Handle the special case of the
6266 // UnsafeGetObject/UnsafeGetObjectVolatile intrinsics, which use
6267 // a register pair as index ("long offset"), of which only the low
6268 // part contains data.
6269 Register index_reg = index.IsRegisterPair()
6270 ? index.AsRegisterPairLow<Register>()
6271 : index.AsRegister<Register>();
6272 __ add(IP, obj, ShifterOperand(index_reg, LSL, scale_factor));
Roland Levillainc9285912015-12-18 10:38:42 +00006273 __ LoadFromOffset(kLoadWord, ref_reg, IP, offset);
6274 }
6275 } else {
6276 // /* HeapReference<Object> */ ref = *(obj + offset)
6277 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
6278 }
6279
6280 // Object* ref = ref_addr->AsMirrorPtr()
6281 __ MaybeUnpoisonHeapReference(ref_reg);
6282
6283 // Slow path used to mark the object `ref` when it is gray.
6284 SlowPathCode* slow_path =
6285 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, ref, ref);
6286 AddSlowPath(slow_path);
6287
6288 // if (rb_state == ReadBarrier::gray_ptr_)
6289 // ref = ReadBarrier::Mark(ref);
Vladimir Markob7d10aa2016-07-11 15:52:00 +01006290 // Given the numeric representation, it's enough to check the low bit of the
6291 // rb_state. We do that by shifting the bit out of the lock word with LSRS
6292 // which can be a 16-bit instruction unlike the TST immediate.
6293 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
6294 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
6295 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
6296 __ Lsrs(temp_reg, temp_reg, LockWord::kReadBarrierStateShift + 1);
6297 __ b(slow_path->GetEntryLabel(), CS); // Carry flag is the last bit shifted out by LSRS.
Roland Levillainc9285912015-12-18 10:38:42 +00006298 __ Bind(slow_path->GetExitLabel());
6299}
6300
6301void CodeGeneratorARM::GenerateReadBarrierSlow(HInstruction* instruction,
6302 Location out,
6303 Location ref,
6304 Location obj,
6305 uint32_t offset,
6306 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006307 DCHECK(kEmitCompilerReadBarrier);
6308
Roland Levillainc9285912015-12-18 10:38:42 +00006309 // Insert a slow path based read barrier *after* the reference load.
6310 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006311 // If heap poisoning is enabled, the unpoisoning of the loaded
6312 // reference will be carried out by the runtime within the slow
6313 // path.
6314 //
6315 // Note that `ref` currently does not get unpoisoned (when heap
6316 // poisoning is enabled), which is alright as the `ref` argument is
6317 // not used by the artReadBarrierSlow entry point.
6318 //
6319 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6320 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6321 ReadBarrierForHeapReferenceSlowPathARM(instruction, out, ref, obj, offset, index);
6322 AddSlowPath(slow_path);
6323
Roland Levillain3b359c72015-11-17 19:35:12 +00006324 __ b(slow_path->GetEntryLabel());
6325 __ Bind(slow_path->GetExitLabel());
6326}
6327
Roland Levillainc9285912015-12-18 10:38:42 +00006328void CodeGeneratorARM::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6329 Location out,
6330 Location ref,
6331 Location obj,
6332 uint32_t offset,
6333 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006334 if (kEmitCompilerReadBarrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00006335 // Baker's read barriers shall be handled by the fast path
6336 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
6337 DCHECK(!kUseBakerReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00006338 // If heap poisoning is enabled, unpoisoning will be taken care of
6339 // by the runtime within the slow path.
Roland Levillainc9285912015-12-18 10:38:42 +00006340 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain3b359c72015-11-17 19:35:12 +00006341 } else if (kPoisonHeapReferences) {
6342 __ UnpoisonHeapReference(out.AsRegister<Register>());
6343 }
6344}
6345
Roland Levillainc9285912015-12-18 10:38:42 +00006346void CodeGeneratorARM::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6347 Location out,
6348 Location root) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006349 DCHECK(kEmitCompilerReadBarrier);
6350
Roland Levillainc9285912015-12-18 10:38:42 +00006351 // Insert a slow path based read barrier *after* the GC root load.
6352 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006353 // Note that GC roots are not affected by heap poisoning, so we do
6354 // not need to do anything special for this here.
6355 SlowPathCode* slow_path =
6356 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM(instruction, out, root);
6357 AddSlowPath(slow_path);
6358
Roland Levillain3b359c72015-11-17 19:35:12 +00006359 __ b(slow_path->GetEntryLabel());
6360 __ Bind(slow_path->GetExitLabel());
6361}
6362
Vladimir Markodc151b22015-10-15 18:02:30 +01006363HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch(
6364 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
6365 MethodReference target_method) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006366 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
6367 // We disable pc-relative load when there is an irreducible loop, as the optimization
6368 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006369 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
6370 // with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006371 if (GetGraph()->HasIrreducibleLoops() &&
6372 (dispatch_info.method_load_kind ==
6373 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
6374 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
6375 }
6376
6377 if (dispatch_info.code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative) {
Vladimir Markodc151b22015-10-15 18:02:30 +01006378 const DexFile& outer_dex_file = GetGraph()->GetDexFile();
6379 if (&outer_dex_file != target_method.dex_file) {
6380 // Calls across dex files are more likely to exceed the available BL range,
6381 // so use absolute patch with fixup if available and kCallArtMethod otherwise.
6382 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location =
6383 (desired_dispatch_info.method_load_kind ==
6384 HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup)
6385 ? HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup
6386 : HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
6387 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006388 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01006389 code_ptr_location,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006390 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01006391 0u
6392 };
6393 }
6394 }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006395 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01006396}
6397
Vladimir Markob4536b72015-11-24 13:45:23 +00006398Register CodeGeneratorARM::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
6399 Register temp) {
6400 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
6401 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
6402 if (!invoke->GetLocations()->Intrinsified()) {
6403 return location.AsRegister<Register>();
6404 }
6405 // For intrinsics we allow any location, so it may be on the stack.
6406 if (!location.IsRegister()) {
6407 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
6408 return temp;
6409 }
6410 // For register locations, check if the register was saved. If so, get it from the stack.
6411 // Note: There is a chance that the register was saved but not overwritten, so we could
6412 // save one load. However, since this is just an intrinsic slow path we prefer this
6413 // simple and more robust approach rather that trying to determine if that's the case.
6414 SlowPathCode* slow_path = GetCurrentSlowPath();
6415 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
6416 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
6417 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
6418 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
6419 return temp;
6420 }
6421 return location.AsRegister<Register>();
6422}
6423
Nicolas Geoffray38207af2015-06-01 15:46:22 +01006424void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00006425 // For better instruction scheduling we load the direct code pointer before the method pointer.
Vladimir Marko58155012015-08-19 12:49:41 +00006426 switch (invoke->GetCodePtrLocation()) {
Vladimir Marko58155012015-08-19 12:49:41 +00006427 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
6428 // LR = code address from literal pool with link-time patch.
6429 __ LoadLiteral(LR, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
Vladimir Marko58155012015-08-19 12:49:41 +00006430 break;
6431 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
6432 // LR = invoke->GetDirectCodePtr();
6433 __ LoadImmediate(LR, invoke->GetDirectCodePtr());
Vladimir Marko58155012015-08-19 12:49:41 +00006434 break;
6435 default:
6436 break;
6437 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006438
Vladimir Marko58155012015-08-19 12:49:41 +00006439 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
6440 switch (invoke->GetMethodLoadKind()) {
6441 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
6442 // temp = thread->string_init_entrypoint
6443 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, invoke->GetStringInitOffset());
6444 break;
6445 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00006446 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006447 break;
6448 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
6449 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
6450 break;
6451 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
6452 __ LoadLiteral(temp.AsRegister<Register>(),
6453 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
6454 break;
Vladimir Markob4536b72015-11-24 13:45:23 +00006455 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
6456 HArmDexCacheArraysBase* base =
6457 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
6458 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
6459 temp.AsRegister<Register>());
6460 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
6461 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
6462 break;
6463 }
Vladimir Marko58155012015-08-19 12:49:41 +00006464 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00006465 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006466 Register method_reg;
6467 Register reg = temp.AsRegister<Register>();
6468 if (current_method.IsRegister()) {
6469 method_reg = current_method.AsRegister<Register>();
6470 } else {
6471 DCHECK(invoke->GetLocations()->Intrinsified());
6472 DCHECK(!current_method.IsValid());
6473 method_reg = reg;
6474 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
6475 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006476 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
6477 __ LoadFromOffset(kLoadWord,
6478 reg,
6479 method_reg,
6480 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01006481 // temp = temp[index_in_cache];
6482 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
6483 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00006484 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
6485 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01006486 }
Vladimir Marko58155012015-08-19 12:49:41 +00006487 }
6488
6489 switch (invoke->GetCodePtrLocation()) {
6490 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
6491 __ bl(GetFrameEntryLabel());
6492 break;
6493 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01006494 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006495 __ BindTrackedLabel(&relative_call_patches_.back().label);
Vladimir Markodc151b22015-10-15 18:02:30 +01006496 // Arbitrarily branch to the BL itself, override at link time.
6497 __ bl(&relative_call_patches_.back().label);
6498 break;
Vladimir Marko58155012015-08-19 12:49:41 +00006499 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
6500 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
6501 // LR prepared above for better instruction scheduling.
Vladimir Marko58155012015-08-19 12:49:41 +00006502 // LR()
6503 __ blx(LR);
6504 break;
6505 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
6506 // LR = callee_method->entry_point_from_quick_compiled_code_
6507 __ LoadFromOffset(
6508 kLoadWord, LR, callee_method.AsRegister<Register>(),
6509 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize).Int32Value());
6510 // LR()
6511 __ blx(LR);
6512 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006513 }
6514
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006515 DCHECK(!IsLeafMethod());
6516}
6517
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006518void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
6519 Register temp = temp_location.AsRegister<Register>();
6520 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
6521 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006522
6523 // Use the calling convention instead of the location of the receiver, as
6524 // intrinsics may have put the receiver in a different register. In the intrinsics
6525 // slow path, the arguments have been moved to the right place, so here we are
6526 // guaranteed that the receiver is the first register of the calling convention.
6527 InvokeDexCallingConvention calling_convention;
6528 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006529 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00006530 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006531 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006532 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00006533 // Instead of simply (possibly) unpoisoning `temp` here, we should
6534 // emit a read barrier for the previous class reference load.
6535 // However this is not required in practice, as this is an
6536 // intermediate/temporary reference and because the current
6537 // concurrent copying collector keeps the from-space memory
6538 // intact/accessible until the end of the marking phase (the
6539 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006540 __ MaybeUnpoisonHeapReference(temp);
6541 // temp = temp->GetMethodAt(method_offset);
6542 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
6543 kArmWordSize).Int32Value();
6544 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
6545 // LR = temp->GetEntryPoint();
6546 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
6547 // LR();
6548 __ blx(LR);
6549}
6550
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006551CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeStringPatch(
6552 const DexFile& dex_file, uint32_t string_index) {
6553 return NewPcRelativePatch(dex_file, string_index, &pc_relative_string_patches_);
6554}
6555
6556CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeDexCacheArrayPatch(
6557 const DexFile& dex_file, uint32_t element_offset) {
6558 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
6559}
6560
6561CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativePatch(
6562 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
6563 patches->emplace_back(dex_file, offset_or_index);
6564 return &patches->back();
6565}
6566
6567Literal* CodeGeneratorARM::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
6568 uint32_t string_index) {
6569 return boot_image_string_patches_.GetOrCreate(
6570 StringReference(&dex_file, string_index),
6571 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
6572}
6573
6574Literal* CodeGeneratorARM::DeduplicateBootImageAddressLiteral(uint32_t address) {
6575 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
6576 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
6577 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
6578}
6579
6580Literal* CodeGeneratorARM::DeduplicateDexCacheAddressLiteral(uint32_t address) {
6581 return DeduplicateUint32Literal(address, &uint32_literals_);
6582}
6583
Vladimir Marko58155012015-08-19 12:49:41 +00006584void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
6585 DCHECK(linker_patches->empty());
Vladimir Markob4536b72015-11-24 13:45:23 +00006586 size_t size =
6587 method_patches_.size() +
6588 call_patches_.size() +
6589 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006590 /* MOVW+MOVT for each base */ 2u * pc_relative_dex_cache_patches_.size() +
6591 boot_image_string_patches_.size() +
6592 /* MOVW+MOVT for each base */ 2u * pc_relative_string_patches_.size() +
6593 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00006594 linker_patches->reserve(size);
6595 for (const auto& entry : method_patches_) {
6596 const MethodReference& target_method = entry.first;
6597 Literal* literal = entry.second;
6598 DCHECK(literal->GetLabel()->IsBound());
6599 uint32_t literal_offset = literal->GetLabel()->Position();
6600 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
6601 target_method.dex_file,
6602 target_method.dex_method_index));
6603 }
6604 for (const auto& entry : call_patches_) {
6605 const MethodReference& target_method = entry.first;
6606 Literal* literal = entry.second;
6607 DCHECK(literal->GetLabel()->IsBound());
6608 uint32_t literal_offset = literal->GetLabel()->Position();
6609 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
6610 target_method.dex_file,
6611 target_method.dex_method_index));
6612 }
6613 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
6614 uint32_t literal_offset = info.label.Position();
6615 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
6616 info.target_method.dex_file,
6617 info.target_method.dex_method_index));
6618 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006619 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
6620 const DexFile& dex_file = info.target_dex_file;
6621 size_t base_element_offset = info.offset_or_index;
6622 DCHECK(info.add_pc_label.IsBound());
6623 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
Vladimir Markob4536b72015-11-24 13:45:23 +00006624 // Add MOVW patch.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006625 DCHECK(info.movw_label.IsBound());
6626 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
Vladimir Markob4536b72015-11-24 13:45:23 +00006627 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(movw_offset,
6628 &dex_file,
6629 add_pc_offset,
6630 base_element_offset));
6631 // Add MOVT patch.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006632 DCHECK(info.movt_label.IsBound());
6633 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
Vladimir Markob4536b72015-11-24 13:45:23 +00006634 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(movt_offset,
6635 &dex_file,
6636 add_pc_offset,
6637 base_element_offset));
6638 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006639 for (const auto& entry : boot_image_string_patches_) {
6640 const StringReference& target_string = entry.first;
6641 Literal* literal = entry.second;
6642 DCHECK(literal->GetLabel()->IsBound());
6643 uint32_t literal_offset = literal->GetLabel()->Position();
6644 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
6645 target_string.dex_file,
6646 target_string.string_index));
6647 }
6648 for (const PcRelativePatchInfo& info : pc_relative_string_patches_) {
6649 const DexFile& dex_file = info.target_dex_file;
6650 uint32_t string_index = info.offset_or_index;
6651 DCHECK(info.add_pc_label.IsBound());
6652 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
6653 // Add MOVW patch.
6654 DCHECK(info.movw_label.IsBound());
6655 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
6656 linker_patches->push_back(LinkerPatch::RelativeStringPatch(movw_offset,
6657 &dex_file,
6658 add_pc_offset,
6659 string_index));
6660 // Add MOVT patch.
6661 DCHECK(info.movt_label.IsBound());
6662 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
6663 linker_patches->push_back(LinkerPatch::RelativeStringPatch(movt_offset,
6664 &dex_file,
6665 add_pc_offset,
6666 string_index));
6667 }
6668 for (const auto& entry : boot_image_address_patches_) {
6669 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
6670 Literal* literal = entry.second;
6671 DCHECK(literal->GetLabel()->IsBound());
6672 uint32_t literal_offset = literal->GetLabel()->Position();
6673 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
6674 }
6675}
6676
6677Literal* CodeGeneratorARM::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
6678 return map->GetOrCreate(
6679 value,
6680 [this, value]() { return __ NewLiteral<uint32_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00006681}
6682
6683Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
6684 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006685 return map->GetOrCreate(
6686 target_method,
6687 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00006688}
6689
6690Literal* CodeGeneratorARM::DeduplicateMethodAddressLiteral(MethodReference target_method) {
6691 return DeduplicateMethodLiteral(target_method, &method_patches_);
6692}
6693
6694Literal* CodeGeneratorARM::DeduplicateMethodCodeLiteral(MethodReference target_method) {
6695 return DeduplicateMethodLiteral(target_method, &call_patches_);
6696}
6697
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006698void LocationsBuilderARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
6699 LocationSummary* locations =
6700 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
6701 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
6702 Location::RequiresRegister());
6703 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
6704 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
6705 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6706}
6707
6708void InstructionCodeGeneratorARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
6709 LocationSummary* locations = instr->GetLocations();
6710 Register res = locations->Out().AsRegister<Register>();
6711 Register accumulator =
6712 locations->InAt(HMultiplyAccumulate::kInputAccumulatorIndex).AsRegister<Register>();
6713 Register mul_left =
6714 locations->InAt(HMultiplyAccumulate::kInputMulLeftIndex).AsRegister<Register>();
6715 Register mul_right =
6716 locations->InAt(HMultiplyAccumulate::kInputMulRightIndex).AsRegister<Register>();
6717
6718 if (instr->GetOpKind() == HInstruction::kAdd) {
6719 __ mla(res, mul_left, mul_right, accumulator);
6720 } else {
6721 __ mls(res, mul_left, mul_right, accumulator);
6722 }
6723}
6724
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006725void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006726 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006727 LOG(FATAL) << "Unreachable";
6728}
6729
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006730void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006731 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006732 LOG(FATAL) << "Unreachable";
6733}
6734
Mark Mendellfe57faa2015-09-18 09:26:15 -04006735// Simple implementation of packed switch - generate cascaded compare/jumps.
6736void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6737 LocationSummary* locations =
6738 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6739 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006740 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006741 codegen_->GetAssembler()->IsThumb()) {
6742 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
6743 if (switch_instr->GetStartValue() != 0) {
6744 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
6745 }
6746 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04006747}
6748
6749void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6750 int32_t lower_bound = switch_instr->GetStartValue();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006751 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006752 LocationSummary* locations = switch_instr->GetLocations();
6753 Register value_reg = locations->InAt(0).AsRegister<Register>();
6754 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6755
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006756 if (num_entries <= kPackedSwitchCompareJumpThreshold || !codegen_->GetAssembler()->IsThumb()) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006757 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006758 Register temp_reg = IP;
6759 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
6760 // the immediate, because IP is used as the destination register. For the other
6761 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
6762 // and they can be encoded in the instruction without making use of IP register.
6763 __ AddConstantSetFlags(temp_reg, value_reg, -lower_bound);
6764
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006765 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006766 // Jump to successors[0] if value == lower_bound.
6767 __ b(codegen_->GetLabelOf(successors[0]), EQ);
6768 int32_t last_index = 0;
6769 for (; num_entries - last_index > 2; last_index += 2) {
6770 __ AddConstantSetFlags(temp_reg, temp_reg, -2);
6771 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6772 __ b(codegen_->GetLabelOf(successors[last_index + 1]), LO);
6773 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6774 __ b(codegen_->GetLabelOf(successors[last_index + 2]), EQ);
6775 }
6776 if (num_entries - last_index == 2) {
6777 // The last missing case_value.
Vladimir Markoac6ac102015-12-17 12:14:00 +00006778 __ CmpConstant(temp_reg, 1);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006779 __ b(codegen_->GetLabelOf(successors[last_index + 1]), EQ);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006780 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04006781
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006782 // And the default for any other value.
6783 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6784 __ b(codegen_->GetLabelOf(default_block));
6785 }
6786 } else {
6787 // Create a table lookup.
6788 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
6789
6790 // Materialize a pointer to the switch table
6791 std::vector<Label*> labels(num_entries);
6792 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6793 for (uint32_t i = 0; i < num_entries; i++) {
6794 labels[i] = codegen_->GetLabelOf(successors[i]);
6795 }
6796 JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg);
6797
6798 // Remove the bias.
6799 Register key_reg;
6800 if (lower_bound != 0) {
6801 key_reg = locations->GetTemp(1).AsRegister<Register>();
6802 __ AddConstant(key_reg, value_reg, -lower_bound);
6803 } else {
6804 key_reg = value_reg;
6805 }
6806
6807 // Check whether the value is in the table, jump to default block if not.
6808 __ CmpConstant(key_reg, num_entries - 1);
6809 __ b(codegen_->GetLabelOf(default_block), Condition::HI);
6810
6811 // Load the displacement from the table.
6812 __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2));
6813
6814 // Dispatch is a direct add to the PC (for Thumb2).
6815 __ EmitJumpTableDispatch(table, temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006816 }
6817}
6818
Vladimir Markob4536b72015-11-24 13:45:23 +00006819void LocationsBuilderARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
6820 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
6821 locations->SetOut(Location::RequiresRegister());
Vladimir Markob4536b72015-11-24 13:45:23 +00006822}
6823
6824void InstructionCodeGeneratorARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
6825 Register base_reg = base->GetLocations()->Out().AsRegister<Register>();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006826 CodeGeneratorARM::PcRelativePatchInfo* labels =
6827 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Vladimir Markob4536b72015-11-24 13:45:23 +00006828 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006829 __ movw(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00006830 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006831 __ movt(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00006832 __ BindTrackedLabel(&labels->add_pc_label);
6833 __ add(base_reg, base_reg, ShifterOperand(PC));
6834}
6835
Andreas Gampe85b62f22015-09-09 13:15:38 -07006836void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
6837 if (!trg.IsValid()) {
Roland Levillainc9285912015-12-18 10:38:42 +00006838 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006839 return;
6840 }
6841
6842 DCHECK_NE(type, Primitive::kPrimVoid);
6843
6844 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
6845 if (return_loc.Equals(trg)) {
6846 return;
6847 }
6848
6849 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
6850 // with the last branch.
6851 if (type == Primitive::kPrimLong) {
6852 HParallelMove parallel_move(GetGraph()->GetArena());
6853 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
6854 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
6855 GetMoveResolver()->EmitNativeCode(&parallel_move);
6856 } else if (type == Primitive::kPrimDouble) {
6857 HParallelMove parallel_move(GetGraph()->GetArena());
6858 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
6859 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
6860 GetMoveResolver()->EmitNativeCode(&parallel_move);
6861 } else {
6862 // Let the parallel move resolver take care of all of this.
6863 HParallelMove parallel_move(GetGraph()->GetArena());
6864 parallel_move.AddMove(return_loc, trg, type, nullptr);
6865 GetMoveResolver()->EmitNativeCode(&parallel_move);
6866 }
6867}
6868
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006869void LocationsBuilderARM::VisitClassTableGet(HClassTableGet* instruction) {
6870 LocationSummary* locations =
6871 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6872 locations->SetInAt(0, Location::RequiresRegister());
6873 locations->SetOut(Location::RequiresRegister());
6874}
6875
6876void InstructionCodeGeneratorARM::VisitClassTableGet(HClassTableGet* instruction) {
6877 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006878 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayb3cd84a2016-07-13 14:13:48 +01006879 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006880 instruction->GetIndex(), kArmPointerSize).SizeValue();
Nicolas Geoffrayb3cd84a2016-07-13 14:13:48 +01006881 __ LoadFromOffset(kLoadWord,
6882 locations->Out().AsRegister<Register>(),
6883 locations->InAt(0).AsRegister<Register>(),
6884 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006885 } else {
Nicolas Geoffrayb3cd84a2016-07-13 14:13:48 +01006886 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Artem Udovichenkodf2d4f22016-06-30 09:18:25 +00006887 instruction->GetIndex() % ImTable::kSize, kArmPointerSize));
Nicolas Geoffrayb3cd84a2016-07-13 14:13:48 +01006888 __ LoadFromOffset(kLoadWord,
6889 locations->Out().AsRegister<Register>(),
6890 locations->InAt(0).AsRegister<Register>(),
6891 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
6892 __ LoadFromOffset(kLoadWord,
6893 locations->Out().AsRegister<Register>(),
6894 locations->Out().AsRegister<Register>(),
6895 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006896 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006897}
6898
Roland Levillain4d027112015-07-01 15:41:14 +01006899#undef __
6900#undef QUICK_ENTRY_POINT
6901
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006902} // namespace arm
6903} // namespace art