blob: 2a79f82588af74b6090a6430e65f616c7817e7ec [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
Calin Juravled6fb6cf2014-11-11 19:07:44 +000044static constexpr Register kRuntimeParameterCoreRegisters[] = { R0, R1, R2, R3 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010045static constexpr size_t kRuntimeParameterCoreRegistersLength =
46 arraysize(kRuntimeParameterCoreRegisters);
Calin Juravled2ec87d2014-12-08 14:24:46 +000047static constexpr SRegister kRuntimeParameterFpuRegisters[] = { S0, S1, S2, S3 };
Roland Levillain624279f2014-12-04 11:54:28 +000048static constexpr size_t kRuntimeParameterFpuRegistersLength =
49 arraysize(kRuntimeParameterFpuRegisters);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +000050// We unconditionally allocate R5 to ensure we can do long operations
51// with baseline.
52static constexpr Register kCoreSavedRegisterForBaseline = R5;
53static constexpr Register kCoreCalleeSaves[] =
54 { R5, R6, R7, R8, R10, R11, PC };
55static constexpr SRegister kFpuCalleeSaves[] =
56 { S16, S17, S18, S19, S20, S21, S22, S23, S24, S25, S26, S27, S28, S29, S30, S31 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010057
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +000058// D31 cannot be split into two S registers, and the register allocator only works on
59// S registers. Therefore there is no need to block it.
60static constexpr DRegister DTMP = D31;
61
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +000062class InvokeRuntimeCallingConvention : public CallingConvention<Register, SRegister> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010063 public:
64 InvokeRuntimeCallingConvention()
65 : CallingConvention(kRuntimeParameterCoreRegisters,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010066 kRuntimeParameterCoreRegistersLength,
67 kRuntimeParameterFpuRegisters,
68 kRuntimeParameterFpuRegistersLength) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010069
70 private:
71 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
72};
73
Nicolas Geoffraye5038322014-07-04 09:41:32 +010074#define __ reinterpret_cast<ArmAssembler*>(codegen->GetAssembler())->
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010075#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmWordSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010076
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010077class NullCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010078 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010079 explicit NullCheckSlowPathARM(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010080
Alexandre Rames67555f72014-11-18 10:55:16 +000081 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010082 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010083 __ Bind(GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010084 arm_codegen->InvokeRuntime(
85 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010086 }
87
88 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010089 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010090 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM);
91};
92
Calin Juravled0d48522014-11-04 16:40:20 +000093class DivZeroCheckSlowPathARM : public SlowPathCodeARM {
94 public:
95 explicit DivZeroCheckSlowPathARM(HDivZeroCheck* instruction) : instruction_(instruction) {}
96
Alexandre Rames67555f72014-11-18 10:55:16 +000097 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000098 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
99 __ Bind(GetEntryLabel());
100 arm_codegen->InvokeRuntime(
101 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc());
102 }
103
104 private:
105 HDivZeroCheck* const instruction_;
106 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM);
107};
108
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100109class SuspendCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000110 public:
Alexandre Rames67555f72014-11-18 10:55:16 +0000111 SuspendCheckSlowPathARM(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100112 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000113
Alexandre Rames67555f72014-11-18 10:55:16 +0000114 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100115 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000116 __ Bind(GetEntryLabel());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100117 codegen->SaveLiveRegisters(instruction_->GetLocations());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100118 arm_codegen->InvokeRuntime(
119 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100120 codegen->RestoreLiveRegisters(instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100121 if (successor_ == nullptr) {
122 __ b(GetReturnLabel());
123 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100124 __ b(arm_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100125 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000126 }
127
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100128 Label* GetReturnLabel() {
129 DCHECK(successor_ == nullptr);
130 return &return_label_;
131 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000132
133 private:
134 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100135 // If not null, the block to branch to after the suspend check.
136 HBasicBlock* const successor_;
137
138 // If `successor_` is null, the label to branch to after the suspend check.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000139 Label return_label_;
140
141 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM);
142};
143
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100144class BoundsCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100145 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100146 BoundsCheckSlowPathARM(HBoundsCheck* instruction,
147 Location index_location,
148 Location length_location)
Nicolas Geoffray39468442014-09-02 15:17:15 +0100149 : instruction_(instruction),
150 index_location_(index_location),
151 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100152
Alexandre Rames67555f72014-11-18 10:55:16 +0000153 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100154 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100155 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000156 // We're moving two locations to locations that could overlap, so we need a parallel
157 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100158 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000159 codegen->EmitParallelMoves(
160 index_location_,
161 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
162 length_location_,
163 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100164 arm_codegen->InvokeRuntime(
165 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100166 }
167
168 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100169 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100170 const Location index_location_;
171 const Location length_location_;
172
173 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM);
174};
175
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000176class LoadClassSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100177 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000178 LoadClassSlowPathARM(HLoadClass* cls,
179 HInstruction* at,
180 uint32_t dex_pc,
181 bool do_clinit)
182 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
183 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
184 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100185
Alexandre Rames67555f72014-11-18 10:55:16 +0000186 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000187 LocationSummary* locations = at_->GetLocations();
188
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100189 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
190 __ Bind(GetEntryLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000191 codegen->SaveLiveRegisters(locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100192
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100193 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000194 __ LoadImmediate(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100195 arm_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000196 int32_t entry_point_offset = do_clinit_
197 ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
198 : QUICK_ENTRY_POINT(pInitializeType);
199 arm_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_);
200
201 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000202 Location out = locations->Out();
203 if (out.IsValid()) {
204 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000205 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
206 }
207 codegen->RestoreLiveRegisters(locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100208 __ b(GetExitLabel());
209 }
210
211 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000212 // The class this slow path will load.
213 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100214
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000215 // The instruction where this slow path is happening.
216 // (Might be the load class or an initialization check).
217 HInstruction* const at_;
218
219 // The dex PC of `at_`.
220 const uint32_t dex_pc_;
221
222 // Whether to initialize the class.
223 const bool do_clinit_;
224
225 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100226};
227
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000228class LoadStringSlowPathARM : public SlowPathCodeARM {
229 public:
230 explicit LoadStringSlowPathARM(HLoadString* instruction) : instruction_(instruction) {}
231
Alexandre Rames67555f72014-11-18 10:55:16 +0000232 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000233 LocationSummary* locations = instruction_->GetLocations();
234 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
235
236 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
237 __ Bind(GetEntryLabel());
238 codegen->SaveLiveRegisters(locations);
239
240 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800241 arm_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
242 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction_->GetStringIndex());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000243 arm_codegen->InvokeRuntime(
244 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc());
245 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
246
247 codegen->RestoreLiveRegisters(locations);
248 __ b(GetExitLabel());
249 }
250
251 private:
252 HLoadString* const instruction_;
253
254 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM);
255};
256
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000257class TypeCheckSlowPathARM : public SlowPathCodeARM {
258 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000259 TypeCheckSlowPathARM(HInstruction* instruction,
260 Location class_to_check,
261 Location object_class,
262 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000263 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000264 class_to_check_(class_to_check),
265 object_class_(object_class),
266 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000267
Alexandre Rames67555f72014-11-18 10:55:16 +0000268 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000269 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000270 DCHECK(instruction_->IsCheckCast()
271 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000272
273 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
274 __ Bind(GetEntryLabel());
275 codegen->SaveLiveRegisters(locations);
276
277 // We're moving two locations to locations that could overlap, so we need a parallel
278 // move resolver.
279 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000280 codegen->EmitParallelMoves(
281 class_to_check_,
282 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
283 object_class_,
284 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000285
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000286 if (instruction_->IsInstanceOf()) {
287 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial), instruction_, dex_pc_);
288 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
289 } else {
290 DCHECK(instruction_->IsCheckCast());
291 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc_);
292 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000293
294 codegen->RestoreLiveRegisters(locations);
295 __ b(GetExitLabel());
296 }
297
298 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000299 HInstruction* const instruction_;
300 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000301 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000302 uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000303
304 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM);
305};
306
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000307#undef __
308
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100309#undef __
310#define __ reinterpret_cast<ArmAssembler*>(GetAssembler())->
Dave Allison20dfc792014-06-16 20:44:29 -0700311
312inline Condition ARMCondition(IfCondition cond) {
313 switch (cond) {
314 case kCondEQ: return EQ;
315 case kCondNE: return NE;
316 case kCondLT: return LT;
317 case kCondLE: return LE;
318 case kCondGT: return GT;
319 case kCondGE: return GE;
320 default:
321 LOG(FATAL) << "Unknown if condition";
322 }
323 return EQ; // Unreachable.
324}
325
326inline Condition ARMOppositeCondition(IfCondition cond) {
327 switch (cond) {
328 case kCondEQ: return NE;
329 case kCondNE: return EQ;
330 case kCondLT: return GE;
331 case kCondLE: return GT;
332 case kCondGT: return LE;
333 case kCondGE: return LT;
334 default:
335 LOG(FATAL) << "Unknown if condition";
336 }
337 return EQ; // Unreachable.
338}
339
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100340void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
341 stream << ArmManagedRegister::FromCoreRegister(Register(reg));
342}
343
344void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000345 stream << ArmManagedRegister::FromSRegister(SRegister(reg));
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100346}
347
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100348size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
349 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
350 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100351}
352
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100353size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
354 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
355 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100356}
357
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000358size_t CodeGeneratorARM::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
359 __ StoreSToOffset(static_cast<SRegister>(reg_id), SP, stack_index);
360 return kArmWordSize;
361}
362
363size_t CodeGeneratorARM::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
364 __ LoadSFromOffset(static_cast<SRegister>(reg_id), SP, stack_index);
365 return kArmWordSize;
366}
367
Calin Juravle34166012014-12-19 17:22:29 +0000368CodeGeneratorARM::CodeGeneratorARM(HGraph* graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000369 const ArmInstructionSetFeatures& isa_features,
370 const CompilerOptions& compiler_options)
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000371 : CodeGenerator(graph,
372 kNumberOfCoreRegisters,
373 kNumberOfSRegisters,
374 kNumberOfRegisterPairs,
375 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
376 arraysize(kCoreCalleeSaves)),
377 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
378 arraysize(kFpuCalleeSaves)),
379 compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100380 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100381 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100382 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100383 move_resolver_(graph->GetArena(), this),
Calin Juravle34166012014-12-19 17:22:29 +0000384 assembler_(true),
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000385 isa_features_(isa_features) {
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000386 // Save the PC register to mimic Quick.
387 AddAllocatedRegister(Location::RegisterLocation(PC));
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100388}
389
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100390Location CodeGeneratorARM::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100391 switch (type) {
392 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100393 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100394 ArmManagedRegister pair =
395 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100396 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
397 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
398
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100399 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
400 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100401 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100402 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100403 }
404
405 case Primitive::kPrimByte:
406 case Primitive::kPrimBoolean:
407 case Primitive::kPrimChar:
408 case Primitive::kPrimShort:
409 case Primitive::kPrimInt:
410 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100411 int reg = FindFreeEntry(blocked_core_registers_, kNumberOfCoreRegisters);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100412 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100413 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
414 ArmManagedRegister current =
415 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
416 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100417 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100418 }
419 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100420 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100421 }
422
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000423 case Primitive::kPrimFloat: {
424 int reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfSRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100425 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100426 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100427
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000428 case Primitive::kPrimDouble: {
Nicolas Geoffray3c035032014-10-28 10:46:40 +0000429 int reg = FindTwoFreeConsecutiveAlignedEntries(blocked_fpu_registers_, kNumberOfSRegisters);
430 DCHECK_EQ(reg % 2, 0);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000431 return Location::FpuRegisterPairLocation(reg, reg + 1);
432 }
433
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100434 case Primitive::kPrimVoid:
435 LOG(FATAL) << "Unreachable type " << type;
436 }
437
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100438 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100439}
440
Nicolas Geoffraya0bb2bd2015-01-26 12:49:35 +0000441void CodeGeneratorARM::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100442 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100443 blocked_register_pairs_[R1_R2] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100444
445 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100446 blocked_core_registers_[SP] = true;
447 blocked_core_registers_[LR] = true;
448 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100449
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100450 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100451 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100452
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100453 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100454 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100455
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000456 if (is_baseline) {
457 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
458 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
459 }
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000460
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000461 blocked_core_registers_[kCoreSavedRegisterForBaseline] = false;
462
463 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
464 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
465 }
466 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100467
468 UpdateBlockedPairRegisters();
469}
470
471void CodeGeneratorARM::UpdateBlockedPairRegisters() const {
472 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
473 ArmManagedRegister current =
474 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
475 if (blocked_core_registers_[current.AsRegisterPairLow()]
476 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
477 blocked_register_pairs_[i] = true;
478 }
479 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100480}
481
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100482InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
483 : HGraphVisitor(graph),
484 assembler_(codegen->GetAssembler()),
485 codegen_(codegen) {}
486
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000487static uint32_t LeastSignificantBit(uint32_t mask) {
488 // ffs starts at 1.
489 return ffs(mask) - 1;
490}
491
492void CodeGeneratorARM::ComputeSpillMask() {
493 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000494 // Save one extra register for baseline. Note that on thumb2, there is no easy
495 // instruction to restore just the PC, so this actually helps both baseline
496 // and non-baseline to save and restore at least two registers at entry and exit.
497 core_spill_mask_ |= (1 << kCoreSavedRegisterForBaseline);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000498 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
499 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
500 // We use vpush and vpop for saving and restoring floating point registers, which take
501 // a SRegister and the number of registers to save/restore after that SRegister. We
502 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
503 // but in the range.
504 if (fpu_spill_mask_ != 0) {
505 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
506 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
507 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
508 fpu_spill_mask_ |= (1 << i);
509 }
510 }
511}
512
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000513void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +0000514 bool skip_overflow_check =
515 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000516 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000517 __ Bind(&frame_entry_label_);
518
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000519 if (HasEmptyFrame()) {
520 return;
521 }
522
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100523 if (!skip_overflow_check) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000524 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
525 __ LoadFromOffset(kLoadWord, IP, IP, 0);
526 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100527 }
528
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000529 // PC is in the list of callee-save to mimic Quick, but we need to push
530 // LR at entry instead.
531 __ PushList((core_spill_mask_ & (~(1 << PC))) | 1 << LR);
532 if (fpu_spill_mask_ != 0) {
533 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
534 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
535 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000536 __ AddConstant(SP, -(GetFrameSize() - FrameEntrySpillSize()));
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100537 __ StoreToOffset(kStoreWord, R0, SP, 0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000538}
539
540void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000541 if (HasEmptyFrame()) {
542 __ bx(LR);
543 return;
544 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000545 __ AddConstant(SP, GetFrameSize() - FrameEntrySpillSize());
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000546 if (fpu_spill_mask_ != 0) {
547 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
548 __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
549 }
550 __ PopList(core_spill_mask_);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000551}
552
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100553void CodeGeneratorARM::Bind(HBasicBlock* block) {
554 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000555}
556
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100557Location CodeGeneratorARM::GetStackLocation(HLoadLocal* load) const {
558 switch (load->GetType()) {
559 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100560 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100561 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
562 break;
563
564 case Primitive::kPrimInt:
565 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100566 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100567 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100568
569 case Primitive::kPrimBoolean:
570 case Primitive::kPrimByte:
571 case Primitive::kPrimChar:
572 case Primitive::kPrimShort:
573 case Primitive::kPrimVoid:
574 LOG(FATAL) << "Unexpected type " << load->GetType();
575 }
576
577 LOG(FATAL) << "Unreachable";
578 return Location();
579}
580
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100581Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
582 switch (type) {
583 case Primitive::kPrimBoolean:
584 case Primitive::kPrimByte:
585 case Primitive::kPrimChar:
586 case Primitive::kPrimShort:
587 case Primitive::kPrimInt:
588 case Primitive::kPrimNot: {
589 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000590 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100591 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100592 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100593 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000594 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100595 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100596 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100597
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000598 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100599 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000600 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100601 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000602 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100603 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000604 if (calling_convention.GetRegisterAt(index) == R1) {
605 // Skip R1, and use R2_R3 instead.
606 gp_index_++;
607 index++;
608 }
609 }
610 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
611 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +0000612 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000613 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +0000614 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100615 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000616 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
617 }
618 }
619
620 case Primitive::kPrimFloat: {
621 uint32_t stack_index = stack_index_++;
622 if (float_index_ % 2 == 0) {
623 float_index_ = std::max(double_index_, float_index_);
624 }
625 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
626 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
627 } else {
628 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
629 }
630 }
631
632 case Primitive::kPrimDouble: {
633 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
634 uint32_t stack_index = stack_index_;
635 stack_index_ += 2;
636 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
637 uint32_t index = double_index_;
638 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000639 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000640 calling_convention.GetFpuRegisterAt(index),
641 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000642 DCHECK(ExpectedPairLayout(result));
643 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000644 } else {
645 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100646 }
647 }
648
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100649 case Primitive::kPrimVoid:
650 LOG(FATAL) << "Unexpected parameter type " << type;
651 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100652 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100653 return Location();
654}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100655
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000656Location InvokeDexCallingConventionVisitor::GetReturnLocation(Primitive::Type type) {
657 switch (type) {
658 case Primitive::kPrimBoolean:
659 case Primitive::kPrimByte:
660 case Primitive::kPrimChar:
661 case Primitive::kPrimShort:
662 case Primitive::kPrimInt:
663 case Primitive::kPrimNot: {
664 return Location::RegisterLocation(R0);
665 }
666
667 case Primitive::kPrimFloat: {
668 return Location::FpuRegisterLocation(S0);
669 }
670
671 case Primitive::kPrimLong: {
672 return Location::RegisterPairLocation(R0, R1);
673 }
674
675 case Primitive::kPrimDouble: {
676 return Location::FpuRegisterPairLocation(S0, S1);
677 }
678
679 case Primitive::kPrimVoid:
680 return Location();
681 }
682 UNREACHABLE();
683 return Location();
684}
685
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100686void CodeGeneratorARM::Move32(Location destination, Location source) {
687 if (source.Equals(destination)) {
688 return;
689 }
690 if (destination.IsRegister()) {
691 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000692 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100693 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000694 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100695 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000696 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100697 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100698 } else if (destination.IsFpuRegister()) {
699 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000700 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100701 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000702 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100703 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000704 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100705 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100706 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +0000707 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100708 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000709 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100710 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000711 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100712 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +0000713 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100714 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
715 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100716 }
717 }
718}
719
720void CodeGeneratorARM::Move64(Location destination, Location source) {
721 if (source.Equals(destination)) {
722 return;
723 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100724 if (destination.IsRegisterPair()) {
725 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000726 EmitParallelMoves(
727 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
728 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
729 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
730 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100731 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000732 UNIMPLEMENTED(FATAL);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100733 } else {
734 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000735 DCHECK(ExpectedPairLayout(destination));
736 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
737 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100738 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000739 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100740 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000741 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
742 SP,
743 source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100744 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000745 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100746 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100747 } else {
748 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100749 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000750 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100751 if (source.AsRegisterPairLow<Register>() == R1) {
752 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100753 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
754 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100755 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100756 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100757 SP, destination.GetStackIndex());
758 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000759 } else if (source.IsFpuRegisterPair()) {
760 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
761 SP,
762 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100763 } else {
764 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000765 EmitParallelMoves(
766 Location::StackSlot(source.GetStackIndex()),
767 Location::StackSlot(destination.GetStackIndex()),
768 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
769 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100770 }
771 }
772}
773
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100774void CodeGeneratorARM::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100775 LocationSummary* locations = instruction->GetLocations();
776 if (locations != nullptr && locations->Out().Equals(location)) {
777 return;
778 }
779
Calin Juravlea21f5982014-11-13 15:53:04 +0000780 if (locations != nullptr && locations->Out().IsConstant()) {
781 HConstant* const_to_move = locations->Out().GetConstant();
782 if (const_to_move->IsIntConstant()) {
783 int32_t value = const_to_move->AsIntConstant()->GetValue();
784 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000785 __ LoadImmediate(location.AsRegister<Register>(), value);
Calin Juravlea21f5982014-11-13 15:53:04 +0000786 } else {
787 DCHECK(location.IsStackSlot());
788 __ LoadImmediate(IP, value);
789 __ StoreToOffset(kStoreWord, IP, SP, location.GetStackIndex());
790 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000791 } else {
Nicolas Geoffray3747b482015-01-19 17:17:16 +0000792 DCHECK(const_to_move->IsLongConstant()) << const_to_move->DebugName();
Calin Juravlea21f5982014-11-13 15:53:04 +0000793 int64_t value = const_to_move->AsLongConstant()->GetValue();
794 if (location.IsRegisterPair()) {
795 __ LoadImmediate(location.AsRegisterPairLow<Register>(), Low32Bits(value));
796 __ LoadImmediate(location.AsRegisterPairHigh<Register>(), High32Bits(value));
797 } else {
798 DCHECK(location.IsDoubleStackSlot());
799 __ LoadImmediate(IP, Low32Bits(value));
800 __ StoreToOffset(kStoreWord, IP, SP, location.GetStackIndex());
801 __ LoadImmediate(IP, High32Bits(value));
802 __ StoreToOffset(kStoreWord, IP, SP, location.GetHighStackIndex(kArmWordSize));
803 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100804 }
Roland Levillain476df552014-10-09 17:51:36 +0100805 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100806 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
807 switch (instruction->GetType()) {
808 case Primitive::kPrimBoolean:
809 case Primitive::kPrimByte:
810 case Primitive::kPrimChar:
811 case Primitive::kPrimShort:
812 case Primitive::kPrimInt:
813 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100814 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100815 Move32(location, Location::StackSlot(stack_slot));
816 break;
817
818 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100819 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100820 Move64(location, Location::DoubleStackSlot(stack_slot));
821 break;
822
823 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100824 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100825 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000826 } else if (instruction->IsTemporary()) {
827 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000828 if (temp_location.IsStackSlot()) {
829 Move32(location, temp_location);
830 } else {
831 DCHECK(temp_location.IsDoubleStackSlot());
832 Move64(location, temp_location);
833 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000834 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100835 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100836 switch (instruction->GetType()) {
837 case Primitive::kPrimBoolean:
838 case Primitive::kPrimByte:
839 case Primitive::kPrimChar:
840 case Primitive::kPrimShort:
841 case Primitive::kPrimNot:
842 case Primitive::kPrimInt:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100843 case Primitive::kPrimFloat:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100844 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100845 break;
846
847 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100848 case Primitive::kPrimDouble:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100849 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100850 break;
851
852 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100853 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100854 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000855 }
856}
857
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100858void CodeGeneratorARM::InvokeRuntime(int32_t entry_point_offset,
859 HInstruction* instruction,
860 uint32_t dex_pc) {
861 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
862 __ blx(LR);
863 RecordPcInfo(instruction, dex_pc);
864 DCHECK(instruction->IsSuspendCheck()
865 || instruction->IsBoundsCheck()
866 || instruction->IsNullCheck()
Calin Juravled0d48522014-11-04 16:40:20 +0000867 || instruction->IsDivZeroCheck()
Roland Levillain624279f2014-12-04 11:54:28 +0000868 || instruction->GetLocations()->CanCall()
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100869 || !IsLeafMethod());
870}
871
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000872void LocationsBuilderARM::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000873 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000874}
875
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000876void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000877 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100878 DCHECK(!successor->IsExitBlock());
879
880 HBasicBlock* block = got->GetBlock();
881 HInstruction* previous = got->GetPrevious();
882
883 HLoopInformation* info = block->GetLoopInformation();
884 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
885 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
886 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
887 return;
888 }
889
890 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
891 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
892 }
893 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000894 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000895 }
896}
897
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000898void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000899 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000900}
901
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000902void InstructionCodeGeneratorARM::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700903 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000904 if (kIsDebugBuild) {
905 __ Comment("Unreachable");
906 __ bkpt(0);
907 }
908}
909
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000910void LocationsBuilderARM::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100911 LocationSummary* locations =
912 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100913 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100914 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100915 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100916 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000917}
918
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000919void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700920 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100921 if (cond->IsIntConstant()) {
922 // Constant condition, statically compared against 1.
923 int32_t cond_value = cond->AsIntConstant()->GetValue();
924 if (cond_value == 1) {
925 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
926 if_instr->IfTrueSuccessor())) {
927 __ b(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100928 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100929 return;
930 } else {
931 DCHECK_EQ(cond_value, 0);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100932 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100933 } else {
934 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
935 // Condition has been materialized, compare the output to 0
936 DCHECK(if_instr->GetLocations()->InAt(0).IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000937 __ cmp(if_instr->GetLocations()->InAt(0).AsRegister<Register>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100938 ShifterOperand(0));
939 __ b(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()), NE);
940 } else {
941 // Condition has not been materialized, use its inputs as the
942 // comparison and its condition as the branch condition.
943 LocationSummary* locations = cond->GetLocations();
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000944 DCHECK(locations->InAt(0).IsRegister()) << locations->InAt(0);
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +0000945 Register left = locations->InAt(0).AsRegister<Register>();
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100946 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +0000947 __ cmp(left, ShifterOperand(locations->InAt(1).AsRegister<Register>()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100948 } else {
949 DCHECK(locations->InAt(1).IsConstant());
950 int32_t value =
951 locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
952 ShifterOperand operand;
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +0000953 if (GetAssembler()->ShifterOperandCanHold(R0, left, CMP, value, &operand)) {
954 __ cmp(left, operand);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100955 } else {
956 Register temp = IP;
957 __ LoadImmediate(temp, value);
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +0000958 __ cmp(left, ShifterOperand(temp));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100959 }
960 }
961 __ b(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()),
962 ARMCondition(cond->AsCondition()->GetCondition()));
963 }
Dave Allison20dfc792014-06-16 20:44:29 -0700964 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100965 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
966 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700967 __ b(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000968 }
969}
970
Dave Allison20dfc792014-06-16 20:44:29 -0700971
972void LocationsBuilderARM::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100973 LocationSummary* locations =
974 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100975 locations->SetInAt(0, Location::RequiresRegister());
976 locations->SetInAt(1, Location::RegisterOrConstant(comp->InputAt(1)));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100977 if (comp->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100978 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100979 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000980}
981
Dave Allison20dfc792014-06-16 20:44:29 -0700982void InstructionCodeGeneratorARM::VisitCondition(HCondition* comp) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100983 if (!comp->NeedsMaterialization()) return;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100984 LocationSummary* locations = comp->GetLocations();
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +0000985 Register left = locations->InAt(0).AsRegister<Register>();
986
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100987 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +0000988 __ cmp(left, ShifterOperand(locations->InAt(1).AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100989 } else {
990 DCHECK(locations->InAt(1).IsConstant());
991 int32_t value = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
992 ShifterOperand operand;
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +0000993 if (GetAssembler()->ShifterOperandCanHold(R0, left, CMP, value, &operand)) {
994 __ cmp(left, operand);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100995 } else {
996 Register temp = IP;
997 __ LoadImmediate(temp, value);
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +0000998 __ cmp(left, ShifterOperand(temp));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100999 }
Dave Allison20dfc792014-06-16 20:44:29 -07001000 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001001 __ it(ARMCondition(comp->GetCondition()), kItElse);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001002 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(1),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001003 ARMCondition(comp->GetCondition()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001004 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(0),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001005 ARMOppositeCondition(comp->GetCondition()));
Dave Allison20dfc792014-06-16 20:44:29 -07001006}
1007
1008void LocationsBuilderARM::VisitEqual(HEqual* comp) {
1009 VisitCondition(comp);
1010}
1011
1012void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
1013 VisitCondition(comp);
1014}
1015
1016void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
1017 VisitCondition(comp);
1018}
1019
1020void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
1021 VisitCondition(comp);
1022}
1023
1024void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
1025 VisitCondition(comp);
1026}
1027
1028void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
1029 VisitCondition(comp);
1030}
1031
1032void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1033 VisitCondition(comp);
1034}
1035
1036void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1037 VisitCondition(comp);
1038}
1039
1040void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
1041 VisitCondition(comp);
1042}
1043
1044void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
1045 VisitCondition(comp);
1046}
1047
1048void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1049 VisitCondition(comp);
1050}
1051
1052void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1053 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001054}
1055
1056void LocationsBuilderARM::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001057 local->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001058}
1059
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001060void InstructionCodeGeneratorARM::VisitLocal(HLocal* local) {
1061 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001062}
1063
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001064void LocationsBuilderARM::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001065 load->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001066}
1067
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001068void InstructionCodeGeneratorARM::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001069 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001070 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001071}
1072
1073void LocationsBuilderARM::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001074 LocationSummary* locations =
1075 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001076 switch (store->InputAt(1)->GetType()) {
1077 case Primitive::kPrimBoolean:
1078 case Primitive::kPrimByte:
1079 case Primitive::kPrimChar:
1080 case Primitive::kPrimShort:
1081 case Primitive::kPrimInt:
1082 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001083 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001084 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1085 break;
1086
1087 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001088 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001089 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1090 break;
1091
1092 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001093 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001094 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001095}
1096
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001097void InstructionCodeGeneratorARM::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001098 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001099}
1100
1101void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001102 LocationSummary* locations =
1103 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001104 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001105}
1106
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001107void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001108 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001109 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001110}
1111
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001112void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001113 LocationSummary* locations =
1114 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001115 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001116}
1117
1118void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant) {
1119 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001120 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001121}
1122
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001123void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
1124 LocationSummary* locations =
1125 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1126 locations->SetOut(Location::ConstantLocation(constant));
1127}
1128
1129void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant) {
1130 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001131 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001132}
1133
1134void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
1135 LocationSummary* locations =
1136 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1137 locations->SetOut(Location::ConstantLocation(constant));
1138}
1139
1140void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant) {
1141 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001142 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001143}
1144
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001145void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001146 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001147}
1148
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001149void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001150 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001151 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001152}
1153
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001154void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001155 LocationSummary* locations =
1156 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001157 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001158}
1159
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001160void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001161 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001162 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001163}
1164
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001165void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001166 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
1167 codegen_->GetInstructionSetFeatures());
1168 if (intrinsic.TryDispatch(invoke)) {
1169 return;
1170 }
1171
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001172 HandleInvoke(invoke);
1173}
1174
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001175void CodeGeneratorARM::LoadCurrentMethod(Register reg) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001176 DCHECK(RequiresCurrentMethod());
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001177 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001178}
1179
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001180static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
1181 if (invoke->GetLocations()->Intrinsified()) {
1182 IntrinsicCodeGeneratorARM intrinsic(codegen);
1183 intrinsic.Dispatch(invoke);
1184 return true;
1185 }
1186 return false;
1187}
1188
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001189void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001190 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1191 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001192 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001193
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001194 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
1195
1196 codegen_->GenerateStaticOrDirectCall(invoke, temp);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001197}
1198
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001199void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001200 LocationSummary* locations =
1201 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001202 locations->AddTemp(Location::RegisterLocation(R0));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001203
1204 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001205 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001206 HInstruction* input = invoke->InputAt(i);
1207 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1208 }
1209
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001210 locations->SetOut(calling_convention_visitor.GetReturnLocation(invoke->GetType()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001211}
1212
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001213void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001214 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
1215 codegen_->GetInstructionSetFeatures());
1216 if (intrinsic.TryDispatch(invoke)) {
1217 return;
1218 }
1219
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001220 HandleInvoke(invoke);
1221}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001222
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001223void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001224 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1225 return;
1226 }
1227
Roland Levillain271ab9c2014-11-27 15:23:57 +00001228 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001229 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
1230 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1231 LocationSummary* locations = invoke->GetLocations();
1232 Location receiver = locations->InAt(0);
1233 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1234 // temp = object->GetClass();
1235 if (receiver.IsStackSlot()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001236 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
1237 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001238 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001239 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001240 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001241 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001242 // temp = temp->GetMethodAt(method_offset);
Mathieu Chartier2d721012014-11-10 11:08:06 -08001243 uint32_t entry_point = mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001244 kArmWordSize).Int32Value();
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001245 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001246 // LR = temp->GetEntryPoint();
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001247 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001248 // LR();
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001249 __ blx(LR);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001250 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001251 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001252}
1253
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001254void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1255 HandleInvoke(invoke);
1256 // Add the hidden argument.
1257 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
1258}
1259
1260void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1261 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001262 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001263 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1264 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1265 LocationSummary* locations = invoke->GetLocations();
1266 Location receiver = locations->InAt(0);
1267 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1268
1269 // Set the hidden argument.
Roland Levillain199f3362014-11-27 17:15:16 +00001270 __ LoadImmediate(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
1271 invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001272
1273 // temp = object->GetClass();
1274 if (receiver.IsStackSlot()) {
1275 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
1276 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
1277 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001278 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001279 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001280 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001281 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartier2d721012014-11-10 11:08:06 -08001282 uint32_t entry_point = mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001283 kArmWordSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001284 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
1285 // LR = temp->GetEntryPoint();
1286 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
1287 // LR();
1288 __ blx(LR);
1289 DCHECK(!codegen_->IsLeafMethod());
1290 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1291}
1292
Roland Levillain88cb1752014-10-20 16:36:47 +01001293void LocationsBuilderARM::VisitNeg(HNeg* neg) {
1294 LocationSummary* locations =
1295 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1296 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001297 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01001298 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001299 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1300 break;
1301 }
1302 case Primitive::kPrimLong: {
1303 locations->SetInAt(0, Location::RequiresRegister());
1304 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01001305 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001306 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001307
Roland Levillain88cb1752014-10-20 16:36:47 +01001308 case Primitive::kPrimFloat:
1309 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001310 locations->SetInAt(0, Location::RequiresFpuRegister());
1311 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01001312 break;
1313
1314 default:
1315 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1316 }
1317}
1318
1319void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
1320 LocationSummary* locations = neg->GetLocations();
1321 Location out = locations->Out();
1322 Location in = locations->InAt(0);
1323 switch (neg->GetResultType()) {
1324 case Primitive::kPrimInt:
1325 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001326 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01001327 break;
1328
1329 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001330 DCHECK(in.IsRegisterPair());
1331 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
1332 __ rsbs(out.AsRegisterPairLow<Register>(),
1333 in.AsRegisterPairLow<Register>(),
1334 ShifterOperand(0));
1335 // We cannot emit an RSC (Reverse Subtract with Carry)
1336 // instruction here, as it does not exist in the Thumb-2
1337 // instruction set. We use the following approach
1338 // using SBC and SUB instead.
1339 //
1340 // out.hi = -C
1341 __ sbc(out.AsRegisterPairHigh<Register>(),
1342 out.AsRegisterPairHigh<Register>(),
1343 ShifterOperand(out.AsRegisterPairHigh<Register>()));
1344 // out.hi = out.hi - in.hi
1345 __ sub(out.AsRegisterPairHigh<Register>(),
1346 out.AsRegisterPairHigh<Register>(),
1347 ShifterOperand(in.AsRegisterPairHigh<Register>()));
1348 break;
1349
Roland Levillain88cb1752014-10-20 16:36:47 +01001350 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001351 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001352 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001353 break;
1354
Roland Levillain88cb1752014-10-20 16:36:47 +01001355 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001356 DCHECK(in.IsFpuRegisterPair());
1357 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
1358 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01001359 break;
1360
1361 default:
1362 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1363 }
1364}
1365
Roland Levillaindff1f282014-11-05 14:15:05 +00001366void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001367 Primitive::Type result_type = conversion->GetResultType();
1368 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001369 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001370
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001371 // The float-to-long and double-to-long type conversions rely on a
1372 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001373 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001374 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1375 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001376 ? LocationSummary::kCall
1377 : LocationSummary::kNoCall;
1378 LocationSummary* locations =
1379 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1380
Roland Levillaindff1f282014-11-05 14:15:05 +00001381 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001382 case Primitive::kPrimByte:
1383 switch (input_type) {
1384 case Primitive::kPrimShort:
1385 case Primitive::kPrimInt:
1386 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001387 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001388 locations->SetInAt(0, Location::RequiresRegister());
1389 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1390 break;
1391
1392 default:
1393 LOG(FATAL) << "Unexpected type conversion from " << input_type
1394 << " to " << result_type;
1395 }
1396 break;
1397
Roland Levillain01a8d712014-11-14 16:27:39 +00001398 case Primitive::kPrimShort:
1399 switch (input_type) {
1400 case Primitive::kPrimByte:
1401 case Primitive::kPrimInt:
1402 case Primitive::kPrimChar:
1403 // Processing a Dex `int-to-short' instruction.
1404 locations->SetInAt(0, Location::RequiresRegister());
1405 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1406 break;
1407
1408 default:
1409 LOG(FATAL) << "Unexpected type conversion from " << input_type
1410 << " to " << result_type;
1411 }
1412 break;
1413
Roland Levillain946e1432014-11-11 17:35:19 +00001414 case Primitive::kPrimInt:
1415 switch (input_type) {
1416 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001417 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001418 locations->SetInAt(0, Location::Any());
1419 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1420 break;
1421
1422 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001423 // Processing a Dex `float-to-int' instruction.
1424 locations->SetInAt(0, Location::RequiresFpuRegister());
1425 locations->SetOut(Location::RequiresRegister());
1426 locations->AddTemp(Location::RequiresFpuRegister());
1427 break;
1428
Roland Levillain946e1432014-11-11 17:35:19 +00001429 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001430 // Processing a Dex `double-to-int' instruction.
1431 locations->SetInAt(0, Location::RequiresFpuRegister());
1432 locations->SetOut(Location::RequiresRegister());
1433 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001434 break;
1435
1436 default:
1437 LOG(FATAL) << "Unexpected type conversion from " << input_type
1438 << " to " << result_type;
1439 }
1440 break;
1441
Roland Levillaindff1f282014-11-05 14:15:05 +00001442 case Primitive::kPrimLong:
1443 switch (input_type) {
1444 case Primitive::kPrimByte:
1445 case Primitive::kPrimShort:
1446 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001447 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001448 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001449 locations->SetInAt(0, Location::RequiresRegister());
1450 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1451 break;
1452
Roland Levillain624279f2014-12-04 11:54:28 +00001453 case Primitive::kPrimFloat: {
1454 // Processing a Dex `float-to-long' instruction.
1455 InvokeRuntimeCallingConvention calling_convention;
1456 locations->SetInAt(0, Location::FpuRegisterLocation(
1457 calling_convention.GetFpuRegisterAt(0)));
1458 locations->SetOut(Location::RegisterPairLocation(R0, R1));
1459 break;
1460 }
1461
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001462 case Primitive::kPrimDouble: {
1463 // Processing a Dex `double-to-long' instruction.
1464 InvokeRuntimeCallingConvention calling_convention;
1465 locations->SetInAt(0, Location::FpuRegisterPairLocation(
1466 calling_convention.GetFpuRegisterAt(0),
1467 calling_convention.GetFpuRegisterAt(1)));
1468 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00001469 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001470 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001471
1472 default:
1473 LOG(FATAL) << "Unexpected type conversion from " << input_type
1474 << " to " << result_type;
1475 }
1476 break;
1477
Roland Levillain981e4542014-11-14 11:47:14 +00001478 case Primitive::kPrimChar:
1479 switch (input_type) {
1480 case Primitive::kPrimByte:
1481 case Primitive::kPrimShort:
1482 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001483 // Processing a Dex `int-to-char' instruction.
1484 locations->SetInAt(0, Location::RequiresRegister());
1485 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1486 break;
1487
1488 default:
1489 LOG(FATAL) << "Unexpected type conversion from " << input_type
1490 << " to " << result_type;
1491 }
1492 break;
1493
Roland Levillaindff1f282014-11-05 14:15:05 +00001494 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001495 switch (input_type) {
1496 case Primitive::kPrimByte:
1497 case Primitive::kPrimShort:
1498 case Primitive::kPrimInt:
1499 case Primitive::kPrimChar:
1500 // Processing a Dex `int-to-float' instruction.
1501 locations->SetInAt(0, Location::RequiresRegister());
1502 locations->SetOut(Location::RequiresFpuRegister());
1503 break;
1504
1505 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001506 // Processing a Dex `long-to-float' instruction.
1507 locations->SetInAt(0, Location::RequiresRegister());
1508 locations->SetOut(Location::RequiresFpuRegister());
1509 locations->AddTemp(Location::RequiresRegister());
1510 locations->AddTemp(Location::RequiresRegister());
1511 locations->AddTemp(Location::RequiresFpuRegister());
1512 locations->AddTemp(Location::RequiresFpuRegister());
1513 break;
1514
Roland Levillaincff13742014-11-17 14:32:17 +00001515 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001516 // Processing a Dex `double-to-float' instruction.
1517 locations->SetInAt(0, Location::RequiresFpuRegister());
1518 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001519 break;
1520
1521 default:
1522 LOG(FATAL) << "Unexpected type conversion from " << input_type
1523 << " to " << result_type;
1524 };
1525 break;
1526
Roland Levillaindff1f282014-11-05 14:15:05 +00001527 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001528 switch (input_type) {
1529 case Primitive::kPrimByte:
1530 case Primitive::kPrimShort:
1531 case Primitive::kPrimInt:
1532 case Primitive::kPrimChar:
1533 // Processing a Dex `int-to-double' instruction.
1534 locations->SetInAt(0, Location::RequiresRegister());
1535 locations->SetOut(Location::RequiresFpuRegister());
1536 break;
1537
1538 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001539 // Processing a Dex `long-to-double' instruction.
1540 locations->SetInAt(0, Location::RequiresRegister());
1541 locations->SetOut(Location::RequiresFpuRegister());
1542 locations->AddTemp(Location::RequiresRegister());
1543 locations->AddTemp(Location::RequiresRegister());
1544 locations->AddTemp(Location::RequiresFpuRegister());
1545 break;
1546
Roland Levillaincff13742014-11-17 14:32:17 +00001547 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001548 // Processing a Dex `float-to-double' instruction.
1549 locations->SetInAt(0, Location::RequiresFpuRegister());
1550 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001551 break;
1552
1553 default:
1554 LOG(FATAL) << "Unexpected type conversion from " << input_type
1555 << " to " << result_type;
1556 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001557 break;
1558
1559 default:
1560 LOG(FATAL) << "Unexpected type conversion from " << input_type
1561 << " to " << result_type;
1562 }
1563}
1564
1565void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
1566 LocationSummary* locations = conversion->GetLocations();
1567 Location out = locations->Out();
1568 Location in = locations->InAt(0);
1569 Primitive::Type result_type = conversion->GetResultType();
1570 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001571 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001572 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001573 case Primitive::kPrimByte:
1574 switch (input_type) {
1575 case Primitive::kPrimShort:
1576 case Primitive::kPrimInt:
1577 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001578 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001579 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001580 break;
1581
1582 default:
1583 LOG(FATAL) << "Unexpected type conversion from " << input_type
1584 << " to " << result_type;
1585 }
1586 break;
1587
Roland Levillain01a8d712014-11-14 16:27:39 +00001588 case Primitive::kPrimShort:
1589 switch (input_type) {
1590 case Primitive::kPrimByte:
1591 case Primitive::kPrimInt:
1592 case Primitive::kPrimChar:
1593 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001594 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00001595 break;
1596
1597 default:
1598 LOG(FATAL) << "Unexpected type conversion from " << input_type
1599 << " to " << result_type;
1600 }
1601 break;
1602
Roland Levillain946e1432014-11-11 17:35:19 +00001603 case Primitive::kPrimInt:
1604 switch (input_type) {
1605 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001606 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001607 DCHECK(out.IsRegister());
1608 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001609 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001610 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001611 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00001612 } else {
1613 DCHECK(in.IsConstant());
1614 DCHECK(in.GetConstant()->IsLongConstant());
1615 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001616 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00001617 }
1618 break;
1619
Roland Levillain3f8f9362014-12-02 17:45:01 +00001620 case Primitive::kPrimFloat: {
1621 // Processing a Dex `float-to-int' instruction.
1622 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
1623 __ vmovs(temp, in.AsFpuRegister<SRegister>());
1624 __ vcvtis(temp, temp);
1625 __ vmovrs(out.AsRegister<Register>(), temp);
1626 break;
1627 }
1628
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001629 case Primitive::kPrimDouble: {
1630 // Processing a Dex `double-to-int' instruction.
1631 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
1632 DRegister temp_d = FromLowSToD(temp_s);
1633 __ vmovd(temp_d, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
1634 __ vcvtid(temp_s, temp_d);
1635 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00001636 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001637 }
Roland Levillain946e1432014-11-11 17:35:19 +00001638
1639 default:
1640 LOG(FATAL) << "Unexpected type conversion from " << input_type
1641 << " to " << result_type;
1642 }
1643 break;
1644
Roland Levillaindff1f282014-11-05 14:15:05 +00001645 case Primitive::kPrimLong:
1646 switch (input_type) {
1647 case Primitive::kPrimByte:
1648 case Primitive::kPrimShort:
1649 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001650 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001651 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001652 DCHECK(out.IsRegisterPair());
1653 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001654 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00001655 // Sign extension.
1656 __ Asr(out.AsRegisterPairHigh<Register>(),
1657 out.AsRegisterPairLow<Register>(),
1658 31);
1659 break;
1660
1661 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001662 // Processing a Dex `float-to-long' instruction.
Roland Levillain624279f2014-12-04 11:54:28 +00001663 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
1664 conversion,
1665 conversion->GetDexPc());
1666 break;
1667
Roland Levillaindff1f282014-11-05 14:15:05 +00001668 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001669 // Processing a Dex `double-to-long' instruction.
1670 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
1671 conversion,
1672 conversion->GetDexPc());
Roland Levillaindff1f282014-11-05 14:15:05 +00001673 break;
1674
1675 default:
1676 LOG(FATAL) << "Unexpected type conversion from " << input_type
1677 << " to " << result_type;
1678 }
1679 break;
1680
Roland Levillain981e4542014-11-14 11:47:14 +00001681 case Primitive::kPrimChar:
1682 switch (input_type) {
1683 case Primitive::kPrimByte:
1684 case Primitive::kPrimShort:
1685 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001686 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001687 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00001688 break;
1689
1690 default:
1691 LOG(FATAL) << "Unexpected type conversion from " << input_type
1692 << " to " << result_type;
1693 }
1694 break;
1695
Roland Levillaindff1f282014-11-05 14:15:05 +00001696 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001697 switch (input_type) {
1698 case Primitive::kPrimByte:
1699 case Primitive::kPrimShort:
1700 case Primitive::kPrimInt:
1701 case Primitive::kPrimChar: {
1702 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001703 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
1704 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001705 break;
1706 }
1707
Roland Levillain6d0e4832014-11-27 18:31:21 +00001708 case Primitive::kPrimLong: {
1709 // Processing a Dex `long-to-float' instruction.
1710 Register low = in.AsRegisterPairLow<Register>();
1711 Register high = in.AsRegisterPairHigh<Register>();
1712 SRegister output = out.AsFpuRegister<SRegister>();
1713 Register constant_low = locations->GetTemp(0).AsRegister<Register>();
1714 Register constant_high = locations->GetTemp(1).AsRegister<Register>();
1715 SRegister temp1_s = locations->GetTemp(2).AsFpuRegisterPairLow<SRegister>();
1716 DRegister temp1_d = FromLowSToD(temp1_s);
1717 SRegister temp2_s = locations->GetTemp(3).AsFpuRegisterPairLow<SRegister>();
1718 DRegister temp2_d = FromLowSToD(temp2_s);
1719
1720 // Operations use doubles for precision reasons (each 32-bit
1721 // half of a long fits in the 53-bit mantissa of a double,
1722 // but not in the 24-bit mantissa of a float). This is
1723 // especially important for the low bits. The result is
1724 // eventually converted to float.
1725
1726 // temp1_d = int-to-double(high)
1727 __ vmovsr(temp1_s, high);
1728 __ vcvtdi(temp1_d, temp1_s);
1729 // Using vmovd to load the `k2Pow32EncodingForDouble` constant
1730 // as an immediate value into `temp2_d` does not work, as
1731 // this instruction only transfers 8 significant bits of its
1732 // immediate operand. Instead, use two 32-bit core
1733 // registers to load `k2Pow32EncodingForDouble` into
1734 // `temp2_d`.
1735 __ LoadImmediate(constant_low, Low32Bits(k2Pow32EncodingForDouble));
1736 __ LoadImmediate(constant_high, High32Bits(k2Pow32EncodingForDouble));
1737 __ vmovdrr(temp2_d, constant_low, constant_high);
1738 // temp1_d = temp1_d * 2^32
1739 __ vmuld(temp1_d, temp1_d, temp2_d);
1740 // temp2_d = unsigned-to-double(low)
1741 __ vmovsr(temp2_s, low);
1742 __ vcvtdu(temp2_d, temp2_s);
1743 // temp1_d = temp1_d + temp2_d
1744 __ vaddd(temp1_d, temp1_d, temp2_d);
1745 // output = double-to-float(temp1_d);
1746 __ vcvtsd(output, temp1_d);
1747 break;
1748 }
1749
Roland Levillaincff13742014-11-17 14:32:17 +00001750 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001751 // Processing a Dex `double-to-float' instruction.
1752 __ vcvtsd(out.AsFpuRegister<SRegister>(),
1753 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00001754 break;
1755
1756 default:
1757 LOG(FATAL) << "Unexpected type conversion from " << input_type
1758 << " to " << result_type;
1759 };
1760 break;
1761
Roland Levillaindff1f282014-11-05 14:15:05 +00001762 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001763 switch (input_type) {
1764 case Primitive::kPrimByte:
1765 case Primitive::kPrimShort:
1766 case Primitive::kPrimInt:
1767 case Primitive::kPrimChar: {
1768 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001769 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001770 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
1771 out.AsFpuRegisterPairLow<SRegister>());
1772 break;
1773 }
1774
Roland Levillain647b9ed2014-11-27 12:06:00 +00001775 case Primitive::kPrimLong: {
1776 // Processing a Dex `long-to-double' instruction.
1777 Register low = in.AsRegisterPairLow<Register>();
1778 Register high = in.AsRegisterPairHigh<Register>();
1779 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
1780 DRegister out_d = FromLowSToD(out_s);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001781 Register constant_low = locations->GetTemp(0).AsRegister<Register>();
1782 Register constant_high = locations->GetTemp(1).AsRegister<Register>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00001783 SRegister temp_s = locations->GetTemp(2).AsFpuRegisterPairLow<SRegister>();
1784 DRegister temp_d = FromLowSToD(temp_s);
1785
Roland Levillain647b9ed2014-11-27 12:06:00 +00001786 // out_d = int-to-double(high)
1787 __ vmovsr(out_s, high);
1788 __ vcvtdi(out_d, out_s);
Roland Levillain6d0e4832014-11-27 18:31:21 +00001789 // Using vmovd to load the `k2Pow32EncodingForDouble` constant
1790 // as an immediate value into `temp_d` does not work, as
1791 // this instruction only transfers 8 significant bits of its
1792 // immediate operand. Instead, use two 32-bit core
1793 // registers to load `k2Pow32EncodingForDouble` into `temp_d`.
1794 __ LoadImmediate(constant_low, Low32Bits(k2Pow32EncodingForDouble));
1795 __ LoadImmediate(constant_high, High32Bits(k2Pow32EncodingForDouble));
Roland Levillain647b9ed2014-11-27 12:06:00 +00001796 __ vmovdrr(temp_d, constant_low, constant_high);
1797 // out_d = out_d * 2^32
1798 __ vmuld(out_d, out_d, temp_d);
1799 // temp_d = unsigned-to-double(low)
1800 __ vmovsr(temp_s, low);
1801 __ vcvtdu(temp_d, temp_s);
1802 // out_d = out_d + temp_d
1803 __ vaddd(out_d, out_d, temp_d);
1804 break;
1805 }
1806
Roland Levillaincff13742014-11-17 14:32:17 +00001807 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001808 // Processing a Dex `float-to-double' instruction.
1809 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
1810 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001811 break;
1812
1813 default:
1814 LOG(FATAL) << "Unexpected type conversion from " << input_type
1815 << " to " << result_type;
1816 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001817 break;
1818
1819 default:
1820 LOG(FATAL) << "Unexpected type conversion from " << input_type
1821 << " to " << result_type;
1822 }
1823}
1824
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001825void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001826 LocationSummary* locations =
1827 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001828 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001829 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001830 locations->SetInAt(0, Location::RequiresRegister());
1831 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001832 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1833 break;
1834 }
1835
1836 case Primitive::kPrimLong: {
1837 locations->SetInAt(0, Location::RequiresRegister());
1838 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001839 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001840 break;
1841 }
1842
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001843 case Primitive::kPrimFloat:
1844 case Primitive::kPrimDouble: {
1845 locations->SetInAt(0, Location::RequiresFpuRegister());
1846 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00001847 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001848 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001849 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001850
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001851 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001852 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001853 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001854}
1855
1856void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
1857 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001858 Location out = locations->Out();
1859 Location first = locations->InAt(0);
1860 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001861 switch (add->GetResultType()) {
1862 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001863 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00001864 __ add(out.AsRegister<Register>(),
1865 first.AsRegister<Register>(),
1866 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001867 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001868 __ AddConstant(out.AsRegister<Register>(),
1869 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001870 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001871 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001872 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001873
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001874 case Primitive::kPrimLong: {
1875 DCHECK(second.IsRegisterPair());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001876 __ adds(out.AsRegisterPairLow<Register>(),
1877 first.AsRegisterPairLow<Register>(),
1878 ShifterOperand(second.AsRegisterPairLow<Register>()));
1879 __ adc(out.AsRegisterPairHigh<Register>(),
1880 first.AsRegisterPairHigh<Register>(),
1881 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001882 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001883 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001884
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001885 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00001886 __ vadds(out.AsFpuRegister<SRegister>(),
1887 first.AsFpuRegister<SRegister>(),
1888 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001889 break;
1890
1891 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001892 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
1893 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
1894 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001895 break;
1896
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001897 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001898 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001899 }
1900}
1901
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001902void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001903 LocationSummary* locations =
1904 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001905 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001906 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001907 locations->SetInAt(0, Location::RequiresRegister());
1908 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001909 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1910 break;
1911 }
1912
1913 case Primitive::kPrimLong: {
1914 locations->SetInAt(0, Location::RequiresRegister());
1915 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001916 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001917 break;
1918 }
Calin Juravle11351682014-10-23 15:38:15 +01001919 case Primitive::kPrimFloat:
1920 case Primitive::kPrimDouble: {
1921 locations->SetInAt(0, Location::RequiresFpuRegister());
1922 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00001923 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001924 break;
Calin Juravle11351682014-10-23 15:38:15 +01001925 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001926 default:
Calin Juravle11351682014-10-23 15:38:15 +01001927 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001928 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001929}
1930
1931void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
1932 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01001933 Location out = locations->Out();
1934 Location first = locations->InAt(0);
1935 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001936 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001937 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01001938 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00001939 __ sub(out.AsRegister<Register>(),
1940 first.AsRegister<Register>(),
1941 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001942 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001943 __ AddConstant(out.AsRegister<Register>(),
1944 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01001945 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001946 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001947 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001948 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001949
Calin Juravle11351682014-10-23 15:38:15 +01001950 case Primitive::kPrimLong: {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001951 DCHECK(second.IsRegisterPair());
Calin Juravle11351682014-10-23 15:38:15 +01001952 __ subs(out.AsRegisterPairLow<Register>(),
1953 first.AsRegisterPairLow<Register>(),
1954 ShifterOperand(second.AsRegisterPairLow<Register>()));
1955 __ sbc(out.AsRegisterPairHigh<Register>(),
1956 first.AsRegisterPairHigh<Register>(),
1957 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001958 break;
Calin Juravle11351682014-10-23 15:38:15 +01001959 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001960
Calin Juravle11351682014-10-23 15:38:15 +01001961 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00001962 __ vsubs(out.AsFpuRegister<SRegister>(),
1963 first.AsFpuRegister<SRegister>(),
1964 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001965 break;
Calin Juravle11351682014-10-23 15:38:15 +01001966 }
1967
1968 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001969 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
1970 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
1971 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01001972 break;
1973 }
1974
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001975
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001976 default:
Calin Juravle11351682014-10-23 15:38:15 +01001977 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001978 }
1979}
1980
Calin Juravle34bacdf2014-10-07 20:23:36 +01001981void LocationsBuilderARM::VisitMul(HMul* mul) {
1982 LocationSummary* locations =
1983 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1984 switch (mul->GetResultType()) {
1985 case Primitive::kPrimInt:
1986 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001987 locations->SetInAt(0, Location::RequiresRegister());
1988 locations->SetInAt(1, Location::RequiresRegister());
1989 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01001990 break;
1991 }
1992
Calin Juravleb5bfa962014-10-21 18:02:24 +01001993 case Primitive::kPrimFloat:
1994 case Primitive::kPrimDouble: {
1995 locations->SetInAt(0, Location::RequiresFpuRegister());
1996 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00001997 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01001998 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001999 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002000
2001 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002002 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002003 }
2004}
2005
2006void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
2007 LocationSummary* locations = mul->GetLocations();
2008 Location out = locations->Out();
2009 Location first = locations->InAt(0);
2010 Location second = locations->InAt(1);
2011 switch (mul->GetResultType()) {
2012 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00002013 __ mul(out.AsRegister<Register>(),
2014 first.AsRegister<Register>(),
2015 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002016 break;
2017 }
2018 case Primitive::kPrimLong: {
2019 Register out_hi = out.AsRegisterPairHigh<Register>();
2020 Register out_lo = out.AsRegisterPairLow<Register>();
2021 Register in1_hi = first.AsRegisterPairHigh<Register>();
2022 Register in1_lo = first.AsRegisterPairLow<Register>();
2023 Register in2_hi = second.AsRegisterPairHigh<Register>();
2024 Register in2_lo = second.AsRegisterPairLow<Register>();
2025
2026 // Extra checks to protect caused by the existence of R1_R2.
2027 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
2028 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
2029 DCHECK_NE(out_hi, in1_lo);
2030 DCHECK_NE(out_hi, in2_lo);
2031
2032 // input: in1 - 64 bits, in2 - 64 bits
2033 // output: out
2034 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2035 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2036 // parts: out.lo = (in1.lo * in2.lo)[31:0]
2037
2038 // IP <- in1.lo * in2.hi
2039 __ mul(IP, in1_lo, in2_hi);
2040 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2041 __ mla(out_hi, in1_hi, in2_lo, IP);
2042 // out.lo <- (in1.lo * in2.lo)[31:0];
2043 __ umull(out_lo, IP, in1_lo, in2_lo);
2044 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2045 __ add(out_hi, out_hi, ShifterOperand(IP));
2046 break;
2047 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002048
2049 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002050 __ vmuls(out.AsFpuRegister<SRegister>(),
2051 first.AsFpuRegister<SRegister>(),
2052 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002053 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002054 }
2055
2056 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002057 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2058 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2059 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01002060 break;
2061 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002062
2063 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002064 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002065 }
2066}
2067
Calin Juravle7c4954d2014-10-28 16:57:40 +00002068void LocationsBuilderARM::VisitDiv(HDiv* div) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002069 LocationSummary::CallKind call_kind = div->GetResultType() == Primitive::kPrimLong
2070 ? LocationSummary::kCall
2071 : LocationSummary::kNoCall;
2072 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2073
Calin Juravle7c4954d2014-10-28 16:57:40 +00002074 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002075 case Primitive::kPrimInt: {
2076 locations->SetInAt(0, Location::RequiresRegister());
2077 locations->SetInAt(1, Location::RequiresRegister());
2078 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2079 break;
2080 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002081 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002082 InvokeRuntimeCallingConvention calling_convention;
2083 locations->SetInAt(0, Location::RegisterPairLocation(
2084 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2085 locations->SetInAt(1, Location::RegisterPairLocation(
2086 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002087 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002088 break;
2089 }
2090 case Primitive::kPrimFloat:
2091 case Primitive::kPrimDouble: {
2092 locations->SetInAt(0, Location::RequiresFpuRegister());
2093 locations->SetInAt(1, Location::RequiresFpuRegister());
2094 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2095 break;
2096 }
2097
2098 default:
2099 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2100 }
2101}
2102
2103void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
2104 LocationSummary* locations = div->GetLocations();
2105 Location out = locations->Out();
2106 Location first = locations->InAt(0);
2107 Location second = locations->InAt(1);
2108
2109 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002110 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00002111 __ sdiv(out.AsRegister<Register>(),
2112 first.AsRegister<Register>(),
2113 second.AsRegister<Register>());
Calin Juravled0d48522014-11-04 16:40:20 +00002114 break;
2115 }
2116
Calin Juravle7c4954d2014-10-28 16:57:40 +00002117 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002118 InvokeRuntimeCallingConvention calling_convention;
2119 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2120 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2121 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2122 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2123 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002124 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002125
2126 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv), div, div->GetDexPc());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002127 break;
2128 }
2129
2130 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002131 __ vdivs(out.AsFpuRegister<SRegister>(),
2132 first.AsFpuRegister<SRegister>(),
2133 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002134 break;
2135 }
2136
2137 case Primitive::kPrimDouble: {
2138 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2139 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2140 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
2141 break;
2142 }
2143
2144 default:
2145 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2146 }
2147}
2148
Calin Juravlebacfec32014-11-14 15:54:36 +00002149void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002150 Primitive::Type type = rem->GetResultType();
2151 LocationSummary::CallKind call_kind = type == Primitive::kPrimInt
2152 ? LocationSummary::kNoCall
2153 : LocationSummary::kCall;
Calin Juravlebacfec32014-11-14 15:54:36 +00002154 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2155
Calin Juravled2ec87d2014-12-08 14:24:46 +00002156 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002157 case Primitive::kPrimInt: {
2158 locations->SetInAt(0, Location::RequiresRegister());
2159 locations->SetInAt(1, Location::RequiresRegister());
2160 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2161 locations->AddTemp(Location::RequiresRegister());
2162 break;
2163 }
2164 case Primitive::kPrimLong: {
2165 InvokeRuntimeCallingConvention calling_convention;
2166 locations->SetInAt(0, Location::RegisterPairLocation(
2167 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2168 locations->SetInAt(1, Location::RegisterPairLocation(
2169 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2170 // The runtime helper puts the output in R2,R3.
2171 locations->SetOut(Location::RegisterPairLocation(R2, R3));
2172 break;
2173 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00002174 case Primitive::kPrimFloat: {
2175 InvokeRuntimeCallingConvention calling_convention;
2176 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
2177 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
2178 locations->SetOut(Location::FpuRegisterLocation(S0));
2179 break;
2180 }
2181
Calin Juravlebacfec32014-11-14 15:54:36 +00002182 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002183 InvokeRuntimeCallingConvention calling_convention;
2184 locations->SetInAt(0, Location::FpuRegisterPairLocation(
2185 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
2186 locations->SetInAt(1, Location::FpuRegisterPairLocation(
2187 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
2188 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00002189 break;
2190 }
2191
2192 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002193 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002194 }
2195}
2196
2197void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
2198 LocationSummary* locations = rem->GetLocations();
2199 Location out = locations->Out();
2200 Location first = locations->InAt(0);
2201 Location second = locations->InAt(1);
2202
Calin Juravled2ec87d2014-12-08 14:24:46 +00002203 Primitive::Type type = rem->GetResultType();
2204 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002205 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002206 Register reg1 = first.AsRegister<Register>();
2207 Register reg2 = second.AsRegister<Register>();
2208 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00002209
2210 // temp = reg1 / reg2 (integer division)
2211 // temp = temp * reg2
2212 // dest = reg1 - temp
2213 __ sdiv(temp, reg1, reg2);
2214 __ mul(temp, temp, reg2);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002215 __ sub(out.AsRegister<Register>(), reg1, ShifterOperand(temp));
Calin Juravlebacfec32014-11-14 15:54:36 +00002216 break;
2217 }
2218
2219 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002220 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod), rem, rem->GetDexPc());
2221 break;
2222 }
2223
Calin Juravled2ec87d2014-12-08 14:24:46 +00002224 case Primitive::kPrimFloat: {
2225 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf), rem, rem->GetDexPc());
2226 break;
2227 }
2228
Calin Juravlebacfec32014-11-14 15:54:36 +00002229 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002230 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod), rem, rem->GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00002231 break;
2232 }
2233
2234 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002235 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002236 }
2237}
2238
Calin Juravled0d48522014-11-04 16:40:20 +00002239void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2240 LocationSummary* locations =
2241 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002242 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00002243 if (instruction->HasUses()) {
2244 locations->SetOut(Location::SameAsFirstInput());
2245 }
2246}
2247
2248void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2249 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
2250 codegen_->AddSlowPath(slow_path);
2251
2252 LocationSummary* locations = instruction->GetLocations();
2253 Location value = locations->InAt(0);
2254
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002255 switch (instruction->GetType()) {
2256 case Primitive::kPrimInt: {
2257 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002258 __ cmp(value.AsRegister<Register>(), ShifterOperand(0));
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002259 __ b(slow_path->GetEntryLabel(), EQ);
2260 } else {
2261 DCHECK(value.IsConstant()) << value;
2262 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2263 __ b(slow_path->GetEntryLabel());
2264 }
2265 }
2266 break;
2267 }
2268 case Primitive::kPrimLong: {
2269 if (value.IsRegisterPair()) {
2270 __ orrs(IP,
2271 value.AsRegisterPairLow<Register>(),
2272 ShifterOperand(value.AsRegisterPairHigh<Register>()));
2273 __ b(slow_path->GetEntryLabel(), EQ);
2274 } else {
2275 DCHECK(value.IsConstant()) << value;
2276 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2277 __ b(slow_path->GetEntryLabel());
2278 }
2279 }
2280 break;
2281 default:
2282 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2283 }
2284 }
Calin Juravled0d48522014-11-04 16:40:20 +00002285}
2286
Calin Juravle9aec02f2014-11-18 23:06:35 +00002287void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
2288 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2289
2290 LocationSummary::CallKind call_kind = op->GetResultType() == Primitive::kPrimLong
2291 ? LocationSummary::kCall
2292 : LocationSummary::kNoCall;
2293 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(op, call_kind);
2294
2295 switch (op->GetResultType()) {
2296 case Primitive::kPrimInt: {
2297 locations->SetInAt(0, Location::RequiresRegister());
2298 locations->SetInAt(1, Location::RegisterOrConstant(op->InputAt(1)));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002299 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002300 break;
2301 }
2302 case Primitive::kPrimLong: {
2303 InvokeRuntimeCallingConvention calling_convention;
2304 locations->SetInAt(0, Location::RegisterPairLocation(
2305 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2306 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002307 // The runtime helper puts the output in R0,R1.
2308 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle9aec02f2014-11-18 23:06:35 +00002309 break;
2310 }
2311 default:
2312 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2313 }
2314}
2315
2316void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
2317 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2318
2319 LocationSummary* locations = op->GetLocations();
2320 Location out = locations->Out();
2321 Location first = locations->InAt(0);
2322 Location second = locations->InAt(1);
2323
2324 Primitive::Type type = op->GetResultType();
2325 switch (type) {
2326 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002327 Register out_reg = out.AsRegister<Register>();
2328 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002329 // Arm doesn't mask the shift count so we need to do it ourselves.
2330 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002331 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002332 __ and_(second_reg, second_reg, ShifterOperand(kMaxIntShiftValue));
2333 if (op->IsShl()) {
2334 __ Lsl(out_reg, first_reg, second_reg);
2335 } else if (op->IsShr()) {
2336 __ Asr(out_reg, first_reg, second_reg);
2337 } else {
2338 __ Lsr(out_reg, first_reg, second_reg);
2339 }
2340 } else {
2341 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
2342 uint32_t shift_value = static_cast<uint32_t>(cst & kMaxIntShiftValue);
2343 if (shift_value == 0) { // arm does not support shifting with 0 immediate.
2344 __ Mov(out_reg, first_reg);
2345 } else if (op->IsShl()) {
2346 __ Lsl(out_reg, first_reg, shift_value);
2347 } else if (op->IsShr()) {
2348 __ Asr(out_reg, first_reg, shift_value);
2349 } else {
2350 __ Lsr(out_reg, first_reg, shift_value);
2351 }
2352 }
2353 break;
2354 }
2355 case Primitive::kPrimLong: {
2356 // TODO: Inline the assembly instead of calling the runtime.
2357 InvokeRuntimeCallingConvention calling_convention;
2358 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2359 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002360 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegister<Register>());
Calin Juravle9aec02f2014-11-18 23:06:35 +00002361 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002362 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravle9aec02f2014-11-18 23:06:35 +00002363
2364 int32_t entry_point_offset;
2365 if (op->IsShl()) {
2366 entry_point_offset = QUICK_ENTRY_POINT(pShlLong);
2367 } else if (op->IsShr()) {
2368 entry_point_offset = QUICK_ENTRY_POINT(pShrLong);
2369 } else {
2370 entry_point_offset = QUICK_ENTRY_POINT(pUshrLong);
2371 }
2372 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
2373 __ blx(LR);
2374 break;
2375 }
2376 default:
2377 LOG(FATAL) << "Unexpected operation type " << type;
2378 }
2379}
2380
2381void LocationsBuilderARM::VisitShl(HShl* shl) {
2382 HandleShift(shl);
2383}
2384
2385void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
2386 HandleShift(shl);
2387}
2388
2389void LocationsBuilderARM::VisitShr(HShr* shr) {
2390 HandleShift(shr);
2391}
2392
2393void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
2394 HandleShift(shr);
2395}
2396
2397void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
2398 HandleShift(ushr);
2399}
2400
2401void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
2402 HandleShift(ushr);
2403}
2404
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002405void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002406 LocationSummary* locations =
2407 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002408 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002409 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2410 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2411 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002412}
2413
2414void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
2415 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002416 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002417 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002418 codegen_->InvokeRuntime(GetThreadOffset<kArmWordSize>(instruction->GetEntrypoint()).Int32Value(),
2419 instruction,
2420 instruction->GetDexPc());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002421}
2422
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002423void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
2424 LocationSummary* locations =
2425 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2426 InvokeRuntimeCallingConvention calling_convention;
2427 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002428 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002429 locations->SetOut(Location::RegisterLocation(R0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002430 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002431}
2432
2433void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
2434 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002435 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(2));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002436 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002437 codegen_->InvokeRuntime(GetThreadOffset<kArmWordSize>(instruction->GetEntrypoint()).Int32Value(),
2438 instruction,
2439 instruction->GetDexPc());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002440}
2441
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002442void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002443 LocationSummary* locations =
2444 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002445 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2446 if (location.IsStackSlot()) {
2447 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2448 } else if (location.IsDoubleStackSlot()) {
2449 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002450 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002451 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002452}
2453
2454void InstructionCodeGeneratorARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002455 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002456 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002457}
2458
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002459void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002460 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002461 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002462 locations->SetInAt(0, Location::RequiresRegister());
2463 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002464}
2465
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002466void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
2467 LocationSummary* locations = not_->GetLocations();
2468 Location out = locations->Out();
2469 Location in = locations->InAt(0);
2470 switch (not_->InputAt(0)->GetType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002471 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002472 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002473 break;
2474
2475 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01002476 __ mvn(out.AsRegisterPairLow<Register>(),
2477 ShifterOperand(in.AsRegisterPairLow<Register>()));
2478 __ mvn(out.AsRegisterPairHigh<Register>(),
2479 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002480 break;
2481
2482 default:
2483 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2484 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002485}
2486
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002487void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002488 LocationSummary* locations =
2489 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00002490 switch (compare->InputAt(0)->GetType()) {
2491 case Primitive::kPrimLong: {
2492 locations->SetInAt(0, Location::RequiresRegister());
2493 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002494 // Output overlaps because it is written before doing the low comparison.
2495 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00002496 break;
2497 }
2498 case Primitive::kPrimFloat:
2499 case Primitive::kPrimDouble: {
2500 locations->SetInAt(0, Location::RequiresFpuRegister());
2501 locations->SetInAt(1, Location::RequiresFpuRegister());
2502 locations->SetOut(Location::RequiresRegister());
2503 break;
2504 }
2505 default:
2506 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
2507 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002508}
2509
2510void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002511 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002512 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00002513 Location left = locations->InAt(0);
2514 Location right = locations->InAt(1);
2515
2516 Label less, greater, done;
2517 Primitive::Type type = compare->InputAt(0)->GetType();
2518 switch (type) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002519 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002520 __ cmp(left.AsRegisterPairHigh<Register>(),
2521 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002522 __ b(&less, LT);
2523 __ b(&greater, GT);
Calin Juravleddb7df22014-11-25 20:56:51 +00002524 // Do LoadImmediate before any `cmp`, as LoadImmediate might affect the status flags.
2525 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002526 __ cmp(left.AsRegisterPairLow<Register>(),
2527 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Calin Juravleddb7df22014-11-25 20:56:51 +00002528 break;
2529 }
2530 case Primitive::kPrimFloat:
2531 case Primitive::kPrimDouble: {
2532 __ LoadImmediate(out, 0);
2533 if (type == Primitive::kPrimFloat) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002534 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00002535 } else {
2536 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
2537 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
2538 }
2539 __ vmstat(); // transfer FP status register to ARM APSR.
2540 __ b(compare->IsGtBias() ? &greater : &less, VS); // VS for unordered.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002541 break;
2542 }
2543 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00002544 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002545 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002546 __ b(&done, EQ);
2547 __ b(&less, CC); // CC is for both: unsigned compare for longs and 'less than' for floats.
2548
2549 __ Bind(&greater);
2550 __ LoadImmediate(out, 1);
2551 __ b(&done);
2552
2553 __ Bind(&less);
2554 __ LoadImmediate(out, -1);
2555
2556 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002557}
2558
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002559void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002560 LocationSummary* locations =
2561 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01002562 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2563 locations->SetInAt(i, Location::Any());
2564 }
2565 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002566}
2567
2568void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002569 UNUSED(instruction);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002570 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002571}
2572
Calin Juravle52c48962014-12-16 17:02:57 +00002573void InstructionCodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
2574 // TODO (ported from quick): revisit Arm barrier kinds
2575 DmbOptions flavour = DmbOptions::ISH; // quiet c++ warnings
2576 switch (kind) {
2577 case MemBarrierKind::kAnyStore:
2578 case MemBarrierKind::kLoadAny:
2579 case MemBarrierKind::kAnyAny: {
2580 flavour = DmbOptions::ISH;
2581 break;
2582 }
2583 case MemBarrierKind::kStoreStore: {
2584 flavour = DmbOptions::ISHST;
2585 break;
2586 }
2587 default:
2588 LOG(FATAL) << "Unexpected memory barrier " << kind;
2589 }
2590 __ dmb(flavour);
2591}
2592
2593void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
2594 uint32_t offset,
2595 Register out_lo,
2596 Register out_hi) {
2597 if (offset != 0) {
2598 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00002599 __ add(IP, addr, ShifterOperand(out_lo));
2600 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00002601 }
2602 __ ldrexd(out_lo, out_hi, addr);
2603}
2604
2605void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
2606 uint32_t offset,
2607 Register value_lo,
2608 Register value_hi,
2609 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00002610 Register temp2,
2611 HInstruction* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00002612 Label fail;
2613 if (offset != 0) {
2614 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00002615 __ add(IP, addr, ShifterOperand(temp1));
2616 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00002617 }
2618 __ Bind(&fail);
2619 // We need a load followed by store. (The address used in a STREX instruction must
2620 // be the same as the address in the most recently executed LDREX instruction.)
2621 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00002622 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002623 __ strexd(temp1, value_lo, value_hi, addr);
2624 __ cmp(temp1, ShifterOperand(0));
2625 __ b(&fail, NE);
2626}
2627
2628void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
2629 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2630
Nicolas Geoffray39468442014-09-02 15:17:15 +01002631 LocationSummary* locations =
2632 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002633 locations->SetInAt(0, Location::RequiresRegister());
2634 locations->SetInAt(1, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00002635
Calin Juravle34166012014-12-19 17:22:29 +00002636
Calin Juravle52c48962014-12-16 17:02:57 +00002637 Primitive::Type field_type = field_info.GetFieldType();
2638 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00002639 bool generate_volatile = field_info.IsVolatile()
2640 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002641 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002642 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00002643 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
2644 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002645 locations->AddTemp(Location::RequiresRegister());
2646 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00002647 } else if (generate_volatile) {
Calin Juravle52c48962014-12-16 17:02:57 +00002648 // Arm encoding have some additional constraints for ldrexd/strexd:
2649 // - registers need to be consecutive
2650 // - the first register should be even but not R14.
2651 // We don't test for Arm yet, and the assertion makes sure that we revisit this if we ever
2652 // enable Arm encoding.
2653 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
2654
2655 locations->AddTemp(Location::RequiresRegister());
2656 locations->AddTemp(Location::RequiresRegister());
2657 if (field_type == Primitive::kPrimDouble) {
2658 // For doubles we need two more registers to copy the value.
2659 locations->AddTemp(Location::RegisterLocation(R2));
2660 locations->AddTemp(Location::RegisterLocation(R3));
2661 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002662 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002663}
2664
Calin Juravle52c48962014-12-16 17:02:57 +00002665void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
2666 const FieldInfo& field_info) {
2667 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2668
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002669 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00002670 Register base = locations->InAt(0).AsRegister<Register>();
2671 Location value = locations->InAt(1);
2672
2673 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002674 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00002675 Primitive::Type field_type = field_info.GetFieldType();
2676 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2677
2678 if (is_volatile) {
2679 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
2680 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002681
2682 switch (field_type) {
2683 case Primitive::kPrimBoolean:
2684 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00002685 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002686 break;
2687 }
2688
2689 case Primitive::kPrimShort:
2690 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00002691 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002692 break;
2693 }
2694
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002695 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002696 case Primitive::kPrimNot: {
Calin Juravle77520bc2015-01-12 18:45:46 +00002697 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002698 break;
2699 }
2700
2701 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00002702 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00002703 GenerateWideAtomicStore(base, offset,
2704 value.AsRegisterPairLow<Register>(),
2705 value.AsRegisterPairHigh<Register>(),
2706 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00002707 locations->GetTemp(1).AsRegister<Register>(),
2708 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002709 } else {
2710 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00002711 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002712 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002713 break;
2714 }
2715
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002716 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00002717 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002718 break;
2719 }
2720
2721 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00002722 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00002723 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00002724 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
2725 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
2726
2727 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
2728
2729 GenerateWideAtomicStore(base, offset,
2730 value_reg_lo,
2731 value_reg_hi,
2732 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00002733 locations->GetTemp(3).AsRegister<Register>(),
2734 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002735 } else {
2736 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00002737 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002738 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002739 break;
2740 }
2741
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002742 case Primitive::kPrimVoid:
2743 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002744 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002745 }
Calin Juravle52c48962014-12-16 17:02:57 +00002746
Calin Juravle77520bc2015-01-12 18:45:46 +00002747 // Longs and doubles are handled in the switch.
2748 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
2749 codegen_->MaybeRecordImplicitNullCheck(instruction);
2750 }
2751
2752 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
2753 Register temp = locations->GetTemp(0).AsRegister<Register>();
2754 Register card = locations->GetTemp(1).AsRegister<Register>();
2755 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>());
2756 }
2757
Calin Juravle52c48962014-12-16 17:02:57 +00002758 if (is_volatile) {
2759 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
2760 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002761}
2762
Calin Juravle52c48962014-12-16 17:02:57 +00002763void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
2764 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002765 LocationSummary* locations =
2766 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002767 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00002768
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002769 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00002770 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002771 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002772 bool overlap = field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong);
2773 locations->SetOut(Location::RequiresRegister(),
2774 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
2775 if (volatile_for_double) {
Calin Juravle52c48962014-12-16 17:02:57 +00002776 // Arm encoding have some additional constraints for ldrexd/strexd:
2777 // - registers need to be consecutive
2778 // - the first register should be even but not R14.
2779 // We don't test for Arm yet, and the assertion makes sure that we revisit this if we ever
2780 // enable Arm encoding.
2781 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
2782 locations->AddTemp(Location::RequiresRegister());
2783 locations->AddTemp(Location::RequiresRegister());
2784 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002785}
2786
Calin Juravle52c48962014-12-16 17:02:57 +00002787void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
2788 const FieldInfo& field_info) {
2789 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002790
Calin Juravle52c48962014-12-16 17:02:57 +00002791 LocationSummary* locations = instruction->GetLocations();
2792 Register base = locations->InAt(0).AsRegister<Register>();
2793 Location out = locations->Out();
2794 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002795 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00002796 Primitive::Type field_type = field_info.GetFieldType();
2797 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2798
2799 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002800 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00002801 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002802 break;
2803 }
2804
2805 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00002806 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002807 break;
2808 }
2809
2810 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00002811 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002812 break;
2813 }
2814
2815 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00002816 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002817 break;
2818 }
2819
2820 case Primitive::kPrimInt:
2821 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00002822 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002823 break;
2824 }
2825
2826 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00002827 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00002828 GenerateWideAtomicLoad(base, offset,
2829 out.AsRegisterPairLow<Register>(),
2830 out.AsRegisterPairHigh<Register>());
2831 } else {
2832 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
2833 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002834 break;
2835 }
2836
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002837 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00002838 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002839 break;
2840 }
2841
2842 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00002843 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00002844 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00002845 Register lo = locations->GetTemp(0).AsRegister<Register>();
2846 Register hi = locations->GetTemp(1).AsRegister<Register>();
2847 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00002848 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002849 __ vmovdrr(out_reg, lo, hi);
2850 } else {
2851 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00002852 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002853 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002854 break;
2855 }
2856
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002857 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00002858 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002859 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002860 }
Calin Juravle52c48962014-12-16 17:02:57 +00002861
Calin Juravle77520bc2015-01-12 18:45:46 +00002862 // Doubles are handled in the switch.
2863 if (field_type != Primitive::kPrimDouble) {
2864 codegen_->MaybeRecordImplicitNullCheck(instruction);
2865 }
2866
Calin Juravle52c48962014-12-16 17:02:57 +00002867 if (is_volatile) {
2868 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
2869 }
2870}
2871
2872void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2873 HandleFieldSet(instruction, instruction->GetFieldInfo());
2874}
2875
2876void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2877 HandleFieldSet(instruction, instruction->GetFieldInfo());
2878}
2879
2880void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2881 HandleFieldGet(instruction, instruction->GetFieldInfo());
2882}
2883
2884void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2885 HandleFieldGet(instruction, instruction->GetFieldInfo());
2886}
2887
2888void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2889 HandleFieldGet(instruction, instruction->GetFieldInfo());
2890}
2891
2892void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2893 HandleFieldGet(instruction, instruction->GetFieldInfo());
2894}
2895
2896void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2897 HandleFieldSet(instruction, instruction->GetFieldInfo());
2898}
2899
2900void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2901 HandleFieldSet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002902}
2903
2904void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002905 LocationSummary* locations =
2906 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle77520bc2015-01-12 18:45:46 +00002907 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002908 if (instruction->HasUses()) {
2909 locations->SetOut(Location::SameAsFirstInput());
2910 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002911}
2912
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002913void InstructionCodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00002914 if (codegen_->CanMoveNullCheckToUser(instruction)) {
2915 return;
2916 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002917 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00002918
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002919 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
2920 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2921}
2922
2923void InstructionCodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002924 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002925 codegen_->AddSlowPath(slow_path);
2926
2927 LocationSummary* locations = instruction->GetLocations();
2928 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002929
Calin Juravle77520bc2015-01-12 18:45:46 +00002930 __ cmp(obj.AsRegister<Register>(), ShifterOperand(0));
2931 __ b(slow_path->GetEntryLabel(), EQ);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002932}
2933
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002934void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
2935 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
2936 GenerateImplicitNullCheck(instruction);
2937 } else {
2938 GenerateExplicitNullCheck(instruction);
2939 }
2940}
2941
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002942void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002943 LocationSummary* locations =
2944 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002945 locations->SetInAt(0, Location::RequiresRegister());
2946 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2947 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002948}
2949
2950void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
2951 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002952 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002953 Location index = locations->InAt(1);
2954
2955 switch (instruction->GetType()) {
2956 case Primitive::kPrimBoolean: {
2957 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002958 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002959 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002960 size_t offset =
2961 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002962 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
2963 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002964 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002965 __ LoadFromOffset(kLoadUnsignedByte, out, IP, data_offset);
2966 }
2967 break;
2968 }
2969
2970 case Primitive::kPrimByte: {
2971 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002972 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002973 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002974 size_t offset =
2975 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002976 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
2977 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002978 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002979 __ LoadFromOffset(kLoadSignedByte, out, IP, data_offset);
2980 }
2981 break;
2982 }
2983
2984 case Primitive::kPrimShort: {
2985 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002986 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002987 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002988 size_t offset =
2989 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002990 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
2991 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002992 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002993 __ LoadFromOffset(kLoadSignedHalfword, out, IP, data_offset);
2994 }
2995 break;
2996 }
2997
2998 case Primitive::kPrimChar: {
2999 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003000 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003001 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003002 size_t offset =
3003 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003004 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
3005 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003006 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003007 __ LoadFromOffset(kLoadUnsignedHalfword, out, IP, data_offset);
3008 }
3009 break;
3010 }
3011
3012 case Primitive::kPrimInt:
3013 case Primitive::kPrimNot: {
3014 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
3015 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003016 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003017 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003018 size_t offset =
3019 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003020 __ LoadFromOffset(kLoadWord, out, obj, offset);
3021 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003022 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003023 __ LoadFromOffset(kLoadWord, out, IP, data_offset);
3024 }
3025 break;
3026 }
3027
3028 case Primitive::kPrimLong: {
3029 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003030 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003031 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003032 size_t offset =
3033 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003034 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003035 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003036 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003037 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003038 }
3039 break;
3040 }
3041
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003042 case Primitive::kPrimFloat: {
3043 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3044 Location out = locations->Out();
3045 DCHECK(out.IsFpuRegister());
3046 if (index.IsConstant()) {
3047 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3048 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), obj, offset);
3049 } else {
3050 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
3051 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), IP, data_offset);
3052 }
3053 break;
3054 }
3055
3056 case Primitive::kPrimDouble: {
3057 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3058 Location out = locations->Out();
3059 DCHECK(out.IsFpuRegisterPair());
3060 if (index.IsConstant()) {
3061 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3062 __ LoadDFromOffset(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()), obj, offset);
3063 } else {
3064 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
3065 __ LoadDFromOffset(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
3066 }
3067 break;
3068 }
3069
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003070 case Primitive::kPrimVoid:
3071 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003072 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003073 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003074 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003075}
3076
3077void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003078 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003079
3080 bool needs_write_barrier =
3081 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3082 bool needs_runtime_call = instruction->NeedsTypeCheck();
3083
Nicolas Geoffray39468442014-09-02 15:17:15 +01003084 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003085 instruction, needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
3086 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003087 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003088 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3089 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3090 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003091 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003092 locations->SetInAt(0, Location::RequiresRegister());
3093 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3094 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003095
3096 if (needs_write_barrier) {
3097 // Temporary registers for the write barrier.
3098 locations->AddTemp(Location::RequiresRegister());
3099 locations->AddTemp(Location::RequiresRegister());
3100 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003101 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003102}
3103
3104void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
3105 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003106 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003107 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003108 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003109 bool needs_runtime_call = locations->WillCall();
3110 bool needs_write_barrier =
3111 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003112
3113 switch (value_type) {
3114 case Primitive::kPrimBoolean:
3115 case Primitive::kPrimByte: {
3116 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003117 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003118 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003119 size_t offset =
3120 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003121 __ StoreToOffset(kStoreByte, value, obj, offset);
3122 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003123 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003124 __ StoreToOffset(kStoreByte, value, IP, data_offset);
3125 }
3126 break;
3127 }
3128
3129 case Primitive::kPrimShort:
3130 case Primitive::kPrimChar: {
3131 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003132 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003133 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003134 size_t offset =
3135 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003136 __ StoreToOffset(kStoreHalfword, value, obj, offset);
3137 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003138 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003139 __ StoreToOffset(kStoreHalfword, value, IP, data_offset);
3140 }
3141 break;
3142 }
3143
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003144 case Primitive::kPrimInt:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003145 case Primitive::kPrimNot: {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003146 if (!needs_runtime_call) {
3147 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003148 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003149 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003150 size_t offset =
3151 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003152 __ StoreToOffset(kStoreWord, value, obj, offset);
3153 } else {
3154 DCHECK(index.IsRegister()) << index;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003155 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003156 __ StoreToOffset(kStoreWord, value, IP, data_offset);
3157 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003158 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003159 if (needs_write_barrier) {
3160 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003161 Register temp = locations->GetTemp(0).AsRegister<Register>();
3162 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003163 codegen_->MarkGCCard(temp, card, obj, value);
3164 }
3165 } else {
3166 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain199f3362014-11-27 17:15:16 +00003167 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
3168 instruction,
3169 instruction->GetDexPc());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003170 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003171 break;
3172 }
3173
3174 case Primitive::kPrimLong: {
3175 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003176 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003177 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003178 size_t offset =
3179 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003180 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003181 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003182 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003183 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003184 }
3185 break;
3186 }
3187
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003188 case Primitive::kPrimFloat: {
3189 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3190 Location value = locations->InAt(2);
3191 DCHECK(value.IsFpuRegister());
3192 if (index.IsConstant()) {
3193 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3194 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), obj, offset);
3195 } else {
3196 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
3197 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
3198 }
3199 break;
3200 }
3201
3202 case Primitive::kPrimDouble: {
3203 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3204 Location value = locations->InAt(2);
3205 DCHECK(value.IsFpuRegisterPair());
3206 if (index.IsConstant()) {
3207 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3208 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), obj, offset);
3209 } else {
3210 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
3211 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
3212 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003213
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003214 break;
3215 }
3216
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003217 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003218 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003219 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003220 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003221
3222 // Ints and objects are handled in the switch.
3223 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
3224 codegen_->MaybeRecordImplicitNullCheck(instruction);
3225 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003226}
3227
3228void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003229 LocationSummary* locations =
3230 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003231 locations->SetInAt(0, Location::RequiresRegister());
3232 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003233}
3234
3235void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
3236 LocationSummary* locations = instruction->GetLocations();
3237 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003238 Register obj = locations->InAt(0).AsRegister<Register>();
3239 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003240 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003241 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003242}
3243
3244void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003245 LocationSummary* locations =
3246 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003247 locations->SetInAt(0, Location::RequiresRegister());
3248 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003249 if (instruction->HasUses()) {
3250 locations->SetOut(Location::SameAsFirstInput());
3251 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003252}
3253
3254void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
3255 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003256 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(
Nicolas Geoffray39468442014-09-02 15:17:15 +01003257 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003258 codegen_->AddSlowPath(slow_path);
3259
Roland Levillain271ab9c2014-11-27 15:23:57 +00003260 Register index = locations->InAt(0).AsRegister<Register>();
3261 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003262
3263 __ cmp(index, ShifterOperand(length));
3264 __ b(slow_path->GetEntryLabel(), CS);
3265}
3266
3267void CodeGeneratorARM::MarkGCCard(Register temp, Register card, Register object, Register value) {
3268 Label is_null;
3269 __ CompareAndBranchIfZero(value, &is_null);
3270 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmWordSize>().Int32Value());
3271 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
3272 __ strb(card, Address(card, temp));
3273 __ Bind(&is_null);
3274}
3275
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003276void LocationsBuilderARM::VisitTemporary(HTemporary* temp) {
3277 temp->SetLocations(nullptr);
3278}
3279
3280void InstructionCodeGeneratorARM::VisitTemporary(HTemporary* temp) {
3281 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003282 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003283}
3284
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003285void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003286 UNUSED(instruction);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003287 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003288}
3289
3290void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003291 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3292}
3293
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003294void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
3295 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3296}
3297
3298void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003299 HBasicBlock* block = instruction->GetBlock();
3300 if (block->GetLoopInformation() != nullptr) {
3301 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3302 // The back edge will generate the suspend check.
3303 return;
3304 }
3305 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3306 // The goto will generate the suspend check.
3307 return;
3308 }
3309 GenerateSuspendCheck(instruction, nullptr);
3310}
3311
3312void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
3313 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003314 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003315 new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003316 codegen_->AddSlowPath(slow_path);
3317
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00003318 __ LoadFromOffset(
3319 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmWordSize>().Int32Value());
3320 __ cmp(IP, ShifterOperand(0));
3321 // TODO: Figure out the branch offsets and use cbz/cbnz.
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003322 if (successor == nullptr) {
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00003323 __ b(slow_path->GetEntryLabel(), NE);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003324 __ Bind(slow_path->GetReturnLabel());
3325 } else {
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00003326 __ b(codegen_->GetLabelOf(successor), EQ);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003327 __ b(slow_path->GetEntryLabel());
3328 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003329}
3330
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003331ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
3332 return codegen_->GetAssembler();
3333}
3334
3335void ParallelMoveResolverARM::EmitMove(size_t index) {
3336 MoveOperands* move = moves_.Get(index);
3337 Location source = move->GetSource();
3338 Location destination = move->GetDestination();
3339
3340 if (source.IsRegister()) {
3341 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003342 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003343 } else {
3344 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003345 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003346 SP, destination.GetStackIndex());
3347 }
3348 } else if (source.IsStackSlot()) {
3349 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003350 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003351 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003352 } else if (destination.IsFpuRegister()) {
3353 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003354 } else {
3355 DCHECK(destination.IsStackSlot());
3356 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
3357 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
3358 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003359 } else if (source.IsFpuRegister()) {
3360 if (destination.IsFpuRegister()) {
3361 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003362 } else {
3363 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003364 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
3365 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003366 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003367 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00003368 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
3369 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003370 } else if (destination.IsRegisterPair()) {
3371 DCHECK(ExpectedPairLayout(destination));
3372 __ LoadFromOffset(
3373 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
3374 } else {
3375 DCHECK(destination.IsFpuRegisterPair()) << destination;
3376 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
3377 SP,
3378 source.GetStackIndex());
3379 }
3380 } else if (source.IsRegisterPair()) {
3381 if (destination.IsRegisterPair()) {
3382 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
3383 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
3384 } else {
3385 DCHECK(destination.IsDoubleStackSlot()) << destination;
3386 DCHECK(ExpectedPairLayout(source));
3387 __ StoreToOffset(
3388 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
3389 }
3390 } else if (source.IsFpuRegisterPair()) {
3391 if (destination.IsFpuRegisterPair()) {
3392 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
3393 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
3394 } else {
3395 DCHECK(destination.IsDoubleStackSlot()) << destination;
3396 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
3397 SP,
3398 destination.GetStackIndex());
3399 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003400 } else {
3401 DCHECK(source.IsConstant()) << source;
3402 HInstruction* constant = source.GetConstant();
3403 if (constant->IsIntConstant()) {
3404 int32_t value = constant->AsIntConstant()->GetValue();
3405 if (destination.IsRegister()) {
3406 __ LoadImmediate(destination.AsRegister<Register>(), value);
3407 } else {
3408 DCHECK(destination.IsStackSlot());
3409 __ LoadImmediate(IP, value);
3410 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
3411 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003412 } else if (constant->IsLongConstant()) {
3413 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003414 if (destination.IsRegisterPair()) {
3415 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
3416 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003417 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003418 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003419 __ LoadImmediate(IP, Low32Bits(value));
3420 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
3421 __ LoadImmediate(IP, High32Bits(value));
3422 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
3423 }
3424 } else if (constant->IsDoubleConstant()) {
3425 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003426 if (destination.IsFpuRegisterPair()) {
3427 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003428 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003429 DCHECK(destination.IsDoubleStackSlot()) << destination;
3430 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003431 __ LoadImmediate(IP, Low32Bits(int_value));
3432 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
3433 __ LoadImmediate(IP, High32Bits(int_value));
3434 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
3435 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003436 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003437 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003438 float value = constant->AsFloatConstant()->GetValue();
3439 if (destination.IsFpuRegister()) {
3440 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
3441 } else {
3442 DCHECK(destination.IsStackSlot());
3443 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
3444 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
3445 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003446 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003447 }
3448}
3449
3450void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
3451 __ Mov(IP, reg);
3452 __ LoadFromOffset(kLoadWord, reg, SP, mem);
3453 __ StoreToOffset(kStoreWord, IP, SP, mem);
3454}
3455
3456void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
3457 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
3458 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
3459 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
3460 SP, mem1 + stack_offset);
3461 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
3462 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
3463 SP, mem2 + stack_offset);
3464 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
3465}
3466
3467void ParallelMoveResolverARM::EmitSwap(size_t index) {
3468 MoveOperands* move = moves_.Get(index);
3469 Location source = move->GetSource();
3470 Location destination = move->GetDestination();
3471
3472 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003473 DCHECK_NE(source.AsRegister<Register>(), IP);
3474 DCHECK_NE(destination.AsRegister<Register>(), IP);
3475 __ Mov(IP, source.AsRegister<Register>());
3476 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
3477 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003478 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003479 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003480 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003481 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003482 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
3483 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003484 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00003485 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003486 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00003487 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003488 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00003489 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003490 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003491 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00003492 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
3493 destination.AsRegisterPairHigh<Register>(),
3494 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003495 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003496 Register low_reg = source.IsRegisterPair()
3497 ? source.AsRegisterPairLow<Register>()
3498 : destination.AsRegisterPairLow<Register>();
3499 int mem = source.IsRegisterPair()
3500 ? destination.GetStackIndex()
3501 : source.GetStackIndex();
3502 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00003503 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003504 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00003505 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003506 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003507 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
3508 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00003509 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003510 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00003511 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003512 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
3513 DRegister reg = source.IsFpuRegisterPair()
3514 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
3515 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
3516 int mem = source.IsFpuRegisterPair()
3517 ? destination.GetStackIndex()
3518 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00003519 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003520 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00003521 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003522 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
3523 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
3524 : destination.AsFpuRegister<SRegister>();
3525 int mem = source.IsFpuRegister()
3526 ? destination.GetStackIndex()
3527 : source.GetStackIndex();
3528
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00003529 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003530 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00003531 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00003532 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00003533 Exchange(source.GetStackIndex(), destination.GetStackIndex());
3534 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003535 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00003536 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003537 }
3538}
3539
3540void ParallelMoveResolverARM::SpillScratch(int reg) {
3541 __ Push(static_cast<Register>(reg));
3542}
3543
3544void ParallelMoveResolverARM::RestoreScratch(int reg) {
3545 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003546}
3547
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003548void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003549 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
3550 ? LocationSummary::kCallOnSlowPath
3551 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003552 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003553 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003554 locations->SetOut(Location::RequiresRegister());
3555}
3556
3557void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003558 Register out = cls->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003559 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003560 DCHECK(!cls->CanCallRuntime());
3561 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003562 codegen_->LoadCurrentMethod(out);
3563 __ LoadFromOffset(kLoadWord, out, out, mirror::ArtMethod::DeclaringClassOffset().Int32Value());
3564 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003565 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003566 codegen_->LoadCurrentMethod(out);
3567 __ LoadFromOffset(
3568 kLoadWord, out, out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value());
3569 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003570
3571 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
3572 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3573 codegen_->AddSlowPath(slow_path);
3574 __ cmp(out, ShifterOperand(0));
3575 __ b(slow_path->GetEntryLabel(), EQ);
3576 if (cls->MustGenerateClinitCheck()) {
3577 GenerateClassInitializationCheck(slow_path, out);
3578 } else {
3579 __ Bind(slow_path->GetExitLabel());
3580 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003581 }
3582}
3583
3584void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
3585 LocationSummary* locations =
3586 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3587 locations->SetInAt(0, Location::RequiresRegister());
3588 if (check->HasUses()) {
3589 locations->SetOut(Location::SameAsFirstInput());
3590 }
3591}
3592
3593void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003594 // We assume the class is not null.
3595 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
3596 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003597 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00003598 GenerateClassInitializationCheck(slow_path,
3599 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003600}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003601
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003602void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
3603 SlowPathCodeARM* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003604 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
3605 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
3606 __ b(slow_path->GetEntryLabel(), LT);
3607 // Even if the initialized flag is set, we may be in a situation where caches are not synced
3608 // properly. Therefore, we do a memory fence.
3609 __ dmb(ISH);
3610 __ Bind(slow_path->GetExitLabel());
3611}
3612
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003613void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
3614 LocationSummary* locations =
3615 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3616 locations->SetOut(Location::RequiresRegister());
3617}
3618
3619void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) {
3620 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
3621 codegen_->AddSlowPath(slow_path);
3622
Roland Levillain271ab9c2014-11-27 15:23:57 +00003623 Register out = load->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003624 codegen_->LoadCurrentMethod(out);
Mathieu Chartiereace4582014-11-24 18:29:54 -08003625 __ LoadFromOffset(kLoadWord, out, out, mirror::ArtMethod::DeclaringClassOffset().Int32Value());
3626 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003627 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
3628 __ cmp(out, ShifterOperand(0));
3629 __ b(slow_path->GetEntryLabel(), EQ);
3630 __ Bind(slow_path->GetExitLabel());
3631}
3632
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003633void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
3634 LocationSummary* locations =
3635 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3636 locations->SetOut(Location::RequiresRegister());
3637}
3638
3639void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003640 Register out = load->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003641 int32_t offset = Thread::ExceptionOffset<kArmWordSize>().Int32Value();
3642 __ LoadFromOffset(kLoadWord, out, TR, offset);
3643 __ LoadImmediate(IP, 0);
3644 __ StoreToOffset(kStoreWord, IP, TR, offset);
3645}
3646
3647void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
3648 LocationSummary* locations =
3649 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3650 InvokeRuntimeCallingConvention calling_convention;
3651 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3652}
3653
3654void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
3655 codegen_->InvokeRuntime(
3656 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc());
3657}
3658
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003659void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003660 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
3661 ? LocationSummary::kNoCall
3662 : LocationSummary::kCallOnSlowPath;
3663 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3664 locations->SetInAt(0, Location::RequiresRegister());
3665 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003666 // The out register is used as a temporary, so it overlaps with the inputs.
3667 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003668}
3669
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003670void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003671 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003672 Register obj = locations->InAt(0).AsRegister<Register>();
3673 Register cls = locations->InAt(1).AsRegister<Register>();
3674 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003675 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3676 Label done, zero;
3677 SlowPathCodeARM* slow_path = nullptr;
3678
3679 // Return 0 if `obj` is null.
3680 // TODO: avoid this check if we know obj is not null.
3681 __ cmp(obj, ShifterOperand(0));
3682 __ b(&zero, EQ);
3683 // Compare the class of `obj` with `cls`.
3684 __ LoadFromOffset(kLoadWord, out, obj, class_offset);
3685 __ cmp(out, ShifterOperand(cls));
3686 if (instruction->IsClassFinal()) {
3687 // Classes must be equal for the instanceof to succeed.
3688 __ b(&zero, NE);
3689 __ LoadImmediate(out, 1);
3690 __ b(&done);
3691 } else {
3692 // If the classes are not equal, we go into a slow path.
3693 DCHECK(locations->OnlyCallsOnSlowPath());
3694 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003695 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003696 codegen_->AddSlowPath(slow_path);
3697 __ b(slow_path->GetEntryLabel(), NE);
3698 __ LoadImmediate(out, 1);
3699 __ b(&done);
3700 }
3701 __ Bind(&zero);
3702 __ LoadImmediate(out, 0);
3703 if (slow_path != nullptr) {
3704 __ Bind(slow_path->GetExitLabel());
3705 }
3706 __ Bind(&done);
3707}
3708
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003709void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
3710 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3711 instruction, LocationSummary::kCallOnSlowPath);
3712 locations->SetInAt(0, Location::RequiresRegister());
3713 locations->SetInAt(1, Location::RequiresRegister());
3714 locations->AddTemp(Location::RequiresRegister());
3715}
3716
3717void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
3718 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003719 Register obj = locations->InAt(0).AsRegister<Register>();
3720 Register cls = locations->InAt(1).AsRegister<Register>();
3721 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003722 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3723
3724 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(
3725 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
3726 codegen_->AddSlowPath(slow_path);
3727
3728 // TODO: avoid this check if we know obj is not null.
3729 __ cmp(obj, ShifterOperand(0));
3730 __ b(slow_path->GetExitLabel(), EQ);
3731 // Compare the class of `obj` with `cls`.
3732 __ LoadFromOffset(kLoadWord, temp, obj, class_offset);
3733 __ cmp(temp, ShifterOperand(cls));
3734 __ b(slow_path->GetEntryLabel(), NE);
3735 __ Bind(slow_path->GetExitLabel());
3736}
3737
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003738void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
3739 LocationSummary* locations =
3740 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3741 InvokeRuntimeCallingConvention calling_convention;
3742 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3743}
3744
3745void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
3746 codegen_->InvokeRuntime(instruction->IsEnter()
3747 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
3748 instruction,
3749 instruction->GetDexPc());
3750}
3751
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003752void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
3753void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
3754void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
3755
3756void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
3757 LocationSummary* locations =
3758 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3759 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
3760 || instruction->GetResultType() == Primitive::kPrimLong);
3761 locations->SetInAt(0, Location::RequiresRegister());
3762 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003763 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003764}
3765
3766void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
3767 HandleBitwiseOperation(instruction);
3768}
3769
3770void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
3771 HandleBitwiseOperation(instruction);
3772}
3773
3774void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
3775 HandleBitwiseOperation(instruction);
3776}
3777
3778void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
3779 LocationSummary* locations = instruction->GetLocations();
3780
3781 if (instruction->GetResultType() == Primitive::kPrimInt) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003782 Register first = locations->InAt(0).AsRegister<Register>();
3783 Register second = locations->InAt(1).AsRegister<Register>();
3784 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003785 if (instruction->IsAnd()) {
3786 __ and_(out, first, ShifterOperand(second));
3787 } else if (instruction->IsOr()) {
3788 __ orr(out, first, ShifterOperand(second));
3789 } else {
3790 DCHECK(instruction->IsXor());
3791 __ eor(out, first, ShifterOperand(second));
3792 }
3793 } else {
3794 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3795 Location first = locations->InAt(0);
3796 Location second = locations->InAt(1);
3797 Location out = locations->Out();
3798 if (instruction->IsAnd()) {
3799 __ and_(out.AsRegisterPairLow<Register>(),
3800 first.AsRegisterPairLow<Register>(),
3801 ShifterOperand(second.AsRegisterPairLow<Register>()));
3802 __ and_(out.AsRegisterPairHigh<Register>(),
3803 first.AsRegisterPairHigh<Register>(),
3804 ShifterOperand(second.AsRegisterPairHigh<Register>()));
3805 } else if (instruction->IsOr()) {
3806 __ orr(out.AsRegisterPairLow<Register>(),
3807 first.AsRegisterPairLow<Register>(),
3808 ShifterOperand(second.AsRegisterPairLow<Register>()));
3809 __ orr(out.AsRegisterPairHigh<Register>(),
3810 first.AsRegisterPairHigh<Register>(),
3811 ShifterOperand(second.AsRegisterPairHigh<Register>()));
3812 } else {
3813 DCHECK(instruction->IsXor());
3814 __ eor(out.AsRegisterPairLow<Register>(),
3815 first.AsRegisterPairLow<Register>(),
3816 ShifterOperand(second.AsRegisterPairLow<Register>()));
3817 __ eor(out.AsRegisterPairHigh<Register>(),
3818 first.AsRegisterPairHigh<Register>(),
3819 ShifterOperand(second.AsRegisterPairHigh<Register>()));
3820 }
3821 }
3822}
3823
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08003824void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Register temp) {
3825 DCHECK_EQ(temp, kArtMethodRegister);
3826
3827 // TODO: Implement all kinds of calls:
3828 // 1) boot -> boot
3829 // 2) app -> boot
3830 // 3) app -> app
3831 //
3832 // Currently we implement the app -> app logic, which looks up in the resolve cache.
3833
3834 // temp = method;
3835 LoadCurrentMethod(temp);
3836 if (!invoke->IsRecursive()) {
3837 // temp = temp->dex_cache_resolved_methods_;
3838 __ LoadFromOffset(
3839 kLoadWord, temp, temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value());
3840 // temp = temp[index_in_cache]
3841 __ LoadFromOffset(
3842 kLoadWord, temp, temp, CodeGenerator::GetCacheOffset(invoke->GetDexMethodIndex()));
3843 // LR = temp[offset_of_quick_compiled_code]
3844 __ LoadFromOffset(kLoadWord, LR, temp,
3845 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
3846 kArmWordSize).Int32Value());
3847 // LR()
3848 __ blx(LR);
3849 } else {
3850 __ bl(GetFrameEntryLabel());
3851 }
3852
3853 RecordPcInfo(invoke, invoke->GetDexPc());
3854 DCHECK(!IsLeafMethod());
3855}
3856
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003857} // namespace arm
3858} // namespace art