blob: bc8858bc8c5eb3e81d03ae7a9d52a3ba9aa94722 [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"
Ian Rogers7e70b002014-10-08 11:47:24 -070022#include "mirror/array-inl.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000023#include "mirror/art_method.h"
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010024#include "mirror/class.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070025#include "thread.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010026#include "utils/arm/assembler_arm.h"
27#include "utils/arm/managed_register_arm.h"
Roland Levillain946e1432014-11-11 17:35:19 +000028#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010029#include "utils/stack_checks.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000030
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000031namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010032
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000033namespace arm {
34
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +000035static DRegister FromLowSToD(SRegister reg) {
36 DCHECK_EQ(reg % 2, 0);
37 return static_cast<DRegister>(reg / 2);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +010038}
39
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +000040static bool ExpectedPairLayout(Location location) {
41 // We expected this for both core and fpu register pairs.
42 return ((location.low() & 1) == 0) && (location.low() + 1 == location.high());
43}
44
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +000045static constexpr int kNumberOfPushedRegistersAtEntry = 1 + 2; // LR, R6, R7
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010046static constexpr int kCurrentMethodStackOffset = 0;
47
Calin Juravled6fb6cf2014-11-11 19:07:44 +000048static constexpr Register kRuntimeParameterCoreRegisters[] = { R0, R1, R2, R3 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010049static constexpr size_t kRuntimeParameterCoreRegistersLength =
50 arraysize(kRuntimeParameterCoreRegisters);
Calin Juravled2ec87d2014-12-08 14:24:46 +000051static constexpr SRegister kRuntimeParameterFpuRegisters[] = { S0, S1, S2, S3 };
Roland Levillain624279f2014-12-04 11:54:28 +000052static constexpr size_t kRuntimeParameterFpuRegistersLength =
53 arraysize(kRuntimeParameterFpuRegisters);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010054
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +000055class InvokeRuntimeCallingConvention : public CallingConvention<Register, SRegister> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010056 public:
57 InvokeRuntimeCallingConvention()
58 : CallingConvention(kRuntimeParameterCoreRegisters,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010059 kRuntimeParameterCoreRegistersLength,
60 kRuntimeParameterFpuRegisters,
61 kRuntimeParameterFpuRegistersLength) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010062
63 private:
64 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
65};
66
Nicolas Geoffraye5038322014-07-04 09:41:32 +010067#define __ reinterpret_cast<ArmAssembler*>(codegen->GetAssembler())->
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010068#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmWordSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010069
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010070class SlowPathCodeARM : public SlowPathCode {
71 public:
72 SlowPathCodeARM() : entry_label_(), exit_label_() {}
73
74 Label* GetEntryLabel() { return &entry_label_; }
75 Label* GetExitLabel() { return &exit_label_; }
76
77 private:
78 Label entry_label_;
79 Label exit_label_;
80
81 DISALLOW_COPY_AND_ASSIGN(SlowPathCodeARM);
82};
83
84class NullCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010085 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010086 explicit NullCheckSlowPathARM(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010087
Alexandre Rames67555f72014-11-18 10:55:16 +000088 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010089 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010090 __ Bind(GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010091 arm_codegen->InvokeRuntime(
92 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010093 }
94
95 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010096 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010097 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM);
98};
99
Calin Juravled0d48522014-11-04 16:40:20 +0000100class DivZeroCheckSlowPathARM : public SlowPathCodeARM {
101 public:
102 explicit DivZeroCheckSlowPathARM(HDivZeroCheck* instruction) : instruction_(instruction) {}
103
Alexandre Rames67555f72014-11-18 10:55:16 +0000104 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000105 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
106 __ Bind(GetEntryLabel());
107 arm_codegen->InvokeRuntime(
108 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc());
109 }
110
111 private:
112 HDivZeroCheck* const instruction_;
113 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM);
114};
115
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100116class StackOverflowCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100117 public:
118 StackOverflowCheckSlowPathARM() {}
119
Alexandre Rames67555f72014-11-18 10:55:16 +0000120 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100121 __ Bind(GetEntryLabel());
122 __ LoadFromOffset(kLoadWord, PC, TR,
123 QUICK_ENTRYPOINT_OFFSET(kArmWordSize, pThrowStackOverflow).Int32Value());
124 }
125
126 private:
127 DISALLOW_COPY_AND_ASSIGN(StackOverflowCheckSlowPathARM);
128};
129
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100130class SuspendCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000131 public:
Alexandre Rames67555f72014-11-18 10:55:16 +0000132 SuspendCheckSlowPathARM(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100133 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000134
Alexandre Rames67555f72014-11-18 10:55:16 +0000135 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100136 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000137 __ Bind(GetEntryLabel());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100138 codegen->SaveLiveRegisters(instruction_->GetLocations());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100139 arm_codegen->InvokeRuntime(
140 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100141 codegen->RestoreLiveRegisters(instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100142 if (successor_ == nullptr) {
143 __ b(GetReturnLabel());
144 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100145 __ b(arm_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100146 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000147 }
148
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100149 Label* GetReturnLabel() {
150 DCHECK(successor_ == nullptr);
151 return &return_label_;
152 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000153
154 private:
155 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100156 // If not null, the block to branch to after the suspend check.
157 HBasicBlock* const successor_;
158
159 // If `successor_` is null, the label to branch to after the suspend check.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000160 Label return_label_;
161
162 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM);
163};
164
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100165class BoundsCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100166 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100167 BoundsCheckSlowPathARM(HBoundsCheck* instruction,
168 Location index_location,
169 Location length_location)
Nicolas Geoffray39468442014-09-02 15:17:15 +0100170 : instruction_(instruction),
171 index_location_(index_location),
172 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100173
Alexandre Rames67555f72014-11-18 10:55:16 +0000174 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100175 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100176 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000177 // We're moving two locations to locations that could overlap, so we need a parallel
178 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100179 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000180 codegen->EmitParallelMoves(
181 index_location_,
182 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
183 length_location_,
184 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100185 arm_codegen->InvokeRuntime(
186 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100187 }
188
189 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100190 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100191 const Location index_location_;
192 const Location length_location_;
193
194 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM);
195};
196
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000197class LoadClassSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100198 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000199 LoadClassSlowPathARM(HLoadClass* cls,
200 HInstruction* at,
201 uint32_t dex_pc,
202 bool do_clinit)
203 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
204 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
205 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100206
Alexandre Rames67555f72014-11-18 10:55:16 +0000207 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000208 LocationSummary* locations = at_->GetLocations();
209
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100210 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
211 __ Bind(GetEntryLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000212 codegen->SaveLiveRegisters(locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100213
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100214 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000215 __ LoadImmediate(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100216 arm_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000217 int32_t entry_point_offset = do_clinit_
218 ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
219 : QUICK_ENTRY_POINT(pInitializeType);
220 arm_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_);
221
222 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000223 Location out = locations->Out();
224 if (out.IsValid()) {
225 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000226 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
227 }
228 codegen->RestoreLiveRegisters(locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100229 __ b(GetExitLabel());
230 }
231
232 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000233 // The class this slow path will load.
234 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100235
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000236 // The instruction where this slow path is happening.
237 // (Might be the load class or an initialization check).
238 HInstruction* const at_;
239
240 // The dex PC of `at_`.
241 const uint32_t dex_pc_;
242
243 // Whether to initialize the class.
244 const bool do_clinit_;
245
246 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100247};
248
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000249class LoadStringSlowPathARM : public SlowPathCodeARM {
250 public:
251 explicit LoadStringSlowPathARM(HLoadString* instruction) : instruction_(instruction) {}
252
Alexandre Rames67555f72014-11-18 10:55:16 +0000253 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000254 LocationSummary* locations = instruction_->GetLocations();
255 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
256
257 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
258 __ Bind(GetEntryLabel());
259 codegen->SaveLiveRegisters(locations);
260
261 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800262 arm_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
263 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction_->GetStringIndex());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000264 arm_codegen->InvokeRuntime(
265 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc());
266 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
267
268 codegen->RestoreLiveRegisters(locations);
269 __ b(GetExitLabel());
270 }
271
272 private:
273 HLoadString* const instruction_;
274
275 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM);
276};
277
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000278class TypeCheckSlowPathARM : public SlowPathCodeARM {
279 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000280 TypeCheckSlowPathARM(HInstruction* instruction,
281 Location class_to_check,
282 Location object_class,
283 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000284 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000285 class_to_check_(class_to_check),
286 object_class_(object_class),
287 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000288
Alexandre Rames67555f72014-11-18 10:55:16 +0000289 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000290 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000291 DCHECK(instruction_->IsCheckCast()
292 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000293
294 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
295 __ Bind(GetEntryLabel());
296 codegen->SaveLiveRegisters(locations);
297
298 // We're moving two locations to locations that could overlap, so we need a parallel
299 // move resolver.
300 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000301 codegen->EmitParallelMoves(
302 class_to_check_,
303 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
304 object_class_,
305 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000306
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000307 if (instruction_->IsInstanceOf()) {
308 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial), instruction_, dex_pc_);
309 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
310 } else {
311 DCHECK(instruction_->IsCheckCast());
312 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc_);
313 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000314
315 codegen->RestoreLiveRegisters(locations);
316 __ b(GetExitLabel());
317 }
318
319 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000320 HInstruction* const instruction_;
321 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000322 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000323 uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000324
325 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM);
326};
327
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000328#undef __
329
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100330#undef __
331#define __ reinterpret_cast<ArmAssembler*>(GetAssembler())->
Dave Allison20dfc792014-06-16 20:44:29 -0700332
333inline Condition ARMCondition(IfCondition cond) {
334 switch (cond) {
335 case kCondEQ: return EQ;
336 case kCondNE: return NE;
337 case kCondLT: return LT;
338 case kCondLE: return LE;
339 case kCondGT: return GT;
340 case kCondGE: return GE;
341 default:
342 LOG(FATAL) << "Unknown if condition";
343 }
344 return EQ; // Unreachable.
345}
346
347inline Condition ARMOppositeCondition(IfCondition cond) {
348 switch (cond) {
349 case kCondEQ: return NE;
350 case kCondNE: return EQ;
351 case kCondLT: return GE;
352 case kCondLE: return GT;
353 case kCondGT: return LE;
354 case kCondGE: return LT;
355 default:
356 LOG(FATAL) << "Unknown if condition";
357 }
358 return EQ; // Unreachable.
359}
360
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100361void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
362 stream << ArmManagedRegister::FromCoreRegister(Register(reg));
363}
364
365void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000366 stream << ArmManagedRegister::FromSRegister(SRegister(reg));
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100367}
368
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100369size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
370 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
371 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100372}
373
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100374size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
375 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
376 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100377}
378
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000379size_t CodeGeneratorARM::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
380 __ StoreSToOffset(static_cast<SRegister>(reg_id), SP, stack_index);
381 return kArmWordSize;
382}
383
384size_t CodeGeneratorARM::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
385 __ LoadSFromOffset(static_cast<SRegister>(reg_id), SP, stack_index);
386 return kArmWordSize;
387}
388
Calin Juravle34166012014-12-19 17:22:29 +0000389CodeGeneratorARM::CodeGeneratorARM(HGraph* graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000390 const ArmInstructionSetFeatures& isa_features,
391 const CompilerOptions& compiler_options)
392 : CodeGenerator(graph, kNumberOfCoreRegisters, kNumberOfSRegisters,
Nicolas Geoffray98893962015-01-21 12:32:32 +0000393 kNumberOfRegisterPairs, 0, 0, compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100394 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100395 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100396 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100397 move_resolver_(graph->GetArena(), this),
Calin Juravle34166012014-12-19 17:22:29 +0000398 assembler_(true),
399 isa_features_(isa_features) {}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100400
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100401size_t CodeGeneratorARM::FrameEntrySpillSize() const {
402 return kNumberOfPushedRegistersAtEntry * kArmWordSize;
403}
404
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100405Location CodeGeneratorARM::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100406 switch (type) {
407 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100408 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100409 ArmManagedRegister pair =
410 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100411 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
412 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
413
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100414 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
415 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100416 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100417 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100418 }
419
420 case Primitive::kPrimByte:
421 case Primitive::kPrimBoolean:
422 case Primitive::kPrimChar:
423 case Primitive::kPrimShort:
424 case Primitive::kPrimInt:
425 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100426 int reg = FindFreeEntry(blocked_core_registers_, kNumberOfCoreRegisters);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100427 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100428 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
429 ArmManagedRegister current =
430 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
431 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100432 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100433 }
434 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100435 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100436 }
437
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000438 case Primitive::kPrimFloat: {
439 int reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfSRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100440 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100441 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100442
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000443 case Primitive::kPrimDouble: {
Nicolas Geoffray3c035032014-10-28 10:46:40 +0000444 int reg = FindTwoFreeConsecutiveAlignedEntries(blocked_fpu_registers_, kNumberOfSRegisters);
445 DCHECK_EQ(reg % 2, 0);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000446 return Location::FpuRegisterPairLocation(reg, reg + 1);
447 }
448
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100449 case Primitive::kPrimVoid:
450 LOG(FATAL) << "Unreachable type " << type;
451 }
452
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100453 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100454}
455
Nicolas Geoffray98893962015-01-21 12:32:32 +0000456void CodeGeneratorARM::SetupBlockedRegisters(bool is_baseline ATTRIBUTE_UNUSED) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100457 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100458 blocked_register_pairs_[R1_R2] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100459
460 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100461 blocked_core_registers_[SP] = true;
462 blocked_core_registers_[LR] = true;
463 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100464
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100465 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100466 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100467
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100468 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100469 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100470
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000471 // TODO: We currently don't use Quick's callee saved registers.
472 // We always save and restore R6 and R7 to make sure we can use three
473 // register pairs for long operations.
474 blocked_core_registers_[R4] = true;
475 blocked_core_registers_[R5] = true;
476 blocked_core_registers_[R8] = true;
477 blocked_core_registers_[R10] = true;
478 blocked_core_registers_[R11] = true;
479
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000480 blocked_fpu_registers_[S16] = true;
481 blocked_fpu_registers_[S17] = true;
482 blocked_fpu_registers_[S18] = true;
483 blocked_fpu_registers_[S19] = true;
484 blocked_fpu_registers_[S20] = true;
485 blocked_fpu_registers_[S21] = true;
486 blocked_fpu_registers_[S22] = true;
487 blocked_fpu_registers_[S23] = true;
Nicolas Geoffray3c035032014-10-28 10:46:40 +0000488 blocked_fpu_registers_[S24] = true;
489 blocked_fpu_registers_[S25] = true;
490 blocked_fpu_registers_[S26] = true;
491 blocked_fpu_registers_[S27] = true;
492 blocked_fpu_registers_[S28] = true;
493 blocked_fpu_registers_[S29] = true;
494 blocked_fpu_registers_[S30] = true;
495 blocked_fpu_registers_[S31] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100496
497 UpdateBlockedPairRegisters();
498}
499
500void CodeGeneratorARM::UpdateBlockedPairRegisters() const {
501 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
502 ArmManagedRegister current =
503 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
504 if (blocked_core_registers_[current.AsRegisterPairLow()]
505 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
506 blocked_register_pairs_[i] = true;
507 }
508 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100509}
510
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100511InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
512 : HGraphVisitor(graph),
513 assembler_(codegen->GetAssembler()),
514 codegen_(codegen) {}
515
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000516void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +0000517 bool skip_overflow_check =
518 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100519 if (!skip_overflow_check) {
Calin Juravle93edf732015-01-20 20:14:07 +0000520 if (GetCompilerOptions().GetImplicitStackOverflowChecks()) {
521 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
522 __ LoadFromOffset(kLoadWord, IP, IP, 0);
523 RecordPcInfo(nullptr, 0);
524 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100525 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) StackOverflowCheckSlowPathARM();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100526 AddSlowPath(slow_path);
527
528 __ LoadFromOffset(kLoadWord, IP, TR, Thread::StackEndOffset<kArmWordSize>().Int32Value());
529 __ cmp(SP, ShifterOperand(IP));
530 __ b(slow_path->GetEntryLabel(), CC);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100531 }
532 }
533
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000534 core_spill_mask_ |= (1 << LR | 1 << R6 | 1 << R7);
535 __ PushList(1 << LR | 1 << R6 | 1 << R7);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000536
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100537 // The return PC has already been pushed on the stack.
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100538 __ AddConstant(SP, -(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kArmWordSize));
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100539 __ StoreToOffset(kStoreWord, R0, SP, 0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000540}
541
542void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100543 __ AddConstant(SP, GetFrameSize() - kNumberOfPushedRegistersAtEntry * kArmWordSize);
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000544 __ PopList(1 << PC | 1 << R6 | 1 << R7);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000545}
546
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100547void CodeGeneratorARM::Bind(HBasicBlock* block) {
548 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000549}
550
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100551Location CodeGeneratorARM::GetStackLocation(HLoadLocal* load) const {
552 switch (load->GetType()) {
553 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100554 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100555 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
556 break;
557
558 case Primitive::kPrimInt:
559 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100560 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100561 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100562
563 case Primitive::kPrimBoolean:
564 case Primitive::kPrimByte:
565 case Primitive::kPrimChar:
566 case Primitive::kPrimShort:
567 case Primitive::kPrimVoid:
568 LOG(FATAL) << "Unexpected type " << load->GetType();
569 }
570
571 LOG(FATAL) << "Unreachable";
572 return Location();
573}
574
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100575Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
576 switch (type) {
577 case Primitive::kPrimBoolean:
578 case Primitive::kPrimByte:
579 case Primitive::kPrimChar:
580 case Primitive::kPrimShort:
581 case Primitive::kPrimInt:
582 case Primitive::kPrimNot: {
583 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000584 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100585 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100586 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100587 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000588 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100589 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100590 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100591
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000592 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100593 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000594 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100595 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000596 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100597 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000598 if (calling_convention.GetRegisterAt(index) == R1) {
599 // Skip R1, and use R2_R3 instead.
600 gp_index_++;
601 index++;
602 }
603 }
604 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
605 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +0000606 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000607 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +0000608 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100609 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000610 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
611 }
612 }
613
614 case Primitive::kPrimFloat: {
615 uint32_t stack_index = stack_index_++;
616 if (float_index_ % 2 == 0) {
617 float_index_ = std::max(double_index_, float_index_);
618 }
619 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
620 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
621 } else {
622 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
623 }
624 }
625
626 case Primitive::kPrimDouble: {
627 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
628 uint32_t stack_index = stack_index_;
629 stack_index_ += 2;
630 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
631 uint32_t index = double_index_;
632 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000633 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000634 calling_convention.GetFpuRegisterAt(index),
635 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000636 DCHECK(ExpectedPairLayout(result));
637 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000638 } else {
639 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100640 }
641 }
642
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100643 case Primitive::kPrimVoid:
644 LOG(FATAL) << "Unexpected parameter type " << type;
645 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100646 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100647 return Location();
648}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100649
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000650Location InvokeDexCallingConventionVisitor::GetReturnLocation(Primitive::Type type) {
651 switch (type) {
652 case Primitive::kPrimBoolean:
653 case Primitive::kPrimByte:
654 case Primitive::kPrimChar:
655 case Primitive::kPrimShort:
656 case Primitive::kPrimInt:
657 case Primitive::kPrimNot: {
658 return Location::RegisterLocation(R0);
659 }
660
661 case Primitive::kPrimFloat: {
662 return Location::FpuRegisterLocation(S0);
663 }
664
665 case Primitive::kPrimLong: {
666 return Location::RegisterPairLocation(R0, R1);
667 }
668
669 case Primitive::kPrimDouble: {
670 return Location::FpuRegisterPairLocation(S0, S1);
671 }
672
673 case Primitive::kPrimVoid:
674 return Location();
675 }
676 UNREACHABLE();
677 return Location();
678}
679
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100680void CodeGeneratorARM::Move32(Location destination, Location source) {
681 if (source.Equals(destination)) {
682 return;
683 }
684 if (destination.IsRegister()) {
685 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000686 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100687 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000688 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100689 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000690 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100691 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100692 } else if (destination.IsFpuRegister()) {
693 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000694 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100695 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000696 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100697 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000698 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100699 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100700 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +0000701 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100702 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000703 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100704 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000705 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100706 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +0000707 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100708 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
709 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100710 }
711 }
712}
713
714void CodeGeneratorARM::Move64(Location destination, Location source) {
715 if (source.Equals(destination)) {
716 return;
717 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100718 if (destination.IsRegisterPair()) {
719 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000720 EmitParallelMoves(
721 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
722 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
723 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
724 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100725 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000726 UNIMPLEMENTED(FATAL);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100727 } else {
728 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000729 DCHECK(ExpectedPairLayout(destination));
730 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
731 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100732 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000733 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100734 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000735 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
736 SP,
737 source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100738 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000739 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100740 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100741 } else {
742 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100743 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000744 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100745 if (source.AsRegisterPairLow<Register>() == R1) {
746 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100747 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
748 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100749 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100750 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100751 SP, destination.GetStackIndex());
752 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000753 } else if (source.IsFpuRegisterPair()) {
754 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
755 SP,
756 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100757 } else {
758 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000759 EmitParallelMoves(
760 Location::StackSlot(source.GetStackIndex()),
761 Location::StackSlot(destination.GetStackIndex()),
762 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
763 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100764 }
765 }
766}
767
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100768void CodeGeneratorARM::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100769 LocationSummary* locations = instruction->GetLocations();
770 if (locations != nullptr && locations->Out().Equals(location)) {
771 return;
772 }
773
Calin Juravlea21f5982014-11-13 15:53:04 +0000774 if (locations != nullptr && locations->Out().IsConstant()) {
775 HConstant* const_to_move = locations->Out().GetConstant();
776 if (const_to_move->IsIntConstant()) {
777 int32_t value = const_to_move->AsIntConstant()->GetValue();
778 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000779 __ LoadImmediate(location.AsRegister<Register>(), value);
Calin Juravlea21f5982014-11-13 15:53:04 +0000780 } else {
781 DCHECK(location.IsStackSlot());
782 __ LoadImmediate(IP, value);
783 __ StoreToOffset(kStoreWord, IP, SP, location.GetStackIndex());
784 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000785 } else {
Nicolas Geoffray3747b482015-01-19 17:17:16 +0000786 DCHECK(const_to_move->IsLongConstant()) << const_to_move->DebugName();
Calin Juravlea21f5982014-11-13 15:53:04 +0000787 int64_t value = const_to_move->AsLongConstant()->GetValue();
788 if (location.IsRegisterPair()) {
789 __ LoadImmediate(location.AsRegisterPairLow<Register>(), Low32Bits(value));
790 __ LoadImmediate(location.AsRegisterPairHigh<Register>(), High32Bits(value));
791 } else {
792 DCHECK(location.IsDoubleStackSlot());
793 __ LoadImmediate(IP, Low32Bits(value));
794 __ StoreToOffset(kStoreWord, IP, SP, location.GetStackIndex());
795 __ LoadImmediate(IP, High32Bits(value));
796 __ StoreToOffset(kStoreWord, IP, SP, location.GetHighStackIndex(kArmWordSize));
797 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100798 }
Roland Levillain476df552014-10-09 17:51:36 +0100799 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100800 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
801 switch (instruction->GetType()) {
802 case Primitive::kPrimBoolean:
803 case Primitive::kPrimByte:
804 case Primitive::kPrimChar:
805 case Primitive::kPrimShort:
806 case Primitive::kPrimInt:
807 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100808 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100809 Move32(location, Location::StackSlot(stack_slot));
810 break;
811
812 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100813 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100814 Move64(location, Location::DoubleStackSlot(stack_slot));
815 break;
816
817 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100818 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100819 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000820 } else if (instruction->IsTemporary()) {
821 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000822 if (temp_location.IsStackSlot()) {
823 Move32(location, temp_location);
824 } else {
825 DCHECK(temp_location.IsDoubleStackSlot());
826 Move64(location, temp_location);
827 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000828 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100829 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100830 switch (instruction->GetType()) {
831 case Primitive::kPrimBoolean:
832 case Primitive::kPrimByte:
833 case Primitive::kPrimChar:
834 case Primitive::kPrimShort:
835 case Primitive::kPrimNot:
836 case Primitive::kPrimInt:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100837 case Primitive::kPrimFloat:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100838 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100839 break;
840
841 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100842 case Primitive::kPrimDouble:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100843 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100844 break;
845
846 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100847 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100848 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000849 }
850}
851
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100852void CodeGeneratorARM::InvokeRuntime(int32_t entry_point_offset,
853 HInstruction* instruction,
854 uint32_t dex_pc) {
855 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
856 __ blx(LR);
857 RecordPcInfo(instruction, dex_pc);
858 DCHECK(instruction->IsSuspendCheck()
859 || instruction->IsBoundsCheck()
860 || instruction->IsNullCheck()
Calin Juravled0d48522014-11-04 16:40:20 +0000861 || instruction->IsDivZeroCheck()
Roland Levillain624279f2014-12-04 11:54:28 +0000862 || instruction->GetLocations()->CanCall()
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100863 || !IsLeafMethod());
864}
865
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000866void LocationsBuilderARM::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000867 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000868}
869
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000870void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000871 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100872 DCHECK(!successor->IsExitBlock());
873
874 HBasicBlock* block = got->GetBlock();
875 HInstruction* previous = got->GetPrevious();
876
877 HLoopInformation* info = block->GetLoopInformation();
878 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
879 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
880 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
881 return;
882 }
883
884 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
885 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
886 }
887 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000888 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000889 }
890}
891
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000892void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000893 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000894}
895
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000896void InstructionCodeGeneratorARM::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700897 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000898 if (kIsDebugBuild) {
899 __ Comment("Unreachable");
900 __ bkpt(0);
901 }
902}
903
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000904void LocationsBuilderARM::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100905 LocationSummary* locations =
906 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100907 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100908 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100909 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100910 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000911}
912
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000913void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700914 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100915 if (cond->IsIntConstant()) {
916 // Constant condition, statically compared against 1.
917 int32_t cond_value = cond->AsIntConstant()->GetValue();
918 if (cond_value == 1) {
919 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
920 if_instr->IfTrueSuccessor())) {
921 __ b(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100922 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100923 return;
924 } else {
925 DCHECK_EQ(cond_value, 0);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100926 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100927 } else {
928 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
929 // Condition has been materialized, compare the output to 0
930 DCHECK(if_instr->GetLocations()->InAt(0).IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000931 __ cmp(if_instr->GetLocations()->InAt(0).AsRegister<Register>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100932 ShifterOperand(0));
933 __ b(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()), NE);
934 } else {
935 // Condition has not been materialized, use its inputs as the
936 // comparison and its condition as the branch condition.
937 LocationSummary* locations = cond->GetLocations();
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000938 DCHECK(locations->InAt(0).IsRegister()) << locations->InAt(0);
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +0000939 Register left = locations->InAt(0).AsRegister<Register>();
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100940 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +0000941 __ cmp(left, ShifterOperand(locations->InAt(1).AsRegister<Register>()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100942 } else {
943 DCHECK(locations->InAt(1).IsConstant());
944 int32_t value =
945 locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
946 ShifterOperand operand;
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +0000947 if (GetAssembler()->ShifterOperandCanHold(R0, left, CMP, value, &operand)) {
948 __ cmp(left, operand);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100949 } else {
950 Register temp = IP;
951 __ LoadImmediate(temp, value);
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +0000952 __ cmp(left, ShifterOperand(temp));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100953 }
954 }
955 __ b(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()),
956 ARMCondition(cond->AsCondition()->GetCondition()));
957 }
Dave Allison20dfc792014-06-16 20:44:29 -0700958 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100959 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
960 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700961 __ b(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000962 }
963}
964
Dave Allison20dfc792014-06-16 20:44:29 -0700965
966void LocationsBuilderARM::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100967 LocationSummary* locations =
968 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100969 locations->SetInAt(0, Location::RequiresRegister());
970 locations->SetInAt(1, Location::RegisterOrConstant(comp->InputAt(1)));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100971 if (comp->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100972 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100973 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000974}
975
Dave Allison20dfc792014-06-16 20:44:29 -0700976void InstructionCodeGeneratorARM::VisitCondition(HCondition* comp) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100977 if (!comp->NeedsMaterialization()) return;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100978 LocationSummary* locations = comp->GetLocations();
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +0000979 Register left = locations->InAt(0).AsRegister<Register>();
980
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100981 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +0000982 __ cmp(left, ShifterOperand(locations->InAt(1).AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100983 } else {
984 DCHECK(locations->InAt(1).IsConstant());
985 int32_t value = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
986 ShifterOperand operand;
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +0000987 if (GetAssembler()->ShifterOperandCanHold(R0, left, CMP, value, &operand)) {
988 __ cmp(left, operand);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100989 } else {
990 Register temp = IP;
991 __ LoadImmediate(temp, value);
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +0000992 __ cmp(left, ShifterOperand(temp));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100993 }
Dave Allison20dfc792014-06-16 20:44:29 -0700994 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100995 __ it(ARMCondition(comp->GetCondition()), kItElse);
Roland Levillain271ab9c2014-11-27 15:23:57 +0000996 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(1),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100997 ARMCondition(comp->GetCondition()));
Roland Levillain271ab9c2014-11-27 15:23:57 +0000998 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(0),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100999 ARMOppositeCondition(comp->GetCondition()));
Dave Allison20dfc792014-06-16 20:44:29 -07001000}
1001
1002void LocationsBuilderARM::VisitEqual(HEqual* comp) {
1003 VisitCondition(comp);
1004}
1005
1006void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
1007 VisitCondition(comp);
1008}
1009
1010void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
1011 VisitCondition(comp);
1012}
1013
1014void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
1015 VisitCondition(comp);
1016}
1017
1018void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
1019 VisitCondition(comp);
1020}
1021
1022void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
1023 VisitCondition(comp);
1024}
1025
1026void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1027 VisitCondition(comp);
1028}
1029
1030void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1031 VisitCondition(comp);
1032}
1033
1034void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
1035 VisitCondition(comp);
1036}
1037
1038void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
1039 VisitCondition(comp);
1040}
1041
1042void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1043 VisitCondition(comp);
1044}
1045
1046void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1047 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001048}
1049
1050void LocationsBuilderARM::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001051 local->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001052}
1053
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001054void InstructionCodeGeneratorARM::VisitLocal(HLocal* local) {
1055 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001056}
1057
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001058void LocationsBuilderARM::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001059 load->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001060}
1061
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001062void InstructionCodeGeneratorARM::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001063 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001064 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001065}
1066
1067void LocationsBuilderARM::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001068 LocationSummary* locations =
1069 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001070 switch (store->InputAt(1)->GetType()) {
1071 case Primitive::kPrimBoolean:
1072 case Primitive::kPrimByte:
1073 case Primitive::kPrimChar:
1074 case Primitive::kPrimShort:
1075 case Primitive::kPrimInt:
1076 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001077 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001078 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1079 break;
1080
1081 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001082 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001083 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1084 break;
1085
1086 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001087 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001088 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001089}
1090
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001091void InstructionCodeGeneratorARM::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001092 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001093}
1094
1095void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001096 LocationSummary* locations =
1097 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001098 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001099}
1100
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001101void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001102 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001103 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001104}
1105
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001106void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001107 LocationSummary* locations =
1108 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001109 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001110}
1111
1112void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant) {
1113 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001114 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001115}
1116
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001117void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
1118 LocationSummary* locations =
1119 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1120 locations->SetOut(Location::ConstantLocation(constant));
1121}
1122
1123void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant) {
1124 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001125 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001126}
1127
1128void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
1129 LocationSummary* locations =
1130 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1131 locations->SetOut(Location::ConstantLocation(constant));
1132}
1133
1134void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant) {
1135 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001136 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001137}
1138
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001139void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001140 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001141}
1142
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001143void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001144 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001145 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001146}
1147
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001148void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001149 LocationSummary* locations =
1150 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001151 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001152}
1153
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001154void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001155 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001156 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001157}
1158
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001159void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001160 HandleInvoke(invoke);
1161}
1162
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001163void CodeGeneratorARM::LoadCurrentMethod(Register reg) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001164 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001165}
1166
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001167void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001168 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001169
1170 // TODO: Implement all kinds of calls:
1171 // 1) boot -> boot
1172 // 2) app -> boot
1173 // 3) app -> app
1174 //
1175 // Currently we implement the app -> app logic, which looks up in the resolve cache.
1176
1177 // temp = method;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001178 codegen_->LoadCurrentMethod(temp);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001179 // temp = temp->dex_cache_resolved_methods_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001180 __ LoadFromOffset(
1181 kLoadWord, temp, temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value());
Nicolas Geoffray4e44c822014-12-17 12:25:12 +00001182 // temp = temp[index_in_cache]
1183 __ LoadFromOffset(
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001184 kLoadWord, temp, temp, CodeGenerator::GetCacheOffset(invoke->GetDexMethodIndex()));
Nicolas Geoffray4e44c822014-12-17 12:25:12 +00001185 // LR = temp[offset_of_quick_compiled_code]
1186 __ LoadFromOffset(kLoadWord, LR, temp,
1187 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
1188 kArmWordSize).Int32Value());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001189 // LR()
1190 __ blx(LR);
1191
1192 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1193 DCHECK(!codegen_->IsLeafMethod());
1194}
1195
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001196void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001197 LocationSummary* locations =
1198 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001199 locations->AddTemp(Location::RegisterLocation(R0));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001200
1201 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001202 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001203 HInstruction* input = invoke->InputAt(i);
1204 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1205 }
1206
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001207 locations->SetOut(calling_convention_visitor.GetReturnLocation(invoke->GetType()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001208}
1209
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001210void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1211 HandleInvoke(invoke);
1212}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001213
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001214void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001215 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001216 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
1217 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1218 LocationSummary* locations = invoke->GetLocations();
1219 Location receiver = locations->InAt(0);
1220 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1221 // temp = object->GetClass();
1222 if (receiver.IsStackSlot()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001223 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
1224 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001225 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001226 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001227 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001228 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001229 // temp = temp->GetMethodAt(method_offset);
Mathieu Chartier2d721012014-11-10 11:08:06 -08001230 uint32_t entry_point = mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001231 kArmWordSize).Int32Value();
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001232 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001233 // LR = temp->GetEntryPoint();
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001234 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001235 // LR();
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001236 __ blx(LR);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001237 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001238 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001239}
1240
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001241void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1242 HandleInvoke(invoke);
1243 // Add the hidden argument.
1244 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
1245}
1246
1247void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1248 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001249 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001250 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1251 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1252 LocationSummary* locations = invoke->GetLocations();
1253 Location receiver = locations->InAt(0);
1254 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1255
1256 // Set the hidden argument.
Roland Levillain199f3362014-11-27 17:15:16 +00001257 __ LoadImmediate(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
1258 invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001259
1260 // temp = object->GetClass();
1261 if (receiver.IsStackSlot()) {
1262 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
1263 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
1264 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001265 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001266 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001267 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001268 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartier2d721012014-11-10 11:08:06 -08001269 uint32_t entry_point = mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001270 kArmWordSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001271 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
1272 // LR = temp->GetEntryPoint();
1273 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
1274 // LR();
1275 __ blx(LR);
1276 DCHECK(!codegen_->IsLeafMethod());
1277 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1278}
1279
Roland Levillain88cb1752014-10-20 16:36:47 +01001280void LocationsBuilderARM::VisitNeg(HNeg* neg) {
1281 LocationSummary* locations =
1282 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1283 switch (neg->GetResultType()) {
1284 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001285 case Primitive::kPrimLong: {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001286 Location::OutputOverlap output_overlaps = (neg->GetResultType() == Primitive::kPrimLong)
1287 ? Location::kOutputOverlap
1288 : Location::kNoOutputOverlap;
Roland Levillain88cb1752014-10-20 16:36:47 +01001289 locations->SetInAt(0, Location::RequiresRegister());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001290 locations->SetOut(Location::RequiresRegister(), output_overlaps);
Roland Levillain88cb1752014-10-20 16:36:47 +01001291 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001292 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001293
Roland Levillain88cb1752014-10-20 16:36:47 +01001294 case Primitive::kPrimFloat:
1295 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001296 locations->SetInAt(0, Location::RequiresFpuRegister());
1297 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01001298 break;
1299
1300 default:
1301 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1302 }
1303}
1304
1305void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
1306 LocationSummary* locations = neg->GetLocations();
1307 Location out = locations->Out();
1308 Location in = locations->InAt(0);
1309 switch (neg->GetResultType()) {
1310 case Primitive::kPrimInt:
1311 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001312 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01001313 break;
1314
1315 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001316 DCHECK(in.IsRegisterPair());
1317 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
1318 __ rsbs(out.AsRegisterPairLow<Register>(),
1319 in.AsRegisterPairLow<Register>(),
1320 ShifterOperand(0));
1321 // We cannot emit an RSC (Reverse Subtract with Carry)
1322 // instruction here, as it does not exist in the Thumb-2
1323 // instruction set. We use the following approach
1324 // using SBC and SUB instead.
1325 //
1326 // out.hi = -C
1327 __ sbc(out.AsRegisterPairHigh<Register>(),
1328 out.AsRegisterPairHigh<Register>(),
1329 ShifterOperand(out.AsRegisterPairHigh<Register>()));
1330 // out.hi = out.hi - in.hi
1331 __ sub(out.AsRegisterPairHigh<Register>(),
1332 out.AsRegisterPairHigh<Register>(),
1333 ShifterOperand(in.AsRegisterPairHigh<Register>()));
1334 break;
1335
Roland Levillain88cb1752014-10-20 16:36:47 +01001336 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001337 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001338 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001339 break;
1340
Roland Levillain88cb1752014-10-20 16:36:47 +01001341 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001342 DCHECK(in.IsFpuRegisterPair());
1343 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
1344 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01001345 break;
1346
1347 default:
1348 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1349 }
1350}
1351
Roland Levillaindff1f282014-11-05 14:15:05 +00001352void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001353 Primitive::Type result_type = conversion->GetResultType();
1354 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001355 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001356
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001357 // The float-to-long and double-to-long type conversions rely on a
1358 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001359 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001360 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1361 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001362 ? LocationSummary::kCall
1363 : LocationSummary::kNoCall;
1364 LocationSummary* locations =
1365 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1366
Roland Levillaindff1f282014-11-05 14:15:05 +00001367 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001368 case Primitive::kPrimByte:
1369 switch (input_type) {
1370 case Primitive::kPrimShort:
1371 case Primitive::kPrimInt:
1372 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001373 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001374 locations->SetInAt(0, Location::RequiresRegister());
1375 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1376 break;
1377
1378 default:
1379 LOG(FATAL) << "Unexpected type conversion from " << input_type
1380 << " to " << result_type;
1381 }
1382 break;
1383
Roland Levillain01a8d712014-11-14 16:27:39 +00001384 case Primitive::kPrimShort:
1385 switch (input_type) {
1386 case Primitive::kPrimByte:
1387 case Primitive::kPrimInt:
1388 case Primitive::kPrimChar:
1389 // Processing a Dex `int-to-short' instruction.
1390 locations->SetInAt(0, Location::RequiresRegister());
1391 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1392 break;
1393
1394 default:
1395 LOG(FATAL) << "Unexpected type conversion from " << input_type
1396 << " to " << result_type;
1397 }
1398 break;
1399
Roland Levillain946e1432014-11-11 17:35:19 +00001400 case Primitive::kPrimInt:
1401 switch (input_type) {
1402 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001403 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001404 locations->SetInAt(0, Location::Any());
1405 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1406 break;
1407
1408 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001409 // Processing a Dex `float-to-int' instruction.
1410 locations->SetInAt(0, Location::RequiresFpuRegister());
1411 locations->SetOut(Location::RequiresRegister());
1412 locations->AddTemp(Location::RequiresFpuRegister());
1413 break;
1414
Roland Levillain946e1432014-11-11 17:35:19 +00001415 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001416 // Processing a Dex `double-to-int' instruction.
1417 locations->SetInAt(0, Location::RequiresFpuRegister());
1418 locations->SetOut(Location::RequiresRegister());
1419 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001420 break;
1421
1422 default:
1423 LOG(FATAL) << "Unexpected type conversion from " << input_type
1424 << " to " << result_type;
1425 }
1426 break;
1427
Roland Levillaindff1f282014-11-05 14:15:05 +00001428 case Primitive::kPrimLong:
1429 switch (input_type) {
1430 case Primitive::kPrimByte:
1431 case Primitive::kPrimShort:
1432 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001433 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001434 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001435 locations->SetInAt(0, Location::RequiresRegister());
1436 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1437 break;
1438
Roland Levillain624279f2014-12-04 11:54:28 +00001439 case Primitive::kPrimFloat: {
1440 // Processing a Dex `float-to-long' instruction.
1441 InvokeRuntimeCallingConvention calling_convention;
1442 locations->SetInAt(0, Location::FpuRegisterLocation(
1443 calling_convention.GetFpuRegisterAt(0)));
1444 locations->SetOut(Location::RegisterPairLocation(R0, R1));
1445 break;
1446 }
1447
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001448 case Primitive::kPrimDouble: {
1449 // Processing a Dex `double-to-long' instruction.
1450 InvokeRuntimeCallingConvention calling_convention;
1451 locations->SetInAt(0, Location::FpuRegisterPairLocation(
1452 calling_convention.GetFpuRegisterAt(0),
1453 calling_convention.GetFpuRegisterAt(1)));
1454 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00001455 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001456 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001457
1458 default:
1459 LOG(FATAL) << "Unexpected type conversion from " << input_type
1460 << " to " << result_type;
1461 }
1462 break;
1463
Roland Levillain981e4542014-11-14 11:47:14 +00001464 case Primitive::kPrimChar:
1465 switch (input_type) {
1466 case Primitive::kPrimByte:
1467 case Primitive::kPrimShort:
1468 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001469 // Processing a Dex `int-to-char' instruction.
1470 locations->SetInAt(0, Location::RequiresRegister());
1471 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1472 break;
1473
1474 default:
1475 LOG(FATAL) << "Unexpected type conversion from " << input_type
1476 << " to " << result_type;
1477 }
1478 break;
1479
Roland Levillaindff1f282014-11-05 14:15:05 +00001480 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001481 switch (input_type) {
1482 case Primitive::kPrimByte:
1483 case Primitive::kPrimShort:
1484 case Primitive::kPrimInt:
1485 case Primitive::kPrimChar:
1486 // Processing a Dex `int-to-float' instruction.
1487 locations->SetInAt(0, Location::RequiresRegister());
1488 locations->SetOut(Location::RequiresFpuRegister());
1489 break;
1490
1491 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001492 // Processing a Dex `long-to-float' instruction.
1493 locations->SetInAt(0, Location::RequiresRegister());
1494 locations->SetOut(Location::RequiresFpuRegister());
1495 locations->AddTemp(Location::RequiresRegister());
1496 locations->AddTemp(Location::RequiresRegister());
1497 locations->AddTemp(Location::RequiresFpuRegister());
1498 locations->AddTemp(Location::RequiresFpuRegister());
1499 break;
1500
Roland Levillaincff13742014-11-17 14:32:17 +00001501 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001502 // Processing a Dex `double-to-float' instruction.
1503 locations->SetInAt(0, Location::RequiresFpuRegister());
1504 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001505 break;
1506
1507 default:
1508 LOG(FATAL) << "Unexpected type conversion from " << input_type
1509 << " to " << result_type;
1510 };
1511 break;
1512
Roland Levillaindff1f282014-11-05 14:15:05 +00001513 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001514 switch (input_type) {
1515 case Primitive::kPrimByte:
1516 case Primitive::kPrimShort:
1517 case Primitive::kPrimInt:
1518 case Primitive::kPrimChar:
1519 // Processing a Dex `int-to-double' instruction.
1520 locations->SetInAt(0, Location::RequiresRegister());
1521 locations->SetOut(Location::RequiresFpuRegister());
1522 break;
1523
1524 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001525 // Processing a Dex `long-to-double' instruction.
1526 locations->SetInAt(0, Location::RequiresRegister());
1527 locations->SetOut(Location::RequiresFpuRegister());
1528 locations->AddTemp(Location::RequiresRegister());
1529 locations->AddTemp(Location::RequiresRegister());
1530 locations->AddTemp(Location::RequiresFpuRegister());
1531 break;
1532
Roland Levillaincff13742014-11-17 14:32:17 +00001533 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001534 // Processing a Dex `float-to-double' instruction.
1535 locations->SetInAt(0, Location::RequiresFpuRegister());
1536 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001537 break;
1538
1539 default:
1540 LOG(FATAL) << "Unexpected type conversion from " << input_type
1541 << " to " << result_type;
1542 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001543 break;
1544
1545 default:
1546 LOG(FATAL) << "Unexpected type conversion from " << input_type
1547 << " to " << result_type;
1548 }
1549}
1550
1551void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
1552 LocationSummary* locations = conversion->GetLocations();
1553 Location out = locations->Out();
1554 Location in = locations->InAt(0);
1555 Primitive::Type result_type = conversion->GetResultType();
1556 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001557 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001558 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001559 case Primitive::kPrimByte:
1560 switch (input_type) {
1561 case Primitive::kPrimShort:
1562 case Primitive::kPrimInt:
1563 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001564 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001565 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001566 break;
1567
1568 default:
1569 LOG(FATAL) << "Unexpected type conversion from " << input_type
1570 << " to " << result_type;
1571 }
1572 break;
1573
Roland Levillain01a8d712014-11-14 16:27:39 +00001574 case Primitive::kPrimShort:
1575 switch (input_type) {
1576 case Primitive::kPrimByte:
1577 case Primitive::kPrimInt:
1578 case Primitive::kPrimChar:
1579 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001580 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00001581 break;
1582
1583 default:
1584 LOG(FATAL) << "Unexpected type conversion from " << input_type
1585 << " to " << result_type;
1586 }
1587 break;
1588
Roland Levillain946e1432014-11-11 17:35:19 +00001589 case Primitive::kPrimInt:
1590 switch (input_type) {
1591 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001592 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001593 DCHECK(out.IsRegister());
1594 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001595 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001596 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001597 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00001598 } else {
1599 DCHECK(in.IsConstant());
1600 DCHECK(in.GetConstant()->IsLongConstant());
1601 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001602 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00001603 }
1604 break;
1605
Roland Levillain3f8f9362014-12-02 17:45:01 +00001606 case Primitive::kPrimFloat: {
1607 // Processing a Dex `float-to-int' instruction.
1608 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
1609 __ vmovs(temp, in.AsFpuRegister<SRegister>());
1610 __ vcvtis(temp, temp);
1611 __ vmovrs(out.AsRegister<Register>(), temp);
1612 break;
1613 }
1614
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001615 case Primitive::kPrimDouble: {
1616 // Processing a Dex `double-to-int' instruction.
1617 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
1618 DRegister temp_d = FromLowSToD(temp_s);
1619 __ vmovd(temp_d, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
1620 __ vcvtid(temp_s, temp_d);
1621 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00001622 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001623 }
Roland Levillain946e1432014-11-11 17:35:19 +00001624
1625 default:
1626 LOG(FATAL) << "Unexpected type conversion from " << input_type
1627 << " to " << result_type;
1628 }
1629 break;
1630
Roland Levillaindff1f282014-11-05 14:15:05 +00001631 case Primitive::kPrimLong:
1632 switch (input_type) {
1633 case Primitive::kPrimByte:
1634 case Primitive::kPrimShort:
1635 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001636 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001637 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001638 DCHECK(out.IsRegisterPair());
1639 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001640 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00001641 // Sign extension.
1642 __ Asr(out.AsRegisterPairHigh<Register>(),
1643 out.AsRegisterPairLow<Register>(),
1644 31);
1645 break;
1646
1647 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001648 // Processing a Dex `float-to-long' instruction.
Roland Levillain624279f2014-12-04 11:54:28 +00001649 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
1650 conversion,
1651 conversion->GetDexPc());
1652 break;
1653
Roland Levillaindff1f282014-11-05 14:15:05 +00001654 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001655 // Processing a Dex `double-to-long' instruction.
1656 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
1657 conversion,
1658 conversion->GetDexPc());
Roland Levillaindff1f282014-11-05 14:15:05 +00001659 break;
1660
1661 default:
1662 LOG(FATAL) << "Unexpected type conversion from " << input_type
1663 << " to " << result_type;
1664 }
1665 break;
1666
Roland Levillain981e4542014-11-14 11:47:14 +00001667 case Primitive::kPrimChar:
1668 switch (input_type) {
1669 case Primitive::kPrimByte:
1670 case Primitive::kPrimShort:
1671 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001672 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001673 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00001674 break;
1675
1676 default:
1677 LOG(FATAL) << "Unexpected type conversion from " << input_type
1678 << " to " << result_type;
1679 }
1680 break;
1681
Roland Levillaindff1f282014-11-05 14:15:05 +00001682 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001683 switch (input_type) {
1684 case Primitive::kPrimByte:
1685 case Primitive::kPrimShort:
1686 case Primitive::kPrimInt:
1687 case Primitive::kPrimChar: {
1688 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001689 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
1690 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001691 break;
1692 }
1693
Roland Levillain6d0e4832014-11-27 18:31:21 +00001694 case Primitive::kPrimLong: {
1695 // Processing a Dex `long-to-float' instruction.
1696 Register low = in.AsRegisterPairLow<Register>();
1697 Register high = in.AsRegisterPairHigh<Register>();
1698 SRegister output = out.AsFpuRegister<SRegister>();
1699 Register constant_low = locations->GetTemp(0).AsRegister<Register>();
1700 Register constant_high = locations->GetTemp(1).AsRegister<Register>();
1701 SRegister temp1_s = locations->GetTemp(2).AsFpuRegisterPairLow<SRegister>();
1702 DRegister temp1_d = FromLowSToD(temp1_s);
1703 SRegister temp2_s = locations->GetTemp(3).AsFpuRegisterPairLow<SRegister>();
1704 DRegister temp2_d = FromLowSToD(temp2_s);
1705
1706 // Operations use doubles for precision reasons (each 32-bit
1707 // half of a long fits in the 53-bit mantissa of a double,
1708 // but not in the 24-bit mantissa of a float). This is
1709 // especially important for the low bits. The result is
1710 // eventually converted to float.
1711
1712 // temp1_d = int-to-double(high)
1713 __ vmovsr(temp1_s, high);
1714 __ vcvtdi(temp1_d, temp1_s);
1715 // Using vmovd to load the `k2Pow32EncodingForDouble` constant
1716 // as an immediate value into `temp2_d` does not work, as
1717 // this instruction only transfers 8 significant bits of its
1718 // immediate operand. Instead, use two 32-bit core
1719 // registers to load `k2Pow32EncodingForDouble` into
1720 // `temp2_d`.
1721 __ LoadImmediate(constant_low, Low32Bits(k2Pow32EncodingForDouble));
1722 __ LoadImmediate(constant_high, High32Bits(k2Pow32EncodingForDouble));
1723 __ vmovdrr(temp2_d, constant_low, constant_high);
1724 // temp1_d = temp1_d * 2^32
1725 __ vmuld(temp1_d, temp1_d, temp2_d);
1726 // temp2_d = unsigned-to-double(low)
1727 __ vmovsr(temp2_s, low);
1728 __ vcvtdu(temp2_d, temp2_s);
1729 // temp1_d = temp1_d + temp2_d
1730 __ vaddd(temp1_d, temp1_d, temp2_d);
1731 // output = double-to-float(temp1_d);
1732 __ vcvtsd(output, temp1_d);
1733 break;
1734 }
1735
Roland Levillaincff13742014-11-17 14:32:17 +00001736 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001737 // Processing a Dex `double-to-float' instruction.
1738 __ vcvtsd(out.AsFpuRegister<SRegister>(),
1739 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00001740 break;
1741
1742 default:
1743 LOG(FATAL) << "Unexpected type conversion from " << input_type
1744 << " to " << result_type;
1745 };
1746 break;
1747
Roland Levillaindff1f282014-11-05 14:15:05 +00001748 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001749 switch (input_type) {
1750 case Primitive::kPrimByte:
1751 case Primitive::kPrimShort:
1752 case Primitive::kPrimInt:
1753 case Primitive::kPrimChar: {
1754 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001755 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001756 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
1757 out.AsFpuRegisterPairLow<SRegister>());
1758 break;
1759 }
1760
Roland Levillain647b9ed2014-11-27 12:06:00 +00001761 case Primitive::kPrimLong: {
1762 // Processing a Dex `long-to-double' instruction.
1763 Register low = in.AsRegisterPairLow<Register>();
1764 Register high = in.AsRegisterPairHigh<Register>();
1765 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
1766 DRegister out_d = FromLowSToD(out_s);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001767 Register constant_low = locations->GetTemp(0).AsRegister<Register>();
1768 Register constant_high = locations->GetTemp(1).AsRegister<Register>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00001769 SRegister temp_s = locations->GetTemp(2).AsFpuRegisterPairLow<SRegister>();
1770 DRegister temp_d = FromLowSToD(temp_s);
1771
Roland Levillain647b9ed2014-11-27 12:06:00 +00001772 // out_d = int-to-double(high)
1773 __ vmovsr(out_s, high);
1774 __ vcvtdi(out_d, out_s);
Roland Levillain6d0e4832014-11-27 18:31:21 +00001775 // Using vmovd to load the `k2Pow32EncodingForDouble` constant
1776 // as an immediate value into `temp_d` does not work, as
1777 // this instruction only transfers 8 significant bits of its
1778 // immediate operand. Instead, use two 32-bit core
1779 // registers to load `k2Pow32EncodingForDouble` into `temp_d`.
1780 __ LoadImmediate(constant_low, Low32Bits(k2Pow32EncodingForDouble));
1781 __ LoadImmediate(constant_high, High32Bits(k2Pow32EncodingForDouble));
Roland Levillain647b9ed2014-11-27 12:06:00 +00001782 __ vmovdrr(temp_d, constant_low, constant_high);
1783 // out_d = out_d * 2^32
1784 __ vmuld(out_d, out_d, temp_d);
1785 // temp_d = unsigned-to-double(low)
1786 __ vmovsr(temp_s, low);
1787 __ vcvtdu(temp_d, temp_s);
1788 // out_d = out_d + temp_d
1789 __ vaddd(out_d, out_d, temp_d);
1790 break;
1791 }
1792
Roland Levillaincff13742014-11-17 14:32:17 +00001793 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001794 // Processing a Dex `float-to-double' instruction.
1795 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
1796 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001797 break;
1798
1799 default:
1800 LOG(FATAL) << "Unexpected type conversion from " << input_type
1801 << " to " << result_type;
1802 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001803 break;
1804
1805 default:
1806 LOG(FATAL) << "Unexpected type conversion from " << input_type
1807 << " to " << result_type;
1808 }
1809}
1810
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001811void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001812 LocationSummary* locations =
1813 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001814 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001815 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001816 locations->SetInAt(0, Location::RequiresRegister());
1817 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001818 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1819 break;
1820 }
1821
1822 case Primitive::kPrimLong: {
1823 locations->SetInAt(0, Location::RequiresRegister());
1824 locations->SetInAt(1, Location::RequiresRegister());
1825 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001826 break;
1827 }
1828
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001829 case Primitive::kPrimFloat:
1830 case Primitive::kPrimDouble: {
1831 locations->SetInAt(0, Location::RequiresFpuRegister());
1832 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00001833 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001834 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001835 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001836
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001837 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001838 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001839 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001840}
1841
1842void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
1843 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001844 Location out = locations->Out();
1845 Location first = locations->InAt(0);
1846 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001847 switch (add->GetResultType()) {
1848 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001849 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00001850 __ add(out.AsRegister<Register>(),
1851 first.AsRegister<Register>(),
1852 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001853 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001854 __ AddConstant(out.AsRegister<Register>(),
1855 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001856 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001857 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001858 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001859
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001860 case Primitive::kPrimLong: {
1861 DCHECK(second.IsRegisterPair());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001862 __ adds(out.AsRegisterPairLow<Register>(),
1863 first.AsRegisterPairLow<Register>(),
1864 ShifterOperand(second.AsRegisterPairLow<Register>()));
1865 __ adc(out.AsRegisterPairHigh<Register>(),
1866 first.AsRegisterPairHigh<Register>(),
1867 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001868 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001869 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001870
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001871 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00001872 __ vadds(out.AsFpuRegister<SRegister>(),
1873 first.AsFpuRegister<SRegister>(),
1874 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001875 break;
1876
1877 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001878 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
1879 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
1880 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001881 break;
1882
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001883 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001884 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001885 }
1886}
1887
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001888void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001889 LocationSummary* locations =
1890 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001891 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001892 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001893 locations->SetInAt(0, Location::RequiresRegister());
1894 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001895 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1896 break;
1897 }
1898
1899 case Primitive::kPrimLong: {
1900 locations->SetInAt(0, Location::RequiresRegister());
1901 locations->SetInAt(1, Location::RequiresRegister());
1902 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001903 break;
1904 }
Calin Juravle11351682014-10-23 15:38:15 +01001905 case Primitive::kPrimFloat:
1906 case Primitive::kPrimDouble: {
1907 locations->SetInAt(0, Location::RequiresFpuRegister());
1908 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00001909 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001910 break;
Calin Juravle11351682014-10-23 15:38:15 +01001911 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001912 default:
Calin Juravle11351682014-10-23 15:38:15 +01001913 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001914 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001915}
1916
1917void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
1918 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01001919 Location out = locations->Out();
1920 Location first = locations->InAt(0);
1921 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001922 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001923 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01001924 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00001925 __ sub(out.AsRegister<Register>(),
1926 first.AsRegister<Register>(),
1927 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001928 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001929 __ AddConstant(out.AsRegister<Register>(),
1930 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01001931 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001932 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001933 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001934 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001935
Calin Juravle11351682014-10-23 15:38:15 +01001936 case Primitive::kPrimLong: {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001937 DCHECK(second.IsRegisterPair());
Calin Juravle11351682014-10-23 15:38:15 +01001938 __ subs(out.AsRegisterPairLow<Register>(),
1939 first.AsRegisterPairLow<Register>(),
1940 ShifterOperand(second.AsRegisterPairLow<Register>()));
1941 __ sbc(out.AsRegisterPairHigh<Register>(),
1942 first.AsRegisterPairHigh<Register>(),
1943 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001944 break;
Calin Juravle11351682014-10-23 15:38:15 +01001945 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001946
Calin Juravle11351682014-10-23 15:38:15 +01001947 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00001948 __ vsubs(out.AsFpuRegister<SRegister>(),
1949 first.AsFpuRegister<SRegister>(),
1950 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001951 break;
Calin Juravle11351682014-10-23 15:38:15 +01001952 }
1953
1954 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001955 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
1956 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
1957 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01001958 break;
1959 }
1960
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001961
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001962 default:
Calin Juravle11351682014-10-23 15:38:15 +01001963 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001964 }
1965}
1966
Calin Juravle34bacdf2014-10-07 20:23:36 +01001967void LocationsBuilderARM::VisitMul(HMul* mul) {
1968 LocationSummary* locations =
1969 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1970 switch (mul->GetResultType()) {
1971 case Primitive::kPrimInt:
1972 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001973 locations->SetInAt(0, Location::RequiresRegister());
1974 locations->SetInAt(1, Location::RequiresRegister());
1975 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01001976 break;
1977 }
1978
Calin Juravleb5bfa962014-10-21 18:02:24 +01001979 case Primitive::kPrimFloat:
1980 case Primitive::kPrimDouble: {
1981 locations->SetInAt(0, Location::RequiresFpuRegister());
1982 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00001983 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01001984 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001985 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001986
1987 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001988 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001989 }
1990}
1991
1992void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
1993 LocationSummary* locations = mul->GetLocations();
1994 Location out = locations->Out();
1995 Location first = locations->InAt(0);
1996 Location second = locations->InAt(1);
1997 switch (mul->GetResultType()) {
1998 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00001999 __ mul(out.AsRegister<Register>(),
2000 first.AsRegister<Register>(),
2001 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002002 break;
2003 }
2004 case Primitive::kPrimLong: {
2005 Register out_hi = out.AsRegisterPairHigh<Register>();
2006 Register out_lo = out.AsRegisterPairLow<Register>();
2007 Register in1_hi = first.AsRegisterPairHigh<Register>();
2008 Register in1_lo = first.AsRegisterPairLow<Register>();
2009 Register in2_hi = second.AsRegisterPairHigh<Register>();
2010 Register in2_lo = second.AsRegisterPairLow<Register>();
2011
2012 // Extra checks to protect caused by the existence of R1_R2.
2013 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
2014 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
2015 DCHECK_NE(out_hi, in1_lo);
2016 DCHECK_NE(out_hi, in2_lo);
2017
2018 // input: in1 - 64 bits, in2 - 64 bits
2019 // output: out
2020 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2021 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2022 // parts: out.lo = (in1.lo * in2.lo)[31:0]
2023
2024 // IP <- in1.lo * in2.hi
2025 __ mul(IP, in1_lo, in2_hi);
2026 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2027 __ mla(out_hi, in1_hi, in2_lo, IP);
2028 // out.lo <- (in1.lo * in2.lo)[31:0];
2029 __ umull(out_lo, IP, in1_lo, in2_lo);
2030 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2031 __ add(out_hi, out_hi, ShifterOperand(IP));
2032 break;
2033 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002034
2035 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002036 __ vmuls(out.AsFpuRegister<SRegister>(),
2037 first.AsFpuRegister<SRegister>(),
2038 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002039 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002040 }
2041
2042 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002043 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2044 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2045 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01002046 break;
2047 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002048
2049 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002050 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002051 }
2052}
2053
Calin Juravle7c4954d2014-10-28 16:57:40 +00002054void LocationsBuilderARM::VisitDiv(HDiv* div) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002055 LocationSummary::CallKind call_kind = div->GetResultType() == Primitive::kPrimLong
2056 ? LocationSummary::kCall
2057 : LocationSummary::kNoCall;
2058 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2059
Calin Juravle7c4954d2014-10-28 16:57:40 +00002060 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002061 case Primitive::kPrimInt: {
2062 locations->SetInAt(0, Location::RequiresRegister());
2063 locations->SetInAt(1, Location::RequiresRegister());
2064 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2065 break;
2066 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002067 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002068 InvokeRuntimeCallingConvention calling_convention;
2069 locations->SetInAt(0, Location::RegisterPairLocation(
2070 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2071 locations->SetInAt(1, Location::RegisterPairLocation(
2072 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002073 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002074 break;
2075 }
2076 case Primitive::kPrimFloat:
2077 case Primitive::kPrimDouble: {
2078 locations->SetInAt(0, Location::RequiresFpuRegister());
2079 locations->SetInAt(1, Location::RequiresFpuRegister());
2080 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2081 break;
2082 }
2083
2084 default:
2085 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2086 }
2087}
2088
2089void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
2090 LocationSummary* locations = div->GetLocations();
2091 Location out = locations->Out();
2092 Location first = locations->InAt(0);
2093 Location second = locations->InAt(1);
2094
2095 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002096 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00002097 __ sdiv(out.AsRegister<Register>(),
2098 first.AsRegister<Register>(),
2099 second.AsRegister<Register>());
Calin Juravled0d48522014-11-04 16:40:20 +00002100 break;
2101 }
2102
Calin Juravle7c4954d2014-10-28 16:57:40 +00002103 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002104 InvokeRuntimeCallingConvention calling_convention;
2105 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2106 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2107 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2108 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2109 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002110 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002111
2112 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv), div, div->GetDexPc());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002113 break;
2114 }
2115
2116 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002117 __ vdivs(out.AsFpuRegister<SRegister>(),
2118 first.AsFpuRegister<SRegister>(),
2119 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002120 break;
2121 }
2122
2123 case Primitive::kPrimDouble: {
2124 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2125 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2126 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
2127 break;
2128 }
2129
2130 default:
2131 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2132 }
2133}
2134
Calin Juravlebacfec32014-11-14 15:54:36 +00002135void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002136 Primitive::Type type = rem->GetResultType();
2137 LocationSummary::CallKind call_kind = type == Primitive::kPrimInt
2138 ? LocationSummary::kNoCall
2139 : LocationSummary::kCall;
Calin Juravlebacfec32014-11-14 15:54:36 +00002140 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2141
Calin Juravled2ec87d2014-12-08 14:24:46 +00002142 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002143 case Primitive::kPrimInt: {
2144 locations->SetInAt(0, Location::RequiresRegister());
2145 locations->SetInAt(1, Location::RequiresRegister());
2146 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2147 locations->AddTemp(Location::RequiresRegister());
2148 break;
2149 }
2150 case Primitive::kPrimLong: {
2151 InvokeRuntimeCallingConvention calling_convention;
2152 locations->SetInAt(0, Location::RegisterPairLocation(
2153 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2154 locations->SetInAt(1, Location::RegisterPairLocation(
2155 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2156 // The runtime helper puts the output in R2,R3.
2157 locations->SetOut(Location::RegisterPairLocation(R2, R3));
2158 break;
2159 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00002160 case Primitive::kPrimFloat: {
2161 InvokeRuntimeCallingConvention calling_convention;
2162 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
2163 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
2164 locations->SetOut(Location::FpuRegisterLocation(S0));
2165 break;
2166 }
2167
Calin Juravlebacfec32014-11-14 15:54:36 +00002168 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002169 InvokeRuntimeCallingConvention calling_convention;
2170 locations->SetInAt(0, Location::FpuRegisterPairLocation(
2171 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
2172 locations->SetInAt(1, Location::FpuRegisterPairLocation(
2173 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
2174 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00002175 break;
2176 }
2177
2178 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002179 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002180 }
2181}
2182
2183void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
2184 LocationSummary* locations = rem->GetLocations();
2185 Location out = locations->Out();
2186 Location first = locations->InAt(0);
2187 Location second = locations->InAt(1);
2188
Calin Juravled2ec87d2014-12-08 14:24:46 +00002189 Primitive::Type type = rem->GetResultType();
2190 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002191 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002192 Register reg1 = first.AsRegister<Register>();
2193 Register reg2 = second.AsRegister<Register>();
2194 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00002195
2196 // temp = reg1 / reg2 (integer division)
2197 // temp = temp * reg2
2198 // dest = reg1 - temp
2199 __ sdiv(temp, reg1, reg2);
2200 __ mul(temp, temp, reg2);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002201 __ sub(out.AsRegister<Register>(), reg1, ShifterOperand(temp));
Calin Juravlebacfec32014-11-14 15:54:36 +00002202 break;
2203 }
2204
2205 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002206 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod), rem, rem->GetDexPc());
2207 break;
2208 }
2209
Calin Juravled2ec87d2014-12-08 14:24:46 +00002210 case Primitive::kPrimFloat: {
2211 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf), rem, rem->GetDexPc());
2212 break;
2213 }
2214
Calin Juravlebacfec32014-11-14 15:54:36 +00002215 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002216 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod), rem, rem->GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00002217 break;
2218 }
2219
2220 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002221 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002222 }
2223}
2224
Calin Juravled0d48522014-11-04 16:40:20 +00002225void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2226 LocationSummary* locations =
2227 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002228 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00002229 if (instruction->HasUses()) {
2230 locations->SetOut(Location::SameAsFirstInput());
2231 }
2232}
2233
2234void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2235 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
2236 codegen_->AddSlowPath(slow_path);
2237
2238 LocationSummary* locations = instruction->GetLocations();
2239 Location value = locations->InAt(0);
2240
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002241 switch (instruction->GetType()) {
2242 case Primitive::kPrimInt: {
2243 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002244 __ cmp(value.AsRegister<Register>(), ShifterOperand(0));
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002245 __ b(slow_path->GetEntryLabel(), EQ);
2246 } else {
2247 DCHECK(value.IsConstant()) << value;
2248 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2249 __ b(slow_path->GetEntryLabel());
2250 }
2251 }
2252 break;
2253 }
2254 case Primitive::kPrimLong: {
2255 if (value.IsRegisterPair()) {
2256 __ orrs(IP,
2257 value.AsRegisterPairLow<Register>(),
2258 ShifterOperand(value.AsRegisterPairHigh<Register>()));
2259 __ b(slow_path->GetEntryLabel(), EQ);
2260 } else {
2261 DCHECK(value.IsConstant()) << value;
2262 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2263 __ b(slow_path->GetEntryLabel());
2264 }
2265 }
2266 break;
2267 default:
2268 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2269 }
2270 }
Calin Juravled0d48522014-11-04 16:40:20 +00002271}
2272
Calin Juravle9aec02f2014-11-18 23:06:35 +00002273void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
2274 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2275
2276 LocationSummary::CallKind call_kind = op->GetResultType() == Primitive::kPrimLong
2277 ? LocationSummary::kCall
2278 : LocationSummary::kNoCall;
2279 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(op, call_kind);
2280
2281 switch (op->GetResultType()) {
2282 case Primitive::kPrimInt: {
2283 locations->SetInAt(0, Location::RequiresRegister());
2284 locations->SetInAt(1, Location::RegisterOrConstant(op->InputAt(1)));
2285 locations->SetOut(Location::RequiresRegister());
2286 break;
2287 }
2288 case Primitive::kPrimLong: {
2289 InvokeRuntimeCallingConvention calling_convention;
2290 locations->SetInAt(0, Location::RegisterPairLocation(
2291 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2292 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002293 // The runtime helper puts the output in R0,R1.
2294 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle9aec02f2014-11-18 23:06:35 +00002295 break;
2296 }
2297 default:
2298 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2299 }
2300}
2301
2302void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
2303 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2304
2305 LocationSummary* locations = op->GetLocations();
2306 Location out = locations->Out();
2307 Location first = locations->InAt(0);
2308 Location second = locations->InAt(1);
2309
2310 Primitive::Type type = op->GetResultType();
2311 switch (type) {
2312 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002313 Register out_reg = out.AsRegister<Register>();
2314 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002315 // Arm doesn't mask the shift count so we need to do it ourselves.
2316 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002317 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002318 __ and_(second_reg, second_reg, ShifterOperand(kMaxIntShiftValue));
2319 if (op->IsShl()) {
2320 __ Lsl(out_reg, first_reg, second_reg);
2321 } else if (op->IsShr()) {
2322 __ Asr(out_reg, first_reg, second_reg);
2323 } else {
2324 __ Lsr(out_reg, first_reg, second_reg);
2325 }
2326 } else {
2327 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
2328 uint32_t shift_value = static_cast<uint32_t>(cst & kMaxIntShiftValue);
2329 if (shift_value == 0) { // arm does not support shifting with 0 immediate.
2330 __ Mov(out_reg, first_reg);
2331 } else if (op->IsShl()) {
2332 __ Lsl(out_reg, first_reg, shift_value);
2333 } else if (op->IsShr()) {
2334 __ Asr(out_reg, first_reg, shift_value);
2335 } else {
2336 __ Lsr(out_reg, first_reg, shift_value);
2337 }
2338 }
2339 break;
2340 }
2341 case Primitive::kPrimLong: {
2342 // TODO: Inline the assembly instead of calling the runtime.
2343 InvokeRuntimeCallingConvention calling_convention;
2344 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2345 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002346 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegister<Register>());
Calin Juravle9aec02f2014-11-18 23:06:35 +00002347 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002348 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravle9aec02f2014-11-18 23:06:35 +00002349
2350 int32_t entry_point_offset;
2351 if (op->IsShl()) {
2352 entry_point_offset = QUICK_ENTRY_POINT(pShlLong);
2353 } else if (op->IsShr()) {
2354 entry_point_offset = QUICK_ENTRY_POINT(pShrLong);
2355 } else {
2356 entry_point_offset = QUICK_ENTRY_POINT(pUshrLong);
2357 }
2358 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
2359 __ blx(LR);
2360 break;
2361 }
2362 default:
2363 LOG(FATAL) << "Unexpected operation type " << type;
2364 }
2365}
2366
2367void LocationsBuilderARM::VisitShl(HShl* shl) {
2368 HandleShift(shl);
2369}
2370
2371void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
2372 HandleShift(shl);
2373}
2374
2375void LocationsBuilderARM::VisitShr(HShr* shr) {
2376 HandleShift(shr);
2377}
2378
2379void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
2380 HandleShift(shr);
2381}
2382
2383void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
2384 HandleShift(ushr);
2385}
2386
2387void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
2388 HandleShift(ushr);
2389}
2390
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002391void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002392 LocationSummary* locations =
2393 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002394 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002395 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2396 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2397 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002398}
2399
2400void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
2401 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002402 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002403 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002404 codegen_->InvokeRuntime(
2405 QUICK_ENTRY_POINT(pAllocObjectWithAccessCheck), instruction, instruction->GetDexPc());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002406}
2407
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002408void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
2409 LocationSummary* locations =
2410 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2411 InvokeRuntimeCallingConvention calling_convention;
2412 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002413 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002414 locations->SetOut(Location::RegisterLocation(R0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002415 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002416}
2417
2418void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
2419 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002420 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(2));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002421 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002422 codegen_->InvokeRuntime(
2423 QUICK_ENTRY_POINT(pAllocArrayWithAccessCheck), instruction, instruction->GetDexPc());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002424}
2425
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002426void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002427 LocationSummary* locations =
2428 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002429 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2430 if (location.IsStackSlot()) {
2431 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2432 } else if (location.IsDoubleStackSlot()) {
2433 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002434 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002435 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002436}
2437
2438void InstructionCodeGeneratorARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002439 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002440 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002441}
2442
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002443void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002444 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002445 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002446 locations->SetInAt(0, Location::RequiresRegister());
2447 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002448}
2449
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002450void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
2451 LocationSummary* locations = not_->GetLocations();
2452 Location out = locations->Out();
2453 Location in = locations->InAt(0);
2454 switch (not_->InputAt(0)->GetType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002455 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002456 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002457 break;
2458
2459 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01002460 __ mvn(out.AsRegisterPairLow<Register>(),
2461 ShifterOperand(in.AsRegisterPairLow<Register>()));
2462 __ mvn(out.AsRegisterPairHigh<Register>(),
2463 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002464 break;
2465
2466 default:
2467 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2468 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002469}
2470
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002471void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002472 LocationSummary* locations =
2473 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00002474 switch (compare->InputAt(0)->GetType()) {
2475 case Primitive::kPrimLong: {
2476 locations->SetInAt(0, Location::RequiresRegister());
2477 locations->SetInAt(1, Location::RequiresRegister());
2478 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2479 break;
2480 }
2481 case Primitive::kPrimFloat:
2482 case Primitive::kPrimDouble: {
2483 locations->SetInAt(0, Location::RequiresFpuRegister());
2484 locations->SetInAt(1, Location::RequiresFpuRegister());
2485 locations->SetOut(Location::RequiresRegister());
2486 break;
2487 }
2488 default:
2489 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
2490 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002491}
2492
2493void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002494 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002495 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00002496 Location left = locations->InAt(0);
2497 Location right = locations->InAt(1);
2498
2499 Label less, greater, done;
2500 Primitive::Type type = compare->InputAt(0)->GetType();
2501 switch (type) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002502 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002503 __ cmp(left.AsRegisterPairHigh<Register>(),
2504 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002505 __ b(&less, LT);
2506 __ b(&greater, GT);
Calin Juravleddb7df22014-11-25 20:56:51 +00002507 // Do LoadImmediate before any `cmp`, as LoadImmediate might affect the status flags.
2508 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002509 __ cmp(left.AsRegisterPairLow<Register>(),
2510 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Calin Juravleddb7df22014-11-25 20:56:51 +00002511 break;
2512 }
2513 case Primitive::kPrimFloat:
2514 case Primitive::kPrimDouble: {
2515 __ LoadImmediate(out, 0);
2516 if (type == Primitive::kPrimFloat) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002517 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00002518 } else {
2519 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
2520 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
2521 }
2522 __ vmstat(); // transfer FP status register to ARM APSR.
2523 __ b(compare->IsGtBias() ? &greater : &less, VS); // VS for unordered.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002524 break;
2525 }
2526 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00002527 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002528 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002529 __ b(&done, EQ);
2530 __ b(&less, CC); // CC is for both: unsigned compare for longs and 'less than' for floats.
2531
2532 __ Bind(&greater);
2533 __ LoadImmediate(out, 1);
2534 __ b(&done);
2535
2536 __ Bind(&less);
2537 __ LoadImmediate(out, -1);
2538
2539 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002540}
2541
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002542void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002543 LocationSummary* locations =
2544 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01002545 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2546 locations->SetInAt(i, Location::Any());
2547 }
2548 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002549}
2550
2551void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002552 UNUSED(instruction);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002553 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002554}
2555
Calin Juravle52c48962014-12-16 17:02:57 +00002556void InstructionCodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
2557 // TODO (ported from quick): revisit Arm barrier kinds
2558 DmbOptions flavour = DmbOptions::ISH; // quiet c++ warnings
2559 switch (kind) {
2560 case MemBarrierKind::kAnyStore:
2561 case MemBarrierKind::kLoadAny:
2562 case MemBarrierKind::kAnyAny: {
2563 flavour = DmbOptions::ISH;
2564 break;
2565 }
2566 case MemBarrierKind::kStoreStore: {
2567 flavour = DmbOptions::ISHST;
2568 break;
2569 }
2570 default:
2571 LOG(FATAL) << "Unexpected memory barrier " << kind;
2572 }
2573 __ dmb(flavour);
2574}
2575
2576void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
2577 uint32_t offset,
2578 Register out_lo,
2579 Register out_hi) {
2580 if (offset != 0) {
2581 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00002582 __ add(IP, addr, ShifterOperand(out_lo));
2583 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00002584 }
2585 __ ldrexd(out_lo, out_hi, addr);
2586}
2587
2588void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
2589 uint32_t offset,
2590 Register value_lo,
2591 Register value_hi,
2592 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00002593 Register temp2,
2594 HInstruction* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00002595 Label fail;
2596 if (offset != 0) {
2597 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00002598 __ add(IP, addr, ShifterOperand(temp1));
2599 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00002600 }
2601 __ Bind(&fail);
2602 // We need a load followed by store. (The address used in a STREX instruction must
2603 // be the same as the address in the most recently executed LDREX instruction.)
2604 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00002605 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002606 __ strexd(temp1, value_lo, value_hi, addr);
2607 __ cmp(temp1, ShifterOperand(0));
2608 __ b(&fail, NE);
2609}
2610
2611void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
2612 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2613
Nicolas Geoffray39468442014-09-02 15:17:15 +01002614 LocationSummary* locations =
2615 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002616 locations->SetInAt(0, Location::RequiresRegister());
2617 locations->SetInAt(1, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00002618
Calin Juravle34166012014-12-19 17:22:29 +00002619
Calin Juravle52c48962014-12-16 17:02:57 +00002620 Primitive::Type field_type = field_info.GetFieldType();
2621 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00002622 bool generate_volatile = field_info.IsVolatile()
2623 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002624 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002625 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00002626 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
2627 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002628 locations->AddTemp(Location::RequiresRegister());
2629 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00002630 } else if (generate_volatile) {
Calin Juravle52c48962014-12-16 17:02:57 +00002631 // Arm encoding have some additional constraints for ldrexd/strexd:
2632 // - registers need to be consecutive
2633 // - the first register should be even but not R14.
2634 // We don't test for Arm yet, and the assertion makes sure that we revisit this if we ever
2635 // enable Arm encoding.
2636 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
2637
2638 locations->AddTemp(Location::RequiresRegister());
2639 locations->AddTemp(Location::RequiresRegister());
2640 if (field_type == Primitive::kPrimDouble) {
2641 // For doubles we need two more registers to copy the value.
2642 locations->AddTemp(Location::RegisterLocation(R2));
2643 locations->AddTemp(Location::RegisterLocation(R3));
2644 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002645 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002646}
2647
Calin Juravle52c48962014-12-16 17:02:57 +00002648void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
2649 const FieldInfo& field_info) {
2650 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2651
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002652 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00002653 Register base = locations->InAt(0).AsRegister<Register>();
2654 Location value = locations->InAt(1);
2655
2656 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002657 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00002658 Primitive::Type field_type = field_info.GetFieldType();
2659 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2660
2661 if (is_volatile) {
2662 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
2663 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002664
2665 switch (field_type) {
2666 case Primitive::kPrimBoolean:
2667 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00002668 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002669 break;
2670 }
2671
2672 case Primitive::kPrimShort:
2673 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00002674 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002675 break;
2676 }
2677
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002678 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002679 case Primitive::kPrimNot: {
Calin Juravle77520bc2015-01-12 18:45:46 +00002680 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002681 break;
2682 }
2683
2684 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00002685 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00002686 GenerateWideAtomicStore(base, offset,
2687 value.AsRegisterPairLow<Register>(),
2688 value.AsRegisterPairHigh<Register>(),
2689 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00002690 locations->GetTemp(1).AsRegister<Register>(),
2691 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002692 } else {
2693 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00002694 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002695 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002696 break;
2697 }
2698
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002699 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00002700 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002701 break;
2702 }
2703
2704 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00002705 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00002706 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00002707 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
2708 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
2709
2710 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
2711
2712 GenerateWideAtomicStore(base, offset,
2713 value_reg_lo,
2714 value_reg_hi,
2715 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00002716 locations->GetTemp(3).AsRegister<Register>(),
2717 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002718 } else {
2719 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00002720 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002721 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002722 break;
2723 }
2724
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002725 case Primitive::kPrimVoid:
2726 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002727 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002728 }
Calin Juravle52c48962014-12-16 17:02:57 +00002729
Calin Juravle77520bc2015-01-12 18:45:46 +00002730 // Longs and doubles are handled in the switch.
2731 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
2732 codegen_->MaybeRecordImplicitNullCheck(instruction);
2733 }
2734
2735 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
2736 Register temp = locations->GetTemp(0).AsRegister<Register>();
2737 Register card = locations->GetTemp(1).AsRegister<Register>();
2738 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>());
2739 }
2740
Calin Juravle52c48962014-12-16 17:02:57 +00002741 if (is_volatile) {
2742 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
2743 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002744}
2745
Calin Juravle52c48962014-12-16 17:02:57 +00002746void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
2747 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002748 LocationSummary* locations =
2749 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002750 locations->SetInAt(0, Location::RequiresRegister());
2751 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle52c48962014-12-16 17:02:57 +00002752
Calin Juravle34166012014-12-19 17:22:29 +00002753 bool generate_volatile = field_info.IsVolatile()
2754 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002755 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle34166012014-12-19 17:22:29 +00002756 if (generate_volatile) {
Calin Juravle52c48962014-12-16 17:02:57 +00002757 // Arm encoding have some additional constraints for ldrexd/strexd:
2758 // - registers need to be consecutive
2759 // - the first register should be even but not R14.
2760 // We don't test for Arm yet, and the assertion makes sure that we revisit this if we ever
2761 // enable Arm encoding.
2762 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
2763 locations->AddTemp(Location::RequiresRegister());
2764 locations->AddTemp(Location::RequiresRegister());
2765 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002766}
2767
Calin Juravle52c48962014-12-16 17:02:57 +00002768void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
2769 const FieldInfo& field_info) {
2770 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002771
Calin Juravle52c48962014-12-16 17:02:57 +00002772 LocationSummary* locations = instruction->GetLocations();
2773 Register base = locations->InAt(0).AsRegister<Register>();
2774 Location out = locations->Out();
2775 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002776 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00002777 Primitive::Type field_type = field_info.GetFieldType();
2778 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2779
2780 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002781 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00002782 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002783 break;
2784 }
2785
2786 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00002787 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002788 break;
2789 }
2790
2791 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00002792 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002793 break;
2794 }
2795
2796 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00002797 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002798 break;
2799 }
2800
2801 case Primitive::kPrimInt:
2802 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00002803 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002804 break;
2805 }
2806
2807 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00002808 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00002809 GenerateWideAtomicLoad(base, offset,
2810 out.AsRegisterPairLow<Register>(),
2811 out.AsRegisterPairHigh<Register>());
2812 } else {
2813 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
2814 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002815 break;
2816 }
2817
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002818 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00002819 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002820 break;
2821 }
2822
2823 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00002824 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00002825 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00002826 Register lo = locations->GetTemp(0).AsRegister<Register>();
2827 Register hi = locations->GetTemp(1).AsRegister<Register>();
2828 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00002829 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002830 __ vmovdrr(out_reg, lo, hi);
2831 } else {
2832 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00002833 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002834 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002835 break;
2836 }
2837
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002838 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00002839 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002840 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002841 }
Calin Juravle52c48962014-12-16 17:02:57 +00002842
Calin Juravle77520bc2015-01-12 18:45:46 +00002843 // Doubles are handled in the switch.
2844 if (field_type != Primitive::kPrimDouble) {
2845 codegen_->MaybeRecordImplicitNullCheck(instruction);
2846 }
2847
Calin Juravle52c48962014-12-16 17:02:57 +00002848 if (is_volatile) {
2849 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
2850 }
2851}
2852
2853void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2854 HandleFieldSet(instruction, instruction->GetFieldInfo());
2855}
2856
2857void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2858 HandleFieldSet(instruction, instruction->GetFieldInfo());
2859}
2860
2861void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2862 HandleFieldGet(instruction, instruction->GetFieldInfo());
2863}
2864
2865void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2866 HandleFieldGet(instruction, instruction->GetFieldInfo());
2867}
2868
2869void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2870 HandleFieldGet(instruction, instruction->GetFieldInfo());
2871}
2872
2873void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2874 HandleFieldGet(instruction, instruction->GetFieldInfo());
2875}
2876
2877void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2878 HandleFieldSet(instruction, instruction->GetFieldInfo());
2879}
2880
2881void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2882 HandleFieldSet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002883}
2884
2885void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002886 LocationSummary* locations =
2887 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle77520bc2015-01-12 18:45:46 +00002888 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002889 if (instruction->HasUses()) {
2890 locations->SetOut(Location::SameAsFirstInput());
2891 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002892}
2893
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002894void InstructionCodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00002895 if (codegen_->CanMoveNullCheckToUser(instruction)) {
2896 return;
2897 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002898 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00002899
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002900 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
2901 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2902}
2903
2904void InstructionCodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002905 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002906 codegen_->AddSlowPath(slow_path);
2907
2908 LocationSummary* locations = instruction->GetLocations();
2909 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002910
Calin Juravle77520bc2015-01-12 18:45:46 +00002911 __ cmp(obj.AsRegister<Register>(), ShifterOperand(0));
2912 __ b(slow_path->GetEntryLabel(), EQ);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002913}
2914
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002915void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
2916 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
2917 GenerateImplicitNullCheck(instruction);
2918 } else {
2919 GenerateExplicitNullCheck(instruction);
2920 }
2921}
2922
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002923void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002924 LocationSummary* locations =
2925 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002926 locations->SetInAt(0, Location::RequiresRegister());
2927 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2928 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002929}
2930
2931void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
2932 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002933 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002934 Location index = locations->InAt(1);
2935
2936 switch (instruction->GetType()) {
2937 case Primitive::kPrimBoolean: {
2938 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002939 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002940 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002941 size_t offset =
2942 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002943 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
2944 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002945 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002946 __ LoadFromOffset(kLoadUnsignedByte, out, IP, data_offset);
2947 }
2948 break;
2949 }
2950
2951 case Primitive::kPrimByte: {
2952 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002953 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002954 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002955 size_t offset =
2956 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002957 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
2958 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002959 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002960 __ LoadFromOffset(kLoadSignedByte, out, IP, data_offset);
2961 }
2962 break;
2963 }
2964
2965 case Primitive::kPrimShort: {
2966 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002967 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002968 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002969 size_t offset =
2970 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002971 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
2972 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002973 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002974 __ LoadFromOffset(kLoadSignedHalfword, out, IP, data_offset);
2975 }
2976 break;
2977 }
2978
2979 case Primitive::kPrimChar: {
2980 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002981 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002982 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002983 size_t offset =
2984 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002985 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
2986 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002987 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002988 __ LoadFromOffset(kLoadUnsignedHalfword, out, IP, data_offset);
2989 }
2990 break;
2991 }
2992
2993 case Primitive::kPrimInt:
2994 case Primitive::kPrimNot: {
2995 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
2996 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002997 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002998 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002999 size_t offset =
3000 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003001 __ LoadFromOffset(kLoadWord, out, obj, offset);
3002 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003003 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003004 __ LoadFromOffset(kLoadWord, out, IP, data_offset);
3005 }
3006 break;
3007 }
3008
3009 case Primitive::kPrimLong: {
3010 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003011 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003012 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003013 size_t offset =
3014 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003015 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003016 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003017 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003018 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003019 }
3020 break;
3021 }
3022
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003023 case Primitive::kPrimFloat: {
3024 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3025 Location out = locations->Out();
3026 DCHECK(out.IsFpuRegister());
3027 if (index.IsConstant()) {
3028 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3029 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), obj, offset);
3030 } else {
3031 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
3032 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), IP, data_offset);
3033 }
3034 break;
3035 }
3036
3037 case Primitive::kPrimDouble: {
3038 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3039 Location out = locations->Out();
3040 DCHECK(out.IsFpuRegisterPair());
3041 if (index.IsConstant()) {
3042 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3043 __ LoadDFromOffset(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()), obj, offset);
3044 } else {
3045 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
3046 __ LoadDFromOffset(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
3047 }
3048 break;
3049 }
3050
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003051 case Primitive::kPrimVoid:
3052 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003053 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003054 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003055 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003056}
3057
3058void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003059 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003060
3061 bool needs_write_barrier =
3062 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3063 bool needs_runtime_call = instruction->NeedsTypeCheck();
3064
Nicolas Geoffray39468442014-09-02 15:17:15 +01003065 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003066 instruction, needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
3067 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003068 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003069 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3070 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3071 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003072 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003073 locations->SetInAt(0, Location::RequiresRegister());
3074 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3075 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003076
3077 if (needs_write_barrier) {
3078 // Temporary registers for the write barrier.
3079 locations->AddTemp(Location::RequiresRegister());
3080 locations->AddTemp(Location::RequiresRegister());
3081 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003082 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003083}
3084
3085void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
3086 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003087 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003088 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003089 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003090 bool needs_runtime_call = locations->WillCall();
3091 bool needs_write_barrier =
3092 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003093
3094 switch (value_type) {
3095 case Primitive::kPrimBoolean:
3096 case Primitive::kPrimByte: {
3097 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003098 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003099 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003100 size_t offset =
3101 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003102 __ StoreToOffset(kStoreByte, value, obj, offset);
3103 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003104 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003105 __ StoreToOffset(kStoreByte, value, IP, data_offset);
3106 }
3107 break;
3108 }
3109
3110 case Primitive::kPrimShort:
3111 case Primitive::kPrimChar: {
3112 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003113 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003114 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003115 size_t offset =
3116 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003117 __ StoreToOffset(kStoreHalfword, value, obj, offset);
3118 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003119 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003120 __ StoreToOffset(kStoreHalfword, value, IP, data_offset);
3121 }
3122 break;
3123 }
3124
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003125 case Primitive::kPrimInt:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003126 case Primitive::kPrimNot: {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003127 if (!needs_runtime_call) {
3128 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003129 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003130 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003131 size_t offset =
3132 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003133 __ StoreToOffset(kStoreWord, value, obj, offset);
3134 } else {
3135 DCHECK(index.IsRegister()) << index;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003136 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003137 __ StoreToOffset(kStoreWord, value, IP, data_offset);
3138 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003139 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003140 if (needs_write_barrier) {
3141 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003142 Register temp = locations->GetTemp(0).AsRegister<Register>();
3143 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003144 codegen_->MarkGCCard(temp, card, obj, value);
3145 }
3146 } else {
3147 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain199f3362014-11-27 17:15:16 +00003148 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
3149 instruction,
3150 instruction->GetDexPc());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003151 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003152 break;
3153 }
3154
3155 case Primitive::kPrimLong: {
3156 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003157 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003158 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003159 size_t offset =
3160 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003161 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003162 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003163 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003164 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003165 }
3166 break;
3167 }
3168
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003169 case Primitive::kPrimFloat: {
3170 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3171 Location value = locations->InAt(2);
3172 DCHECK(value.IsFpuRegister());
3173 if (index.IsConstant()) {
3174 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3175 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), obj, offset);
3176 } else {
3177 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
3178 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
3179 }
3180 break;
3181 }
3182
3183 case Primitive::kPrimDouble: {
3184 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3185 Location value = locations->InAt(2);
3186 DCHECK(value.IsFpuRegisterPair());
3187 if (index.IsConstant()) {
3188 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3189 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), obj, offset);
3190 } else {
3191 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
3192 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
3193 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003194
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003195 break;
3196 }
3197
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003198 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003199 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003200 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003201 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003202
3203 // Ints and objects are handled in the switch.
3204 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
3205 codegen_->MaybeRecordImplicitNullCheck(instruction);
3206 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003207}
3208
3209void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003210 LocationSummary* locations =
3211 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003212 locations->SetInAt(0, Location::RequiresRegister());
3213 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003214}
3215
3216void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
3217 LocationSummary* locations = instruction->GetLocations();
3218 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003219 Register obj = locations->InAt(0).AsRegister<Register>();
3220 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003221 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003222 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003223}
3224
3225void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003226 LocationSummary* locations =
3227 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003228 locations->SetInAt(0, Location::RequiresRegister());
3229 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003230 if (instruction->HasUses()) {
3231 locations->SetOut(Location::SameAsFirstInput());
3232 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003233}
3234
3235void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
3236 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003237 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(
Nicolas Geoffray39468442014-09-02 15:17:15 +01003238 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003239 codegen_->AddSlowPath(slow_path);
3240
Roland Levillain271ab9c2014-11-27 15:23:57 +00003241 Register index = locations->InAt(0).AsRegister<Register>();
3242 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003243
3244 __ cmp(index, ShifterOperand(length));
3245 __ b(slow_path->GetEntryLabel(), CS);
3246}
3247
3248void CodeGeneratorARM::MarkGCCard(Register temp, Register card, Register object, Register value) {
3249 Label is_null;
3250 __ CompareAndBranchIfZero(value, &is_null);
3251 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmWordSize>().Int32Value());
3252 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
3253 __ strb(card, Address(card, temp));
3254 __ Bind(&is_null);
3255}
3256
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003257void LocationsBuilderARM::VisitTemporary(HTemporary* temp) {
3258 temp->SetLocations(nullptr);
3259}
3260
3261void InstructionCodeGeneratorARM::VisitTemporary(HTemporary* temp) {
3262 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003263 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003264}
3265
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003266void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003267 UNUSED(instruction);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003268 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003269}
3270
3271void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003272 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3273}
3274
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003275void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
3276 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3277}
3278
3279void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003280 HBasicBlock* block = instruction->GetBlock();
3281 if (block->GetLoopInformation() != nullptr) {
3282 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3283 // The back edge will generate the suspend check.
3284 return;
3285 }
3286 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3287 // The goto will generate the suspend check.
3288 return;
3289 }
3290 GenerateSuspendCheck(instruction, nullptr);
3291}
3292
3293void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
3294 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003295 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003296 new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003297 codegen_->AddSlowPath(slow_path);
3298
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00003299 __ LoadFromOffset(
3300 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmWordSize>().Int32Value());
3301 __ cmp(IP, ShifterOperand(0));
3302 // TODO: Figure out the branch offsets and use cbz/cbnz.
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003303 if (successor == nullptr) {
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00003304 __ b(slow_path->GetEntryLabel(), NE);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003305 __ Bind(slow_path->GetReturnLabel());
3306 } else {
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00003307 __ b(codegen_->GetLabelOf(successor), EQ);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003308 __ b(slow_path->GetEntryLabel());
3309 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003310}
3311
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003312ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
3313 return codegen_->GetAssembler();
3314}
3315
3316void ParallelMoveResolverARM::EmitMove(size_t index) {
3317 MoveOperands* move = moves_.Get(index);
3318 Location source = move->GetSource();
3319 Location destination = move->GetDestination();
3320
3321 if (source.IsRegister()) {
3322 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003323 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003324 } else {
3325 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003326 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003327 SP, destination.GetStackIndex());
3328 }
3329 } else if (source.IsStackSlot()) {
3330 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003331 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003332 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003333 } else if (destination.IsFpuRegister()) {
3334 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003335 } else {
3336 DCHECK(destination.IsStackSlot());
3337 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
3338 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
3339 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003340 } else if (source.IsFpuRegister()) {
3341 if (destination.IsFpuRegister()) {
3342 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003343 } else {
3344 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003345 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
3346 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003347 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003348 DCHECK(destination.IsDoubleStackSlot()) << destination;
3349 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
3350 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
3351 __ LoadFromOffset(kLoadWord, IP, SP, source.GetHighStackIndex(kArmWordSize));
3352 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003353 } else {
3354 DCHECK(source.IsConstant()) << source;
3355 HInstruction* constant = source.GetConstant();
3356 if (constant->IsIntConstant()) {
3357 int32_t value = constant->AsIntConstant()->GetValue();
3358 if (destination.IsRegister()) {
3359 __ LoadImmediate(destination.AsRegister<Register>(), value);
3360 } else {
3361 DCHECK(destination.IsStackSlot());
3362 __ LoadImmediate(IP, value);
3363 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
3364 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003365 } else if (constant->IsLongConstant()) {
3366 int64_t value = constant->AsLongConstant()->GetValue();
3367 if (destination.IsRegister()) {
3368 // In the presence of long or double constants, the parallel move resolver will
3369 // split the move into two, but keeps the same constant for both moves. Here,
3370 // we use the low or high part depending on which register this move goes to.
3371 if (destination.reg() % 2 == 0) {
3372 __ LoadImmediate(destination.AsRegister<Register>(), Low32Bits(value));
3373 } else {
3374 __ LoadImmediate(destination.AsRegister<Register>(), High32Bits(value));
3375 }
3376 } else {
3377 DCHECK(destination.IsDoubleStackSlot());
3378 __ LoadImmediate(IP, Low32Bits(value));
3379 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
3380 __ LoadImmediate(IP, High32Bits(value));
3381 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
3382 }
3383 } else if (constant->IsDoubleConstant()) {
3384 double value = constant->AsDoubleConstant()->GetValue();
3385 uint64_t int_value = bit_cast<uint64_t, double>(value);
3386 if (destination.IsFpuRegister()) {
3387 // In the presence of long or double constants, the parallel move resolver will
3388 // split the move into two, but keeps the same constant for both moves. Here,
3389 // we use the low or high part depending on which register this move goes to.
3390 if (destination.reg() % 2 == 0) {
3391 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(),
3392 bit_cast<float, uint32_t>(Low32Bits(int_value)));
3393 } else {
3394 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(),
3395 bit_cast<float, uint32_t>(High32Bits(int_value)));
3396 }
3397 } else {
3398 DCHECK(destination.IsDoubleStackSlot());
3399 __ LoadImmediate(IP, Low32Bits(int_value));
3400 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
3401 __ LoadImmediate(IP, High32Bits(int_value));
3402 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
3403 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003404 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003405 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003406 float value = constant->AsFloatConstant()->GetValue();
3407 if (destination.IsFpuRegister()) {
3408 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
3409 } else {
3410 DCHECK(destination.IsStackSlot());
3411 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
3412 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
3413 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003414 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003415 }
3416}
3417
3418void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
3419 __ Mov(IP, reg);
3420 __ LoadFromOffset(kLoadWord, reg, SP, mem);
3421 __ StoreToOffset(kStoreWord, IP, SP, mem);
3422}
3423
3424void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
3425 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
3426 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
3427 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
3428 SP, mem1 + stack_offset);
3429 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
3430 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
3431 SP, mem2 + stack_offset);
3432 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
3433}
3434
3435void ParallelMoveResolverARM::EmitSwap(size_t index) {
3436 MoveOperands* move = moves_.Get(index);
3437 Location source = move->GetSource();
3438 Location destination = move->GetDestination();
3439
3440 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003441 DCHECK_NE(source.AsRegister<Register>(), IP);
3442 DCHECK_NE(destination.AsRegister<Register>(), IP);
3443 __ Mov(IP, source.AsRegister<Register>());
3444 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
3445 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003446 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003447 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003448 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003449 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003450 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
3451 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003452 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00003453 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003454 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00003455 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003456 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
3457 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
3458 : destination.AsFpuRegister<SRegister>();
3459 int mem = source.IsFpuRegister()
3460 ? destination.GetStackIndex()
3461 : source.GetStackIndex();
3462
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00003463 __ vmovrs(IP, reg);
3464 __ LoadFromOffset(kLoadWord, IP, SP, mem);
3465 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00003466 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00003467 Exchange(source.GetStackIndex(), destination.GetStackIndex());
3468 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003469 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00003470 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003471 }
3472}
3473
3474void ParallelMoveResolverARM::SpillScratch(int reg) {
3475 __ Push(static_cast<Register>(reg));
3476}
3477
3478void ParallelMoveResolverARM::RestoreScratch(int reg) {
3479 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003480}
3481
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003482void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003483 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
3484 ? LocationSummary::kCallOnSlowPath
3485 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003486 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003487 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003488 locations->SetOut(Location::RequiresRegister());
3489}
3490
3491void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003492 Register out = cls->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003493 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003494 DCHECK(!cls->CanCallRuntime());
3495 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003496 codegen_->LoadCurrentMethod(out);
3497 __ LoadFromOffset(kLoadWord, out, out, mirror::ArtMethod::DeclaringClassOffset().Int32Value());
3498 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003499 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003500 codegen_->LoadCurrentMethod(out);
3501 __ LoadFromOffset(
3502 kLoadWord, out, out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value());
3503 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003504
3505 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
3506 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3507 codegen_->AddSlowPath(slow_path);
3508 __ cmp(out, ShifterOperand(0));
3509 __ b(slow_path->GetEntryLabel(), EQ);
3510 if (cls->MustGenerateClinitCheck()) {
3511 GenerateClassInitializationCheck(slow_path, out);
3512 } else {
3513 __ Bind(slow_path->GetExitLabel());
3514 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003515 }
3516}
3517
3518void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
3519 LocationSummary* locations =
3520 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3521 locations->SetInAt(0, Location::RequiresRegister());
3522 if (check->HasUses()) {
3523 locations->SetOut(Location::SameAsFirstInput());
3524 }
3525}
3526
3527void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003528 // We assume the class is not null.
3529 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
3530 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003531 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00003532 GenerateClassInitializationCheck(slow_path,
3533 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003534}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003535
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003536void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
3537 SlowPathCodeARM* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003538 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
3539 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
3540 __ b(slow_path->GetEntryLabel(), LT);
3541 // Even if the initialized flag is set, we may be in a situation where caches are not synced
3542 // properly. Therefore, we do a memory fence.
3543 __ dmb(ISH);
3544 __ Bind(slow_path->GetExitLabel());
3545}
3546
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003547void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
3548 LocationSummary* locations =
3549 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3550 locations->SetOut(Location::RequiresRegister());
3551}
3552
3553void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) {
3554 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
3555 codegen_->AddSlowPath(slow_path);
3556
Roland Levillain271ab9c2014-11-27 15:23:57 +00003557 Register out = load->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003558 codegen_->LoadCurrentMethod(out);
Mathieu Chartiereace4582014-11-24 18:29:54 -08003559 __ LoadFromOffset(kLoadWord, out, out, mirror::ArtMethod::DeclaringClassOffset().Int32Value());
3560 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003561 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
3562 __ cmp(out, ShifterOperand(0));
3563 __ b(slow_path->GetEntryLabel(), EQ);
3564 __ Bind(slow_path->GetExitLabel());
3565}
3566
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003567void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
3568 LocationSummary* locations =
3569 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3570 locations->SetOut(Location::RequiresRegister());
3571}
3572
3573void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003574 Register out = load->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003575 int32_t offset = Thread::ExceptionOffset<kArmWordSize>().Int32Value();
3576 __ LoadFromOffset(kLoadWord, out, TR, offset);
3577 __ LoadImmediate(IP, 0);
3578 __ StoreToOffset(kStoreWord, IP, TR, offset);
3579}
3580
3581void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
3582 LocationSummary* locations =
3583 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3584 InvokeRuntimeCallingConvention calling_convention;
3585 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3586}
3587
3588void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
3589 codegen_->InvokeRuntime(
3590 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc());
3591}
3592
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003593void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003594 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
3595 ? LocationSummary::kNoCall
3596 : LocationSummary::kCallOnSlowPath;
3597 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3598 locations->SetInAt(0, Location::RequiresRegister());
3599 locations->SetInAt(1, Location::RequiresRegister());
3600 locations->SetOut(Location::RequiresRegister());
3601}
3602
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003603void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003604 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003605 Register obj = locations->InAt(0).AsRegister<Register>();
3606 Register cls = locations->InAt(1).AsRegister<Register>();
3607 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003608 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3609 Label done, zero;
3610 SlowPathCodeARM* slow_path = nullptr;
3611
3612 // Return 0 if `obj` is null.
3613 // TODO: avoid this check if we know obj is not null.
3614 __ cmp(obj, ShifterOperand(0));
3615 __ b(&zero, EQ);
3616 // Compare the class of `obj` with `cls`.
3617 __ LoadFromOffset(kLoadWord, out, obj, class_offset);
3618 __ cmp(out, ShifterOperand(cls));
3619 if (instruction->IsClassFinal()) {
3620 // Classes must be equal for the instanceof to succeed.
3621 __ b(&zero, NE);
3622 __ LoadImmediate(out, 1);
3623 __ b(&done);
3624 } else {
3625 // If the classes are not equal, we go into a slow path.
3626 DCHECK(locations->OnlyCallsOnSlowPath());
3627 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003628 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003629 codegen_->AddSlowPath(slow_path);
3630 __ b(slow_path->GetEntryLabel(), NE);
3631 __ LoadImmediate(out, 1);
3632 __ b(&done);
3633 }
3634 __ Bind(&zero);
3635 __ LoadImmediate(out, 0);
3636 if (slow_path != nullptr) {
3637 __ Bind(slow_path->GetExitLabel());
3638 }
3639 __ Bind(&done);
3640}
3641
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003642void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
3643 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3644 instruction, LocationSummary::kCallOnSlowPath);
3645 locations->SetInAt(0, Location::RequiresRegister());
3646 locations->SetInAt(1, Location::RequiresRegister());
3647 locations->AddTemp(Location::RequiresRegister());
3648}
3649
3650void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
3651 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003652 Register obj = locations->InAt(0).AsRegister<Register>();
3653 Register cls = locations->InAt(1).AsRegister<Register>();
3654 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003655 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3656
3657 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(
3658 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
3659 codegen_->AddSlowPath(slow_path);
3660
3661 // TODO: avoid this check if we know obj is not null.
3662 __ cmp(obj, ShifterOperand(0));
3663 __ b(slow_path->GetExitLabel(), EQ);
3664 // Compare the class of `obj` with `cls`.
3665 __ LoadFromOffset(kLoadWord, temp, obj, class_offset);
3666 __ cmp(temp, ShifterOperand(cls));
3667 __ b(slow_path->GetEntryLabel(), NE);
3668 __ Bind(slow_path->GetExitLabel());
3669}
3670
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003671void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
3672 LocationSummary* locations =
3673 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3674 InvokeRuntimeCallingConvention calling_convention;
3675 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3676}
3677
3678void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
3679 codegen_->InvokeRuntime(instruction->IsEnter()
3680 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
3681 instruction,
3682 instruction->GetDexPc());
3683}
3684
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003685void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
3686void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
3687void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
3688
3689void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
3690 LocationSummary* locations =
3691 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3692 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
3693 || instruction->GetResultType() == Primitive::kPrimLong);
3694 locations->SetInAt(0, Location::RequiresRegister());
3695 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003696 Location::OutputOverlap output_overlaps = (instruction->GetResultType() == Primitive::kPrimLong)
3697 ? Location::kOutputOverlap
3698 : Location::kNoOutputOverlap;
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003699 locations->SetOut(Location::RequiresRegister(), output_overlaps);
3700}
3701
3702void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
3703 HandleBitwiseOperation(instruction);
3704}
3705
3706void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
3707 HandleBitwiseOperation(instruction);
3708}
3709
3710void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
3711 HandleBitwiseOperation(instruction);
3712}
3713
3714void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
3715 LocationSummary* locations = instruction->GetLocations();
3716
3717 if (instruction->GetResultType() == Primitive::kPrimInt) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003718 Register first = locations->InAt(0).AsRegister<Register>();
3719 Register second = locations->InAt(1).AsRegister<Register>();
3720 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003721 if (instruction->IsAnd()) {
3722 __ and_(out, first, ShifterOperand(second));
3723 } else if (instruction->IsOr()) {
3724 __ orr(out, first, ShifterOperand(second));
3725 } else {
3726 DCHECK(instruction->IsXor());
3727 __ eor(out, first, ShifterOperand(second));
3728 }
3729 } else {
3730 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3731 Location first = locations->InAt(0);
3732 Location second = locations->InAt(1);
3733 Location out = locations->Out();
3734 if (instruction->IsAnd()) {
3735 __ and_(out.AsRegisterPairLow<Register>(),
3736 first.AsRegisterPairLow<Register>(),
3737 ShifterOperand(second.AsRegisterPairLow<Register>()));
3738 __ and_(out.AsRegisterPairHigh<Register>(),
3739 first.AsRegisterPairHigh<Register>(),
3740 ShifterOperand(second.AsRegisterPairHigh<Register>()));
3741 } else if (instruction->IsOr()) {
3742 __ orr(out.AsRegisterPairLow<Register>(),
3743 first.AsRegisterPairLow<Register>(),
3744 ShifterOperand(second.AsRegisterPairLow<Register>()));
3745 __ orr(out.AsRegisterPairHigh<Register>(),
3746 first.AsRegisterPairHigh<Register>(),
3747 ShifterOperand(second.AsRegisterPairHigh<Register>()));
3748 } else {
3749 DCHECK(instruction->IsXor());
3750 __ eor(out.AsRegisterPairLow<Register>(),
3751 first.AsRegisterPairLow<Register>(),
3752 ShifterOperand(second.AsRegisterPairLow<Register>()));
3753 __ eor(out.AsRegisterPairHigh<Register>(),
3754 first.AsRegisterPairHigh<Register>(),
3755 ShifterOperand(second.AsRegisterPairHigh<Register>()));
3756 }
3757 }
3758}
3759
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003760} // namespace arm
3761} // namespace art