blob: 11721363256ba028bb8e8fb88c4539405af2c1ef [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"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070020#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010021#include "gc/accounting/card_table.h"
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -080022#include "intrinsics.h"
23#include "intrinsics_arm.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070024#include "mirror/array-inl.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000025#include "mirror/art_method.h"
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010026#include "mirror/class.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070027#include "thread.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010028#include "utils/arm/assembler_arm.h"
29#include "utils/arm/managed_register_arm.h"
Roland Levillain946e1432014-11-11 17:35:19 +000030#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010031#include "utils/stack_checks.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000032
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000033namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010034
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000035namespace arm {
36
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +000037static bool ExpectedPairLayout(Location location) {
38 // We expected this for both core and fpu register pairs.
39 return ((location.low() & 1) == 0) && (location.low() + 1 == location.high());
40}
41
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010042static constexpr int kCurrentMethodStackOffset = 0;
43
Nicolas Geoffray4dee6362015-01-23 18:23:14 +000044// We unconditionally allocate R5 to ensure we can do long operations
45// with baseline.
46static constexpr Register kCoreSavedRegisterForBaseline = R5;
47static constexpr Register kCoreCalleeSaves[] =
48 { R5, R6, R7, R8, R10, R11, PC };
49static constexpr SRegister kFpuCalleeSaves[] =
50 { S16, S17, S18, S19, S20, S21, S22, S23, S24, S25, S26, S27, S28, S29, S30, S31 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010051
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +000052// D31 cannot be split into two S registers, and the register allocator only works on
53// S registers. Therefore there is no need to block it.
54static constexpr DRegister DTMP = D31;
55
Nicolas Geoffraye5038322014-07-04 09:41:32 +010056#define __ reinterpret_cast<ArmAssembler*>(codegen->GetAssembler())->
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010057#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmWordSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010058
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010059class NullCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010060 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010061 explicit NullCheckSlowPathARM(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010062
Alexandre Rames67555f72014-11-18 10:55:16 +000063 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010064 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010065 __ Bind(GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010066 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +000067 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010068 }
69
70 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010071 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010072 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM);
73};
74
Calin Juravled0d48522014-11-04 16:40:20 +000075class DivZeroCheckSlowPathARM : public SlowPathCodeARM {
76 public:
77 explicit DivZeroCheckSlowPathARM(HDivZeroCheck* instruction) : instruction_(instruction) {}
78
Alexandre Rames67555f72014-11-18 10:55:16 +000079 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000080 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
81 __ Bind(GetEntryLabel());
82 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +000083 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Calin Juravled0d48522014-11-04 16:40:20 +000084 }
85
86 private:
87 HDivZeroCheck* const instruction_;
88 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM);
89};
90
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010091class SuspendCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000092 public:
Alexandre Rames67555f72014-11-18 10:55:16 +000093 SuspendCheckSlowPathARM(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +010094 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000095
Alexandre Rames67555f72014-11-18 10:55:16 +000096 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010097 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000098 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +000099 SaveLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100100 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000101 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000102 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100103 if (successor_ == nullptr) {
104 __ b(GetReturnLabel());
105 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100106 __ b(arm_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100107 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000108 }
109
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100110 Label* GetReturnLabel() {
111 DCHECK(successor_ == nullptr);
112 return &return_label_;
113 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000114
115 private:
116 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100117 // If not null, the block to branch to after the suspend check.
118 HBasicBlock* const successor_;
119
120 // If `successor_` is null, the label to branch to after the suspend check.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000121 Label return_label_;
122
123 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM);
124};
125
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100126class BoundsCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100127 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100128 BoundsCheckSlowPathARM(HBoundsCheck* instruction,
129 Location index_location,
130 Location length_location)
Nicolas Geoffray39468442014-09-02 15:17:15 +0100131 : instruction_(instruction),
132 index_location_(index_location),
133 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100134
Alexandre Rames67555f72014-11-18 10:55:16 +0000135 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100136 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100137 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000138 // We're moving two locations to locations that could overlap, so we need a parallel
139 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100140 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000141 codegen->EmitParallelMoves(
142 index_location_,
143 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100144 Primitive::kPrimInt,
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000145 length_location_,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100146 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
147 Primitive::kPrimInt);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100148 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000149 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100150 }
151
152 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100153 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100154 const Location index_location_;
155 const Location length_location_;
156
157 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM);
158};
159
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000160class LoadClassSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100161 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000162 LoadClassSlowPathARM(HLoadClass* cls,
163 HInstruction* at,
164 uint32_t dex_pc,
165 bool do_clinit)
166 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
167 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
168 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100169
Alexandre Rames67555f72014-11-18 10:55:16 +0000170 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000171 LocationSummary* locations = at_->GetLocations();
172
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100173 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
174 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000175 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100176
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100177 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000178 __ LoadImmediate(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000179 int32_t entry_point_offset = do_clinit_
180 ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
181 : QUICK_ENTRY_POINT(pInitializeType);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000182 arm_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000183
184 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000185 Location out = locations->Out();
186 if (out.IsValid()) {
187 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000188 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
189 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000190 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100191 __ b(GetExitLabel());
192 }
193
194 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000195 // The class this slow path will load.
196 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100197
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000198 // The instruction where this slow path is happening.
199 // (Might be the load class or an initialization check).
200 HInstruction* const at_;
201
202 // The dex PC of `at_`.
203 const uint32_t dex_pc_;
204
205 // Whether to initialize the class.
206 const bool do_clinit_;
207
208 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100209};
210
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000211class LoadStringSlowPathARM : public SlowPathCodeARM {
212 public:
213 explicit LoadStringSlowPathARM(HLoadString* instruction) : instruction_(instruction) {}
214
Alexandre Rames67555f72014-11-18 10:55:16 +0000215 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000216 LocationSummary* locations = instruction_->GetLocations();
217 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
218
219 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
220 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000221 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000222
223 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800224 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction_->GetStringIndex());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000225 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000226 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000227 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
228
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000229 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000230 __ b(GetExitLabel());
231 }
232
233 private:
234 HLoadString* const instruction_;
235
236 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM);
237};
238
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000239class TypeCheckSlowPathARM : public SlowPathCodeARM {
240 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000241 TypeCheckSlowPathARM(HInstruction* instruction,
242 Location class_to_check,
243 Location object_class,
244 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000245 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000246 class_to_check_(class_to_check),
247 object_class_(object_class),
248 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000249
Alexandre Rames67555f72014-11-18 10:55:16 +0000250 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000251 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000252 DCHECK(instruction_->IsCheckCast()
253 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000254
255 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
256 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000257 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000258
259 // We're moving two locations to locations that could overlap, so we need a parallel
260 // move resolver.
261 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000262 codegen->EmitParallelMoves(
263 class_to_check_,
264 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100265 Primitive::kPrimNot,
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000266 object_class_,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100267 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
268 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000269
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000270 if (instruction_->IsInstanceOf()) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000271 arm_codegen->InvokeRuntime(
272 QUICK_ENTRY_POINT(pInstanceofNonTrivial), instruction_, dex_pc_, this);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000273 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
274 } else {
275 DCHECK(instruction_->IsCheckCast());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000276 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc_, this);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000277 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000278
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000279 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000280 __ b(GetExitLabel());
281 }
282
283 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000284 HInstruction* const instruction_;
285 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000286 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000287 uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000288
289 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM);
290};
291
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700292class DeoptimizationSlowPathARM : public SlowPathCodeARM {
293 public:
294 explicit DeoptimizationSlowPathARM(HInstruction* instruction)
295 : instruction_(instruction) {}
296
297 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
298 __ Bind(GetEntryLabel());
299 SaveLiveRegisters(codegen, instruction_->GetLocations());
300 DCHECK(instruction_->IsDeoptimize());
301 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
302 uint32_t dex_pc = deoptimize->GetDexPc();
303 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
304 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize), instruction_, dex_pc, this);
305 }
306
307 private:
308 HInstruction* const instruction_;
309 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM);
310};
311
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000312#undef __
313
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100314#undef __
315#define __ reinterpret_cast<ArmAssembler*>(GetAssembler())->
Dave Allison20dfc792014-06-16 20:44:29 -0700316
317inline Condition ARMCondition(IfCondition cond) {
318 switch (cond) {
319 case kCondEQ: return EQ;
320 case kCondNE: return NE;
321 case kCondLT: return LT;
322 case kCondLE: return LE;
323 case kCondGT: return GT;
324 case kCondGE: return GE;
325 default:
326 LOG(FATAL) << "Unknown if condition";
327 }
328 return EQ; // Unreachable.
329}
330
331inline Condition ARMOppositeCondition(IfCondition cond) {
332 switch (cond) {
333 case kCondEQ: return NE;
334 case kCondNE: return EQ;
335 case kCondLT: return GE;
336 case kCondLE: return GT;
337 case kCondGT: return LE;
338 case kCondGE: return LT;
339 default:
340 LOG(FATAL) << "Unknown if condition";
341 }
342 return EQ; // Unreachable.
343}
344
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100345void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
346 stream << ArmManagedRegister::FromCoreRegister(Register(reg));
347}
348
349void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000350 stream << ArmManagedRegister::FromSRegister(SRegister(reg));
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100351}
352
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100353size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
354 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
355 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100356}
357
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100358size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
359 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
360 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100361}
362
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000363size_t CodeGeneratorARM::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
364 __ StoreSToOffset(static_cast<SRegister>(reg_id), SP, stack_index);
365 return kArmWordSize;
366}
367
368size_t CodeGeneratorARM::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
369 __ LoadSFromOffset(static_cast<SRegister>(reg_id), SP, stack_index);
370 return kArmWordSize;
371}
372
Calin Juravle34166012014-12-19 17:22:29 +0000373CodeGeneratorARM::CodeGeneratorARM(HGraph* graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000374 const ArmInstructionSetFeatures& isa_features,
375 const CompilerOptions& compiler_options)
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000376 : CodeGenerator(graph,
377 kNumberOfCoreRegisters,
378 kNumberOfSRegisters,
379 kNumberOfRegisterPairs,
380 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
381 arraysize(kCoreCalleeSaves)),
382 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
383 arraysize(kFpuCalleeSaves)),
384 compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100385 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100386 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100387 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100388 move_resolver_(graph->GetArena(), this),
Calin Juravle34166012014-12-19 17:22:29 +0000389 assembler_(true),
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000390 isa_features_(isa_features) {
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000391 // Save the PC register to mimic Quick.
392 AddAllocatedRegister(Location::RegisterLocation(PC));
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100393}
394
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100395Location CodeGeneratorARM::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100396 switch (type) {
397 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100398 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100399 ArmManagedRegister pair =
400 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100401 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
402 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
403
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100404 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
405 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100406 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100407 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100408 }
409
410 case Primitive::kPrimByte:
411 case Primitive::kPrimBoolean:
412 case Primitive::kPrimChar:
413 case Primitive::kPrimShort:
414 case Primitive::kPrimInt:
415 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100416 int reg = FindFreeEntry(blocked_core_registers_, kNumberOfCoreRegisters);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100417 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100418 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
419 ArmManagedRegister current =
420 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
421 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100422 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100423 }
424 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100425 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100426 }
427
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000428 case Primitive::kPrimFloat: {
429 int reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfSRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100430 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100431 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100432
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000433 case Primitive::kPrimDouble: {
Nicolas Geoffray3c035032014-10-28 10:46:40 +0000434 int reg = FindTwoFreeConsecutiveAlignedEntries(blocked_fpu_registers_, kNumberOfSRegisters);
435 DCHECK_EQ(reg % 2, 0);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000436 return Location::FpuRegisterPairLocation(reg, reg + 1);
437 }
438
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100439 case Primitive::kPrimVoid:
440 LOG(FATAL) << "Unreachable type " << type;
441 }
442
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100443 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100444}
445
Nicolas Geoffraya0bb2bd2015-01-26 12:49:35 +0000446void CodeGeneratorARM::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100447 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100448 blocked_register_pairs_[R1_R2] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100449
450 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100451 blocked_core_registers_[SP] = true;
452 blocked_core_registers_[LR] = true;
453 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100454
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100455 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100456 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100457
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100458 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100459 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100460
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000461 if (is_baseline) {
462 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
463 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
464 }
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000465
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000466 blocked_core_registers_[kCoreSavedRegisterForBaseline] = false;
467
468 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
469 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
470 }
471 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100472
473 UpdateBlockedPairRegisters();
474}
475
476void CodeGeneratorARM::UpdateBlockedPairRegisters() const {
477 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
478 ArmManagedRegister current =
479 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
480 if (blocked_core_registers_[current.AsRegisterPairLow()]
481 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
482 blocked_register_pairs_[i] = true;
483 }
484 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100485}
486
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100487InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
488 : HGraphVisitor(graph),
489 assembler_(codegen->GetAssembler()),
490 codegen_(codegen) {}
491
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000492static uint32_t LeastSignificantBit(uint32_t mask) {
493 // ffs starts at 1.
494 return ffs(mask) - 1;
495}
496
497void CodeGeneratorARM::ComputeSpillMask() {
498 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000499 // Save one extra register for baseline. Note that on thumb2, there is no easy
500 // instruction to restore just the PC, so this actually helps both baseline
501 // and non-baseline to save and restore at least two registers at entry and exit.
502 core_spill_mask_ |= (1 << kCoreSavedRegisterForBaseline);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000503 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
504 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
505 // We use vpush and vpop for saving and restoring floating point registers, which take
506 // a SRegister and the number of registers to save/restore after that SRegister. We
507 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
508 // but in the range.
509 if (fpu_spill_mask_ != 0) {
510 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
511 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
512 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
513 fpu_spill_mask_ |= (1 << i);
514 }
515 }
516}
517
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100518static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100519 return dwarf::Reg::ArmCore(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100520}
521
522static dwarf::Reg DWARFReg(SRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100523 return dwarf::Reg::ArmFp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100524}
525
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000526void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +0000527 bool skip_overflow_check =
528 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000529 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000530 __ Bind(&frame_entry_label_);
531
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000532 if (HasEmptyFrame()) {
533 return;
534 }
535
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100536 if (!skip_overflow_check) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000537 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
538 __ LoadFromOffset(kLoadWord, IP, IP, 0);
539 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100540 }
541
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000542 // PC is in the list of callee-save to mimic Quick, but we need to push
543 // LR at entry instead.
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100544 uint32_t push_mask = (core_spill_mask_ & (~(1 << PC))) | 1 << LR;
545 __ PushList(push_mask);
546 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(push_mask));
David Srbecky9d8606d2015-04-12 09:35:32 +0100547 __ cfi().RelOffsetForMany(DWARFReg(R0), 0, push_mask, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000548 if (fpu_spill_mask_ != 0) {
549 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
550 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100551 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
David Srbecky9d8606d2015-04-12 09:35:32 +0100552 __ cfi().RelOffsetForMany(DWARFReg(S0), 0, fpu_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000553 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100554 int adjust = GetFrameSize() - FrameEntrySpillSize();
555 __ AddConstant(SP, -adjust);
556 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100557 __ StoreToOffset(kStoreWord, R0, SP, 0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000558}
559
560void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000561 if (HasEmptyFrame()) {
562 __ bx(LR);
563 return;
564 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100565 __ cfi().RememberState();
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100566 int adjust = GetFrameSize() - FrameEntrySpillSize();
567 __ AddConstant(SP, adjust);
568 __ cfi().AdjustCFAOffset(-adjust);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000569 if (fpu_spill_mask_ != 0) {
570 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
571 __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100572 __ cfi().AdjustCFAOffset(-kArmPointerSize * POPCOUNT(fpu_spill_mask_));
573 __ cfi().RestoreMany(DWARFReg(SRegister(0)), fpu_spill_mask_);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000574 }
575 __ PopList(core_spill_mask_);
David Srbeckyc34dc932015-04-12 09:27:43 +0100576 __ cfi().RestoreState();
577 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000578}
579
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100580void CodeGeneratorARM::Bind(HBasicBlock* block) {
581 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000582}
583
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100584Location CodeGeneratorARM::GetStackLocation(HLoadLocal* load) const {
585 switch (load->GetType()) {
586 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100587 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100588 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100589
590 case Primitive::kPrimInt:
591 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100592 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100593 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100594
595 case Primitive::kPrimBoolean:
596 case Primitive::kPrimByte:
597 case Primitive::kPrimChar:
598 case Primitive::kPrimShort:
599 case Primitive::kPrimVoid:
600 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700601 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100602 }
603
604 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700605 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100606}
607
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100608Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
609 switch (type) {
610 case Primitive::kPrimBoolean:
611 case Primitive::kPrimByte:
612 case Primitive::kPrimChar:
613 case Primitive::kPrimShort:
614 case Primitive::kPrimInt:
615 case Primitive::kPrimNot: {
616 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000617 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100618 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100619 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100620 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000621 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100622 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100623 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100624
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000625 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100626 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000627 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100628 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000629 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100630 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000631 if (calling_convention.GetRegisterAt(index) == R1) {
632 // Skip R1, and use R2_R3 instead.
633 gp_index_++;
634 index++;
635 }
636 }
637 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
638 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +0000639 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000640 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +0000641 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100642 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000643 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
644 }
645 }
646
647 case Primitive::kPrimFloat: {
648 uint32_t stack_index = stack_index_++;
649 if (float_index_ % 2 == 0) {
650 float_index_ = std::max(double_index_, float_index_);
651 }
652 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
653 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
654 } else {
655 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
656 }
657 }
658
659 case Primitive::kPrimDouble: {
660 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
661 uint32_t stack_index = stack_index_;
662 stack_index_ += 2;
663 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
664 uint32_t index = double_index_;
665 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000666 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000667 calling_convention.GetFpuRegisterAt(index),
668 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000669 DCHECK(ExpectedPairLayout(result));
670 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000671 } else {
672 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100673 }
674 }
675
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100676 case Primitive::kPrimVoid:
677 LOG(FATAL) << "Unexpected parameter type " << type;
678 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100679 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100680 return Location();
681}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100682
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000683Location InvokeDexCallingConventionVisitor::GetReturnLocation(Primitive::Type type) {
684 switch (type) {
685 case Primitive::kPrimBoolean:
686 case Primitive::kPrimByte:
687 case Primitive::kPrimChar:
688 case Primitive::kPrimShort:
689 case Primitive::kPrimInt:
690 case Primitive::kPrimNot: {
691 return Location::RegisterLocation(R0);
692 }
693
694 case Primitive::kPrimFloat: {
695 return Location::FpuRegisterLocation(S0);
696 }
697
698 case Primitive::kPrimLong: {
699 return Location::RegisterPairLocation(R0, R1);
700 }
701
702 case Primitive::kPrimDouble: {
703 return Location::FpuRegisterPairLocation(S0, S1);
704 }
705
706 case Primitive::kPrimVoid:
707 return Location();
708 }
709 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000710}
711
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100712void CodeGeneratorARM::Move32(Location destination, Location source) {
713 if (source.Equals(destination)) {
714 return;
715 }
716 if (destination.IsRegister()) {
717 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000718 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100719 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000720 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100721 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000722 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100723 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100724 } else if (destination.IsFpuRegister()) {
725 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000726 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100727 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000728 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100729 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000730 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100731 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100732 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +0000733 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100734 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000735 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100736 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000737 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100738 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +0000739 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100740 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
741 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100742 }
743 }
744}
745
746void CodeGeneratorARM::Move64(Location destination, Location source) {
747 if (source.Equals(destination)) {
748 return;
749 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100750 if (destination.IsRegisterPair()) {
751 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000752 EmitParallelMoves(
753 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
754 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100755 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000756 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100757 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
758 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100759 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000760 UNIMPLEMENTED(FATAL);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100761 } else {
762 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000763 DCHECK(ExpectedPairLayout(destination));
764 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
765 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100766 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000767 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100768 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000769 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
770 SP,
771 source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100772 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000773 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100774 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100775 } else {
776 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100777 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000778 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100779 if (source.AsRegisterPairLow<Register>() == R1) {
780 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100781 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
782 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100783 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100784 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100785 SP, destination.GetStackIndex());
786 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000787 } else if (source.IsFpuRegisterPair()) {
788 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
789 SP,
790 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100791 } else {
792 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000793 EmitParallelMoves(
794 Location::StackSlot(source.GetStackIndex()),
795 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100796 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000797 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100798 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)),
799 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100800 }
801 }
802}
803
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100804void CodeGeneratorARM::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100805 LocationSummary* locations = instruction->GetLocations();
806 if (locations != nullptr && locations->Out().Equals(location)) {
807 return;
808 }
809
Calin Juravlea21f5982014-11-13 15:53:04 +0000810 if (locations != nullptr && locations->Out().IsConstant()) {
811 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000812 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
813 int32_t value = GetInt32ValueOf(const_to_move);
Calin Juravlea21f5982014-11-13 15:53:04 +0000814 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000815 __ LoadImmediate(location.AsRegister<Register>(), value);
Calin Juravlea21f5982014-11-13 15:53:04 +0000816 } else {
817 DCHECK(location.IsStackSlot());
818 __ LoadImmediate(IP, value);
819 __ StoreToOffset(kStoreWord, IP, SP, location.GetStackIndex());
820 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000821 } else {
Nicolas Geoffray3747b482015-01-19 17:17:16 +0000822 DCHECK(const_to_move->IsLongConstant()) << const_to_move->DebugName();
Calin Juravlea21f5982014-11-13 15:53:04 +0000823 int64_t value = const_to_move->AsLongConstant()->GetValue();
824 if (location.IsRegisterPair()) {
825 __ LoadImmediate(location.AsRegisterPairLow<Register>(), Low32Bits(value));
826 __ LoadImmediate(location.AsRegisterPairHigh<Register>(), High32Bits(value));
827 } else {
828 DCHECK(location.IsDoubleStackSlot());
829 __ LoadImmediate(IP, Low32Bits(value));
830 __ StoreToOffset(kStoreWord, IP, SP, location.GetStackIndex());
831 __ LoadImmediate(IP, High32Bits(value));
832 __ StoreToOffset(kStoreWord, IP, SP, location.GetHighStackIndex(kArmWordSize));
833 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100834 }
Roland Levillain476df552014-10-09 17:51:36 +0100835 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100836 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
837 switch (instruction->GetType()) {
838 case Primitive::kPrimBoolean:
839 case Primitive::kPrimByte:
840 case Primitive::kPrimChar:
841 case Primitive::kPrimShort:
842 case Primitive::kPrimInt:
843 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100844 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100845 Move32(location, Location::StackSlot(stack_slot));
846 break;
847
848 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100849 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100850 Move64(location, Location::DoubleStackSlot(stack_slot));
851 break;
852
853 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100854 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100855 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000856 } else if (instruction->IsTemporary()) {
857 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000858 if (temp_location.IsStackSlot()) {
859 Move32(location, temp_location);
860 } else {
861 DCHECK(temp_location.IsDoubleStackSlot());
862 Move64(location, temp_location);
863 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000864 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100865 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100866 switch (instruction->GetType()) {
867 case Primitive::kPrimBoolean:
868 case Primitive::kPrimByte:
869 case Primitive::kPrimChar:
870 case Primitive::kPrimShort:
871 case Primitive::kPrimNot:
872 case Primitive::kPrimInt:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100873 case Primitive::kPrimFloat:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100874 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100875 break;
876
877 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100878 case Primitive::kPrimDouble:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100879 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100880 break;
881
882 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100883 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100884 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000885 }
886}
887
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100888void CodeGeneratorARM::InvokeRuntime(int32_t entry_point_offset,
889 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000890 uint32_t dex_pc,
891 SlowPathCode* slow_path) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100892 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
893 __ blx(LR);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000894 RecordPcInfo(instruction, dex_pc, slow_path);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100895 DCHECK(instruction->IsSuspendCheck()
896 || instruction->IsBoundsCheck()
897 || instruction->IsNullCheck()
Calin Juravled0d48522014-11-04 16:40:20 +0000898 || instruction->IsDivZeroCheck()
Roland Levillain624279f2014-12-04 11:54:28 +0000899 || instruction->GetLocations()->CanCall()
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100900 || !IsLeafMethod());
901}
902
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000903void LocationsBuilderARM::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000904 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000905}
906
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000907void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000908 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100909 DCHECK(!successor->IsExitBlock());
910
911 HBasicBlock* block = got->GetBlock();
912 HInstruction* previous = got->GetPrevious();
913
914 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000915 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100916 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
917 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
918 return;
919 }
920
921 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
922 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
923 }
924 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000925 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000926 }
927}
928
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000929void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000930 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000931}
932
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000933void InstructionCodeGeneratorARM::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700934 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000935}
936
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700937void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
938 Label* true_target,
939 Label* false_target,
940 Label* always_true_target) {
941 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100942 if (cond->IsIntConstant()) {
943 // Constant condition, statically compared against 1.
944 int32_t cond_value = cond->AsIntConstant()->GetValue();
945 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700946 if (always_true_target != nullptr) {
947 __ b(always_true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100948 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100949 return;
950 } else {
951 DCHECK_EQ(cond_value, 0);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100952 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100953 } else {
954 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
955 // Condition has been materialized, compare the output to 0
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700956 DCHECK(instruction->GetLocations()->InAt(0).IsRegister());
957 __ cmp(instruction->GetLocations()->InAt(0).AsRegister<Register>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100958 ShifterOperand(0));
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700959 __ b(true_target, NE);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100960 } else {
961 // Condition has not been materialized, use its inputs as the
962 // comparison and its condition as the branch condition.
963 LocationSummary* locations = cond->GetLocations();
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000964 DCHECK(locations->InAt(0).IsRegister()) << locations->InAt(0);
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +0000965 Register left = locations->InAt(0).AsRegister<Register>();
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100966 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +0000967 __ cmp(left, ShifterOperand(locations->InAt(1).AsRegister<Register>()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100968 } else {
969 DCHECK(locations->InAt(1).IsConstant());
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000970 HConstant* constant = locations->InAt(1).GetConstant();
971 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100972 ShifterOperand operand;
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +0000973 if (GetAssembler()->ShifterOperandCanHold(R0, left, CMP, value, &operand)) {
974 __ cmp(left, operand);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100975 } else {
976 Register temp = IP;
977 __ LoadImmediate(temp, value);
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +0000978 __ cmp(left, ShifterOperand(temp));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100979 }
980 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700981 __ b(true_target, ARMCondition(cond->AsCondition()->GetCondition()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100982 }
Dave Allison20dfc792014-06-16 20:44:29 -0700983 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700984 if (false_target != nullptr) {
985 __ b(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000986 }
987}
988
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700989void LocationsBuilderARM::VisitIf(HIf* if_instr) {
990 LocationSummary* locations =
991 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
992 HInstruction* cond = if_instr->InputAt(0);
993 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
994 locations->SetInAt(0, Location::RequiresRegister());
995 }
996}
997
998void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
999 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1000 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1001 Label* always_true_target = true_target;
1002 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1003 if_instr->IfTrueSuccessor())) {
1004 always_true_target = nullptr;
1005 }
1006 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1007 if_instr->IfFalseSuccessor())) {
1008 false_target = nullptr;
1009 }
1010 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
1011}
1012
1013void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
1014 LocationSummary* locations = new (GetGraph()->GetArena())
1015 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1016 HInstruction* cond = deoptimize->InputAt(0);
1017 DCHECK(cond->IsCondition());
1018 if (cond->AsCondition()->NeedsMaterialization()) {
1019 locations->SetInAt(0, Location::RequiresRegister());
1020 }
1021}
1022
1023void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) {
1024 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena())
1025 DeoptimizationSlowPathARM(deoptimize);
1026 codegen_->AddSlowPath(slow_path);
1027 Label* slow_path_entry = slow_path->GetEntryLabel();
1028 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
1029}
Dave Allison20dfc792014-06-16 20:44:29 -07001030
1031void LocationsBuilderARM::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001032 LocationSummary* locations =
1033 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001034 locations->SetInAt(0, Location::RequiresRegister());
1035 locations->SetInAt(1, Location::RegisterOrConstant(comp->InputAt(1)));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001036 if (comp->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001037 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001038 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001039}
1040
Dave Allison20dfc792014-06-16 20:44:29 -07001041void InstructionCodeGeneratorARM::VisitCondition(HCondition* comp) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001042 if (!comp->NeedsMaterialization()) return;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001043 LocationSummary* locations = comp->GetLocations();
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +00001044 Register left = locations->InAt(0).AsRegister<Register>();
1045
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001046 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +00001047 __ cmp(left, ShifterOperand(locations->InAt(1).AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001048 } else {
1049 DCHECK(locations->InAt(1).IsConstant());
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001050 int32_t value = CodeGenerator::GetInt32ValueOf(locations->InAt(1).GetConstant());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001051 ShifterOperand operand;
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +00001052 if (GetAssembler()->ShifterOperandCanHold(R0, left, CMP, value, &operand)) {
1053 __ cmp(left, operand);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001054 } else {
1055 Register temp = IP;
1056 __ LoadImmediate(temp, value);
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +00001057 __ cmp(left, ShifterOperand(temp));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001058 }
Dave Allison20dfc792014-06-16 20:44:29 -07001059 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001060 __ it(ARMCondition(comp->GetCondition()), kItElse);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001061 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(1),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001062 ARMCondition(comp->GetCondition()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001063 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(0),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001064 ARMOppositeCondition(comp->GetCondition()));
Dave Allison20dfc792014-06-16 20:44:29 -07001065}
1066
1067void LocationsBuilderARM::VisitEqual(HEqual* comp) {
1068 VisitCondition(comp);
1069}
1070
1071void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
1072 VisitCondition(comp);
1073}
1074
1075void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
1076 VisitCondition(comp);
1077}
1078
1079void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
1080 VisitCondition(comp);
1081}
1082
1083void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
1084 VisitCondition(comp);
1085}
1086
1087void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
1088 VisitCondition(comp);
1089}
1090
1091void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1092 VisitCondition(comp);
1093}
1094
1095void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1096 VisitCondition(comp);
1097}
1098
1099void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
1100 VisitCondition(comp);
1101}
1102
1103void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
1104 VisitCondition(comp);
1105}
1106
1107void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1108 VisitCondition(comp);
1109}
1110
1111void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1112 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001113}
1114
1115void LocationsBuilderARM::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001116 local->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001117}
1118
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001119void InstructionCodeGeneratorARM::VisitLocal(HLocal* local) {
1120 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001121}
1122
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001123void LocationsBuilderARM::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001124 load->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001125}
1126
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001127void InstructionCodeGeneratorARM::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001128 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001129 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001130}
1131
1132void LocationsBuilderARM::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001133 LocationSummary* locations =
1134 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001135 switch (store->InputAt(1)->GetType()) {
1136 case Primitive::kPrimBoolean:
1137 case Primitive::kPrimByte:
1138 case Primitive::kPrimChar:
1139 case Primitive::kPrimShort:
1140 case Primitive::kPrimInt:
1141 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001142 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001143 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1144 break;
1145
1146 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001147 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001148 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1149 break;
1150
1151 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001152 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001153 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001154}
1155
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001156void InstructionCodeGeneratorARM::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001157 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001158}
1159
1160void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001161 LocationSummary* locations =
1162 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001163 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001164}
1165
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001166void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001167 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001168 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001169}
1170
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001171void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
1172 LocationSummary* locations =
1173 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1174 locations->SetOut(Location::ConstantLocation(constant));
1175}
1176
1177void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant) {
1178 // Will be generated at use site.
1179 UNUSED(constant);
1180}
1181
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001182void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001183 LocationSummary* locations =
1184 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001185 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001186}
1187
1188void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant) {
1189 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001190 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001191}
1192
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001193void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
1194 LocationSummary* locations =
1195 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1196 locations->SetOut(Location::ConstantLocation(constant));
1197}
1198
1199void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant) {
1200 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001201 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001202}
1203
1204void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
1205 LocationSummary* locations =
1206 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1207 locations->SetOut(Location::ConstantLocation(constant));
1208}
1209
1210void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant) {
1211 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001212 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001213}
1214
Calin Juravle27df7582015-04-17 19:12:31 +01001215void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1216 memory_barrier->SetLocations(nullptr);
1217}
1218
1219void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1220 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1221}
1222
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001223void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001224 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001225}
1226
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001227void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001228 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001229 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001230}
1231
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001232void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001233 LocationSummary* locations =
1234 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001235 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001236}
1237
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001238void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001239 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001240 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001241}
1242
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001243void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001244 // When we do not run baseline, explicit clinit checks triggered by static
1245 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1246 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001247
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001248 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
1249 codegen_->GetInstructionSetFeatures());
1250 if (intrinsic.TryDispatch(invoke)) {
1251 return;
1252 }
1253
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001254 HandleInvoke(invoke);
1255}
1256
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001257void CodeGeneratorARM::LoadCurrentMethod(Register reg) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001258 DCHECK(RequiresCurrentMethod());
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001259 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001260}
1261
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001262static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
1263 if (invoke->GetLocations()->Intrinsified()) {
1264 IntrinsicCodeGeneratorARM intrinsic(codegen);
1265 intrinsic.Dispatch(invoke);
1266 return true;
1267 }
1268 return false;
1269}
1270
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001271void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001272 // When we do not run baseline, explicit clinit checks triggered by static
1273 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1274 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001275
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001276 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1277 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001278 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001279
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001280 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
1281
1282 codegen_->GenerateStaticOrDirectCall(invoke, temp);
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001283 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001284}
1285
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001286void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001287 LocationSummary* locations =
1288 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001289 locations->AddTemp(Location::RegisterLocation(R0));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001290
1291 InvokeDexCallingConventionVisitor calling_convention_visitor;
Roland Levillain3e3d7332015-04-28 11:00:54 +01001292 for (size_t i = 0; i < invoke->GetNumberOfArguments(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001293 HInstruction* input = invoke->InputAt(i);
1294 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1295 }
1296
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001297 locations->SetOut(calling_convention_visitor.GetReturnLocation(invoke->GetType()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001298}
1299
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001300void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001301 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
1302 codegen_->GetInstructionSetFeatures());
1303 if (intrinsic.TryDispatch(invoke)) {
1304 return;
1305 }
1306
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001307 HandleInvoke(invoke);
1308}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001309
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001310void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001311 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1312 return;
1313 }
1314
Roland Levillain271ab9c2014-11-27 15:23:57 +00001315 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001316 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
1317 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1318 LocationSummary* locations = invoke->GetLocations();
1319 Location receiver = locations->InAt(0);
1320 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1321 // temp = object->GetClass();
1322 if (receiver.IsStackSlot()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001323 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
1324 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001325 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001326 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001327 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001328 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001329 // temp = temp->GetMethodAt(method_offset);
Mathieu Chartier2d721012014-11-10 11:08:06 -08001330 uint32_t entry_point = mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001331 kArmWordSize).Int32Value();
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001332 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001333 // LR = temp->GetEntryPoint();
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001334 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001335 // LR();
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001336 __ blx(LR);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001337 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001338 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001339}
1340
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001341void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1342 HandleInvoke(invoke);
1343 // Add the hidden argument.
1344 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
1345}
1346
1347void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1348 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001349 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001350 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1351 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1352 LocationSummary* locations = invoke->GetLocations();
1353 Location receiver = locations->InAt(0);
1354 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1355
1356 // Set the hidden argument.
Roland Levillain199f3362014-11-27 17:15:16 +00001357 __ LoadImmediate(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
1358 invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001359
1360 // temp = object->GetClass();
1361 if (receiver.IsStackSlot()) {
1362 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
1363 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
1364 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001365 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001366 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001367 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001368 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartier2d721012014-11-10 11:08:06 -08001369 uint32_t entry_point = mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001370 kArmWordSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001371 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
1372 // LR = temp->GetEntryPoint();
1373 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
1374 // LR();
1375 __ blx(LR);
1376 DCHECK(!codegen_->IsLeafMethod());
1377 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1378}
1379
Roland Levillain88cb1752014-10-20 16:36:47 +01001380void LocationsBuilderARM::VisitNeg(HNeg* neg) {
1381 LocationSummary* locations =
1382 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1383 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001384 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01001385 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001386 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1387 break;
1388 }
1389 case Primitive::kPrimLong: {
1390 locations->SetInAt(0, Location::RequiresRegister());
1391 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01001392 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001393 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001394
Roland Levillain88cb1752014-10-20 16:36:47 +01001395 case Primitive::kPrimFloat:
1396 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001397 locations->SetInAt(0, Location::RequiresFpuRegister());
1398 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01001399 break;
1400
1401 default:
1402 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1403 }
1404}
1405
1406void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
1407 LocationSummary* locations = neg->GetLocations();
1408 Location out = locations->Out();
1409 Location in = locations->InAt(0);
1410 switch (neg->GetResultType()) {
1411 case Primitive::kPrimInt:
1412 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001413 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01001414 break;
1415
1416 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001417 DCHECK(in.IsRegisterPair());
1418 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
1419 __ rsbs(out.AsRegisterPairLow<Register>(),
1420 in.AsRegisterPairLow<Register>(),
1421 ShifterOperand(0));
1422 // We cannot emit an RSC (Reverse Subtract with Carry)
1423 // instruction here, as it does not exist in the Thumb-2
1424 // instruction set. We use the following approach
1425 // using SBC and SUB instead.
1426 //
1427 // out.hi = -C
1428 __ sbc(out.AsRegisterPairHigh<Register>(),
1429 out.AsRegisterPairHigh<Register>(),
1430 ShifterOperand(out.AsRegisterPairHigh<Register>()));
1431 // out.hi = out.hi - in.hi
1432 __ sub(out.AsRegisterPairHigh<Register>(),
1433 out.AsRegisterPairHigh<Register>(),
1434 ShifterOperand(in.AsRegisterPairHigh<Register>()));
1435 break;
1436
Roland Levillain88cb1752014-10-20 16:36:47 +01001437 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001438 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001439 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001440 break;
1441
Roland Levillain88cb1752014-10-20 16:36:47 +01001442 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001443 DCHECK(in.IsFpuRegisterPair());
1444 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
1445 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01001446 break;
1447
1448 default:
1449 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1450 }
1451}
1452
Roland Levillaindff1f282014-11-05 14:15:05 +00001453void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001454 Primitive::Type result_type = conversion->GetResultType();
1455 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001456 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001457
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001458 // The float-to-long and double-to-long type conversions rely on a
1459 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001460 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001461 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1462 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001463 ? LocationSummary::kCall
1464 : LocationSummary::kNoCall;
1465 LocationSummary* locations =
1466 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1467
David Brazdilb2bd1c52015-03-25 11:17:37 +00001468 // The Java language does not allow treating boolean as an integral type but
1469 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001470
Roland Levillaindff1f282014-11-05 14:15:05 +00001471 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001472 case Primitive::kPrimByte:
1473 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001474 case Primitive::kPrimBoolean:
1475 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001476 case Primitive::kPrimShort:
1477 case Primitive::kPrimInt:
1478 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001479 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001480 locations->SetInAt(0, Location::RequiresRegister());
1481 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1482 break;
1483
1484 default:
1485 LOG(FATAL) << "Unexpected type conversion from " << input_type
1486 << " to " << result_type;
1487 }
1488 break;
1489
Roland Levillain01a8d712014-11-14 16:27:39 +00001490 case Primitive::kPrimShort:
1491 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001492 case Primitive::kPrimBoolean:
1493 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001494 case Primitive::kPrimByte:
1495 case Primitive::kPrimInt:
1496 case Primitive::kPrimChar:
1497 // Processing a Dex `int-to-short' instruction.
1498 locations->SetInAt(0, Location::RequiresRegister());
1499 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1500 break;
1501
1502 default:
1503 LOG(FATAL) << "Unexpected type conversion from " << input_type
1504 << " to " << result_type;
1505 }
1506 break;
1507
Roland Levillain946e1432014-11-11 17:35:19 +00001508 case Primitive::kPrimInt:
1509 switch (input_type) {
1510 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001511 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001512 locations->SetInAt(0, Location::Any());
1513 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1514 break;
1515
1516 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001517 // Processing a Dex `float-to-int' instruction.
1518 locations->SetInAt(0, Location::RequiresFpuRegister());
1519 locations->SetOut(Location::RequiresRegister());
1520 locations->AddTemp(Location::RequiresFpuRegister());
1521 break;
1522
Roland Levillain946e1432014-11-11 17:35:19 +00001523 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001524 // Processing a Dex `double-to-int' instruction.
1525 locations->SetInAt(0, Location::RequiresFpuRegister());
1526 locations->SetOut(Location::RequiresRegister());
1527 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001528 break;
1529
1530 default:
1531 LOG(FATAL) << "Unexpected type conversion from " << input_type
1532 << " to " << result_type;
1533 }
1534 break;
1535
Roland Levillaindff1f282014-11-05 14:15:05 +00001536 case Primitive::kPrimLong:
1537 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001538 case Primitive::kPrimBoolean:
1539 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001540 case Primitive::kPrimByte:
1541 case Primitive::kPrimShort:
1542 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001543 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001544 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001545 locations->SetInAt(0, Location::RequiresRegister());
1546 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1547 break;
1548
Roland Levillain624279f2014-12-04 11:54:28 +00001549 case Primitive::kPrimFloat: {
1550 // Processing a Dex `float-to-long' instruction.
1551 InvokeRuntimeCallingConvention calling_convention;
1552 locations->SetInAt(0, Location::FpuRegisterLocation(
1553 calling_convention.GetFpuRegisterAt(0)));
1554 locations->SetOut(Location::RegisterPairLocation(R0, R1));
1555 break;
1556 }
1557
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001558 case Primitive::kPrimDouble: {
1559 // Processing a Dex `double-to-long' instruction.
1560 InvokeRuntimeCallingConvention calling_convention;
1561 locations->SetInAt(0, Location::FpuRegisterPairLocation(
1562 calling_convention.GetFpuRegisterAt(0),
1563 calling_convention.GetFpuRegisterAt(1)));
1564 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00001565 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001566 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001567
1568 default:
1569 LOG(FATAL) << "Unexpected type conversion from " << input_type
1570 << " to " << result_type;
1571 }
1572 break;
1573
Roland Levillain981e4542014-11-14 11:47:14 +00001574 case Primitive::kPrimChar:
1575 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001576 case Primitive::kPrimBoolean:
1577 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001578 case Primitive::kPrimByte:
1579 case Primitive::kPrimShort:
1580 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001581 // Processing a Dex `int-to-char' instruction.
1582 locations->SetInAt(0, Location::RequiresRegister());
1583 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1584 break;
1585
1586 default:
1587 LOG(FATAL) << "Unexpected type conversion from " << input_type
1588 << " to " << result_type;
1589 }
1590 break;
1591
Roland Levillaindff1f282014-11-05 14:15:05 +00001592 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001593 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001594 case Primitive::kPrimBoolean:
1595 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001596 case Primitive::kPrimByte:
1597 case Primitive::kPrimShort:
1598 case Primitive::kPrimInt:
1599 case Primitive::kPrimChar:
1600 // Processing a Dex `int-to-float' instruction.
1601 locations->SetInAt(0, Location::RequiresRegister());
1602 locations->SetOut(Location::RequiresFpuRegister());
1603 break;
1604
1605 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001606 // Processing a Dex `long-to-float' instruction.
1607 locations->SetInAt(0, Location::RequiresRegister());
1608 locations->SetOut(Location::RequiresFpuRegister());
1609 locations->AddTemp(Location::RequiresRegister());
1610 locations->AddTemp(Location::RequiresRegister());
1611 locations->AddTemp(Location::RequiresFpuRegister());
1612 locations->AddTemp(Location::RequiresFpuRegister());
1613 break;
1614
Roland Levillaincff13742014-11-17 14:32:17 +00001615 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001616 // Processing a Dex `double-to-float' instruction.
1617 locations->SetInAt(0, Location::RequiresFpuRegister());
1618 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001619 break;
1620
1621 default:
1622 LOG(FATAL) << "Unexpected type conversion from " << input_type
1623 << " to " << result_type;
1624 };
1625 break;
1626
Roland Levillaindff1f282014-11-05 14:15:05 +00001627 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001628 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001629 case Primitive::kPrimBoolean:
1630 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001631 case Primitive::kPrimByte:
1632 case Primitive::kPrimShort:
1633 case Primitive::kPrimInt:
1634 case Primitive::kPrimChar:
1635 // Processing a Dex `int-to-double' instruction.
1636 locations->SetInAt(0, Location::RequiresRegister());
1637 locations->SetOut(Location::RequiresFpuRegister());
1638 break;
1639
1640 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001641 // Processing a Dex `long-to-double' instruction.
1642 locations->SetInAt(0, Location::RequiresRegister());
1643 locations->SetOut(Location::RequiresFpuRegister());
1644 locations->AddTemp(Location::RequiresRegister());
1645 locations->AddTemp(Location::RequiresRegister());
1646 locations->AddTemp(Location::RequiresFpuRegister());
1647 break;
1648
Roland Levillaincff13742014-11-17 14:32:17 +00001649 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001650 // Processing a Dex `float-to-double' instruction.
1651 locations->SetInAt(0, Location::RequiresFpuRegister());
1652 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001653 break;
1654
1655 default:
1656 LOG(FATAL) << "Unexpected type conversion from " << input_type
1657 << " to " << result_type;
1658 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001659 break;
1660
1661 default:
1662 LOG(FATAL) << "Unexpected type conversion from " << input_type
1663 << " to " << result_type;
1664 }
1665}
1666
1667void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
1668 LocationSummary* locations = conversion->GetLocations();
1669 Location out = locations->Out();
1670 Location in = locations->InAt(0);
1671 Primitive::Type result_type = conversion->GetResultType();
1672 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001673 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001674 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001675 case Primitive::kPrimByte:
1676 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001677 case Primitive::kPrimBoolean:
1678 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001679 case Primitive::kPrimShort:
1680 case Primitive::kPrimInt:
1681 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001682 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001683 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001684 break;
1685
1686 default:
1687 LOG(FATAL) << "Unexpected type conversion from " << input_type
1688 << " to " << result_type;
1689 }
1690 break;
1691
Roland Levillain01a8d712014-11-14 16:27:39 +00001692 case Primitive::kPrimShort:
1693 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001694 case Primitive::kPrimBoolean:
1695 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001696 case Primitive::kPrimByte:
1697 case Primitive::kPrimInt:
1698 case Primitive::kPrimChar:
1699 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001700 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00001701 break;
1702
1703 default:
1704 LOG(FATAL) << "Unexpected type conversion from " << input_type
1705 << " to " << result_type;
1706 }
1707 break;
1708
Roland Levillain946e1432014-11-11 17:35:19 +00001709 case Primitive::kPrimInt:
1710 switch (input_type) {
1711 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001712 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001713 DCHECK(out.IsRegister());
1714 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001715 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001716 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001717 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00001718 } else {
1719 DCHECK(in.IsConstant());
1720 DCHECK(in.GetConstant()->IsLongConstant());
1721 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001722 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00001723 }
1724 break;
1725
Roland Levillain3f8f9362014-12-02 17:45:01 +00001726 case Primitive::kPrimFloat: {
1727 // Processing a Dex `float-to-int' instruction.
1728 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
1729 __ vmovs(temp, in.AsFpuRegister<SRegister>());
1730 __ vcvtis(temp, temp);
1731 __ vmovrs(out.AsRegister<Register>(), temp);
1732 break;
1733 }
1734
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001735 case Primitive::kPrimDouble: {
1736 // Processing a Dex `double-to-int' instruction.
1737 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
1738 DRegister temp_d = FromLowSToD(temp_s);
1739 __ vmovd(temp_d, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
1740 __ vcvtid(temp_s, temp_d);
1741 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00001742 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001743 }
Roland Levillain946e1432014-11-11 17:35:19 +00001744
1745 default:
1746 LOG(FATAL) << "Unexpected type conversion from " << input_type
1747 << " to " << result_type;
1748 }
1749 break;
1750
Roland Levillaindff1f282014-11-05 14:15:05 +00001751 case Primitive::kPrimLong:
1752 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001753 case Primitive::kPrimBoolean:
1754 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001755 case Primitive::kPrimByte:
1756 case Primitive::kPrimShort:
1757 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001758 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001759 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001760 DCHECK(out.IsRegisterPair());
1761 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001762 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00001763 // Sign extension.
1764 __ Asr(out.AsRegisterPairHigh<Register>(),
1765 out.AsRegisterPairLow<Register>(),
1766 31);
1767 break;
1768
1769 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001770 // Processing a Dex `float-to-long' instruction.
Roland Levillain624279f2014-12-04 11:54:28 +00001771 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
1772 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001773 conversion->GetDexPc(),
1774 nullptr);
Roland Levillain624279f2014-12-04 11:54:28 +00001775 break;
1776
Roland Levillaindff1f282014-11-05 14:15:05 +00001777 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001778 // Processing a Dex `double-to-long' instruction.
1779 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
1780 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001781 conversion->GetDexPc(),
1782 nullptr);
Roland Levillaindff1f282014-11-05 14:15:05 +00001783 break;
1784
1785 default:
1786 LOG(FATAL) << "Unexpected type conversion from " << input_type
1787 << " to " << result_type;
1788 }
1789 break;
1790
Roland Levillain981e4542014-11-14 11:47:14 +00001791 case Primitive::kPrimChar:
1792 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001793 case Primitive::kPrimBoolean:
1794 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001795 case Primitive::kPrimByte:
1796 case Primitive::kPrimShort:
1797 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001798 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001799 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00001800 break;
1801
1802 default:
1803 LOG(FATAL) << "Unexpected type conversion from " << input_type
1804 << " to " << result_type;
1805 }
1806 break;
1807
Roland Levillaindff1f282014-11-05 14:15:05 +00001808 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001809 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001810 case Primitive::kPrimBoolean:
1811 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001812 case Primitive::kPrimByte:
1813 case Primitive::kPrimShort:
1814 case Primitive::kPrimInt:
1815 case Primitive::kPrimChar: {
1816 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001817 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
1818 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001819 break;
1820 }
1821
Roland Levillain6d0e4832014-11-27 18:31:21 +00001822 case Primitive::kPrimLong: {
1823 // Processing a Dex `long-to-float' instruction.
1824 Register low = in.AsRegisterPairLow<Register>();
1825 Register high = in.AsRegisterPairHigh<Register>();
1826 SRegister output = out.AsFpuRegister<SRegister>();
1827 Register constant_low = locations->GetTemp(0).AsRegister<Register>();
1828 Register constant_high = locations->GetTemp(1).AsRegister<Register>();
1829 SRegister temp1_s = locations->GetTemp(2).AsFpuRegisterPairLow<SRegister>();
1830 DRegister temp1_d = FromLowSToD(temp1_s);
1831 SRegister temp2_s = locations->GetTemp(3).AsFpuRegisterPairLow<SRegister>();
1832 DRegister temp2_d = FromLowSToD(temp2_s);
1833
1834 // Operations use doubles for precision reasons (each 32-bit
1835 // half of a long fits in the 53-bit mantissa of a double,
1836 // but not in the 24-bit mantissa of a float). This is
1837 // especially important for the low bits. The result is
1838 // eventually converted to float.
1839
1840 // temp1_d = int-to-double(high)
1841 __ vmovsr(temp1_s, high);
1842 __ vcvtdi(temp1_d, temp1_s);
1843 // Using vmovd to load the `k2Pow32EncodingForDouble` constant
1844 // as an immediate value into `temp2_d` does not work, as
1845 // this instruction only transfers 8 significant bits of its
1846 // immediate operand. Instead, use two 32-bit core
1847 // registers to load `k2Pow32EncodingForDouble` into
1848 // `temp2_d`.
1849 __ LoadImmediate(constant_low, Low32Bits(k2Pow32EncodingForDouble));
1850 __ LoadImmediate(constant_high, High32Bits(k2Pow32EncodingForDouble));
1851 __ vmovdrr(temp2_d, constant_low, constant_high);
1852 // temp1_d = temp1_d * 2^32
1853 __ vmuld(temp1_d, temp1_d, temp2_d);
1854 // temp2_d = unsigned-to-double(low)
1855 __ vmovsr(temp2_s, low);
1856 __ vcvtdu(temp2_d, temp2_s);
1857 // temp1_d = temp1_d + temp2_d
1858 __ vaddd(temp1_d, temp1_d, temp2_d);
1859 // output = double-to-float(temp1_d);
1860 __ vcvtsd(output, temp1_d);
1861 break;
1862 }
1863
Roland Levillaincff13742014-11-17 14:32:17 +00001864 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001865 // Processing a Dex `double-to-float' instruction.
1866 __ vcvtsd(out.AsFpuRegister<SRegister>(),
1867 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00001868 break;
1869
1870 default:
1871 LOG(FATAL) << "Unexpected type conversion from " << input_type
1872 << " to " << result_type;
1873 };
1874 break;
1875
Roland Levillaindff1f282014-11-05 14:15:05 +00001876 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001877 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001878 case Primitive::kPrimBoolean:
1879 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001880 case Primitive::kPrimByte:
1881 case Primitive::kPrimShort:
1882 case Primitive::kPrimInt:
1883 case Primitive::kPrimChar: {
1884 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001885 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001886 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
1887 out.AsFpuRegisterPairLow<SRegister>());
1888 break;
1889 }
1890
Roland Levillain647b9ed2014-11-27 12:06:00 +00001891 case Primitive::kPrimLong: {
1892 // Processing a Dex `long-to-double' instruction.
1893 Register low = in.AsRegisterPairLow<Register>();
1894 Register high = in.AsRegisterPairHigh<Register>();
1895 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
1896 DRegister out_d = FromLowSToD(out_s);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001897 Register constant_low = locations->GetTemp(0).AsRegister<Register>();
1898 Register constant_high = locations->GetTemp(1).AsRegister<Register>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00001899 SRegister temp_s = locations->GetTemp(2).AsFpuRegisterPairLow<SRegister>();
1900 DRegister temp_d = FromLowSToD(temp_s);
1901
Roland Levillain647b9ed2014-11-27 12:06:00 +00001902 // out_d = int-to-double(high)
1903 __ vmovsr(out_s, high);
1904 __ vcvtdi(out_d, out_s);
Roland Levillain6d0e4832014-11-27 18:31:21 +00001905 // Using vmovd to load the `k2Pow32EncodingForDouble` constant
1906 // as an immediate value into `temp_d` does not work, as
1907 // this instruction only transfers 8 significant bits of its
1908 // immediate operand. Instead, use two 32-bit core
1909 // registers to load `k2Pow32EncodingForDouble` into `temp_d`.
1910 __ LoadImmediate(constant_low, Low32Bits(k2Pow32EncodingForDouble));
1911 __ LoadImmediate(constant_high, High32Bits(k2Pow32EncodingForDouble));
Roland Levillain647b9ed2014-11-27 12:06:00 +00001912 __ vmovdrr(temp_d, constant_low, constant_high);
1913 // out_d = out_d * 2^32
1914 __ vmuld(out_d, out_d, temp_d);
1915 // temp_d = unsigned-to-double(low)
1916 __ vmovsr(temp_s, low);
1917 __ vcvtdu(temp_d, temp_s);
1918 // out_d = out_d + temp_d
1919 __ vaddd(out_d, out_d, temp_d);
1920 break;
1921 }
1922
Roland Levillaincff13742014-11-17 14:32:17 +00001923 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001924 // Processing a Dex `float-to-double' instruction.
1925 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
1926 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001927 break;
1928
1929 default:
1930 LOG(FATAL) << "Unexpected type conversion from " << input_type
1931 << " to " << result_type;
1932 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001933 break;
1934
1935 default:
1936 LOG(FATAL) << "Unexpected type conversion from " << input_type
1937 << " to " << result_type;
1938 }
1939}
1940
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001941void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001942 LocationSummary* locations =
1943 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001944 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001945 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001946 locations->SetInAt(0, Location::RequiresRegister());
1947 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001948 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1949 break;
1950 }
1951
1952 case Primitive::kPrimLong: {
1953 locations->SetInAt(0, Location::RequiresRegister());
1954 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001955 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001956 break;
1957 }
1958
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001959 case Primitive::kPrimFloat:
1960 case Primitive::kPrimDouble: {
1961 locations->SetInAt(0, Location::RequiresFpuRegister());
1962 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00001963 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001964 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001965 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001966
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001967 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001968 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001969 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001970}
1971
1972void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
1973 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001974 Location out = locations->Out();
1975 Location first = locations->InAt(0);
1976 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001977 switch (add->GetResultType()) {
1978 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001979 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00001980 __ add(out.AsRegister<Register>(),
1981 first.AsRegister<Register>(),
1982 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001983 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001984 __ AddConstant(out.AsRegister<Register>(),
1985 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001986 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001987 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001988 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001989
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001990 case Primitive::kPrimLong: {
1991 DCHECK(second.IsRegisterPair());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001992 __ adds(out.AsRegisterPairLow<Register>(),
1993 first.AsRegisterPairLow<Register>(),
1994 ShifterOperand(second.AsRegisterPairLow<Register>()));
1995 __ adc(out.AsRegisterPairHigh<Register>(),
1996 first.AsRegisterPairHigh<Register>(),
1997 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001998 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001999 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002000
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002001 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00002002 __ vadds(out.AsFpuRegister<SRegister>(),
2003 first.AsFpuRegister<SRegister>(),
2004 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002005 break;
2006
2007 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002008 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2009 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2010 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002011 break;
2012
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002013 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002014 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002015 }
2016}
2017
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002018void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002019 LocationSummary* locations =
2020 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002021 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002022 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002023 locations->SetInAt(0, Location::RequiresRegister());
2024 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002025 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2026 break;
2027 }
2028
2029 case Primitive::kPrimLong: {
2030 locations->SetInAt(0, Location::RequiresRegister());
2031 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002032 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002033 break;
2034 }
Calin Juravle11351682014-10-23 15:38:15 +01002035 case Primitive::kPrimFloat:
2036 case Primitive::kPrimDouble: {
2037 locations->SetInAt(0, Location::RequiresFpuRegister());
2038 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002039 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002040 break;
Calin Juravle11351682014-10-23 15:38:15 +01002041 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002042 default:
Calin Juravle11351682014-10-23 15:38:15 +01002043 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002044 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002045}
2046
2047void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
2048 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002049 Location out = locations->Out();
2050 Location first = locations->InAt(0);
2051 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002052 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002053 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002054 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002055 __ sub(out.AsRegister<Register>(),
2056 first.AsRegister<Register>(),
2057 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002058 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002059 __ AddConstant(out.AsRegister<Register>(),
2060 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01002061 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002062 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002063 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002064 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002065
Calin Juravle11351682014-10-23 15:38:15 +01002066 case Primitive::kPrimLong: {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002067 DCHECK(second.IsRegisterPair());
Calin Juravle11351682014-10-23 15:38:15 +01002068 __ subs(out.AsRegisterPairLow<Register>(),
2069 first.AsRegisterPairLow<Register>(),
2070 ShifterOperand(second.AsRegisterPairLow<Register>()));
2071 __ sbc(out.AsRegisterPairHigh<Register>(),
2072 first.AsRegisterPairHigh<Register>(),
2073 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002074 break;
Calin Juravle11351682014-10-23 15:38:15 +01002075 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002076
Calin Juravle11351682014-10-23 15:38:15 +01002077 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002078 __ vsubs(out.AsFpuRegister<SRegister>(),
2079 first.AsFpuRegister<SRegister>(),
2080 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002081 break;
Calin Juravle11351682014-10-23 15:38:15 +01002082 }
2083
2084 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002085 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2086 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2087 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01002088 break;
2089 }
2090
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002091
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002092 default:
Calin Juravle11351682014-10-23 15:38:15 +01002093 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002094 }
2095}
2096
Calin Juravle34bacdf2014-10-07 20:23:36 +01002097void LocationsBuilderARM::VisitMul(HMul* mul) {
2098 LocationSummary* locations =
2099 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2100 switch (mul->GetResultType()) {
2101 case Primitive::kPrimInt:
2102 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002103 locations->SetInAt(0, Location::RequiresRegister());
2104 locations->SetInAt(1, Location::RequiresRegister());
2105 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002106 break;
2107 }
2108
Calin Juravleb5bfa962014-10-21 18:02:24 +01002109 case Primitive::kPrimFloat:
2110 case Primitive::kPrimDouble: {
2111 locations->SetInAt(0, Location::RequiresFpuRegister());
2112 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002113 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002114 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002115 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002116
2117 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002118 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002119 }
2120}
2121
2122void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
2123 LocationSummary* locations = mul->GetLocations();
2124 Location out = locations->Out();
2125 Location first = locations->InAt(0);
2126 Location second = locations->InAt(1);
2127 switch (mul->GetResultType()) {
2128 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00002129 __ mul(out.AsRegister<Register>(),
2130 first.AsRegister<Register>(),
2131 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002132 break;
2133 }
2134 case Primitive::kPrimLong: {
2135 Register out_hi = out.AsRegisterPairHigh<Register>();
2136 Register out_lo = out.AsRegisterPairLow<Register>();
2137 Register in1_hi = first.AsRegisterPairHigh<Register>();
2138 Register in1_lo = first.AsRegisterPairLow<Register>();
2139 Register in2_hi = second.AsRegisterPairHigh<Register>();
2140 Register in2_lo = second.AsRegisterPairLow<Register>();
2141
2142 // Extra checks to protect caused by the existence of R1_R2.
2143 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
2144 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
2145 DCHECK_NE(out_hi, in1_lo);
2146 DCHECK_NE(out_hi, in2_lo);
2147
2148 // input: in1 - 64 bits, in2 - 64 bits
2149 // output: out
2150 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2151 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2152 // parts: out.lo = (in1.lo * in2.lo)[31:0]
2153
2154 // IP <- in1.lo * in2.hi
2155 __ mul(IP, in1_lo, in2_hi);
2156 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2157 __ mla(out_hi, in1_hi, in2_lo, IP);
2158 // out.lo <- (in1.lo * in2.lo)[31:0];
2159 __ umull(out_lo, IP, in1_lo, in2_lo);
2160 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2161 __ add(out_hi, out_hi, ShifterOperand(IP));
2162 break;
2163 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002164
2165 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002166 __ vmuls(out.AsFpuRegister<SRegister>(),
2167 first.AsFpuRegister<SRegister>(),
2168 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002169 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002170 }
2171
2172 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002173 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2174 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2175 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01002176 break;
2177 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002178
2179 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002180 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002181 }
2182}
2183
Calin Juravle7c4954d2014-10-28 16:57:40 +00002184void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002185 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2186 if (div->GetResultType() == Primitive::kPrimLong) {
2187 // pLdiv runtime call.
2188 call_kind = LocationSummary::kCall;
2189 } else if (div->GetResultType() == Primitive::kPrimInt &&
2190 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
2191 // pIdivmod runtime call.
2192 call_kind = LocationSummary::kCall;
2193 }
2194
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002195 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2196
Calin Juravle7c4954d2014-10-28 16:57:40 +00002197 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002198 case Primitive::kPrimInt: {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002199 if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
2200 locations->SetInAt(0, Location::RequiresRegister());
2201 locations->SetInAt(1, Location::RequiresRegister());
2202 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2203 } else {
2204 InvokeRuntimeCallingConvention calling_convention;
2205 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2206 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2207 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
2208 // we only need the former.
2209 locations->SetOut(Location::RegisterLocation(R0));
2210 }
Calin Juravled0d48522014-11-04 16:40:20 +00002211 break;
2212 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002213 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002214 InvokeRuntimeCallingConvention calling_convention;
2215 locations->SetInAt(0, Location::RegisterPairLocation(
2216 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2217 locations->SetInAt(1, Location::RegisterPairLocation(
2218 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002219 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002220 break;
2221 }
2222 case Primitive::kPrimFloat:
2223 case Primitive::kPrimDouble: {
2224 locations->SetInAt(0, Location::RequiresFpuRegister());
2225 locations->SetInAt(1, Location::RequiresFpuRegister());
2226 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2227 break;
2228 }
2229
2230 default:
2231 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2232 }
2233}
2234
2235void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
2236 LocationSummary* locations = div->GetLocations();
2237 Location out = locations->Out();
2238 Location first = locations->InAt(0);
2239 Location second = locations->InAt(1);
2240
2241 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002242 case Primitive::kPrimInt: {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002243 if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
2244 __ sdiv(out.AsRegister<Register>(),
2245 first.AsRegister<Register>(),
2246 second.AsRegister<Register>());
2247 } else {
2248 InvokeRuntimeCallingConvention calling_convention;
2249 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
2250 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
2251 DCHECK_EQ(R0, out.AsRegister<Register>());
2252
2253 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), div, div->GetDexPc(), nullptr);
2254 }
Calin Juravled0d48522014-11-04 16:40:20 +00002255 break;
2256 }
2257
Calin Juravle7c4954d2014-10-28 16:57:40 +00002258 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002259 InvokeRuntimeCallingConvention calling_convention;
2260 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2261 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2262 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2263 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2264 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002265 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002266
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002267 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv), div, div->GetDexPc(), nullptr);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002268 break;
2269 }
2270
2271 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002272 __ vdivs(out.AsFpuRegister<SRegister>(),
2273 first.AsFpuRegister<SRegister>(),
2274 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002275 break;
2276 }
2277
2278 case Primitive::kPrimDouble: {
2279 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2280 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2281 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
2282 break;
2283 }
2284
2285 default:
2286 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2287 }
2288}
2289
Calin Juravlebacfec32014-11-14 15:54:36 +00002290void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002291 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002292
2293 // Most remainders are implemented in the runtime.
2294 LocationSummary::CallKind call_kind = LocationSummary::kCall;
2295 if (rem->GetResultType() == Primitive::kPrimInt &&
2296 codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
2297 // Have hardware divide instruction for int, do it with three instructions.
2298 call_kind = LocationSummary::kNoCall;
2299 }
2300
Calin Juravlebacfec32014-11-14 15:54:36 +00002301 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2302
Calin Juravled2ec87d2014-12-08 14:24:46 +00002303 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002304 case Primitive::kPrimInt: {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002305 if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
2306 locations->SetInAt(0, Location::RequiresRegister());
2307 locations->SetInAt(1, Location::RequiresRegister());
2308 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2309 locations->AddTemp(Location::RequiresRegister());
2310 } else {
2311 InvokeRuntimeCallingConvention calling_convention;
2312 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2313 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2314 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
2315 // we only need the latter.
2316 locations->SetOut(Location::RegisterLocation(R1));
2317 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002318 break;
2319 }
2320 case Primitive::kPrimLong: {
2321 InvokeRuntimeCallingConvention calling_convention;
2322 locations->SetInAt(0, Location::RegisterPairLocation(
2323 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2324 locations->SetInAt(1, Location::RegisterPairLocation(
2325 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2326 // The runtime helper puts the output in R2,R3.
2327 locations->SetOut(Location::RegisterPairLocation(R2, R3));
2328 break;
2329 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00002330 case Primitive::kPrimFloat: {
2331 InvokeRuntimeCallingConvention calling_convention;
2332 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
2333 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
2334 locations->SetOut(Location::FpuRegisterLocation(S0));
2335 break;
2336 }
2337
Calin Juravlebacfec32014-11-14 15:54:36 +00002338 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002339 InvokeRuntimeCallingConvention calling_convention;
2340 locations->SetInAt(0, Location::FpuRegisterPairLocation(
2341 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
2342 locations->SetInAt(1, Location::FpuRegisterPairLocation(
2343 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
2344 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00002345 break;
2346 }
2347
2348 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002349 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002350 }
2351}
2352
2353void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
2354 LocationSummary* locations = rem->GetLocations();
2355 Location out = locations->Out();
2356 Location first = locations->InAt(0);
2357 Location second = locations->InAt(1);
2358
Calin Juravled2ec87d2014-12-08 14:24:46 +00002359 Primitive::Type type = rem->GetResultType();
2360 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002361 case Primitive::kPrimInt: {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002362 if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
2363 Register reg1 = first.AsRegister<Register>();
2364 Register reg2 = second.AsRegister<Register>();
2365 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00002366
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002367 // temp = reg1 / reg2 (integer division)
2368 // temp = temp * reg2
2369 // dest = reg1 - temp
2370 __ sdiv(temp, reg1, reg2);
2371 __ mul(temp, temp, reg2);
2372 __ sub(out.AsRegister<Register>(), reg1, ShifterOperand(temp));
2373 } else {
2374 InvokeRuntimeCallingConvention calling_convention;
2375 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
2376 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
2377 DCHECK_EQ(R1, out.AsRegister<Register>());
2378
2379 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), rem, rem->GetDexPc(), nullptr);
2380 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002381 break;
2382 }
2383
2384 case Primitive::kPrimLong: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002385 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod), rem, rem->GetDexPc(), nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00002386 break;
2387 }
2388
Calin Juravled2ec87d2014-12-08 14:24:46 +00002389 case Primitive::kPrimFloat: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002390 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf), rem, rem->GetDexPc(), nullptr);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002391 break;
2392 }
2393
Calin Juravlebacfec32014-11-14 15:54:36 +00002394 case Primitive::kPrimDouble: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002395 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod), rem, rem->GetDexPc(), nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00002396 break;
2397 }
2398
2399 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002400 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002401 }
2402}
2403
Calin Juravled0d48522014-11-04 16:40:20 +00002404void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2405 LocationSummary* locations =
2406 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002407 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00002408 if (instruction->HasUses()) {
2409 locations->SetOut(Location::SameAsFirstInput());
2410 }
2411}
2412
2413void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2414 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
2415 codegen_->AddSlowPath(slow_path);
2416
2417 LocationSummary* locations = instruction->GetLocations();
2418 Location value = locations->InAt(0);
2419
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002420 switch (instruction->GetType()) {
2421 case Primitive::kPrimInt: {
2422 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002423 __ cmp(value.AsRegister<Register>(), ShifterOperand(0));
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002424 __ b(slow_path->GetEntryLabel(), EQ);
2425 } else {
2426 DCHECK(value.IsConstant()) << value;
2427 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2428 __ b(slow_path->GetEntryLabel());
2429 }
2430 }
2431 break;
2432 }
2433 case Primitive::kPrimLong: {
2434 if (value.IsRegisterPair()) {
2435 __ orrs(IP,
2436 value.AsRegisterPairLow<Register>(),
2437 ShifterOperand(value.AsRegisterPairHigh<Register>()));
2438 __ b(slow_path->GetEntryLabel(), EQ);
2439 } else {
2440 DCHECK(value.IsConstant()) << value;
2441 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2442 __ b(slow_path->GetEntryLabel());
2443 }
2444 }
2445 break;
2446 default:
2447 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2448 }
2449 }
Calin Juravled0d48522014-11-04 16:40:20 +00002450}
2451
Calin Juravle9aec02f2014-11-18 23:06:35 +00002452void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
2453 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2454
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002455 LocationSummary* locations =
2456 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002457
2458 switch (op->GetResultType()) {
2459 case Primitive::kPrimInt: {
2460 locations->SetInAt(0, Location::RequiresRegister());
2461 locations->SetInAt(1, Location::RegisterOrConstant(op->InputAt(1)));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002462 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002463 break;
2464 }
2465 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002466 locations->SetInAt(0, Location::RequiresRegister());
2467 locations->SetInAt(1, Location::RequiresRegister());
2468 locations->AddTemp(Location::RequiresRegister());
2469 locations->SetOut(Location::RequiresRegister());
Calin Juravle9aec02f2014-11-18 23:06:35 +00002470 break;
2471 }
2472 default:
2473 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2474 }
2475}
2476
2477void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
2478 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2479
2480 LocationSummary* locations = op->GetLocations();
2481 Location out = locations->Out();
2482 Location first = locations->InAt(0);
2483 Location second = locations->InAt(1);
2484
2485 Primitive::Type type = op->GetResultType();
2486 switch (type) {
2487 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002488 Register out_reg = out.AsRegister<Register>();
2489 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002490 // Arm doesn't mask the shift count so we need to do it ourselves.
2491 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002492 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002493 __ and_(second_reg, second_reg, ShifterOperand(kMaxIntShiftValue));
2494 if (op->IsShl()) {
2495 __ Lsl(out_reg, first_reg, second_reg);
2496 } else if (op->IsShr()) {
2497 __ Asr(out_reg, first_reg, second_reg);
2498 } else {
2499 __ Lsr(out_reg, first_reg, second_reg);
2500 }
2501 } else {
2502 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
2503 uint32_t shift_value = static_cast<uint32_t>(cst & kMaxIntShiftValue);
2504 if (shift_value == 0) { // arm does not support shifting with 0 immediate.
2505 __ Mov(out_reg, first_reg);
2506 } else if (op->IsShl()) {
2507 __ Lsl(out_reg, first_reg, shift_value);
2508 } else if (op->IsShr()) {
2509 __ Asr(out_reg, first_reg, shift_value);
2510 } else {
2511 __ Lsr(out_reg, first_reg, shift_value);
2512 }
2513 }
2514 break;
2515 }
2516 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002517 Register o_h = out.AsRegisterPairHigh<Register>();
2518 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002519
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002520 Register temp = locations->GetTemp(0).AsRegister<Register>();
2521
2522 Register high = first.AsRegisterPairHigh<Register>();
2523 Register low = first.AsRegisterPairLow<Register>();
2524
2525 Register second_reg = second.AsRegister<Register>();
2526
Calin Juravle9aec02f2014-11-18 23:06:35 +00002527 if (op->IsShl()) {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002528 // Shift the high part
2529 __ and_(second_reg, second_reg, ShifterOperand(63));
2530 __ Lsl(o_h, high, second_reg);
2531 // Shift the low part and `or` what overflew on the high part
2532 __ rsb(temp, second_reg, ShifterOperand(32));
2533 __ Lsr(temp, low, temp);
2534 __ orr(o_h, o_h, ShifterOperand(temp));
2535 // If the shift is > 32 bits, override the high part
2536 __ subs(temp, second_reg, ShifterOperand(32));
2537 __ it(PL);
2538 __ Lsl(o_h, low, temp, false, PL);
2539 // Shift the low part
2540 __ Lsl(o_l, low, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002541 } else if (op->IsShr()) {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002542 // Shift the low part
2543 __ and_(second_reg, second_reg, ShifterOperand(63));
2544 __ Lsr(o_l, low, second_reg);
2545 // Shift the high part and `or` what underflew on the low part
2546 __ rsb(temp, second_reg, ShifterOperand(32));
2547 __ Lsl(temp, high, temp);
2548 __ orr(o_l, o_l, ShifterOperand(temp));
2549 // If the shift is > 32 bits, override the low part
2550 __ subs(temp, second_reg, ShifterOperand(32));
2551 __ it(PL);
2552 __ Asr(o_l, high, temp, false, PL);
2553 // Shift the high part
2554 __ Asr(o_h, high, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002555 } else {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002556 // same as Shr except we use `Lsr`s and not `Asr`s
2557 __ and_(second_reg, second_reg, ShifterOperand(63));
2558 __ Lsr(o_l, low, second_reg);
2559 __ rsb(temp, second_reg, ShifterOperand(32));
2560 __ Lsl(temp, high, temp);
2561 __ orr(o_l, o_l, ShifterOperand(temp));
2562 __ subs(temp, second_reg, ShifterOperand(32));
2563 __ it(PL);
2564 __ Lsr(o_l, high, temp, false, PL);
2565 __ Lsr(o_h, high, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002566 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00002567 break;
2568 }
2569 default:
2570 LOG(FATAL) << "Unexpected operation type " << type;
2571 }
2572}
2573
2574void LocationsBuilderARM::VisitShl(HShl* shl) {
2575 HandleShift(shl);
2576}
2577
2578void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
2579 HandleShift(shl);
2580}
2581
2582void LocationsBuilderARM::VisitShr(HShr* shr) {
2583 HandleShift(shr);
2584}
2585
2586void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
2587 HandleShift(shr);
2588}
2589
2590void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
2591 HandleShift(ushr);
2592}
2593
2594void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
2595 HandleShift(ushr);
2596}
2597
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002598void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002599 LocationSummary* locations =
2600 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002601 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002602 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2603 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2604 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002605}
2606
2607void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
2608 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002609 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002610 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002611 codegen_->InvokeRuntime(GetThreadOffset<kArmWordSize>(instruction->GetEntrypoint()).Int32Value(),
2612 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002613 instruction->GetDexPc(),
2614 nullptr);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002615}
2616
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002617void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
2618 LocationSummary* locations =
2619 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2620 InvokeRuntimeCallingConvention calling_convention;
2621 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002622 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002623 locations->SetOut(Location::RegisterLocation(R0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002624 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002625}
2626
2627void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
2628 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002629 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(2));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002630 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002631 codegen_->InvokeRuntime(GetThreadOffset<kArmWordSize>(instruction->GetEntrypoint()).Int32Value(),
2632 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002633 instruction->GetDexPc(),
2634 nullptr);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002635}
2636
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002637void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002638 LocationSummary* locations =
2639 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002640 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2641 if (location.IsStackSlot()) {
2642 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2643 } else if (location.IsDoubleStackSlot()) {
2644 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002645 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002646 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002647}
2648
2649void InstructionCodeGeneratorARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002650 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002651 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002652}
2653
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002654void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002655 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002656 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002657 locations->SetInAt(0, Location::RequiresRegister());
2658 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002659}
2660
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002661void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
2662 LocationSummary* locations = not_->GetLocations();
2663 Location out = locations->Out();
2664 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00002665 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002666 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002667 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002668 break;
2669
2670 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01002671 __ mvn(out.AsRegisterPairLow<Register>(),
2672 ShifterOperand(in.AsRegisterPairLow<Register>()));
2673 __ mvn(out.AsRegisterPairHigh<Register>(),
2674 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002675 break;
2676
2677 default:
2678 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2679 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002680}
2681
David Brazdil66d126e2015-04-03 16:02:44 +01002682void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
2683 LocationSummary* locations =
2684 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
2685 locations->SetInAt(0, Location::RequiresRegister());
2686 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2687}
2688
2689void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01002690 LocationSummary* locations = bool_not->GetLocations();
2691 Location out = locations->Out();
2692 Location in = locations->InAt(0);
2693 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
2694}
2695
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002696void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002697 LocationSummary* locations =
2698 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00002699 switch (compare->InputAt(0)->GetType()) {
2700 case Primitive::kPrimLong: {
2701 locations->SetInAt(0, Location::RequiresRegister());
2702 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002703 // Output overlaps because it is written before doing the low comparison.
2704 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00002705 break;
2706 }
2707 case Primitive::kPrimFloat:
2708 case Primitive::kPrimDouble: {
2709 locations->SetInAt(0, Location::RequiresFpuRegister());
2710 locations->SetInAt(1, Location::RequiresFpuRegister());
2711 locations->SetOut(Location::RequiresRegister());
2712 break;
2713 }
2714 default:
2715 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
2716 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002717}
2718
2719void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002720 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002721 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00002722 Location left = locations->InAt(0);
2723 Location right = locations->InAt(1);
2724
2725 Label less, greater, done;
2726 Primitive::Type type = compare->InputAt(0)->GetType();
2727 switch (type) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002728 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002729 __ cmp(left.AsRegisterPairHigh<Register>(),
2730 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002731 __ b(&less, LT);
2732 __ b(&greater, GT);
Calin Juravleddb7df22014-11-25 20:56:51 +00002733 // Do LoadImmediate before any `cmp`, as LoadImmediate might affect the status flags.
2734 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002735 __ cmp(left.AsRegisterPairLow<Register>(),
2736 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Calin Juravleddb7df22014-11-25 20:56:51 +00002737 break;
2738 }
2739 case Primitive::kPrimFloat:
2740 case Primitive::kPrimDouble: {
2741 __ LoadImmediate(out, 0);
2742 if (type == Primitive::kPrimFloat) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002743 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00002744 } else {
2745 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
2746 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
2747 }
2748 __ vmstat(); // transfer FP status register to ARM APSR.
2749 __ b(compare->IsGtBias() ? &greater : &less, VS); // VS for unordered.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002750 break;
2751 }
2752 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00002753 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002754 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002755 __ b(&done, EQ);
2756 __ b(&less, CC); // CC is for both: unsigned compare for longs and 'less than' for floats.
2757
2758 __ Bind(&greater);
2759 __ LoadImmediate(out, 1);
2760 __ b(&done);
2761
2762 __ Bind(&less);
2763 __ LoadImmediate(out, -1);
2764
2765 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002766}
2767
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002768void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002769 LocationSummary* locations =
2770 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01002771 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2772 locations->SetInAt(i, Location::Any());
2773 }
2774 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002775}
2776
2777void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002778 UNUSED(instruction);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002779 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002780}
2781
Calin Juravle52c48962014-12-16 17:02:57 +00002782void InstructionCodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
2783 // TODO (ported from quick): revisit Arm barrier kinds
2784 DmbOptions flavour = DmbOptions::ISH; // quiet c++ warnings
2785 switch (kind) {
2786 case MemBarrierKind::kAnyStore:
2787 case MemBarrierKind::kLoadAny:
2788 case MemBarrierKind::kAnyAny: {
2789 flavour = DmbOptions::ISH;
2790 break;
2791 }
2792 case MemBarrierKind::kStoreStore: {
2793 flavour = DmbOptions::ISHST;
2794 break;
2795 }
2796 default:
2797 LOG(FATAL) << "Unexpected memory barrier " << kind;
2798 }
2799 __ dmb(flavour);
2800}
2801
2802void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
2803 uint32_t offset,
2804 Register out_lo,
2805 Register out_hi) {
2806 if (offset != 0) {
2807 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00002808 __ add(IP, addr, ShifterOperand(out_lo));
2809 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00002810 }
2811 __ ldrexd(out_lo, out_hi, addr);
2812}
2813
2814void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
2815 uint32_t offset,
2816 Register value_lo,
2817 Register value_hi,
2818 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00002819 Register temp2,
2820 HInstruction* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00002821 Label fail;
2822 if (offset != 0) {
2823 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00002824 __ add(IP, addr, ShifterOperand(temp1));
2825 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00002826 }
2827 __ Bind(&fail);
2828 // We need a load followed by store. (The address used in a STREX instruction must
2829 // be the same as the address in the most recently executed LDREX instruction.)
2830 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00002831 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002832 __ strexd(temp1, value_lo, value_hi, addr);
2833 __ cmp(temp1, ShifterOperand(0));
2834 __ b(&fail, NE);
2835}
2836
2837void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
2838 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2839
Nicolas Geoffray39468442014-09-02 15:17:15 +01002840 LocationSummary* locations =
2841 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002842 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00002843
Calin Juravle52c48962014-12-16 17:02:57 +00002844 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002845 if (Primitive::IsFloatingPointType(field_type)) {
2846 locations->SetInAt(1, Location::RequiresFpuRegister());
2847 } else {
2848 locations->SetInAt(1, Location::RequiresRegister());
2849 }
2850
Calin Juravle52c48962014-12-16 17:02:57 +00002851 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00002852 bool generate_volatile = field_info.IsVolatile()
2853 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002854 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002855 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00002856 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
2857 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002858 locations->AddTemp(Location::RequiresRegister());
2859 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00002860 } else if (generate_volatile) {
Calin Juravle52c48962014-12-16 17:02:57 +00002861 // Arm encoding have some additional constraints for ldrexd/strexd:
2862 // - registers need to be consecutive
2863 // - the first register should be even but not R14.
2864 // We don't test for Arm yet, and the assertion makes sure that we revisit this if we ever
2865 // enable Arm encoding.
2866 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
2867
2868 locations->AddTemp(Location::RequiresRegister());
2869 locations->AddTemp(Location::RequiresRegister());
2870 if (field_type == Primitive::kPrimDouble) {
2871 // For doubles we need two more registers to copy the value.
2872 locations->AddTemp(Location::RegisterLocation(R2));
2873 locations->AddTemp(Location::RegisterLocation(R3));
2874 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002875 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002876}
2877
Calin Juravle52c48962014-12-16 17:02:57 +00002878void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
2879 const FieldInfo& field_info) {
2880 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2881
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002882 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00002883 Register base = locations->InAt(0).AsRegister<Register>();
2884 Location value = locations->InAt(1);
2885
2886 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002887 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00002888 Primitive::Type field_type = field_info.GetFieldType();
2889 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2890
2891 if (is_volatile) {
2892 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
2893 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002894
2895 switch (field_type) {
2896 case Primitive::kPrimBoolean:
2897 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00002898 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002899 break;
2900 }
2901
2902 case Primitive::kPrimShort:
2903 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00002904 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002905 break;
2906 }
2907
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002908 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002909 case Primitive::kPrimNot: {
Calin Juravle77520bc2015-01-12 18:45:46 +00002910 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002911 break;
2912 }
2913
2914 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00002915 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00002916 GenerateWideAtomicStore(base, offset,
2917 value.AsRegisterPairLow<Register>(),
2918 value.AsRegisterPairHigh<Register>(),
2919 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00002920 locations->GetTemp(1).AsRegister<Register>(),
2921 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002922 } else {
2923 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00002924 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002925 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002926 break;
2927 }
2928
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002929 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00002930 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002931 break;
2932 }
2933
2934 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00002935 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00002936 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00002937 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
2938 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
2939
2940 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
2941
2942 GenerateWideAtomicStore(base, offset,
2943 value_reg_lo,
2944 value_reg_hi,
2945 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00002946 locations->GetTemp(3).AsRegister<Register>(),
2947 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002948 } else {
2949 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00002950 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002951 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002952 break;
2953 }
2954
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002955 case Primitive::kPrimVoid:
2956 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002957 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002958 }
Calin Juravle52c48962014-12-16 17:02:57 +00002959
Calin Juravle77520bc2015-01-12 18:45:46 +00002960 // Longs and doubles are handled in the switch.
2961 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
2962 codegen_->MaybeRecordImplicitNullCheck(instruction);
2963 }
2964
2965 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
2966 Register temp = locations->GetTemp(0).AsRegister<Register>();
2967 Register card = locations->GetTemp(1).AsRegister<Register>();
2968 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>());
2969 }
2970
Calin Juravle52c48962014-12-16 17:02:57 +00002971 if (is_volatile) {
2972 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
2973 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002974}
2975
Calin Juravle52c48962014-12-16 17:02:57 +00002976void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
2977 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002978 LocationSummary* locations =
2979 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002980 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00002981
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002982 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00002983 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002984 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002985 bool overlap = field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong);
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01002986
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002987 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2988 locations->SetOut(Location::RequiresFpuRegister());
2989 } else {
2990 locations->SetOut(Location::RequiresRegister(),
2991 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
2992 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002993 if (volatile_for_double) {
Calin Juravle52c48962014-12-16 17:02:57 +00002994 // Arm encoding have some additional constraints for ldrexd/strexd:
2995 // - registers need to be consecutive
2996 // - the first register should be even but not R14.
2997 // We don't test for Arm yet, and the assertion makes sure that we revisit this if we ever
2998 // enable Arm encoding.
2999 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3000 locations->AddTemp(Location::RequiresRegister());
3001 locations->AddTemp(Location::RequiresRegister());
3002 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003003}
3004
Calin Juravle52c48962014-12-16 17:02:57 +00003005void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
3006 const FieldInfo& field_info) {
3007 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003008
Calin Juravle52c48962014-12-16 17:02:57 +00003009 LocationSummary* locations = instruction->GetLocations();
3010 Register base = locations->InAt(0).AsRegister<Register>();
3011 Location out = locations->Out();
3012 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003013 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00003014 Primitive::Type field_type = field_info.GetFieldType();
3015 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3016
3017 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003018 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003019 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003020 break;
3021 }
3022
3023 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003024 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003025 break;
3026 }
3027
3028 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003029 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003030 break;
3031 }
3032
3033 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003034 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003035 break;
3036 }
3037
3038 case Primitive::kPrimInt:
3039 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003040 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003041 break;
3042 }
3043
3044 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00003045 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003046 GenerateWideAtomicLoad(base, offset,
3047 out.AsRegisterPairLow<Register>(),
3048 out.AsRegisterPairHigh<Register>());
3049 } else {
3050 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
3051 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003052 break;
3053 }
3054
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003055 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003056 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003057 break;
3058 }
3059
3060 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003061 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00003062 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003063 Register lo = locations->GetTemp(0).AsRegister<Register>();
3064 Register hi = locations->GetTemp(1).AsRegister<Register>();
3065 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00003066 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003067 __ vmovdrr(out_reg, lo, hi);
3068 } else {
3069 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003070 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003071 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003072 break;
3073 }
3074
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003075 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003076 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003077 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003078 }
Calin Juravle52c48962014-12-16 17:02:57 +00003079
Calin Juravle77520bc2015-01-12 18:45:46 +00003080 // Doubles are handled in the switch.
3081 if (field_type != Primitive::kPrimDouble) {
3082 codegen_->MaybeRecordImplicitNullCheck(instruction);
3083 }
3084
Calin Juravle52c48962014-12-16 17:02:57 +00003085 if (is_volatile) {
3086 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3087 }
3088}
3089
3090void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3091 HandleFieldSet(instruction, instruction->GetFieldInfo());
3092}
3093
3094void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3095 HandleFieldSet(instruction, instruction->GetFieldInfo());
3096}
3097
3098void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3099 HandleFieldGet(instruction, instruction->GetFieldInfo());
3100}
3101
3102void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3103 HandleFieldGet(instruction, instruction->GetFieldInfo());
3104}
3105
3106void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3107 HandleFieldGet(instruction, instruction->GetFieldInfo());
3108}
3109
3110void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3111 HandleFieldGet(instruction, instruction->GetFieldInfo());
3112}
3113
3114void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3115 HandleFieldSet(instruction, instruction->GetFieldInfo());
3116}
3117
3118void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3119 HandleFieldSet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003120}
3121
3122void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003123 LocationSummary* locations =
3124 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle77520bc2015-01-12 18:45:46 +00003125 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003126 if (instruction->HasUses()) {
3127 locations->SetOut(Location::SameAsFirstInput());
3128 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003129}
3130
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003131void InstructionCodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003132 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3133 return;
3134 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003135 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003136
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003137 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
3138 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3139}
3140
3141void InstructionCodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003142 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003143 codegen_->AddSlowPath(slow_path);
3144
3145 LocationSummary* locations = instruction->GetLocations();
3146 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003147
Calin Juravle77520bc2015-01-12 18:45:46 +00003148 __ cmp(obj.AsRegister<Register>(), ShifterOperand(0));
3149 __ b(slow_path->GetEntryLabel(), EQ);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003150}
3151
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003152void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
3153 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3154 GenerateImplicitNullCheck(instruction);
3155 } else {
3156 GenerateExplicitNullCheck(instruction);
3157 }
3158}
3159
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003160void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003161 LocationSummary* locations =
3162 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003163 locations->SetInAt(0, Location::RequiresRegister());
3164 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003165 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3166 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3167 } else {
3168 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3169 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003170}
3171
3172void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
3173 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003174 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003175 Location index = locations->InAt(1);
3176
3177 switch (instruction->GetType()) {
3178 case Primitive::kPrimBoolean: {
3179 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003180 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003181 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003182 size_t offset =
3183 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003184 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
3185 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003186 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003187 __ LoadFromOffset(kLoadUnsignedByte, out, IP, data_offset);
3188 }
3189 break;
3190 }
3191
3192 case Primitive::kPrimByte: {
3193 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003194 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003195 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003196 size_t offset =
3197 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003198 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
3199 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003200 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003201 __ LoadFromOffset(kLoadSignedByte, out, IP, data_offset);
3202 }
3203 break;
3204 }
3205
3206 case Primitive::kPrimShort: {
3207 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003208 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003209 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003210 size_t offset =
3211 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003212 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
3213 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003214 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003215 __ LoadFromOffset(kLoadSignedHalfword, out, IP, data_offset);
3216 }
3217 break;
3218 }
3219
3220 case Primitive::kPrimChar: {
3221 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003222 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003223 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003224 size_t offset =
3225 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003226 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
3227 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003228 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003229 __ LoadFromOffset(kLoadUnsignedHalfword, out, IP, data_offset);
3230 }
3231 break;
3232 }
3233
3234 case Primitive::kPrimInt:
3235 case Primitive::kPrimNot: {
3236 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
3237 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003238 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003239 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003240 size_t offset =
3241 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003242 __ LoadFromOffset(kLoadWord, out, obj, offset);
3243 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003244 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003245 __ LoadFromOffset(kLoadWord, out, IP, data_offset);
3246 }
3247 break;
3248 }
3249
3250 case Primitive::kPrimLong: {
3251 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003252 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003253 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003254 size_t offset =
3255 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003256 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003257 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003258 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003259 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003260 }
3261 break;
3262 }
3263
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003264 case Primitive::kPrimFloat: {
3265 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3266 Location out = locations->Out();
3267 DCHECK(out.IsFpuRegister());
3268 if (index.IsConstant()) {
3269 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3270 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), obj, offset);
3271 } else {
3272 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
3273 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), IP, data_offset);
3274 }
3275 break;
3276 }
3277
3278 case Primitive::kPrimDouble: {
3279 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3280 Location out = locations->Out();
3281 DCHECK(out.IsFpuRegisterPair());
3282 if (index.IsConstant()) {
3283 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3284 __ LoadDFromOffset(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()), obj, offset);
3285 } else {
3286 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
3287 __ LoadDFromOffset(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
3288 }
3289 break;
3290 }
3291
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003292 case Primitive::kPrimVoid:
3293 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003294 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003295 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003296 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003297}
3298
3299void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003300 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003301
3302 bool needs_write_barrier =
3303 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3304 bool needs_runtime_call = instruction->NeedsTypeCheck();
3305
Nicolas Geoffray39468442014-09-02 15:17:15 +01003306 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003307 instruction, needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
3308 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003309 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003310 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3311 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3312 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003313 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003314 locations->SetInAt(0, Location::RequiresRegister());
3315 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003316 if (Primitive::IsFloatingPointType(value_type)) {
3317 locations->SetInAt(2, Location::RequiresFpuRegister());
3318 } else {
3319 locations->SetInAt(2, Location::RequiresRegister());
3320 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003321
3322 if (needs_write_barrier) {
3323 // Temporary registers for the write barrier.
3324 locations->AddTemp(Location::RequiresRegister());
3325 locations->AddTemp(Location::RequiresRegister());
3326 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003327 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003328}
3329
3330void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
3331 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003332 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003333 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003334 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003335 bool needs_runtime_call = locations->WillCall();
3336 bool needs_write_barrier =
3337 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003338
3339 switch (value_type) {
3340 case Primitive::kPrimBoolean:
3341 case Primitive::kPrimByte: {
3342 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003343 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003344 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003345 size_t offset =
3346 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003347 __ StoreToOffset(kStoreByte, value, obj, offset);
3348 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003349 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003350 __ StoreToOffset(kStoreByte, value, IP, data_offset);
3351 }
3352 break;
3353 }
3354
3355 case Primitive::kPrimShort:
3356 case Primitive::kPrimChar: {
3357 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003358 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003359 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003360 size_t offset =
3361 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003362 __ StoreToOffset(kStoreHalfword, value, obj, offset);
3363 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003364 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003365 __ StoreToOffset(kStoreHalfword, value, IP, data_offset);
3366 }
3367 break;
3368 }
3369
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003370 case Primitive::kPrimInt:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003371 case Primitive::kPrimNot: {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003372 if (!needs_runtime_call) {
3373 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003374 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003375 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003376 size_t offset =
3377 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003378 __ StoreToOffset(kStoreWord, value, obj, offset);
3379 } else {
3380 DCHECK(index.IsRegister()) << index;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003381 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003382 __ StoreToOffset(kStoreWord, value, IP, data_offset);
3383 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003384 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003385 if (needs_write_barrier) {
3386 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003387 Register temp = locations->GetTemp(0).AsRegister<Register>();
3388 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003389 codegen_->MarkGCCard(temp, card, obj, value);
3390 }
3391 } else {
3392 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain199f3362014-11-27 17:15:16 +00003393 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
3394 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003395 instruction->GetDexPc(),
3396 nullptr);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003397 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003398 break;
3399 }
3400
3401 case Primitive::kPrimLong: {
3402 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003403 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003404 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003405 size_t offset =
3406 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003407 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003408 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003409 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003410 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003411 }
3412 break;
3413 }
3414
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003415 case Primitive::kPrimFloat: {
3416 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3417 Location value = locations->InAt(2);
3418 DCHECK(value.IsFpuRegister());
3419 if (index.IsConstant()) {
3420 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3421 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), obj, offset);
3422 } else {
3423 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
3424 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
3425 }
3426 break;
3427 }
3428
3429 case Primitive::kPrimDouble: {
3430 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3431 Location value = locations->InAt(2);
3432 DCHECK(value.IsFpuRegisterPair());
3433 if (index.IsConstant()) {
3434 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3435 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), obj, offset);
3436 } else {
3437 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
3438 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
3439 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003440
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003441 break;
3442 }
3443
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003444 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003445 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003446 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003447 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003448
3449 // Ints and objects are handled in the switch.
3450 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
3451 codegen_->MaybeRecordImplicitNullCheck(instruction);
3452 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003453}
3454
3455void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003456 LocationSummary* locations =
3457 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003458 locations->SetInAt(0, Location::RequiresRegister());
3459 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003460}
3461
3462void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
3463 LocationSummary* locations = instruction->GetLocations();
3464 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003465 Register obj = locations->InAt(0).AsRegister<Register>();
3466 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003467 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003468 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003469}
3470
3471void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003472 LocationSummary* locations =
3473 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003474 locations->SetInAt(0, Location::RequiresRegister());
3475 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003476 if (instruction->HasUses()) {
3477 locations->SetOut(Location::SameAsFirstInput());
3478 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003479}
3480
3481void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
3482 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003483 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(
Nicolas Geoffray39468442014-09-02 15:17:15 +01003484 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003485 codegen_->AddSlowPath(slow_path);
3486
Roland Levillain271ab9c2014-11-27 15:23:57 +00003487 Register index = locations->InAt(0).AsRegister<Register>();
3488 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003489
3490 __ cmp(index, ShifterOperand(length));
3491 __ b(slow_path->GetEntryLabel(), CS);
3492}
3493
3494void CodeGeneratorARM::MarkGCCard(Register temp, Register card, Register object, Register value) {
3495 Label is_null;
3496 __ CompareAndBranchIfZero(value, &is_null);
3497 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmWordSize>().Int32Value());
3498 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
3499 __ strb(card, Address(card, temp));
3500 __ Bind(&is_null);
3501}
3502
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003503void LocationsBuilderARM::VisitTemporary(HTemporary* temp) {
3504 temp->SetLocations(nullptr);
3505}
3506
3507void InstructionCodeGeneratorARM::VisitTemporary(HTemporary* temp) {
3508 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003509 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003510}
3511
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003512void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003513 UNUSED(instruction);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003514 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003515}
3516
3517void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003518 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3519}
3520
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003521void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
3522 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3523}
3524
3525void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003526 HBasicBlock* block = instruction->GetBlock();
3527 if (block->GetLoopInformation() != nullptr) {
3528 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3529 // The back edge will generate the suspend check.
3530 return;
3531 }
3532 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3533 // The goto will generate the suspend check.
3534 return;
3535 }
3536 GenerateSuspendCheck(instruction, nullptr);
3537}
3538
3539void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
3540 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003541 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003542 new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003543 codegen_->AddSlowPath(slow_path);
3544
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00003545 __ LoadFromOffset(
3546 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmWordSize>().Int32Value());
3547 __ cmp(IP, ShifterOperand(0));
3548 // TODO: Figure out the branch offsets and use cbz/cbnz.
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003549 if (successor == nullptr) {
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00003550 __ b(slow_path->GetEntryLabel(), NE);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003551 __ Bind(slow_path->GetReturnLabel());
3552 } else {
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00003553 __ b(codegen_->GetLabelOf(successor), EQ);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003554 __ b(slow_path->GetEntryLabel());
3555 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003556}
3557
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003558ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
3559 return codegen_->GetAssembler();
3560}
3561
3562void ParallelMoveResolverARM::EmitMove(size_t index) {
3563 MoveOperands* move = moves_.Get(index);
3564 Location source = move->GetSource();
3565 Location destination = move->GetDestination();
3566
3567 if (source.IsRegister()) {
3568 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003569 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003570 } else {
3571 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003572 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003573 SP, destination.GetStackIndex());
3574 }
3575 } else if (source.IsStackSlot()) {
3576 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003577 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003578 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003579 } else if (destination.IsFpuRegister()) {
3580 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003581 } else {
3582 DCHECK(destination.IsStackSlot());
3583 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
3584 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
3585 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003586 } else if (source.IsFpuRegister()) {
3587 if (destination.IsFpuRegister()) {
3588 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003589 } else {
3590 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003591 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
3592 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003593 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003594 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00003595 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
3596 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003597 } else if (destination.IsRegisterPair()) {
3598 DCHECK(ExpectedPairLayout(destination));
3599 __ LoadFromOffset(
3600 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
3601 } else {
3602 DCHECK(destination.IsFpuRegisterPair()) << destination;
3603 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
3604 SP,
3605 source.GetStackIndex());
3606 }
3607 } else if (source.IsRegisterPair()) {
3608 if (destination.IsRegisterPair()) {
3609 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
3610 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
3611 } else {
3612 DCHECK(destination.IsDoubleStackSlot()) << destination;
3613 DCHECK(ExpectedPairLayout(source));
3614 __ StoreToOffset(
3615 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
3616 }
3617 } else if (source.IsFpuRegisterPair()) {
3618 if (destination.IsFpuRegisterPair()) {
3619 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
3620 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
3621 } else {
3622 DCHECK(destination.IsDoubleStackSlot()) << destination;
3623 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
3624 SP,
3625 destination.GetStackIndex());
3626 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003627 } else {
3628 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003629 HConstant* constant = source.GetConstant();
3630 if (constant->IsIntConstant() || constant->IsNullConstant()) {
3631 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003632 if (destination.IsRegister()) {
3633 __ LoadImmediate(destination.AsRegister<Register>(), value);
3634 } else {
3635 DCHECK(destination.IsStackSlot());
3636 __ LoadImmediate(IP, value);
3637 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
3638 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003639 } else if (constant->IsLongConstant()) {
3640 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003641 if (destination.IsRegisterPair()) {
3642 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
3643 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003644 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003645 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003646 __ LoadImmediate(IP, Low32Bits(value));
3647 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
3648 __ LoadImmediate(IP, High32Bits(value));
3649 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
3650 }
3651 } else if (constant->IsDoubleConstant()) {
3652 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003653 if (destination.IsFpuRegisterPair()) {
3654 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003655 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003656 DCHECK(destination.IsDoubleStackSlot()) << destination;
3657 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003658 __ LoadImmediate(IP, Low32Bits(int_value));
3659 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
3660 __ LoadImmediate(IP, High32Bits(int_value));
3661 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
3662 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003663 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003664 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003665 float value = constant->AsFloatConstant()->GetValue();
3666 if (destination.IsFpuRegister()) {
3667 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
3668 } else {
3669 DCHECK(destination.IsStackSlot());
3670 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
3671 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
3672 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003673 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003674 }
3675}
3676
3677void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
3678 __ Mov(IP, reg);
3679 __ LoadFromOffset(kLoadWord, reg, SP, mem);
3680 __ StoreToOffset(kStoreWord, IP, SP, mem);
3681}
3682
3683void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
3684 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
3685 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
3686 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
3687 SP, mem1 + stack_offset);
3688 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
3689 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
3690 SP, mem2 + stack_offset);
3691 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
3692}
3693
3694void ParallelMoveResolverARM::EmitSwap(size_t index) {
3695 MoveOperands* move = moves_.Get(index);
3696 Location source = move->GetSource();
3697 Location destination = move->GetDestination();
3698
3699 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003700 DCHECK_NE(source.AsRegister<Register>(), IP);
3701 DCHECK_NE(destination.AsRegister<Register>(), IP);
3702 __ Mov(IP, source.AsRegister<Register>());
3703 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
3704 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003705 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003706 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003707 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003708 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003709 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
3710 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003711 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00003712 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003713 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00003714 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003715 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00003716 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003717 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003718 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00003719 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
3720 destination.AsRegisterPairHigh<Register>(),
3721 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003722 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003723 Register low_reg = source.IsRegisterPair()
3724 ? source.AsRegisterPairLow<Register>()
3725 : destination.AsRegisterPairLow<Register>();
3726 int mem = source.IsRegisterPair()
3727 ? destination.GetStackIndex()
3728 : source.GetStackIndex();
3729 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00003730 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003731 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00003732 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003733 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003734 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
3735 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00003736 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003737 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00003738 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003739 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
3740 DRegister reg = source.IsFpuRegisterPair()
3741 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
3742 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
3743 int mem = source.IsFpuRegisterPair()
3744 ? destination.GetStackIndex()
3745 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00003746 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003747 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00003748 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003749 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
3750 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
3751 : destination.AsFpuRegister<SRegister>();
3752 int mem = source.IsFpuRegister()
3753 ? destination.GetStackIndex()
3754 : source.GetStackIndex();
3755
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00003756 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003757 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00003758 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00003759 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00003760 Exchange(source.GetStackIndex(), destination.GetStackIndex());
3761 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003762 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00003763 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003764 }
3765}
3766
3767void ParallelMoveResolverARM::SpillScratch(int reg) {
3768 __ Push(static_cast<Register>(reg));
3769}
3770
3771void ParallelMoveResolverARM::RestoreScratch(int reg) {
3772 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003773}
3774
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003775void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003776 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
3777 ? LocationSummary::kCallOnSlowPath
3778 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003779 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003780 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003781 locations->SetOut(Location::RequiresRegister());
3782}
3783
3784void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003785 Register out = cls->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003786 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003787 DCHECK(!cls->CanCallRuntime());
3788 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003789 codegen_->LoadCurrentMethod(out);
3790 __ LoadFromOffset(kLoadWord, out, out, mirror::ArtMethod::DeclaringClassOffset().Int32Value());
3791 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003792 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003793 codegen_->LoadCurrentMethod(out);
3794 __ LoadFromOffset(
3795 kLoadWord, out, out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value());
3796 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003797
3798 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
3799 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3800 codegen_->AddSlowPath(slow_path);
3801 __ cmp(out, ShifterOperand(0));
3802 __ b(slow_path->GetEntryLabel(), EQ);
3803 if (cls->MustGenerateClinitCheck()) {
3804 GenerateClassInitializationCheck(slow_path, out);
3805 } else {
3806 __ Bind(slow_path->GetExitLabel());
3807 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003808 }
3809}
3810
3811void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
3812 LocationSummary* locations =
3813 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3814 locations->SetInAt(0, Location::RequiresRegister());
3815 if (check->HasUses()) {
3816 locations->SetOut(Location::SameAsFirstInput());
3817 }
3818}
3819
3820void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003821 // We assume the class is not null.
3822 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
3823 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003824 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00003825 GenerateClassInitializationCheck(slow_path,
3826 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003827}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003828
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003829void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
3830 SlowPathCodeARM* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003831 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
3832 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
3833 __ b(slow_path->GetEntryLabel(), LT);
3834 // Even if the initialized flag is set, we may be in a situation where caches are not synced
3835 // properly. Therefore, we do a memory fence.
3836 __ dmb(ISH);
3837 __ Bind(slow_path->GetExitLabel());
3838}
3839
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003840void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
3841 LocationSummary* locations =
3842 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3843 locations->SetOut(Location::RequiresRegister());
3844}
3845
3846void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) {
3847 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
3848 codegen_->AddSlowPath(slow_path);
3849
Roland Levillain271ab9c2014-11-27 15:23:57 +00003850 Register out = load->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003851 codegen_->LoadCurrentMethod(out);
Mathieu Chartiereace4582014-11-24 18:29:54 -08003852 __ LoadFromOffset(kLoadWord, out, out, mirror::ArtMethod::DeclaringClassOffset().Int32Value());
3853 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003854 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
3855 __ cmp(out, ShifterOperand(0));
3856 __ b(slow_path->GetEntryLabel(), EQ);
3857 __ Bind(slow_path->GetExitLabel());
3858}
3859
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003860void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
3861 LocationSummary* locations =
3862 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3863 locations->SetOut(Location::RequiresRegister());
3864}
3865
3866void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003867 Register out = load->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003868 int32_t offset = Thread::ExceptionOffset<kArmWordSize>().Int32Value();
3869 __ LoadFromOffset(kLoadWord, out, TR, offset);
3870 __ LoadImmediate(IP, 0);
3871 __ StoreToOffset(kStoreWord, IP, TR, offset);
3872}
3873
3874void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
3875 LocationSummary* locations =
3876 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3877 InvokeRuntimeCallingConvention calling_convention;
3878 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3879}
3880
3881void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
3882 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003883 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003884}
3885
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003886void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003887 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
3888 ? LocationSummary::kNoCall
3889 : LocationSummary::kCallOnSlowPath;
3890 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3891 locations->SetInAt(0, Location::RequiresRegister());
3892 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003893 // The out register is used as a temporary, so it overlaps with the inputs.
3894 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003895}
3896
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003897void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003898 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003899 Register obj = locations->InAt(0).AsRegister<Register>();
3900 Register cls = locations->InAt(1).AsRegister<Register>();
3901 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003902 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3903 Label done, zero;
3904 SlowPathCodeARM* slow_path = nullptr;
3905
3906 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003907 // avoid null check if we know obj is not null.
3908 if (instruction->MustDoNullCheck()) {
3909 __ cmp(obj, ShifterOperand(0));
3910 __ b(&zero, EQ);
3911 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003912 // Compare the class of `obj` with `cls`.
3913 __ LoadFromOffset(kLoadWord, out, obj, class_offset);
3914 __ cmp(out, ShifterOperand(cls));
3915 if (instruction->IsClassFinal()) {
3916 // Classes must be equal for the instanceof to succeed.
3917 __ b(&zero, NE);
3918 __ LoadImmediate(out, 1);
3919 __ b(&done);
3920 } else {
3921 // If the classes are not equal, we go into a slow path.
3922 DCHECK(locations->OnlyCallsOnSlowPath());
3923 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003924 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003925 codegen_->AddSlowPath(slow_path);
3926 __ b(slow_path->GetEntryLabel(), NE);
3927 __ LoadImmediate(out, 1);
3928 __ b(&done);
3929 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003930
3931 if (instruction->MustDoNullCheck() || instruction->IsClassFinal()) {
3932 __ Bind(&zero);
3933 __ LoadImmediate(out, 0);
3934 }
3935
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003936 if (slow_path != nullptr) {
3937 __ Bind(slow_path->GetExitLabel());
3938 }
3939 __ Bind(&done);
3940}
3941
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003942void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
3943 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3944 instruction, LocationSummary::kCallOnSlowPath);
3945 locations->SetInAt(0, Location::RequiresRegister());
3946 locations->SetInAt(1, Location::RequiresRegister());
3947 locations->AddTemp(Location::RequiresRegister());
3948}
3949
3950void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
3951 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003952 Register obj = locations->InAt(0).AsRegister<Register>();
3953 Register cls = locations->InAt(1).AsRegister<Register>();
3954 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003955 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3956
3957 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(
3958 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
3959 codegen_->AddSlowPath(slow_path);
3960
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003961 // avoid null check if we know obj is not null.
3962 if (instruction->MustDoNullCheck()) {
3963 __ cmp(obj, ShifterOperand(0));
3964 __ b(slow_path->GetExitLabel(), EQ);
3965 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003966 // Compare the class of `obj` with `cls`.
3967 __ LoadFromOffset(kLoadWord, temp, obj, class_offset);
3968 __ cmp(temp, ShifterOperand(cls));
3969 __ b(slow_path->GetEntryLabel(), NE);
3970 __ Bind(slow_path->GetExitLabel());
3971}
3972
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003973void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
3974 LocationSummary* locations =
3975 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3976 InvokeRuntimeCallingConvention calling_convention;
3977 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3978}
3979
3980void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
3981 codegen_->InvokeRuntime(instruction->IsEnter()
3982 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
3983 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003984 instruction->GetDexPc(),
3985 nullptr);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003986}
3987
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003988void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
3989void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
3990void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
3991
3992void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
3993 LocationSummary* locations =
3994 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3995 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
3996 || instruction->GetResultType() == Primitive::kPrimLong);
3997 locations->SetInAt(0, Location::RequiresRegister());
3998 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003999 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004000}
4001
4002void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
4003 HandleBitwiseOperation(instruction);
4004}
4005
4006void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
4007 HandleBitwiseOperation(instruction);
4008}
4009
4010void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
4011 HandleBitwiseOperation(instruction);
4012}
4013
4014void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
4015 LocationSummary* locations = instruction->GetLocations();
4016
4017 if (instruction->GetResultType() == Primitive::kPrimInt) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004018 Register first = locations->InAt(0).AsRegister<Register>();
4019 Register second = locations->InAt(1).AsRegister<Register>();
4020 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004021 if (instruction->IsAnd()) {
4022 __ and_(out, first, ShifterOperand(second));
4023 } else if (instruction->IsOr()) {
4024 __ orr(out, first, ShifterOperand(second));
4025 } else {
4026 DCHECK(instruction->IsXor());
4027 __ eor(out, first, ShifterOperand(second));
4028 }
4029 } else {
4030 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
4031 Location first = locations->InAt(0);
4032 Location second = locations->InAt(1);
4033 Location out = locations->Out();
4034 if (instruction->IsAnd()) {
4035 __ and_(out.AsRegisterPairLow<Register>(),
4036 first.AsRegisterPairLow<Register>(),
4037 ShifterOperand(second.AsRegisterPairLow<Register>()));
4038 __ and_(out.AsRegisterPairHigh<Register>(),
4039 first.AsRegisterPairHigh<Register>(),
4040 ShifterOperand(second.AsRegisterPairHigh<Register>()));
4041 } else if (instruction->IsOr()) {
4042 __ orr(out.AsRegisterPairLow<Register>(),
4043 first.AsRegisterPairLow<Register>(),
4044 ShifterOperand(second.AsRegisterPairLow<Register>()));
4045 __ orr(out.AsRegisterPairHigh<Register>(),
4046 first.AsRegisterPairHigh<Register>(),
4047 ShifterOperand(second.AsRegisterPairHigh<Register>()));
4048 } else {
4049 DCHECK(instruction->IsXor());
4050 __ eor(out.AsRegisterPairLow<Register>(),
4051 first.AsRegisterPairLow<Register>(),
4052 ShifterOperand(second.AsRegisterPairLow<Register>()));
4053 __ eor(out.AsRegisterPairHigh<Register>(),
4054 first.AsRegisterPairHigh<Register>(),
4055 ShifterOperand(second.AsRegisterPairHigh<Register>()));
4056 }
4057 }
4058}
4059
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08004060void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Register temp) {
4061 DCHECK_EQ(temp, kArtMethodRegister);
4062
4063 // TODO: Implement all kinds of calls:
4064 // 1) boot -> boot
4065 // 2) app -> boot
4066 // 3) app -> app
4067 //
4068 // Currently we implement the app -> app logic, which looks up in the resolve cache.
4069
4070 // temp = method;
4071 LoadCurrentMethod(temp);
4072 if (!invoke->IsRecursive()) {
4073 // temp = temp->dex_cache_resolved_methods_;
4074 __ LoadFromOffset(
4075 kLoadWord, temp, temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value());
4076 // temp = temp[index_in_cache]
4077 __ LoadFromOffset(
4078 kLoadWord, temp, temp, CodeGenerator::GetCacheOffset(invoke->GetDexMethodIndex()));
4079 // LR = temp[offset_of_quick_compiled_code]
4080 __ LoadFromOffset(kLoadWord, LR, temp,
4081 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
4082 kArmWordSize).Int32Value());
4083 // LR()
4084 __ blx(LR);
4085 } else {
4086 __ bl(GetFrameEntryLabel());
4087 }
4088
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08004089 DCHECK(!IsLeafMethod());
4090}
4091
Calin Juravleb1498f62015-02-16 13:13:29 +00004092void LocationsBuilderARM::VisitBoundType(HBoundType* instruction) {
4093 // Nothing to do, this should be removed during prepare for register allocator.
4094 UNUSED(instruction);
4095 LOG(FATAL) << "Unreachable";
4096}
4097
4098void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction) {
4099 // Nothing to do, this should be removed during prepare for register allocator.
4100 UNUSED(instruction);
4101 LOG(FATAL) << "Unreachable";
4102}
4103
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004104} // namespace arm
4105} // namespace art