blob: 8c07b461735f5b8c943875d440ed31ad7ebdc6ce [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,
393 kNumberOfRegisterPairs, 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 Geoffray71175b72014-10-09 22:13:55 +0100456void CodeGeneratorARM::SetupBlockedRegisters() 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()) {
2455 case Primitive::kPrimBoolean:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002456 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002457 break;
2458
2459 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002460 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002461 break;
2462
2463 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01002464 __ mvn(out.AsRegisterPairLow<Register>(),
2465 ShifterOperand(in.AsRegisterPairLow<Register>()));
2466 __ mvn(out.AsRegisterPairHigh<Register>(),
2467 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002468 break;
2469
2470 default:
2471 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2472 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002473}
2474
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002475void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002476 LocationSummary* locations =
2477 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00002478 switch (compare->InputAt(0)->GetType()) {
2479 case Primitive::kPrimLong: {
2480 locations->SetInAt(0, Location::RequiresRegister());
2481 locations->SetInAt(1, Location::RequiresRegister());
2482 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2483 break;
2484 }
2485 case Primitive::kPrimFloat:
2486 case Primitive::kPrimDouble: {
2487 locations->SetInAt(0, Location::RequiresFpuRegister());
2488 locations->SetInAt(1, Location::RequiresFpuRegister());
2489 locations->SetOut(Location::RequiresRegister());
2490 break;
2491 }
2492 default:
2493 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
2494 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002495}
2496
2497void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002498 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002499 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00002500 Location left = locations->InAt(0);
2501 Location right = locations->InAt(1);
2502
2503 Label less, greater, done;
2504 Primitive::Type type = compare->InputAt(0)->GetType();
2505 switch (type) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002506 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002507 __ cmp(left.AsRegisterPairHigh<Register>(),
2508 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002509 __ b(&less, LT);
2510 __ b(&greater, GT);
Calin Juravleddb7df22014-11-25 20:56:51 +00002511 // Do LoadImmediate before any `cmp`, as LoadImmediate might affect the status flags.
2512 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002513 __ cmp(left.AsRegisterPairLow<Register>(),
2514 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Calin Juravleddb7df22014-11-25 20:56:51 +00002515 break;
2516 }
2517 case Primitive::kPrimFloat:
2518 case Primitive::kPrimDouble: {
2519 __ LoadImmediate(out, 0);
2520 if (type == Primitive::kPrimFloat) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002521 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00002522 } else {
2523 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
2524 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
2525 }
2526 __ vmstat(); // transfer FP status register to ARM APSR.
2527 __ b(compare->IsGtBias() ? &greater : &less, VS); // VS for unordered.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002528 break;
2529 }
2530 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00002531 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002532 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002533 __ b(&done, EQ);
2534 __ b(&less, CC); // CC is for both: unsigned compare for longs and 'less than' for floats.
2535
2536 __ Bind(&greater);
2537 __ LoadImmediate(out, 1);
2538 __ b(&done);
2539
2540 __ Bind(&less);
2541 __ LoadImmediate(out, -1);
2542
2543 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002544}
2545
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002546void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002547 LocationSummary* locations =
2548 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01002549 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2550 locations->SetInAt(i, Location::Any());
2551 }
2552 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002553}
2554
2555void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002556 UNUSED(instruction);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002557 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002558}
2559
Calin Juravle52c48962014-12-16 17:02:57 +00002560void InstructionCodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
2561 // TODO (ported from quick): revisit Arm barrier kinds
2562 DmbOptions flavour = DmbOptions::ISH; // quiet c++ warnings
2563 switch (kind) {
2564 case MemBarrierKind::kAnyStore:
2565 case MemBarrierKind::kLoadAny:
2566 case MemBarrierKind::kAnyAny: {
2567 flavour = DmbOptions::ISH;
2568 break;
2569 }
2570 case MemBarrierKind::kStoreStore: {
2571 flavour = DmbOptions::ISHST;
2572 break;
2573 }
2574 default:
2575 LOG(FATAL) << "Unexpected memory barrier " << kind;
2576 }
2577 __ dmb(flavour);
2578}
2579
2580void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
2581 uint32_t offset,
2582 Register out_lo,
2583 Register out_hi) {
2584 if (offset != 0) {
2585 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00002586 __ add(IP, addr, ShifterOperand(out_lo));
2587 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00002588 }
2589 __ ldrexd(out_lo, out_hi, addr);
2590}
2591
2592void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
2593 uint32_t offset,
2594 Register value_lo,
2595 Register value_hi,
2596 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00002597 Register temp2,
2598 HInstruction* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00002599 Label fail;
2600 if (offset != 0) {
2601 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00002602 __ add(IP, addr, ShifterOperand(temp1));
2603 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00002604 }
2605 __ Bind(&fail);
2606 // We need a load followed by store. (The address used in a STREX instruction must
2607 // be the same as the address in the most recently executed LDREX instruction.)
2608 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00002609 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002610 __ strexd(temp1, value_lo, value_hi, addr);
2611 __ cmp(temp1, ShifterOperand(0));
2612 __ b(&fail, NE);
2613}
2614
2615void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
2616 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2617
Nicolas Geoffray39468442014-09-02 15:17:15 +01002618 LocationSummary* locations =
2619 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002620 locations->SetInAt(0, Location::RequiresRegister());
2621 locations->SetInAt(1, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00002622
Calin Juravle34166012014-12-19 17:22:29 +00002623
Calin Juravle52c48962014-12-16 17:02:57 +00002624 Primitive::Type field_type = field_info.GetFieldType();
2625 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00002626 bool generate_volatile = field_info.IsVolatile()
2627 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002628 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002629 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00002630 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
2631 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002632 locations->AddTemp(Location::RequiresRegister());
2633 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00002634 } else if (generate_volatile) {
Calin Juravle52c48962014-12-16 17:02:57 +00002635 // Arm encoding have some additional constraints for ldrexd/strexd:
2636 // - registers need to be consecutive
2637 // - the first register should be even but not R14.
2638 // We don't test for Arm yet, and the assertion makes sure that we revisit this if we ever
2639 // enable Arm encoding.
2640 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
2641
2642 locations->AddTemp(Location::RequiresRegister());
2643 locations->AddTemp(Location::RequiresRegister());
2644 if (field_type == Primitive::kPrimDouble) {
2645 // For doubles we need two more registers to copy the value.
2646 locations->AddTemp(Location::RegisterLocation(R2));
2647 locations->AddTemp(Location::RegisterLocation(R3));
2648 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002649 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002650}
2651
Calin Juravle52c48962014-12-16 17:02:57 +00002652void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
2653 const FieldInfo& field_info) {
2654 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2655
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002656 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00002657 Register base = locations->InAt(0).AsRegister<Register>();
2658 Location value = locations->InAt(1);
2659
2660 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002661 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00002662 Primitive::Type field_type = field_info.GetFieldType();
2663 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2664
2665 if (is_volatile) {
2666 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
2667 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002668
2669 switch (field_type) {
2670 case Primitive::kPrimBoolean:
2671 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00002672 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002673 break;
2674 }
2675
2676 case Primitive::kPrimShort:
2677 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00002678 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002679 break;
2680 }
2681
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002682 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002683 case Primitive::kPrimNot: {
Calin Juravle77520bc2015-01-12 18:45:46 +00002684 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002685 break;
2686 }
2687
2688 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00002689 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00002690 GenerateWideAtomicStore(base, offset,
2691 value.AsRegisterPairLow<Register>(),
2692 value.AsRegisterPairHigh<Register>(),
2693 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00002694 locations->GetTemp(1).AsRegister<Register>(),
2695 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002696 } else {
2697 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00002698 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002699 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002700 break;
2701 }
2702
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002703 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00002704 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002705 break;
2706 }
2707
2708 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00002709 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00002710 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00002711 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
2712 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
2713
2714 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
2715
2716 GenerateWideAtomicStore(base, offset,
2717 value_reg_lo,
2718 value_reg_hi,
2719 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00002720 locations->GetTemp(3).AsRegister<Register>(),
2721 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002722 } else {
2723 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00002724 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002725 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002726 break;
2727 }
2728
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002729 case Primitive::kPrimVoid:
2730 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002731 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002732 }
Calin Juravle52c48962014-12-16 17:02:57 +00002733
Calin Juravle77520bc2015-01-12 18:45:46 +00002734 // Longs and doubles are handled in the switch.
2735 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
2736 codegen_->MaybeRecordImplicitNullCheck(instruction);
2737 }
2738
2739 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
2740 Register temp = locations->GetTemp(0).AsRegister<Register>();
2741 Register card = locations->GetTemp(1).AsRegister<Register>();
2742 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>());
2743 }
2744
Calin Juravle52c48962014-12-16 17:02:57 +00002745 if (is_volatile) {
2746 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
2747 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002748}
2749
Calin Juravle52c48962014-12-16 17:02:57 +00002750void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
2751 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002752 LocationSummary* locations =
2753 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002754 locations->SetInAt(0, Location::RequiresRegister());
2755 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle52c48962014-12-16 17:02:57 +00002756
Calin Juravle34166012014-12-19 17:22:29 +00002757 bool generate_volatile = field_info.IsVolatile()
2758 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002759 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle34166012014-12-19 17:22:29 +00002760 if (generate_volatile) {
Calin Juravle52c48962014-12-16 17:02:57 +00002761 // Arm encoding have some additional constraints for ldrexd/strexd:
2762 // - registers need to be consecutive
2763 // - the first register should be even but not R14.
2764 // We don't test for Arm yet, and the assertion makes sure that we revisit this if we ever
2765 // enable Arm encoding.
2766 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
2767 locations->AddTemp(Location::RequiresRegister());
2768 locations->AddTemp(Location::RequiresRegister());
2769 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002770}
2771
Calin Juravle52c48962014-12-16 17:02:57 +00002772void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
2773 const FieldInfo& field_info) {
2774 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002775
Calin Juravle52c48962014-12-16 17:02:57 +00002776 LocationSummary* locations = instruction->GetLocations();
2777 Register base = locations->InAt(0).AsRegister<Register>();
2778 Location out = locations->Out();
2779 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002780 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00002781 Primitive::Type field_type = field_info.GetFieldType();
2782 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2783
2784 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002785 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00002786 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002787 break;
2788 }
2789
2790 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00002791 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002792 break;
2793 }
2794
2795 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00002796 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002797 break;
2798 }
2799
2800 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00002801 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002802 break;
2803 }
2804
2805 case Primitive::kPrimInt:
2806 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00002807 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002808 break;
2809 }
2810
2811 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00002812 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00002813 GenerateWideAtomicLoad(base, offset,
2814 out.AsRegisterPairLow<Register>(),
2815 out.AsRegisterPairHigh<Register>());
2816 } else {
2817 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
2818 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002819 break;
2820 }
2821
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002822 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00002823 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002824 break;
2825 }
2826
2827 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00002828 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00002829 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00002830 Register lo = locations->GetTemp(0).AsRegister<Register>();
2831 Register hi = locations->GetTemp(1).AsRegister<Register>();
2832 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00002833 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002834 __ vmovdrr(out_reg, lo, hi);
2835 } else {
2836 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00002837 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002838 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002839 break;
2840 }
2841
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002842 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00002843 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002844 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002845 }
Calin Juravle52c48962014-12-16 17:02:57 +00002846
Calin Juravle77520bc2015-01-12 18:45:46 +00002847 // Doubles are handled in the switch.
2848 if (field_type != Primitive::kPrimDouble) {
2849 codegen_->MaybeRecordImplicitNullCheck(instruction);
2850 }
2851
Calin Juravle52c48962014-12-16 17:02:57 +00002852 if (is_volatile) {
2853 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
2854 }
2855}
2856
2857void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2858 HandleFieldSet(instruction, instruction->GetFieldInfo());
2859}
2860
2861void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2862 HandleFieldSet(instruction, instruction->GetFieldInfo());
2863}
2864
2865void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2866 HandleFieldGet(instruction, instruction->GetFieldInfo());
2867}
2868
2869void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2870 HandleFieldGet(instruction, instruction->GetFieldInfo());
2871}
2872
2873void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2874 HandleFieldGet(instruction, instruction->GetFieldInfo());
2875}
2876
2877void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2878 HandleFieldGet(instruction, instruction->GetFieldInfo());
2879}
2880
2881void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2882 HandleFieldSet(instruction, instruction->GetFieldInfo());
2883}
2884
2885void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2886 HandleFieldSet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002887}
2888
2889void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002890 LocationSummary* locations =
2891 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle77520bc2015-01-12 18:45:46 +00002892 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002893 if (instruction->HasUses()) {
2894 locations->SetOut(Location::SameAsFirstInput());
2895 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002896}
2897
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002898void InstructionCodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00002899 if (codegen_->CanMoveNullCheckToUser(instruction)) {
2900 return;
2901 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002902 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00002903
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002904 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
2905 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2906}
2907
2908void InstructionCodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002909 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002910 codegen_->AddSlowPath(slow_path);
2911
2912 LocationSummary* locations = instruction->GetLocations();
2913 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002914
Calin Juravle77520bc2015-01-12 18:45:46 +00002915 __ cmp(obj.AsRegister<Register>(), ShifterOperand(0));
2916 __ b(slow_path->GetEntryLabel(), EQ);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002917}
2918
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002919void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
2920 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
2921 GenerateImplicitNullCheck(instruction);
2922 } else {
2923 GenerateExplicitNullCheck(instruction);
2924 }
2925}
2926
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002927void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002928 LocationSummary* locations =
2929 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002930 locations->SetInAt(0, Location::RequiresRegister());
2931 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2932 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002933}
2934
2935void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
2936 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002937 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002938 Location index = locations->InAt(1);
2939
2940 switch (instruction->GetType()) {
2941 case Primitive::kPrimBoolean: {
2942 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002943 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002944 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002945 size_t offset =
2946 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002947 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
2948 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002949 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002950 __ LoadFromOffset(kLoadUnsignedByte, out, IP, data_offset);
2951 }
2952 break;
2953 }
2954
2955 case Primitive::kPrimByte: {
2956 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002957 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002958 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002959 size_t offset =
2960 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002961 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
2962 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002963 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002964 __ LoadFromOffset(kLoadSignedByte, out, IP, data_offset);
2965 }
2966 break;
2967 }
2968
2969 case Primitive::kPrimShort: {
2970 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002971 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002972 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002973 size_t offset =
2974 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002975 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
2976 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002977 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002978 __ LoadFromOffset(kLoadSignedHalfword, out, IP, data_offset);
2979 }
2980 break;
2981 }
2982
2983 case Primitive::kPrimChar: {
2984 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002985 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002986 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002987 size_t offset =
2988 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002989 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
2990 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002991 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002992 __ LoadFromOffset(kLoadUnsignedHalfword, out, IP, data_offset);
2993 }
2994 break;
2995 }
2996
2997 case Primitive::kPrimInt:
2998 case Primitive::kPrimNot: {
2999 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
3000 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003001 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003002 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003003 size_t offset =
3004 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003005 __ LoadFromOffset(kLoadWord, out, obj, offset);
3006 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003007 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003008 __ LoadFromOffset(kLoadWord, out, IP, data_offset);
3009 }
3010 break;
3011 }
3012
3013 case Primitive::kPrimLong: {
3014 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003015 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003016 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003017 size_t offset =
3018 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003019 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003020 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003021 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003022 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003023 }
3024 break;
3025 }
3026
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003027 case Primitive::kPrimFloat: {
3028 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3029 Location out = locations->Out();
3030 DCHECK(out.IsFpuRegister());
3031 if (index.IsConstant()) {
3032 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3033 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), obj, offset);
3034 } else {
3035 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
3036 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), IP, data_offset);
3037 }
3038 break;
3039 }
3040
3041 case Primitive::kPrimDouble: {
3042 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3043 Location out = locations->Out();
3044 DCHECK(out.IsFpuRegisterPair());
3045 if (index.IsConstant()) {
3046 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3047 __ LoadDFromOffset(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()), obj, offset);
3048 } else {
3049 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
3050 __ LoadDFromOffset(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
3051 }
3052 break;
3053 }
3054
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003055 case Primitive::kPrimVoid:
3056 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003057 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003058 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003059 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003060}
3061
3062void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003063 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003064
3065 bool needs_write_barrier =
3066 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3067 bool needs_runtime_call = instruction->NeedsTypeCheck();
3068
Nicolas Geoffray39468442014-09-02 15:17:15 +01003069 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003070 instruction, needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
3071 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003072 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003073 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3074 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3075 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003076 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003077 locations->SetInAt(0, Location::RequiresRegister());
3078 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3079 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003080
3081 if (needs_write_barrier) {
3082 // Temporary registers for the write barrier.
3083 locations->AddTemp(Location::RequiresRegister());
3084 locations->AddTemp(Location::RequiresRegister());
3085 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003086 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003087}
3088
3089void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
3090 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003091 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003092 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003093 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003094 bool needs_runtime_call = locations->WillCall();
3095 bool needs_write_barrier =
3096 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003097
3098 switch (value_type) {
3099 case Primitive::kPrimBoolean:
3100 case Primitive::kPrimByte: {
3101 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003102 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003103 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003104 size_t offset =
3105 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003106 __ StoreToOffset(kStoreByte, value, obj, offset);
3107 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003108 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003109 __ StoreToOffset(kStoreByte, value, IP, data_offset);
3110 }
3111 break;
3112 }
3113
3114 case Primitive::kPrimShort:
3115 case Primitive::kPrimChar: {
3116 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003117 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003118 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003119 size_t offset =
3120 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003121 __ StoreToOffset(kStoreHalfword, value, obj, offset);
3122 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003123 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003124 __ StoreToOffset(kStoreHalfword, value, IP, data_offset);
3125 }
3126 break;
3127 }
3128
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003129 case Primitive::kPrimInt:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003130 case Primitive::kPrimNot: {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003131 if (!needs_runtime_call) {
3132 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003133 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003134 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003135 size_t offset =
3136 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003137 __ StoreToOffset(kStoreWord, value, obj, offset);
3138 } else {
3139 DCHECK(index.IsRegister()) << index;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003140 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003141 __ StoreToOffset(kStoreWord, value, IP, data_offset);
3142 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003143 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003144 if (needs_write_barrier) {
3145 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003146 Register temp = locations->GetTemp(0).AsRegister<Register>();
3147 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003148 codegen_->MarkGCCard(temp, card, obj, value);
3149 }
3150 } else {
3151 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain199f3362014-11-27 17:15:16 +00003152 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
3153 instruction,
3154 instruction->GetDexPc());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003155 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003156 break;
3157 }
3158
3159 case Primitive::kPrimLong: {
3160 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003161 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003162 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003163 size_t offset =
3164 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003165 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003166 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003167 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003168 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003169 }
3170 break;
3171 }
3172
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003173 case Primitive::kPrimFloat: {
3174 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3175 Location value = locations->InAt(2);
3176 DCHECK(value.IsFpuRegister());
3177 if (index.IsConstant()) {
3178 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3179 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), obj, offset);
3180 } else {
3181 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
3182 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
3183 }
3184 break;
3185 }
3186
3187 case Primitive::kPrimDouble: {
3188 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3189 Location value = locations->InAt(2);
3190 DCHECK(value.IsFpuRegisterPair());
3191 if (index.IsConstant()) {
3192 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3193 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), obj, offset);
3194 } else {
3195 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
3196 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
3197 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003198
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003199 break;
3200 }
3201
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003202 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003203 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003204 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003205 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003206
3207 // Ints and objects are handled in the switch.
3208 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
3209 codegen_->MaybeRecordImplicitNullCheck(instruction);
3210 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003211}
3212
3213void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003214 LocationSummary* locations =
3215 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003216 locations->SetInAt(0, Location::RequiresRegister());
3217 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003218}
3219
3220void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
3221 LocationSummary* locations = instruction->GetLocations();
3222 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003223 Register obj = locations->InAt(0).AsRegister<Register>();
3224 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003225 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003226 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003227}
3228
3229void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003230 LocationSummary* locations =
3231 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003232 locations->SetInAt(0, Location::RequiresRegister());
3233 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003234 if (instruction->HasUses()) {
3235 locations->SetOut(Location::SameAsFirstInput());
3236 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003237}
3238
3239void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
3240 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003241 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(
Nicolas Geoffray39468442014-09-02 15:17:15 +01003242 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003243 codegen_->AddSlowPath(slow_path);
3244
Roland Levillain271ab9c2014-11-27 15:23:57 +00003245 Register index = locations->InAt(0).AsRegister<Register>();
3246 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003247
3248 __ cmp(index, ShifterOperand(length));
3249 __ b(slow_path->GetEntryLabel(), CS);
3250}
3251
3252void CodeGeneratorARM::MarkGCCard(Register temp, Register card, Register object, Register value) {
3253 Label is_null;
3254 __ CompareAndBranchIfZero(value, &is_null);
3255 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmWordSize>().Int32Value());
3256 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
3257 __ strb(card, Address(card, temp));
3258 __ Bind(&is_null);
3259}
3260
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003261void LocationsBuilderARM::VisitTemporary(HTemporary* temp) {
3262 temp->SetLocations(nullptr);
3263}
3264
3265void InstructionCodeGeneratorARM::VisitTemporary(HTemporary* temp) {
3266 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003267 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003268}
3269
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003270void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003271 UNUSED(instruction);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003272 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003273}
3274
3275void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003276 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3277}
3278
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003279void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
3280 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3281}
3282
3283void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003284 HBasicBlock* block = instruction->GetBlock();
3285 if (block->GetLoopInformation() != nullptr) {
3286 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3287 // The back edge will generate the suspend check.
3288 return;
3289 }
3290 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3291 // The goto will generate the suspend check.
3292 return;
3293 }
3294 GenerateSuspendCheck(instruction, nullptr);
3295}
3296
3297void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
3298 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003299 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003300 new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003301 codegen_->AddSlowPath(slow_path);
3302
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00003303 __ LoadFromOffset(
3304 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmWordSize>().Int32Value());
3305 __ cmp(IP, ShifterOperand(0));
3306 // TODO: Figure out the branch offsets and use cbz/cbnz.
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003307 if (successor == nullptr) {
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00003308 __ b(slow_path->GetEntryLabel(), NE);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003309 __ Bind(slow_path->GetReturnLabel());
3310 } else {
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00003311 __ b(codegen_->GetLabelOf(successor), EQ);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003312 __ b(slow_path->GetEntryLabel());
3313 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003314}
3315
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003316ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
3317 return codegen_->GetAssembler();
3318}
3319
3320void ParallelMoveResolverARM::EmitMove(size_t index) {
3321 MoveOperands* move = moves_.Get(index);
3322 Location source = move->GetSource();
3323 Location destination = move->GetDestination();
3324
3325 if (source.IsRegister()) {
3326 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003327 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003328 } else {
3329 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003330 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003331 SP, destination.GetStackIndex());
3332 }
3333 } else if (source.IsStackSlot()) {
3334 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003335 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003336 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003337 } else if (destination.IsFpuRegister()) {
3338 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003339 } else {
3340 DCHECK(destination.IsStackSlot());
3341 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
3342 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
3343 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003344 } else if (source.IsFpuRegister()) {
3345 if (destination.IsFpuRegister()) {
3346 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003347 } else {
3348 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003349 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
3350 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003351 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003352 DCHECK(destination.IsDoubleStackSlot()) << destination;
3353 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
3354 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
3355 __ LoadFromOffset(kLoadWord, IP, SP, source.GetHighStackIndex(kArmWordSize));
3356 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003357 } else {
3358 DCHECK(source.IsConstant()) << source;
3359 HInstruction* constant = source.GetConstant();
3360 if (constant->IsIntConstant()) {
3361 int32_t value = constant->AsIntConstant()->GetValue();
3362 if (destination.IsRegister()) {
3363 __ LoadImmediate(destination.AsRegister<Register>(), value);
3364 } else {
3365 DCHECK(destination.IsStackSlot());
3366 __ LoadImmediate(IP, value);
3367 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
3368 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003369 } else if (constant->IsLongConstant()) {
3370 int64_t value = constant->AsLongConstant()->GetValue();
3371 if (destination.IsRegister()) {
3372 // In the presence of long or double constants, the parallel move resolver will
3373 // split the move into two, but keeps the same constant for both moves. Here,
3374 // we use the low or high part depending on which register this move goes to.
3375 if (destination.reg() % 2 == 0) {
3376 __ LoadImmediate(destination.AsRegister<Register>(), Low32Bits(value));
3377 } else {
3378 __ LoadImmediate(destination.AsRegister<Register>(), High32Bits(value));
3379 }
3380 } else {
3381 DCHECK(destination.IsDoubleStackSlot());
3382 __ LoadImmediate(IP, Low32Bits(value));
3383 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
3384 __ LoadImmediate(IP, High32Bits(value));
3385 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
3386 }
3387 } else if (constant->IsDoubleConstant()) {
3388 double value = constant->AsDoubleConstant()->GetValue();
3389 uint64_t int_value = bit_cast<uint64_t, double>(value);
3390 if (destination.IsFpuRegister()) {
3391 // In the presence of long or double constants, the parallel move resolver will
3392 // split the move into two, but keeps the same constant for both moves. Here,
3393 // we use the low or high part depending on which register this move goes to.
3394 if (destination.reg() % 2 == 0) {
3395 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(),
3396 bit_cast<float, uint32_t>(Low32Bits(int_value)));
3397 } else {
3398 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(),
3399 bit_cast<float, uint32_t>(High32Bits(int_value)));
3400 }
3401 } else {
3402 DCHECK(destination.IsDoubleStackSlot());
3403 __ LoadImmediate(IP, Low32Bits(int_value));
3404 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
3405 __ LoadImmediate(IP, High32Bits(int_value));
3406 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
3407 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003408 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003409 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003410 float value = constant->AsFloatConstant()->GetValue();
3411 if (destination.IsFpuRegister()) {
3412 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
3413 } else {
3414 DCHECK(destination.IsStackSlot());
3415 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
3416 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
3417 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003418 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003419 }
3420}
3421
3422void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
3423 __ Mov(IP, reg);
3424 __ LoadFromOffset(kLoadWord, reg, SP, mem);
3425 __ StoreToOffset(kStoreWord, IP, SP, mem);
3426}
3427
3428void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
3429 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
3430 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
3431 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
3432 SP, mem1 + stack_offset);
3433 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
3434 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
3435 SP, mem2 + stack_offset);
3436 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
3437}
3438
3439void ParallelMoveResolverARM::EmitSwap(size_t index) {
3440 MoveOperands* move = moves_.Get(index);
3441 Location source = move->GetSource();
3442 Location destination = move->GetDestination();
3443
3444 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003445 DCHECK_NE(source.AsRegister<Register>(), IP);
3446 DCHECK_NE(destination.AsRegister<Register>(), IP);
3447 __ Mov(IP, source.AsRegister<Register>());
3448 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
3449 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003450 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003451 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003452 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003453 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003454 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
3455 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003456 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00003457 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003458 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00003459 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003460 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
3461 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
3462 : destination.AsFpuRegister<SRegister>();
3463 int mem = source.IsFpuRegister()
3464 ? destination.GetStackIndex()
3465 : source.GetStackIndex();
3466
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00003467 __ vmovrs(IP, reg);
3468 __ LoadFromOffset(kLoadWord, IP, SP, mem);
3469 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00003470 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00003471 Exchange(source.GetStackIndex(), destination.GetStackIndex());
3472 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003473 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00003474 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003475 }
3476}
3477
3478void ParallelMoveResolverARM::SpillScratch(int reg) {
3479 __ Push(static_cast<Register>(reg));
3480}
3481
3482void ParallelMoveResolverARM::RestoreScratch(int reg) {
3483 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003484}
3485
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003486void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003487 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
3488 ? LocationSummary::kCallOnSlowPath
3489 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003490 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003491 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003492 locations->SetOut(Location::RequiresRegister());
3493}
3494
3495void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003496 Register out = cls->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003497 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003498 DCHECK(!cls->CanCallRuntime());
3499 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003500 codegen_->LoadCurrentMethod(out);
3501 __ LoadFromOffset(kLoadWord, out, out, mirror::ArtMethod::DeclaringClassOffset().Int32Value());
3502 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003503 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003504 codegen_->LoadCurrentMethod(out);
3505 __ LoadFromOffset(
3506 kLoadWord, out, out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value());
3507 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003508
3509 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
3510 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3511 codegen_->AddSlowPath(slow_path);
3512 __ cmp(out, ShifterOperand(0));
3513 __ b(slow_path->GetEntryLabel(), EQ);
3514 if (cls->MustGenerateClinitCheck()) {
3515 GenerateClassInitializationCheck(slow_path, out);
3516 } else {
3517 __ Bind(slow_path->GetExitLabel());
3518 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003519 }
3520}
3521
3522void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
3523 LocationSummary* locations =
3524 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3525 locations->SetInAt(0, Location::RequiresRegister());
3526 if (check->HasUses()) {
3527 locations->SetOut(Location::SameAsFirstInput());
3528 }
3529}
3530
3531void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003532 // We assume the class is not null.
3533 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
3534 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003535 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00003536 GenerateClassInitializationCheck(slow_path,
3537 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003538}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003539
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003540void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
3541 SlowPathCodeARM* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003542 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
3543 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
3544 __ b(slow_path->GetEntryLabel(), LT);
3545 // Even if the initialized flag is set, we may be in a situation where caches are not synced
3546 // properly. Therefore, we do a memory fence.
3547 __ dmb(ISH);
3548 __ Bind(slow_path->GetExitLabel());
3549}
3550
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003551void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
3552 LocationSummary* locations =
3553 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3554 locations->SetOut(Location::RequiresRegister());
3555}
3556
3557void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) {
3558 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
3559 codegen_->AddSlowPath(slow_path);
3560
Roland Levillain271ab9c2014-11-27 15:23:57 +00003561 Register out = load->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003562 codegen_->LoadCurrentMethod(out);
Mathieu Chartiereace4582014-11-24 18:29:54 -08003563 __ LoadFromOffset(kLoadWord, out, out, mirror::ArtMethod::DeclaringClassOffset().Int32Value());
3564 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003565 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
3566 __ cmp(out, ShifterOperand(0));
3567 __ b(slow_path->GetEntryLabel(), EQ);
3568 __ Bind(slow_path->GetExitLabel());
3569}
3570
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003571void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
3572 LocationSummary* locations =
3573 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3574 locations->SetOut(Location::RequiresRegister());
3575}
3576
3577void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003578 Register out = load->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003579 int32_t offset = Thread::ExceptionOffset<kArmWordSize>().Int32Value();
3580 __ LoadFromOffset(kLoadWord, out, TR, offset);
3581 __ LoadImmediate(IP, 0);
3582 __ StoreToOffset(kStoreWord, IP, TR, offset);
3583}
3584
3585void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
3586 LocationSummary* locations =
3587 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3588 InvokeRuntimeCallingConvention calling_convention;
3589 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3590}
3591
3592void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
3593 codegen_->InvokeRuntime(
3594 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc());
3595}
3596
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003597void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003598 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
3599 ? LocationSummary::kNoCall
3600 : LocationSummary::kCallOnSlowPath;
3601 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3602 locations->SetInAt(0, Location::RequiresRegister());
3603 locations->SetInAt(1, Location::RequiresRegister());
3604 locations->SetOut(Location::RequiresRegister());
3605}
3606
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003607void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003608 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003609 Register obj = locations->InAt(0).AsRegister<Register>();
3610 Register cls = locations->InAt(1).AsRegister<Register>();
3611 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003612 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3613 Label done, zero;
3614 SlowPathCodeARM* slow_path = nullptr;
3615
3616 // Return 0 if `obj` is null.
3617 // TODO: avoid this check if we know obj is not null.
3618 __ cmp(obj, ShifterOperand(0));
3619 __ b(&zero, EQ);
3620 // Compare the class of `obj` with `cls`.
3621 __ LoadFromOffset(kLoadWord, out, obj, class_offset);
3622 __ cmp(out, ShifterOperand(cls));
3623 if (instruction->IsClassFinal()) {
3624 // Classes must be equal for the instanceof to succeed.
3625 __ b(&zero, NE);
3626 __ LoadImmediate(out, 1);
3627 __ b(&done);
3628 } else {
3629 // If the classes are not equal, we go into a slow path.
3630 DCHECK(locations->OnlyCallsOnSlowPath());
3631 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003632 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003633 codegen_->AddSlowPath(slow_path);
3634 __ b(slow_path->GetEntryLabel(), NE);
3635 __ LoadImmediate(out, 1);
3636 __ b(&done);
3637 }
3638 __ Bind(&zero);
3639 __ LoadImmediate(out, 0);
3640 if (slow_path != nullptr) {
3641 __ Bind(slow_path->GetExitLabel());
3642 }
3643 __ Bind(&done);
3644}
3645
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003646void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
3647 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3648 instruction, LocationSummary::kCallOnSlowPath);
3649 locations->SetInAt(0, Location::RequiresRegister());
3650 locations->SetInAt(1, Location::RequiresRegister());
3651 locations->AddTemp(Location::RequiresRegister());
3652}
3653
3654void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
3655 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003656 Register obj = locations->InAt(0).AsRegister<Register>();
3657 Register cls = locations->InAt(1).AsRegister<Register>();
3658 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003659 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3660
3661 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(
3662 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
3663 codegen_->AddSlowPath(slow_path);
3664
3665 // TODO: avoid this check if we know obj is not null.
3666 __ cmp(obj, ShifterOperand(0));
3667 __ b(slow_path->GetExitLabel(), EQ);
3668 // Compare the class of `obj` with `cls`.
3669 __ LoadFromOffset(kLoadWord, temp, obj, class_offset);
3670 __ cmp(temp, ShifterOperand(cls));
3671 __ b(slow_path->GetEntryLabel(), NE);
3672 __ Bind(slow_path->GetExitLabel());
3673}
3674
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003675void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
3676 LocationSummary* locations =
3677 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3678 InvokeRuntimeCallingConvention calling_convention;
3679 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3680}
3681
3682void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
3683 codegen_->InvokeRuntime(instruction->IsEnter()
3684 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
3685 instruction,
3686 instruction->GetDexPc());
3687}
3688
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003689void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
3690void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
3691void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
3692
3693void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
3694 LocationSummary* locations =
3695 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3696 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
3697 || instruction->GetResultType() == Primitive::kPrimLong);
3698 locations->SetInAt(0, Location::RequiresRegister());
3699 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003700 Location::OutputOverlap output_overlaps = (instruction->GetResultType() == Primitive::kPrimLong)
3701 ? Location::kOutputOverlap
3702 : Location::kNoOutputOverlap;
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003703 locations->SetOut(Location::RequiresRegister(), output_overlaps);
3704}
3705
3706void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
3707 HandleBitwiseOperation(instruction);
3708}
3709
3710void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
3711 HandleBitwiseOperation(instruction);
3712}
3713
3714void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
3715 HandleBitwiseOperation(instruction);
3716}
3717
3718void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
3719 LocationSummary* locations = instruction->GetLocations();
3720
3721 if (instruction->GetResultType() == Primitive::kPrimInt) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003722 Register first = locations->InAt(0).AsRegister<Register>();
3723 Register second = locations->InAt(1).AsRegister<Register>();
3724 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003725 if (instruction->IsAnd()) {
3726 __ and_(out, first, ShifterOperand(second));
3727 } else if (instruction->IsOr()) {
3728 __ orr(out, first, ShifterOperand(second));
3729 } else {
3730 DCHECK(instruction->IsXor());
3731 __ eor(out, first, ShifterOperand(second));
3732 }
3733 } else {
3734 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3735 Location first = locations->InAt(0);
3736 Location second = locations->InAt(1);
3737 Location out = locations->Out();
3738 if (instruction->IsAnd()) {
3739 __ and_(out.AsRegisterPairLow<Register>(),
3740 first.AsRegisterPairLow<Register>(),
3741 ShifterOperand(second.AsRegisterPairLow<Register>()));
3742 __ and_(out.AsRegisterPairHigh<Register>(),
3743 first.AsRegisterPairHigh<Register>(),
3744 ShifterOperand(second.AsRegisterPairHigh<Register>()));
3745 } else if (instruction->IsOr()) {
3746 __ orr(out.AsRegisterPairLow<Register>(),
3747 first.AsRegisterPairLow<Register>(),
3748 ShifterOperand(second.AsRegisterPairLow<Register>()));
3749 __ orr(out.AsRegisterPairHigh<Register>(),
3750 first.AsRegisterPairHigh<Register>(),
3751 ShifterOperand(second.AsRegisterPairHigh<Register>()));
3752 } else {
3753 DCHECK(instruction->IsXor());
3754 __ eor(out.AsRegisterPairLow<Register>(),
3755 first.AsRegisterPairLow<Register>(),
3756 ShifterOperand(second.AsRegisterPairLow<Register>()));
3757 __ eor(out.AsRegisterPairHigh<Register>(),
3758 first.AsRegisterPairHigh<Register>(),
3759 ShifterOperand(second.AsRegisterPairHigh<Register>()));
3760 }
3761 }
3762}
3763
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003764} // namespace arm
3765} // namespace art