blob: c6c1fd73f907cf4ccb0d87990838ab22f6e3c4bb [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000018
Calin Juravle34166012014-12-19 17:22:29 +000019#include "arch/arm/instruction_set_features_arm.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070020#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010021#include "gc/accounting/card_table.h"
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -080022#include "intrinsics.h"
23#include "intrinsics_arm.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070024#include "mirror/array-inl.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000025#include "mirror/art_method.h"
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010026#include "mirror/class.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070027#include "thread.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010028#include "utils/arm/assembler_arm.h"
29#include "utils/arm/managed_register_arm.h"
Roland Levillain946e1432014-11-11 17:35:19 +000030#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010031#include "utils/stack_checks.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000032
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000033namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010034
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000035namespace arm {
36
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +000037static bool ExpectedPairLayout(Location location) {
38 // We expected this for both core and fpu register pairs.
39 return ((location.low() & 1) == 0) && (location.low() + 1 == location.high());
40}
41
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010042static constexpr int kCurrentMethodStackOffset = 0;
43
Calin Juravled6fb6cf2014-11-11 19:07:44 +000044static constexpr Register kRuntimeParameterCoreRegisters[] = { R0, R1, R2, R3 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010045static constexpr size_t kRuntimeParameterCoreRegistersLength =
46 arraysize(kRuntimeParameterCoreRegisters);
Calin Juravled2ec87d2014-12-08 14:24:46 +000047static constexpr SRegister kRuntimeParameterFpuRegisters[] = { S0, S1, S2, S3 };
Roland Levillain624279f2014-12-04 11:54:28 +000048static constexpr size_t kRuntimeParameterFpuRegistersLength =
49 arraysize(kRuntimeParameterFpuRegisters);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +000050// We unconditionally allocate R5 to ensure we can do long operations
51// with baseline.
52static constexpr Register kCoreSavedRegisterForBaseline = R5;
53static constexpr Register kCoreCalleeSaves[] =
54 { R5, R6, R7, R8, R10, R11, PC };
55static constexpr SRegister kFpuCalleeSaves[] =
56 { S16, S17, S18, S19, S20, S21, S22, S23, S24, S25, S26, S27, S28, S29, S30, S31 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010057
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +000058class InvokeRuntimeCallingConvention : public CallingConvention<Register, SRegister> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010059 public:
60 InvokeRuntimeCallingConvention()
61 : CallingConvention(kRuntimeParameterCoreRegisters,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010062 kRuntimeParameterCoreRegistersLength,
63 kRuntimeParameterFpuRegisters,
64 kRuntimeParameterFpuRegistersLength) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010065
66 private:
67 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
68};
69
Nicolas Geoffraye5038322014-07-04 09:41:32 +010070#define __ reinterpret_cast<ArmAssembler*>(codegen->GetAssembler())->
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010071#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmWordSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010072
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010073class NullCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010074 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010075 explicit NullCheckSlowPathARM(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010076
Alexandre Rames67555f72014-11-18 10:55:16 +000077 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010078 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010079 __ Bind(GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010080 arm_codegen->InvokeRuntime(
81 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010082 }
83
84 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010085 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010086 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM);
87};
88
Calin Juravled0d48522014-11-04 16:40:20 +000089class DivZeroCheckSlowPathARM : public SlowPathCodeARM {
90 public:
91 explicit DivZeroCheckSlowPathARM(HDivZeroCheck* instruction) : instruction_(instruction) {}
92
Alexandre Rames67555f72014-11-18 10:55:16 +000093 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000094 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
95 __ Bind(GetEntryLabel());
96 arm_codegen->InvokeRuntime(
97 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc());
98 }
99
100 private:
101 HDivZeroCheck* const instruction_;
102 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM);
103};
104
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100105class SuspendCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000106 public:
Alexandre Rames67555f72014-11-18 10:55:16 +0000107 SuspendCheckSlowPathARM(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100108 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000109
Alexandre Rames67555f72014-11-18 10:55:16 +0000110 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100111 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000112 __ Bind(GetEntryLabel());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100113 codegen->SaveLiveRegisters(instruction_->GetLocations());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100114 arm_codegen->InvokeRuntime(
115 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100116 codegen->RestoreLiveRegisters(instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100117 if (successor_ == nullptr) {
118 __ b(GetReturnLabel());
119 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100120 __ b(arm_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100121 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000122 }
123
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100124 Label* GetReturnLabel() {
125 DCHECK(successor_ == nullptr);
126 return &return_label_;
127 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000128
129 private:
130 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100131 // If not null, the block to branch to after the suspend check.
132 HBasicBlock* const successor_;
133
134 // If `successor_` is null, the label to branch to after the suspend check.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000135 Label return_label_;
136
137 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM);
138};
139
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100140class BoundsCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100141 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100142 BoundsCheckSlowPathARM(HBoundsCheck* instruction,
143 Location index_location,
144 Location length_location)
Nicolas Geoffray39468442014-09-02 15:17:15 +0100145 : instruction_(instruction),
146 index_location_(index_location),
147 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100148
Alexandre Rames67555f72014-11-18 10:55:16 +0000149 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100150 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100151 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000152 // We're moving two locations to locations that could overlap, so we need a parallel
153 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100154 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000155 codegen->EmitParallelMoves(
156 index_location_,
157 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
158 length_location_,
159 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100160 arm_codegen->InvokeRuntime(
161 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100162 }
163
164 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100165 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100166 const Location index_location_;
167 const Location length_location_;
168
169 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM);
170};
171
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000172class LoadClassSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100173 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000174 LoadClassSlowPathARM(HLoadClass* cls,
175 HInstruction* at,
176 uint32_t dex_pc,
177 bool do_clinit)
178 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
179 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
180 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100181
Alexandre Rames67555f72014-11-18 10:55:16 +0000182 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000183 LocationSummary* locations = at_->GetLocations();
184
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100185 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
186 __ Bind(GetEntryLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000187 codegen->SaveLiveRegisters(locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100188
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100189 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000190 __ LoadImmediate(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100191 arm_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000192 int32_t entry_point_offset = do_clinit_
193 ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
194 : QUICK_ENTRY_POINT(pInitializeType);
195 arm_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_);
196
197 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000198 Location out = locations->Out();
199 if (out.IsValid()) {
200 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000201 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
202 }
203 codegen->RestoreLiveRegisters(locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100204 __ b(GetExitLabel());
205 }
206
207 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000208 // The class this slow path will load.
209 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100210
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000211 // The instruction where this slow path is happening.
212 // (Might be the load class or an initialization check).
213 HInstruction* const at_;
214
215 // The dex PC of `at_`.
216 const uint32_t dex_pc_;
217
218 // Whether to initialize the class.
219 const bool do_clinit_;
220
221 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100222};
223
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000224class LoadStringSlowPathARM : public SlowPathCodeARM {
225 public:
226 explicit LoadStringSlowPathARM(HLoadString* instruction) : instruction_(instruction) {}
227
Alexandre Rames67555f72014-11-18 10:55:16 +0000228 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000229 LocationSummary* locations = instruction_->GetLocations();
230 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
231
232 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
233 __ Bind(GetEntryLabel());
234 codegen->SaveLiveRegisters(locations);
235
236 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800237 arm_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
238 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction_->GetStringIndex());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000239 arm_codegen->InvokeRuntime(
240 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc());
241 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
242
243 codegen->RestoreLiveRegisters(locations);
244 __ b(GetExitLabel());
245 }
246
247 private:
248 HLoadString* const instruction_;
249
250 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM);
251};
252
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000253class TypeCheckSlowPathARM : public SlowPathCodeARM {
254 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000255 TypeCheckSlowPathARM(HInstruction* instruction,
256 Location class_to_check,
257 Location object_class,
258 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000259 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000260 class_to_check_(class_to_check),
261 object_class_(object_class),
262 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000263
Alexandre Rames67555f72014-11-18 10:55:16 +0000264 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000265 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000266 DCHECK(instruction_->IsCheckCast()
267 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000268
269 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
270 __ Bind(GetEntryLabel());
271 codegen->SaveLiveRegisters(locations);
272
273 // We're moving two locations to locations that could overlap, so we need a parallel
274 // move resolver.
275 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000276 codegen->EmitParallelMoves(
277 class_to_check_,
278 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
279 object_class_,
280 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000281
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000282 if (instruction_->IsInstanceOf()) {
283 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial), instruction_, dex_pc_);
284 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
285 } else {
286 DCHECK(instruction_->IsCheckCast());
287 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc_);
288 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000289
290 codegen->RestoreLiveRegisters(locations);
291 __ b(GetExitLabel());
292 }
293
294 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000295 HInstruction* const instruction_;
296 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000297 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000298 uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000299
300 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM);
301};
302
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000303#undef __
304
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100305#undef __
306#define __ reinterpret_cast<ArmAssembler*>(GetAssembler())->
Dave Allison20dfc792014-06-16 20:44:29 -0700307
308inline Condition ARMCondition(IfCondition cond) {
309 switch (cond) {
310 case kCondEQ: return EQ;
311 case kCondNE: return NE;
312 case kCondLT: return LT;
313 case kCondLE: return LE;
314 case kCondGT: return GT;
315 case kCondGE: return GE;
316 default:
317 LOG(FATAL) << "Unknown if condition";
318 }
319 return EQ; // Unreachable.
320}
321
322inline Condition ARMOppositeCondition(IfCondition cond) {
323 switch (cond) {
324 case kCondEQ: return NE;
325 case kCondNE: return EQ;
326 case kCondLT: return GE;
327 case kCondLE: return GT;
328 case kCondGT: return LE;
329 case kCondGE: return LT;
330 default:
331 LOG(FATAL) << "Unknown if condition";
332 }
333 return EQ; // Unreachable.
334}
335
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100336void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
337 stream << ArmManagedRegister::FromCoreRegister(Register(reg));
338}
339
340void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000341 stream << ArmManagedRegister::FromSRegister(SRegister(reg));
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100342}
343
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100344size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
345 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
346 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100347}
348
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100349size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
350 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
351 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100352}
353
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000354size_t CodeGeneratorARM::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
355 __ StoreSToOffset(static_cast<SRegister>(reg_id), SP, stack_index);
356 return kArmWordSize;
357}
358
359size_t CodeGeneratorARM::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
360 __ LoadSFromOffset(static_cast<SRegister>(reg_id), SP, stack_index);
361 return kArmWordSize;
362}
363
Calin Juravle34166012014-12-19 17:22:29 +0000364CodeGeneratorARM::CodeGeneratorARM(HGraph* graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000365 const ArmInstructionSetFeatures& isa_features,
366 const CompilerOptions& compiler_options)
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000367 : CodeGenerator(graph,
368 kNumberOfCoreRegisters,
369 kNumberOfSRegisters,
370 kNumberOfRegisterPairs,
371 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
372 arraysize(kCoreCalleeSaves)),
373 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
374 arraysize(kFpuCalleeSaves)),
375 compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100376 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100377 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100378 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100379 move_resolver_(graph->GetArena(), this),
Calin Juravle34166012014-12-19 17:22:29 +0000380 assembler_(true),
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000381 isa_features_(isa_features) {
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000382 // Save the PC register to mimic Quick.
383 AddAllocatedRegister(Location::RegisterLocation(PC));
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100384}
385
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100386Location CodeGeneratorARM::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100387 switch (type) {
388 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100389 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100390 ArmManagedRegister pair =
391 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100392 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
393 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
394
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100395 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
396 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100397 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100398 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100399 }
400
401 case Primitive::kPrimByte:
402 case Primitive::kPrimBoolean:
403 case Primitive::kPrimChar:
404 case Primitive::kPrimShort:
405 case Primitive::kPrimInt:
406 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100407 int reg = FindFreeEntry(blocked_core_registers_, kNumberOfCoreRegisters);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100408 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100409 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
410 ArmManagedRegister current =
411 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
412 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100413 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100414 }
415 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100416 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100417 }
418
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000419 case Primitive::kPrimFloat: {
420 int reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfSRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100421 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100422 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100423
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000424 case Primitive::kPrimDouble: {
Nicolas Geoffray3c035032014-10-28 10:46:40 +0000425 int reg = FindTwoFreeConsecutiveAlignedEntries(blocked_fpu_registers_, kNumberOfSRegisters);
426 DCHECK_EQ(reg % 2, 0);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000427 return Location::FpuRegisterPairLocation(reg, reg + 1);
428 }
429
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100430 case Primitive::kPrimVoid:
431 LOG(FATAL) << "Unreachable type " << type;
432 }
433
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100434 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100435}
436
Nicolas Geoffraya0bb2bd2015-01-26 12:49:35 +0000437void CodeGeneratorARM::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100438 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100439 blocked_register_pairs_[R1_R2] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100440
441 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100442 blocked_core_registers_[SP] = true;
443 blocked_core_registers_[LR] = true;
444 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100445
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100446 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100447 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100448
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100449 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100450 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100451
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000452 if (is_baseline) {
453 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
454 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
455 }
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000456
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000457 blocked_core_registers_[kCoreSavedRegisterForBaseline] = false;
458
459 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
460 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
461 }
462 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100463
464 UpdateBlockedPairRegisters();
465}
466
467void CodeGeneratorARM::UpdateBlockedPairRegisters() const {
468 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
469 ArmManagedRegister current =
470 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
471 if (blocked_core_registers_[current.AsRegisterPairLow()]
472 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
473 blocked_register_pairs_[i] = true;
474 }
475 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100476}
477
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100478InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
479 : HGraphVisitor(graph),
480 assembler_(codegen->GetAssembler()),
481 codegen_(codegen) {}
482
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000483static uint32_t LeastSignificantBit(uint32_t mask) {
484 // ffs starts at 1.
485 return ffs(mask) - 1;
486}
487
488void CodeGeneratorARM::ComputeSpillMask() {
489 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000490 // Save one extra register for baseline. Note that on thumb2, there is no easy
491 // instruction to restore just the PC, so this actually helps both baseline
492 // and non-baseline to save and restore at least two registers at entry and exit.
493 core_spill_mask_ |= (1 << kCoreSavedRegisterForBaseline);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000494 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
495 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
496 // We use vpush and vpop for saving and restoring floating point registers, which take
497 // a SRegister and the number of registers to save/restore after that SRegister. We
498 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
499 // but in the range.
500 if (fpu_spill_mask_ != 0) {
501 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
502 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
503 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
504 fpu_spill_mask_ |= (1 << i);
505 }
506 }
507}
508
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000509void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +0000510 bool skip_overflow_check =
511 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000512 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000513 __ Bind(&frame_entry_label_);
514
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000515 if (HasEmptyFrame()) {
516 return;
517 }
518
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100519 if (!skip_overflow_check) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000520 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
521 __ LoadFromOffset(kLoadWord, IP, IP, 0);
522 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100523 }
524
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000525 // PC is in the list of callee-save to mimic Quick, but we need to push
526 // LR at entry instead.
527 __ PushList((core_spill_mask_ & (~(1 << PC))) | 1 << LR);
528 if (fpu_spill_mask_ != 0) {
529 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
530 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
531 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000532 __ AddConstant(SP, -(GetFrameSize() - FrameEntrySpillSize()));
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100533 __ StoreToOffset(kStoreWord, R0, SP, 0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000534}
535
536void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000537 if (HasEmptyFrame()) {
538 __ bx(LR);
539 return;
540 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000541 __ AddConstant(SP, GetFrameSize() - FrameEntrySpillSize());
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000542 if (fpu_spill_mask_ != 0) {
543 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
544 __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
545 }
546 __ PopList(core_spill_mask_);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000547}
548
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100549void CodeGeneratorARM::Bind(HBasicBlock* block) {
550 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000551}
552
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100553Location CodeGeneratorARM::GetStackLocation(HLoadLocal* load) const {
554 switch (load->GetType()) {
555 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100556 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100557 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
558 break;
559
560 case Primitive::kPrimInt:
561 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100562 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100563 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100564
565 case Primitive::kPrimBoolean:
566 case Primitive::kPrimByte:
567 case Primitive::kPrimChar:
568 case Primitive::kPrimShort:
569 case Primitive::kPrimVoid:
570 LOG(FATAL) << "Unexpected type " << load->GetType();
571 }
572
573 LOG(FATAL) << "Unreachable";
574 return Location();
575}
576
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100577Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
578 switch (type) {
579 case Primitive::kPrimBoolean:
580 case Primitive::kPrimByte:
581 case Primitive::kPrimChar:
582 case Primitive::kPrimShort:
583 case Primitive::kPrimInt:
584 case Primitive::kPrimNot: {
585 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000586 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100587 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100588 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100589 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000590 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100591 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100592 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100593
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000594 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100595 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000596 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100597 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000598 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100599 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000600 if (calling_convention.GetRegisterAt(index) == R1) {
601 // Skip R1, and use R2_R3 instead.
602 gp_index_++;
603 index++;
604 }
605 }
606 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
607 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +0000608 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000609 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +0000610 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100611 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000612 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
613 }
614 }
615
616 case Primitive::kPrimFloat: {
617 uint32_t stack_index = stack_index_++;
618 if (float_index_ % 2 == 0) {
619 float_index_ = std::max(double_index_, float_index_);
620 }
621 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
622 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
623 } else {
624 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
625 }
626 }
627
628 case Primitive::kPrimDouble: {
629 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
630 uint32_t stack_index = stack_index_;
631 stack_index_ += 2;
632 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
633 uint32_t index = double_index_;
634 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000635 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000636 calling_convention.GetFpuRegisterAt(index),
637 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000638 DCHECK(ExpectedPairLayout(result));
639 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000640 } else {
641 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100642 }
643 }
644
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100645 case Primitive::kPrimVoid:
646 LOG(FATAL) << "Unexpected parameter type " << type;
647 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100648 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100649 return Location();
650}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100651
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000652Location InvokeDexCallingConventionVisitor::GetReturnLocation(Primitive::Type type) {
653 switch (type) {
654 case Primitive::kPrimBoolean:
655 case Primitive::kPrimByte:
656 case Primitive::kPrimChar:
657 case Primitive::kPrimShort:
658 case Primitive::kPrimInt:
659 case Primitive::kPrimNot: {
660 return Location::RegisterLocation(R0);
661 }
662
663 case Primitive::kPrimFloat: {
664 return Location::FpuRegisterLocation(S0);
665 }
666
667 case Primitive::kPrimLong: {
668 return Location::RegisterPairLocation(R0, R1);
669 }
670
671 case Primitive::kPrimDouble: {
672 return Location::FpuRegisterPairLocation(S0, S1);
673 }
674
675 case Primitive::kPrimVoid:
676 return Location();
677 }
678 UNREACHABLE();
679 return Location();
680}
681
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100682void CodeGeneratorARM::Move32(Location destination, Location source) {
683 if (source.Equals(destination)) {
684 return;
685 }
686 if (destination.IsRegister()) {
687 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000688 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100689 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000690 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100691 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000692 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100693 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100694 } else if (destination.IsFpuRegister()) {
695 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000696 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100697 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000698 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100699 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000700 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100701 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100702 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +0000703 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100704 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000705 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100706 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000707 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100708 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +0000709 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100710 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
711 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100712 }
713 }
714}
715
716void CodeGeneratorARM::Move64(Location destination, Location source) {
717 if (source.Equals(destination)) {
718 return;
719 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100720 if (destination.IsRegisterPair()) {
721 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000722 EmitParallelMoves(
723 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
724 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
725 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
726 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100727 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000728 UNIMPLEMENTED(FATAL);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100729 } else {
730 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000731 DCHECK(ExpectedPairLayout(destination));
732 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
733 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100734 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000735 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100736 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000737 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
738 SP,
739 source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100740 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000741 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100742 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100743 } else {
744 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100745 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000746 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100747 if (source.AsRegisterPairLow<Register>() == R1) {
748 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100749 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
750 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100751 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100752 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100753 SP, destination.GetStackIndex());
754 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000755 } else if (source.IsFpuRegisterPair()) {
756 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
757 SP,
758 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100759 } else {
760 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000761 EmitParallelMoves(
762 Location::StackSlot(source.GetStackIndex()),
763 Location::StackSlot(destination.GetStackIndex()),
764 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
765 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100766 }
767 }
768}
769
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100770void CodeGeneratorARM::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100771 LocationSummary* locations = instruction->GetLocations();
772 if (locations != nullptr && locations->Out().Equals(location)) {
773 return;
774 }
775
Calin Juravlea21f5982014-11-13 15:53:04 +0000776 if (locations != nullptr && locations->Out().IsConstant()) {
777 HConstant* const_to_move = locations->Out().GetConstant();
778 if (const_to_move->IsIntConstant()) {
779 int32_t value = const_to_move->AsIntConstant()->GetValue();
780 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000781 __ LoadImmediate(location.AsRegister<Register>(), value);
Calin Juravlea21f5982014-11-13 15:53:04 +0000782 } else {
783 DCHECK(location.IsStackSlot());
784 __ LoadImmediate(IP, value);
785 __ StoreToOffset(kStoreWord, IP, SP, location.GetStackIndex());
786 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000787 } else {
Nicolas Geoffray3747b482015-01-19 17:17:16 +0000788 DCHECK(const_to_move->IsLongConstant()) << const_to_move->DebugName();
Calin Juravlea21f5982014-11-13 15:53:04 +0000789 int64_t value = const_to_move->AsLongConstant()->GetValue();
790 if (location.IsRegisterPair()) {
791 __ LoadImmediate(location.AsRegisterPairLow<Register>(), Low32Bits(value));
792 __ LoadImmediate(location.AsRegisterPairHigh<Register>(), High32Bits(value));
793 } else {
794 DCHECK(location.IsDoubleStackSlot());
795 __ LoadImmediate(IP, Low32Bits(value));
796 __ StoreToOffset(kStoreWord, IP, SP, location.GetStackIndex());
797 __ LoadImmediate(IP, High32Bits(value));
798 __ StoreToOffset(kStoreWord, IP, SP, location.GetHighStackIndex(kArmWordSize));
799 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100800 }
Roland Levillain476df552014-10-09 17:51:36 +0100801 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100802 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
803 switch (instruction->GetType()) {
804 case Primitive::kPrimBoolean:
805 case Primitive::kPrimByte:
806 case Primitive::kPrimChar:
807 case Primitive::kPrimShort:
808 case Primitive::kPrimInt:
809 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100810 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100811 Move32(location, Location::StackSlot(stack_slot));
812 break;
813
814 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100815 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100816 Move64(location, Location::DoubleStackSlot(stack_slot));
817 break;
818
819 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100820 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100821 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000822 } else if (instruction->IsTemporary()) {
823 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000824 if (temp_location.IsStackSlot()) {
825 Move32(location, temp_location);
826 } else {
827 DCHECK(temp_location.IsDoubleStackSlot());
828 Move64(location, temp_location);
829 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000830 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100831 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100832 switch (instruction->GetType()) {
833 case Primitive::kPrimBoolean:
834 case Primitive::kPrimByte:
835 case Primitive::kPrimChar:
836 case Primitive::kPrimShort:
837 case Primitive::kPrimNot:
838 case Primitive::kPrimInt:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100839 case Primitive::kPrimFloat:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100840 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100841 break;
842
843 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100844 case Primitive::kPrimDouble:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100845 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100846 break;
847
848 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100849 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100850 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000851 }
852}
853
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100854void CodeGeneratorARM::InvokeRuntime(int32_t entry_point_offset,
855 HInstruction* instruction,
856 uint32_t dex_pc) {
857 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
858 __ blx(LR);
859 RecordPcInfo(instruction, dex_pc);
860 DCHECK(instruction->IsSuspendCheck()
861 || instruction->IsBoundsCheck()
862 || instruction->IsNullCheck()
Calin Juravled0d48522014-11-04 16:40:20 +0000863 || instruction->IsDivZeroCheck()
Roland Levillain624279f2014-12-04 11:54:28 +0000864 || instruction->GetLocations()->CanCall()
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100865 || !IsLeafMethod());
866}
867
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000868void LocationsBuilderARM::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000869 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000870}
871
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000872void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000873 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100874 DCHECK(!successor->IsExitBlock());
875
876 HBasicBlock* block = got->GetBlock();
877 HInstruction* previous = got->GetPrevious();
878
879 HLoopInformation* info = block->GetLoopInformation();
880 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
881 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
882 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
883 return;
884 }
885
886 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
887 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
888 }
889 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000890 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000891 }
892}
893
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000894void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000895 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000896}
897
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000898void InstructionCodeGeneratorARM::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700899 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000900 if (kIsDebugBuild) {
901 __ Comment("Unreachable");
902 __ bkpt(0);
903 }
904}
905
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000906void LocationsBuilderARM::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100907 LocationSummary* locations =
908 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100909 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100910 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100911 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100912 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000913}
914
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000915void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700916 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100917 if (cond->IsIntConstant()) {
918 // Constant condition, statically compared against 1.
919 int32_t cond_value = cond->AsIntConstant()->GetValue();
920 if (cond_value == 1) {
921 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
922 if_instr->IfTrueSuccessor())) {
923 __ b(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100924 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100925 return;
926 } else {
927 DCHECK_EQ(cond_value, 0);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100928 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100929 } else {
930 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
931 // Condition has been materialized, compare the output to 0
932 DCHECK(if_instr->GetLocations()->InAt(0).IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000933 __ cmp(if_instr->GetLocations()->InAt(0).AsRegister<Register>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100934 ShifterOperand(0));
935 __ b(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()), NE);
936 } else {
937 // Condition has not been materialized, use its inputs as the
938 // comparison and its condition as the branch condition.
939 LocationSummary* locations = cond->GetLocations();
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000940 DCHECK(locations->InAt(0).IsRegister()) << locations->InAt(0);
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +0000941 Register left = locations->InAt(0).AsRegister<Register>();
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100942 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +0000943 __ cmp(left, ShifterOperand(locations->InAt(1).AsRegister<Register>()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100944 } else {
945 DCHECK(locations->InAt(1).IsConstant());
946 int32_t value =
947 locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
948 ShifterOperand operand;
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +0000949 if (GetAssembler()->ShifterOperandCanHold(R0, left, CMP, value, &operand)) {
950 __ cmp(left, operand);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100951 } else {
952 Register temp = IP;
953 __ LoadImmediate(temp, value);
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +0000954 __ cmp(left, ShifterOperand(temp));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100955 }
956 }
957 __ b(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()),
958 ARMCondition(cond->AsCondition()->GetCondition()));
959 }
Dave Allison20dfc792014-06-16 20:44:29 -0700960 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100961 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
962 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700963 __ b(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000964 }
965}
966
Dave Allison20dfc792014-06-16 20:44:29 -0700967
968void LocationsBuilderARM::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100969 LocationSummary* locations =
970 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100971 locations->SetInAt(0, Location::RequiresRegister());
972 locations->SetInAt(1, Location::RegisterOrConstant(comp->InputAt(1)));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100973 if (comp->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100974 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100975 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000976}
977
Dave Allison20dfc792014-06-16 20:44:29 -0700978void InstructionCodeGeneratorARM::VisitCondition(HCondition* comp) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100979 if (!comp->NeedsMaterialization()) return;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100980 LocationSummary* locations = comp->GetLocations();
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +0000981 Register left = locations->InAt(0).AsRegister<Register>();
982
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100983 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +0000984 __ cmp(left, ShifterOperand(locations->InAt(1).AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100985 } else {
986 DCHECK(locations->InAt(1).IsConstant());
987 int32_t value = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
988 ShifterOperand operand;
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +0000989 if (GetAssembler()->ShifterOperandCanHold(R0, left, CMP, value, &operand)) {
990 __ cmp(left, operand);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100991 } else {
992 Register temp = IP;
993 __ LoadImmediate(temp, value);
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +0000994 __ cmp(left, ShifterOperand(temp));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100995 }
Dave Allison20dfc792014-06-16 20:44:29 -0700996 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100997 __ it(ARMCondition(comp->GetCondition()), kItElse);
Roland Levillain271ab9c2014-11-27 15:23:57 +0000998 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(1),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100999 ARMCondition(comp->GetCondition()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001000 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(0),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001001 ARMOppositeCondition(comp->GetCondition()));
Dave Allison20dfc792014-06-16 20:44:29 -07001002}
1003
1004void LocationsBuilderARM::VisitEqual(HEqual* comp) {
1005 VisitCondition(comp);
1006}
1007
1008void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
1009 VisitCondition(comp);
1010}
1011
1012void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
1013 VisitCondition(comp);
1014}
1015
1016void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
1017 VisitCondition(comp);
1018}
1019
1020void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
1021 VisitCondition(comp);
1022}
1023
1024void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
1025 VisitCondition(comp);
1026}
1027
1028void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1029 VisitCondition(comp);
1030}
1031
1032void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1033 VisitCondition(comp);
1034}
1035
1036void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
1037 VisitCondition(comp);
1038}
1039
1040void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
1041 VisitCondition(comp);
1042}
1043
1044void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1045 VisitCondition(comp);
1046}
1047
1048void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1049 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001050}
1051
1052void LocationsBuilderARM::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001053 local->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001054}
1055
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001056void InstructionCodeGeneratorARM::VisitLocal(HLocal* local) {
1057 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001058}
1059
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001060void LocationsBuilderARM::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001061 load->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001062}
1063
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001064void InstructionCodeGeneratorARM::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001065 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001066 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001067}
1068
1069void LocationsBuilderARM::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001070 LocationSummary* locations =
1071 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001072 switch (store->InputAt(1)->GetType()) {
1073 case Primitive::kPrimBoolean:
1074 case Primitive::kPrimByte:
1075 case Primitive::kPrimChar:
1076 case Primitive::kPrimShort:
1077 case Primitive::kPrimInt:
1078 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001079 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001080 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1081 break;
1082
1083 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001084 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001085 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1086 break;
1087
1088 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001089 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001090 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001091}
1092
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001093void InstructionCodeGeneratorARM::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001094 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001095}
1096
1097void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001098 LocationSummary* locations =
1099 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001100 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001101}
1102
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001103void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001104 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001105 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001106}
1107
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001108void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001109 LocationSummary* locations =
1110 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001111 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001112}
1113
1114void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant) {
1115 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001116 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001117}
1118
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001119void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
1120 LocationSummary* locations =
1121 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1122 locations->SetOut(Location::ConstantLocation(constant));
1123}
1124
1125void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant) {
1126 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001127 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001128}
1129
1130void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
1131 LocationSummary* locations =
1132 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1133 locations->SetOut(Location::ConstantLocation(constant));
1134}
1135
1136void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant) {
1137 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001138 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001139}
1140
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001141void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001142 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001143}
1144
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001145void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001146 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001147 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001148}
1149
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001150void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001151 LocationSummary* locations =
1152 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001153 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001154}
1155
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001156void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001157 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001158 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001159}
1160
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001161void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001162 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
1163 codegen_->GetInstructionSetFeatures());
1164 if (intrinsic.TryDispatch(invoke)) {
1165 return;
1166 }
1167
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001168 HandleInvoke(invoke);
1169}
1170
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001171void CodeGeneratorARM::LoadCurrentMethod(Register reg) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001172 DCHECK(RequiresCurrentMethod());
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001173 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001174}
1175
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001176static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
1177 if (invoke->GetLocations()->Intrinsified()) {
1178 IntrinsicCodeGeneratorARM intrinsic(codegen);
1179 intrinsic.Dispatch(invoke);
1180 return true;
1181 }
1182 return false;
1183}
1184
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001185void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001186 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1187 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001188 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001189
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001190 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
1191
1192 codegen_->GenerateStaticOrDirectCall(invoke, temp);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001193}
1194
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001195void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001196 LocationSummary* locations =
1197 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001198 locations->AddTemp(Location::RegisterLocation(R0));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001199
1200 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001201 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001202 HInstruction* input = invoke->InputAt(i);
1203 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1204 }
1205
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001206 locations->SetOut(calling_convention_visitor.GetReturnLocation(invoke->GetType()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001207}
1208
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001209void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001210 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
1211 codegen_->GetInstructionSetFeatures());
1212 if (intrinsic.TryDispatch(invoke)) {
1213 return;
1214 }
1215
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001216 HandleInvoke(invoke);
1217}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001218
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001219void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001220 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1221 return;
1222 }
1223
Roland Levillain271ab9c2014-11-27 15:23:57 +00001224 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001225 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
1226 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1227 LocationSummary* locations = invoke->GetLocations();
1228 Location receiver = locations->InAt(0);
1229 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1230 // temp = object->GetClass();
1231 if (receiver.IsStackSlot()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001232 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
1233 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001234 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001235 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001236 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001237 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001238 // temp = temp->GetMethodAt(method_offset);
Mathieu Chartier2d721012014-11-10 11:08:06 -08001239 uint32_t entry_point = mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001240 kArmWordSize).Int32Value();
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001241 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001242 // LR = temp->GetEntryPoint();
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001243 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001244 // LR();
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001245 __ blx(LR);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001246 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001247 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001248}
1249
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001250void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1251 HandleInvoke(invoke);
1252 // Add the hidden argument.
1253 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
1254}
1255
1256void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1257 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001258 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001259 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1260 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1261 LocationSummary* locations = invoke->GetLocations();
1262 Location receiver = locations->InAt(0);
1263 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1264
1265 // Set the hidden argument.
Roland Levillain199f3362014-11-27 17:15:16 +00001266 __ LoadImmediate(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
1267 invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001268
1269 // temp = object->GetClass();
1270 if (receiver.IsStackSlot()) {
1271 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
1272 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
1273 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001274 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001275 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001276 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001277 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartier2d721012014-11-10 11:08:06 -08001278 uint32_t entry_point = mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001279 kArmWordSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001280 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
1281 // LR = temp->GetEntryPoint();
1282 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
1283 // LR();
1284 __ blx(LR);
1285 DCHECK(!codegen_->IsLeafMethod());
1286 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1287}
1288
Roland Levillain88cb1752014-10-20 16:36:47 +01001289void LocationsBuilderARM::VisitNeg(HNeg* neg) {
1290 LocationSummary* locations =
1291 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1292 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001293 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01001294 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001295 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1296 break;
1297 }
1298 case Primitive::kPrimLong: {
1299 locations->SetInAt(0, Location::RequiresRegister());
1300 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01001301 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001302 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001303
Roland Levillain88cb1752014-10-20 16:36:47 +01001304 case Primitive::kPrimFloat:
1305 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001306 locations->SetInAt(0, Location::RequiresFpuRegister());
1307 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01001308 break;
1309
1310 default:
1311 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1312 }
1313}
1314
1315void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
1316 LocationSummary* locations = neg->GetLocations();
1317 Location out = locations->Out();
1318 Location in = locations->InAt(0);
1319 switch (neg->GetResultType()) {
1320 case Primitive::kPrimInt:
1321 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001322 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01001323 break;
1324
1325 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001326 DCHECK(in.IsRegisterPair());
1327 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
1328 __ rsbs(out.AsRegisterPairLow<Register>(),
1329 in.AsRegisterPairLow<Register>(),
1330 ShifterOperand(0));
1331 // We cannot emit an RSC (Reverse Subtract with Carry)
1332 // instruction here, as it does not exist in the Thumb-2
1333 // instruction set. We use the following approach
1334 // using SBC and SUB instead.
1335 //
1336 // out.hi = -C
1337 __ sbc(out.AsRegisterPairHigh<Register>(),
1338 out.AsRegisterPairHigh<Register>(),
1339 ShifterOperand(out.AsRegisterPairHigh<Register>()));
1340 // out.hi = out.hi - in.hi
1341 __ sub(out.AsRegisterPairHigh<Register>(),
1342 out.AsRegisterPairHigh<Register>(),
1343 ShifterOperand(in.AsRegisterPairHigh<Register>()));
1344 break;
1345
Roland Levillain88cb1752014-10-20 16:36:47 +01001346 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001347 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001348 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001349 break;
1350
Roland Levillain88cb1752014-10-20 16:36:47 +01001351 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001352 DCHECK(in.IsFpuRegisterPair());
1353 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
1354 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01001355 break;
1356
1357 default:
1358 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1359 }
1360}
1361
Roland Levillaindff1f282014-11-05 14:15:05 +00001362void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001363 Primitive::Type result_type = conversion->GetResultType();
1364 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001365 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001366
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001367 // The float-to-long and double-to-long type conversions rely on a
1368 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001369 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001370 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1371 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001372 ? LocationSummary::kCall
1373 : LocationSummary::kNoCall;
1374 LocationSummary* locations =
1375 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1376
Roland Levillaindff1f282014-11-05 14:15:05 +00001377 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001378 case Primitive::kPrimByte:
1379 switch (input_type) {
1380 case Primitive::kPrimShort:
1381 case Primitive::kPrimInt:
1382 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001383 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001384 locations->SetInAt(0, Location::RequiresRegister());
1385 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1386 break;
1387
1388 default:
1389 LOG(FATAL) << "Unexpected type conversion from " << input_type
1390 << " to " << result_type;
1391 }
1392 break;
1393
Roland Levillain01a8d712014-11-14 16:27:39 +00001394 case Primitive::kPrimShort:
1395 switch (input_type) {
1396 case Primitive::kPrimByte:
1397 case Primitive::kPrimInt:
1398 case Primitive::kPrimChar:
1399 // Processing a Dex `int-to-short' instruction.
1400 locations->SetInAt(0, Location::RequiresRegister());
1401 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1402 break;
1403
1404 default:
1405 LOG(FATAL) << "Unexpected type conversion from " << input_type
1406 << " to " << result_type;
1407 }
1408 break;
1409
Roland Levillain946e1432014-11-11 17:35:19 +00001410 case Primitive::kPrimInt:
1411 switch (input_type) {
1412 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001413 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001414 locations->SetInAt(0, Location::Any());
1415 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1416 break;
1417
1418 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001419 // Processing a Dex `float-to-int' instruction.
1420 locations->SetInAt(0, Location::RequiresFpuRegister());
1421 locations->SetOut(Location::RequiresRegister());
1422 locations->AddTemp(Location::RequiresFpuRegister());
1423 break;
1424
Roland Levillain946e1432014-11-11 17:35:19 +00001425 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001426 // Processing a Dex `double-to-int' instruction.
1427 locations->SetInAt(0, Location::RequiresFpuRegister());
1428 locations->SetOut(Location::RequiresRegister());
1429 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001430 break;
1431
1432 default:
1433 LOG(FATAL) << "Unexpected type conversion from " << input_type
1434 << " to " << result_type;
1435 }
1436 break;
1437
Roland Levillaindff1f282014-11-05 14:15:05 +00001438 case Primitive::kPrimLong:
1439 switch (input_type) {
1440 case Primitive::kPrimByte:
1441 case Primitive::kPrimShort:
1442 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001443 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001444 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001445 locations->SetInAt(0, Location::RequiresRegister());
1446 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1447 break;
1448
Roland Levillain624279f2014-12-04 11:54:28 +00001449 case Primitive::kPrimFloat: {
1450 // Processing a Dex `float-to-long' instruction.
1451 InvokeRuntimeCallingConvention calling_convention;
1452 locations->SetInAt(0, Location::FpuRegisterLocation(
1453 calling_convention.GetFpuRegisterAt(0)));
1454 locations->SetOut(Location::RegisterPairLocation(R0, R1));
1455 break;
1456 }
1457
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001458 case Primitive::kPrimDouble: {
1459 // Processing a Dex `double-to-long' instruction.
1460 InvokeRuntimeCallingConvention calling_convention;
1461 locations->SetInAt(0, Location::FpuRegisterPairLocation(
1462 calling_convention.GetFpuRegisterAt(0),
1463 calling_convention.GetFpuRegisterAt(1)));
1464 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00001465 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001466 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001467
1468 default:
1469 LOG(FATAL) << "Unexpected type conversion from " << input_type
1470 << " to " << result_type;
1471 }
1472 break;
1473
Roland Levillain981e4542014-11-14 11:47:14 +00001474 case Primitive::kPrimChar:
1475 switch (input_type) {
1476 case Primitive::kPrimByte:
1477 case Primitive::kPrimShort:
1478 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001479 // Processing a Dex `int-to-char' instruction.
1480 locations->SetInAt(0, Location::RequiresRegister());
1481 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1482 break;
1483
1484 default:
1485 LOG(FATAL) << "Unexpected type conversion from " << input_type
1486 << " to " << result_type;
1487 }
1488 break;
1489
Roland Levillaindff1f282014-11-05 14:15:05 +00001490 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001491 switch (input_type) {
1492 case Primitive::kPrimByte:
1493 case Primitive::kPrimShort:
1494 case Primitive::kPrimInt:
1495 case Primitive::kPrimChar:
1496 // Processing a Dex `int-to-float' instruction.
1497 locations->SetInAt(0, Location::RequiresRegister());
1498 locations->SetOut(Location::RequiresFpuRegister());
1499 break;
1500
1501 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001502 // Processing a Dex `long-to-float' instruction.
1503 locations->SetInAt(0, Location::RequiresRegister());
1504 locations->SetOut(Location::RequiresFpuRegister());
1505 locations->AddTemp(Location::RequiresRegister());
1506 locations->AddTemp(Location::RequiresRegister());
1507 locations->AddTemp(Location::RequiresFpuRegister());
1508 locations->AddTemp(Location::RequiresFpuRegister());
1509 break;
1510
Roland Levillaincff13742014-11-17 14:32:17 +00001511 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001512 // Processing a Dex `double-to-float' instruction.
1513 locations->SetInAt(0, Location::RequiresFpuRegister());
1514 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001515 break;
1516
1517 default:
1518 LOG(FATAL) << "Unexpected type conversion from " << input_type
1519 << " to " << result_type;
1520 };
1521 break;
1522
Roland Levillaindff1f282014-11-05 14:15:05 +00001523 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001524 switch (input_type) {
1525 case Primitive::kPrimByte:
1526 case Primitive::kPrimShort:
1527 case Primitive::kPrimInt:
1528 case Primitive::kPrimChar:
1529 // Processing a Dex `int-to-double' instruction.
1530 locations->SetInAt(0, Location::RequiresRegister());
1531 locations->SetOut(Location::RequiresFpuRegister());
1532 break;
1533
1534 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001535 // Processing a Dex `long-to-double' instruction.
1536 locations->SetInAt(0, Location::RequiresRegister());
1537 locations->SetOut(Location::RequiresFpuRegister());
1538 locations->AddTemp(Location::RequiresRegister());
1539 locations->AddTemp(Location::RequiresRegister());
1540 locations->AddTemp(Location::RequiresFpuRegister());
1541 break;
1542
Roland Levillaincff13742014-11-17 14:32:17 +00001543 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001544 // Processing a Dex `float-to-double' instruction.
1545 locations->SetInAt(0, Location::RequiresFpuRegister());
1546 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001547 break;
1548
1549 default:
1550 LOG(FATAL) << "Unexpected type conversion from " << input_type
1551 << " to " << result_type;
1552 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001553 break;
1554
1555 default:
1556 LOG(FATAL) << "Unexpected type conversion from " << input_type
1557 << " to " << result_type;
1558 }
1559}
1560
1561void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
1562 LocationSummary* locations = conversion->GetLocations();
1563 Location out = locations->Out();
1564 Location in = locations->InAt(0);
1565 Primitive::Type result_type = conversion->GetResultType();
1566 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001567 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001568 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001569 case Primitive::kPrimByte:
1570 switch (input_type) {
1571 case Primitive::kPrimShort:
1572 case Primitive::kPrimInt:
1573 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001574 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001575 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001576 break;
1577
1578 default:
1579 LOG(FATAL) << "Unexpected type conversion from " << input_type
1580 << " to " << result_type;
1581 }
1582 break;
1583
Roland Levillain01a8d712014-11-14 16:27:39 +00001584 case Primitive::kPrimShort:
1585 switch (input_type) {
1586 case Primitive::kPrimByte:
1587 case Primitive::kPrimInt:
1588 case Primitive::kPrimChar:
1589 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001590 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00001591 break;
1592
1593 default:
1594 LOG(FATAL) << "Unexpected type conversion from " << input_type
1595 << " to " << result_type;
1596 }
1597 break;
1598
Roland Levillain946e1432014-11-11 17:35:19 +00001599 case Primitive::kPrimInt:
1600 switch (input_type) {
1601 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001602 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001603 DCHECK(out.IsRegister());
1604 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001605 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001606 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001607 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00001608 } else {
1609 DCHECK(in.IsConstant());
1610 DCHECK(in.GetConstant()->IsLongConstant());
1611 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001612 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00001613 }
1614 break;
1615
Roland Levillain3f8f9362014-12-02 17:45:01 +00001616 case Primitive::kPrimFloat: {
1617 // Processing a Dex `float-to-int' instruction.
1618 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
1619 __ vmovs(temp, in.AsFpuRegister<SRegister>());
1620 __ vcvtis(temp, temp);
1621 __ vmovrs(out.AsRegister<Register>(), temp);
1622 break;
1623 }
1624
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001625 case Primitive::kPrimDouble: {
1626 // Processing a Dex `double-to-int' instruction.
1627 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
1628 DRegister temp_d = FromLowSToD(temp_s);
1629 __ vmovd(temp_d, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
1630 __ vcvtid(temp_s, temp_d);
1631 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00001632 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001633 }
Roland Levillain946e1432014-11-11 17:35:19 +00001634
1635 default:
1636 LOG(FATAL) << "Unexpected type conversion from " << input_type
1637 << " to " << result_type;
1638 }
1639 break;
1640
Roland Levillaindff1f282014-11-05 14:15:05 +00001641 case Primitive::kPrimLong:
1642 switch (input_type) {
1643 case Primitive::kPrimByte:
1644 case Primitive::kPrimShort:
1645 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001646 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001647 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001648 DCHECK(out.IsRegisterPair());
1649 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001650 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00001651 // Sign extension.
1652 __ Asr(out.AsRegisterPairHigh<Register>(),
1653 out.AsRegisterPairLow<Register>(),
1654 31);
1655 break;
1656
1657 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001658 // Processing a Dex `float-to-long' instruction.
Roland Levillain624279f2014-12-04 11:54:28 +00001659 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
1660 conversion,
1661 conversion->GetDexPc());
1662 break;
1663
Roland Levillaindff1f282014-11-05 14:15:05 +00001664 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001665 // Processing a Dex `double-to-long' instruction.
1666 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
1667 conversion,
1668 conversion->GetDexPc());
Roland Levillaindff1f282014-11-05 14:15:05 +00001669 break;
1670
1671 default:
1672 LOG(FATAL) << "Unexpected type conversion from " << input_type
1673 << " to " << result_type;
1674 }
1675 break;
1676
Roland Levillain981e4542014-11-14 11:47:14 +00001677 case Primitive::kPrimChar:
1678 switch (input_type) {
1679 case Primitive::kPrimByte:
1680 case Primitive::kPrimShort:
1681 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001682 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001683 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00001684 break;
1685
1686 default:
1687 LOG(FATAL) << "Unexpected type conversion from " << input_type
1688 << " to " << result_type;
1689 }
1690 break;
1691
Roland Levillaindff1f282014-11-05 14:15:05 +00001692 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001693 switch (input_type) {
1694 case Primitive::kPrimByte:
1695 case Primitive::kPrimShort:
1696 case Primitive::kPrimInt:
1697 case Primitive::kPrimChar: {
1698 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001699 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
1700 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001701 break;
1702 }
1703
Roland Levillain6d0e4832014-11-27 18:31:21 +00001704 case Primitive::kPrimLong: {
1705 // Processing a Dex `long-to-float' instruction.
1706 Register low = in.AsRegisterPairLow<Register>();
1707 Register high = in.AsRegisterPairHigh<Register>();
1708 SRegister output = out.AsFpuRegister<SRegister>();
1709 Register constant_low = locations->GetTemp(0).AsRegister<Register>();
1710 Register constant_high = locations->GetTemp(1).AsRegister<Register>();
1711 SRegister temp1_s = locations->GetTemp(2).AsFpuRegisterPairLow<SRegister>();
1712 DRegister temp1_d = FromLowSToD(temp1_s);
1713 SRegister temp2_s = locations->GetTemp(3).AsFpuRegisterPairLow<SRegister>();
1714 DRegister temp2_d = FromLowSToD(temp2_s);
1715
1716 // Operations use doubles for precision reasons (each 32-bit
1717 // half of a long fits in the 53-bit mantissa of a double,
1718 // but not in the 24-bit mantissa of a float). This is
1719 // especially important for the low bits. The result is
1720 // eventually converted to float.
1721
1722 // temp1_d = int-to-double(high)
1723 __ vmovsr(temp1_s, high);
1724 __ vcvtdi(temp1_d, temp1_s);
1725 // Using vmovd to load the `k2Pow32EncodingForDouble` constant
1726 // as an immediate value into `temp2_d` does not work, as
1727 // this instruction only transfers 8 significant bits of its
1728 // immediate operand. Instead, use two 32-bit core
1729 // registers to load `k2Pow32EncodingForDouble` into
1730 // `temp2_d`.
1731 __ LoadImmediate(constant_low, Low32Bits(k2Pow32EncodingForDouble));
1732 __ LoadImmediate(constant_high, High32Bits(k2Pow32EncodingForDouble));
1733 __ vmovdrr(temp2_d, constant_low, constant_high);
1734 // temp1_d = temp1_d * 2^32
1735 __ vmuld(temp1_d, temp1_d, temp2_d);
1736 // temp2_d = unsigned-to-double(low)
1737 __ vmovsr(temp2_s, low);
1738 __ vcvtdu(temp2_d, temp2_s);
1739 // temp1_d = temp1_d + temp2_d
1740 __ vaddd(temp1_d, temp1_d, temp2_d);
1741 // output = double-to-float(temp1_d);
1742 __ vcvtsd(output, temp1_d);
1743 break;
1744 }
1745
Roland Levillaincff13742014-11-17 14:32:17 +00001746 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001747 // Processing a Dex `double-to-float' instruction.
1748 __ vcvtsd(out.AsFpuRegister<SRegister>(),
1749 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00001750 break;
1751
1752 default:
1753 LOG(FATAL) << "Unexpected type conversion from " << input_type
1754 << " to " << result_type;
1755 };
1756 break;
1757
Roland Levillaindff1f282014-11-05 14:15:05 +00001758 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001759 switch (input_type) {
1760 case Primitive::kPrimByte:
1761 case Primitive::kPrimShort:
1762 case Primitive::kPrimInt:
1763 case Primitive::kPrimChar: {
1764 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001765 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001766 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
1767 out.AsFpuRegisterPairLow<SRegister>());
1768 break;
1769 }
1770
Roland Levillain647b9ed2014-11-27 12:06:00 +00001771 case Primitive::kPrimLong: {
1772 // Processing a Dex `long-to-double' instruction.
1773 Register low = in.AsRegisterPairLow<Register>();
1774 Register high = in.AsRegisterPairHigh<Register>();
1775 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
1776 DRegister out_d = FromLowSToD(out_s);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001777 Register constant_low = locations->GetTemp(0).AsRegister<Register>();
1778 Register constant_high = locations->GetTemp(1).AsRegister<Register>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00001779 SRegister temp_s = locations->GetTemp(2).AsFpuRegisterPairLow<SRegister>();
1780 DRegister temp_d = FromLowSToD(temp_s);
1781
Roland Levillain647b9ed2014-11-27 12:06:00 +00001782 // out_d = int-to-double(high)
1783 __ vmovsr(out_s, high);
1784 __ vcvtdi(out_d, out_s);
Roland Levillain6d0e4832014-11-27 18:31:21 +00001785 // Using vmovd to load the `k2Pow32EncodingForDouble` constant
1786 // as an immediate value into `temp_d` does not work, as
1787 // this instruction only transfers 8 significant bits of its
1788 // immediate operand. Instead, use two 32-bit core
1789 // registers to load `k2Pow32EncodingForDouble` into `temp_d`.
1790 __ LoadImmediate(constant_low, Low32Bits(k2Pow32EncodingForDouble));
1791 __ LoadImmediate(constant_high, High32Bits(k2Pow32EncodingForDouble));
Roland Levillain647b9ed2014-11-27 12:06:00 +00001792 __ vmovdrr(temp_d, constant_low, constant_high);
1793 // out_d = out_d * 2^32
1794 __ vmuld(out_d, out_d, temp_d);
1795 // temp_d = unsigned-to-double(low)
1796 __ vmovsr(temp_s, low);
1797 __ vcvtdu(temp_d, temp_s);
1798 // out_d = out_d + temp_d
1799 __ vaddd(out_d, out_d, temp_d);
1800 break;
1801 }
1802
Roland Levillaincff13742014-11-17 14:32:17 +00001803 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001804 // Processing a Dex `float-to-double' instruction.
1805 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
1806 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001807 break;
1808
1809 default:
1810 LOG(FATAL) << "Unexpected type conversion from " << input_type
1811 << " to " << result_type;
1812 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001813 break;
1814
1815 default:
1816 LOG(FATAL) << "Unexpected type conversion from " << input_type
1817 << " to " << result_type;
1818 }
1819}
1820
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001821void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001822 LocationSummary* locations =
1823 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001824 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001825 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001826 locations->SetInAt(0, Location::RequiresRegister());
1827 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001828 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1829 break;
1830 }
1831
1832 case Primitive::kPrimLong: {
1833 locations->SetInAt(0, Location::RequiresRegister());
1834 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001835 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001836 break;
1837 }
1838
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001839 case Primitive::kPrimFloat:
1840 case Primitive::kPrimDouble: {
1841 locations->SetInAt(0, Location::RequiresFpuRegister());
1842 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00001843 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001844 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001845 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001846
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001847 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001848 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001849 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001850}
1851
1852void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
1853 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001854 Location out = locations->Out();
1855 Location first = locations->InAt(0);
1856 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001857 switch (add->GetResultType()) {
1858 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001859 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00001860 __ add(out.AsRegister<Register>(),
1861 first.AsRegister<Register>(),
1862 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001863 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001864 __ AddConstant(out.AsRegister<Register>(),
1865 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001866 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001867 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001868 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001869
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001870 case Primitive::kPrimLong: {
1871 DCHECK(second.IsRegisterPair());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001872 __ adds(out.AsRegisterPairLow<Register>(),
1873 first.AsRegisterPairLow<Register>(),
1874 ShifterOperand(second.AsRegisterPairLow<Register>()));
1875 __ adc(out.AsRegisterPairHigh<Register>(),
1876 first.AsRegisterPairHigh<Register>(),
1877 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001878 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001879 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001880
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001881 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00001882 __ vadds(out.AsFpuRegister<SRegister>(),
1883 first.AsFpuRegister<SRegister>(),
1884 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001885 break;
1886
1887 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001888 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
1889 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
1890 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001891 break;
1892
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001893 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001894 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001895 }
1896}
1897
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001898void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001899 LocationSummary* locations =
1900 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001901 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001902 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001903 locations->SetInAt(0, Location::RequiresRegister());
1904 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001905 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1906 break;
1907 }
1908
1909 case Primitive::kPrimLong: {
1910 locations->SetInAt(0, Location::RequiresRegister());
1911 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001912 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001913 break;
1914 }
Calin Juravle11351682014-10-23 15:38:15 +01001915 case Primitive::kPrimFloat:
1916 case Primitive::kPrimDouble: {
1917 locations->SetInAt(0, Location::RequiresFpuRegister());
1918 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00001919 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001920 break;
Calin Juravle11351682014-10-23 15:38:15 +01001921 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001922 default:
Calin Juravle11351682014-10-23 15:38:15 +01001923 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001924 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001925}
1926
1927void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
1928 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01001929 Location out = locations->Out();
1930 Location first = locations->InAt(0);
1931 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001932 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001933 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01001934 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00001935 __ sub(out.AsRegister<Register>(),
1936 first.AsRegister<Register>(),
1937 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001938 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001939 __ AddConstant(out.AsRegister<Register>(),
1940 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01001941 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001942 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001943 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001944 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001945
Calin Juravle11351682014-10-23 15:38:15 +01001946 case Primitive::kPrimLong: {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001947 DCHECK(second.IsRegisterPair());
Calin Juravle11351682014-10-23 15:38:15 +01001948 __ subs(out.AsRegisterPairLow<Register>(),
1949 first.AsRegisterPairLow<Register>(),
1950 ShifterOperand(second.AsRegisterPairLow<Register>()));
1951 __ sbc(out.AsRegisterPairHigh<Register>(),
1952 first.AsRegisterPairHigh<Register>(),
1953 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001954 break;
Calin Juravle11351682014-10-23 15:38:15 +01001955 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001956
Calin Juravle11351682014-10-23 15:38:15 +01001957 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00001958 __ vsubs(out.AsFpuRegister<SRegister>(),
1959 first.AsFpuRegister<SRegister>(),
1960 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001961 break;
Calin Juravle11351682014-10-23 15:38:15 +01001962 }
1963
1964 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001965 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
1966 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
1967 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01001968 break;
1969 }
1970
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001971
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001972 default:
Calin Juravle11351682014-10-23 15:38:15 +01001973 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001974 }
1975}
1976
Calin Juravle34bacdf2014-10-07 20:23:36 +01001977void LocationsBuilderARM::VisitMul(HMul* mul) {
1978 LocationSummary* locations =
1979 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1980 switch (mul->GetResultType()) {
1981 case Primitive::kPrimInt:
1982 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001983 locations->SetInAt(0, Location::RequiresRegister());
1984 locations->SetInAt(1, Location::RequiresRegister());
1985 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01001986 break;
1987 }
1988
Calin Juravleb5bfa962014-10-21 18:02:24 +01001989 case Primitive::kPrimFloat:
1990 case Primitive::kPrimDouble: {
1991 locations->SetInAt(0, Location::RequiresFpuRegister());
1992 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00001993 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01001994 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001995 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001996
1997 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001998 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001999 }
2000}
2001
2002void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
2003 LocationSummary* locations = mul->GetLocations();
2004 Location out = locations->Out();
2005 Location first = locations->InAt(0);
2006 Location second = locations->InAt(1);
2007 switch (mul->GetResultType()) {
2008 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00002009 __ mul(out.AsRegister<Register>(),
2010 first.AsRegister<Register>(),
2011 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002012 break;
2013 }
2014 case Primitive::kPrimLong: {
2015 Register out_hi = out.AsRegisterPairHigh<Register>();
2016 Register out_lo = out.AsRegisterPairLow<Register>();
2017 Register in1_hi = first.AsRegisterPairHigh<Register>();
2018 Register in1_lo = first.AsRegisterPairLow<Register>();
2019 Register in2_hi = second.AsRegisterPairHigh<Register>();
2020 Register in2_lo = second.AsRegisterPairLow<Register>();
2021
2022 // Extra checks to protect caused by the existence of R1_R2.
2023 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
2024 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
2025 DCHECK_NE(out_hi, in1_lo);
2026 DCHECK_NE(out_hi, in2_lo);
2027
2028 // input: in1 - 64 bits, in2 - 64 bits
2029 // output: out
2030 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2031 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2032 // parts: out.lo = (in1.lo * in2.lo)[31:0]
2033
2034 // IP <- in1.lo * in2.hi
2035 __ mul(IP, in1_lo, in2_hi);
2036 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2037 __ mla(out_hi, in1_hi, in2_lo, IP);
2038 // out.lo <- (in1.lo * in2.lo)[31:0];
2039 __ umull(out_lo, IP, in1_lo, in2_lo);
2040 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2041 __ add(out_hi, out_hi, ShifterOperand(IP));
2042 break;
2043 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002044
2045 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002046 __ vmuls(out.AsFpuRegister<SRegister>(),
2047 first.AsFpuRegister<SRegister>(),
2048 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002049 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002050 }
2051
2052 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002053 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2054 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2055 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01002056 break;
2057 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002058
2059 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002060 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002061 }
2062}
2063
Calin Juravle7c4954d2014-10-28 16:57:40 +00002064void LocationsBuilderARM::VisitDiv(HDiv* div) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002065 LocationSummary::CallKind call_kind = div->GetResultType() == Primitive::kPrimLong
2066 ? LocationSummary::kCall
2067 : LocationSummary::kNoCall;
2068 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2069
Calin Juravle7c4954d2014-10-28 16:57:40 +00002070 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002071 case Primitive::kPrimInt: {
2072 locations->SetInAt(0, Location::RequiresRegister());
2073 locations->SetInAt(1, Location::RequiresRegister());
2074 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2075 break;
2076 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002077 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002078 InvokeRuntimeCallingConvention calling_convention;
2079 locations->SetInAt(0, Location::RegisterPairLocation(
2080 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2081 locations->SetInAt(1, Location::RegisterPairLocation(
2082 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002083 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002084 break;
2085 }
2086 case Primitive::kPrimFloat:
2087 case Primitive::kPrimDouble: {
2088 locations->SetInAt(0, Location::RequiresFpuRegister());
2089 locations->SetInAt(1, Location::RequiresFpuRegister());
2090 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2091 break;
2092 }
2093
2094 default:
2095 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2096 }
2097}
2098
2099void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
2100 LocationSummary* locations = div->GetLocations();
2101 Location out = locations->Out();
2102 Location first = locations->InAt(0);
2103 Location second = locations->InAt(1);
2104
2105 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002106 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00002107 __ sdiv(out.AsRegister<Register>(),
2108 first.AsRegister<Register>(),
2109 second.AsRegister<Register>());
Calin Juravled0d48522014-11-04 16:40:20 +00002110 break;
2111 }
2112
Calin Juravle7c4954d2014-10-28 16:57:40 +00002113 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002114 InvokeRuntimeCallingConvention calling_convention;
2115 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2116 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2117 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2118 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2119 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002120 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002121
2122 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv), div, div->GetDexPc());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002123 break;
2124 }
2125
2126 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002127 __ vdivs(out.AsFpuRegister<SRegister>(),
2128 first.AsFpuRegister<SRegister>(),
2129 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002130 break;
2131 }
2132
2133 case Primitive::kPrimDouble: {
2134 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2135 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2136 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
2137 break;
2138 }
2139
2140 default:
2141 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2142 }
2143}
2144
Calin Juravlebacfec32014-11-14 15:54:36 +00002145void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002146 Primitive::Type type = rem->GetResultType();
2147 LocationSummary::CallKind call_kind = type == Primitive::kPrimInt
2148 ? LocationSummary::kNoCall
2149 : LocationSummary::kCall;
Calin Juravlebacfec32014-11-14 15:54:36 +00002150 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2151
Calin Juravled2ec87d2014-12-08 14:24:46 +00002152 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002153 case Primitive::kPrimInt: {
2154 locations->SetInAt(0, Location::RequiresRegister());
2155 locations->SetInAt(1, Location::RequiresRegister());
2156 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2157 locations->AddTemp(Location::RequiresRegister());
2158 break;
2159 }
2160 case Primitive::kPrimLong: {
2161 InvokeRuntimeCallingConvention calling_convention;
2162 locations->SetInAt(0, Location::RegisterPairLocation(
2163 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2164 locations->SetInAt(1, Location::RegisterPairLocation(
2165 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2166 // The runtime helper puts the output in R2,R3.
2167 locations->SetOut(Location::RegisterPairLocation(R2, R3));
2168 break;
2169 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00002170 case Primitive::kPrimFloat: {
2171 InvokeRuntimeCallingConvention calling_convention;
2172 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
2173 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
2174 locations->SetOut(Location::FpuRegisterLocation(S0));
2175 break;
2176 }
2177
Calin Juravlebacfec32014-11-14 15:54:36 +00002178 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002179 InvokeRuntimeCallingConvention calling_convention;
2180 locations->SetInAt(0, Location::FpuRegisterPairLocation(
2181 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
2182 locations->SetInAt(1, Location::FpuRegisterPairLocation(
2183 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
2184 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00002185 break;
2186 }
2187
2188 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002189 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002190 }
2191}
2192
2193void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
2194 LocationSummary* locations = rem->GetLocations();
2195 Location out = locations->Out();
2196 Location first = locations->InAt(0);
2197 Location second = locations->InAt(1);
2198
Calin Juravled2ec87d2014-12-08 14:24:46 +00002199 Primitive::Type type = rem->GetResultType();
2200 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002201 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002202 Register reg1 = first.AsRegister<Register>();
2203 Register reg2 = second.AsRegister<Register>();
2204 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00002205
2206 // temp = reg1 / reg2 (integer division)
2207 // temp = temp * reg2
2208 // dest = reg1 - temp
2209 __ sdiv(temp, reg1, reg2);
2210 __ mul(temp, temp, reg2);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002211 __ sub(out.AsRegister<Register>(), reg1, ShifterOperand(temp));
Calin Juravlebacfec32014-11-14 15:54:36 +00002212 break;
2213 }
2214
2215 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002216 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod), rem, rem->GetDexPc());
2217 break;
2218 }
2219
Calin Juravled2ec87d2014-12-08 14:24:46 +00002220 case Primitive::kPrimFloat: {
2221 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf), rem, rem->GetDexPc());
2222 break;
2223 }
2224
Calin Juravlebacfec32014-11-14 15:54:36 +00002225 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002226 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod), rem, rem->GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00002227 break;
2228 }
2229
2230 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002231 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002232 }
2233}
2234
Calin Juravled0d48522014-11-04 16:40:20 +00002235void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2236 LocationSummary* locations =
2237 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002238 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00002239 if (instruction->HasUses()) {
2240 locations->SetOut(Location::SameAsFirstInput());
2241 }
2242}
2243
2244void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2245 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
2246 codegen_->AddSlowPath(slow_path);
2247
2248 LocationSummary* locations = instruction->GetLocations();
2249 Location value = locations->InAt(0);
2250
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002251 switch (instruction->GetType()) {
2252 case Primitive::kPrimInt: {
2253 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002254 __ cmp(value.AsRegister<Register>(), ShifterOperand(0));
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002255 __ b(slow_path->GetEntryLabel(), EQ);
2256 } else {
2257 DCHECK(value.IsConstant()) << value;
2258 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2259 __ b(slow_path->GetEntryLabel());
2260 }
2261 }
2262 break;
2263 }
2264 case Primitive::kPrimLong: {
2265 if (value.IsRegisterPair()) {
2266 __ orrs(IP,
2267 value.AsRegisterPairLow<Register>(),
2268 ShifterOperand(value.AsRegisterPairHigh<Register>()));
2269 __ b(slow_path->GetEntryLabel(), EQ);
2270 } else {
2271 DCHECK(value.IsConstant()) << value;
2272 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2273 __ b(slow_path->GetEntryLabel());
2274 }
2275 }
2276 break;
2277 default:
2278 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2279 }
2280 }
Calin Juravled0d48522014-11-04 16:40:20 +00002281}
2282
Calin Juravle9aec02f2014-11-18 23:06:35 +00002283void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
2284 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2285
2286 LocationSummary::CallKind call_kind = op->GetResultType() == Primitive::kPrimLong
2287 ? LocationSummary::kCall
2288 : LocationSummary::kNoCall;
2289 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(op, call_kind);
2290
2291 switch (op->GetResultType()) {
2292 case Primitive::kPrimInt: {
2293 locations->SetInAt(0, Location::RequiresRegister());
2294 locations->SetInAt(1, Location::RegisterOrConstant(op->InputAt(1)));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002295 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002296 break;
2297 }
2298 case Primitive::kPrimLong: {
2299 InvokeRuntimeCallingConvention calling_convention;
2300 locations->SetInAt(0, Location::RegisterPairLocation(
2301 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2302 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002303 // The runtime helper puts the output in R0,R1.
2304 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle9aec02f2014-11-18 23:06:35 +00002305 break;
2306 }
2307 default:
2308 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2309 }
2310}
2311
2312void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
2313 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2314
2315 LocationSummary* locations = op->GetLocations();
2316 Location out = locations->Out();
2317 Location first = locations->InAt(0);
2318 Location second = locations->InAt(1);
2319
2320 Primitive::Type type = op->GetResultType();
2321 switch (type) {
2322 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002323 Register out_reg = out.AsRegister<Register>();
2324 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002325 // Arm doesn't mask the shift count so we need to do it ourselves.
2326 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002327 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002328 __ and_(second_reg, second_reg, ShifterOperand(kMaxIntShiftValue));
2329 if (op->IsShl()) {
2330 __ Lsl(out_reg, first_reg, second_reg);
2331 } else if (op->IsShr()) {
2332 __ Asr(out_reg, first_reg, second_reg);
2333 } else {
2334 __ Lsr(out_reg, first_reg, second_reg);
2335 }
2336 } else {
2337 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
2338 uint32_t shift_value = static_cast<uint32_t>(cst & kMaxIntShiftValue);
2339 if (shift_value == 0) { // arm does not support shifting with 0 immediate.
2340 __ Mov(out_reg, first_reg);
2341 } else if (op->IsShl()) {
2342 __ Lsl(out_reg, first_reg, shift_value);
2343 } else if (op->IsShr()) {
2344 __ Asr(out_reg, first_reg, shift_value);
2345 } else {
2346 __ Lsr(out_reg, first_reg, shift_value);
2347 }
2348 }
2349 break;
2350 }
2351 case Primitive::kPrimLong: {
2352 // TODO: Inline the assembly instead of calling the runtime.
2353 InvokeRuntimeCallingConvention calling_convention;
2354 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2355 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002356 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegister<Register>());
Calin Juravle9aec02f2014-11-18 23:06:35 +00002357 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002358 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravle9aec02f2014-11-18 23:06:35 +00002359
2360 int32_t entry_point_offset;
2361 if (op->IsShl()) {
2362 entry_point_offset = QUICK_ENTRY_POINT(pShlLong);
2363 } else if (op->IsShr()) {
2364 entry_point_offset = QUICK_ENTRY_POINT(pShrLong);
2365 } else {
2366 entry_point_offset = QUICK_ENTRY_POINT(pUshrLong);
2367 }
2368 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
2369 __ blx(LR);
2370 break;
2371 }
2372 default:
2373 LOG(FATAL) << "Unexpected operation type " << type;
2374 }
2375}
2376
2377void LocationsBuilderARM::VisitShl(HShl* shl) {
2378 HandleShift(shl);
2379}
2380
2381void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
2382 HandleShift(shl);
2383}
2384
2385void LocationsBuilderARM::VisitShr(HShr* shr) {
2386 HandleShift(shr);
2387}
2388
2389void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
2390 HandleShift(shr);
2391}
2392
2393void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
2394 HandleShift(ushr);
2395}
2396
2397void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
2398 HandleShift(ushr);
2399}
2400
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002401void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002402 LocationSummary* locations =
2403 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002404 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002405 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2406 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2407 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002408}
2409
2410void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
2411 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002412 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002413 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002414 codegen_->InvokeRuntime(GetThreadOffset<kArmWordSize>(instruction->GetEntrypoint()).Int32Value(),
2415 instruction,
2416 instruction->GetDexPc());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002417}
2418
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002419void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
2420 LocationSummary* locations =
2421 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2422 InvokeRuntimeCallingConvention calling_convention;
2423 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002424 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002425 locations->SetOut(Location::RegisterLocation(R0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002426 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002427}
2428
2429void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
2430 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002431 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(2));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002432 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002433 codegen_->InvokeRuntime(GetThreadOffset<kArmWordSize>(instruction->GetEntrypoint()).Int32Value(),
2434 instruction,
2435 instruction->GetDexPc());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002436}
2437
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002438void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002439 LocationSummary* locations =
2440 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002441 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2442 if (location.IsStackSlot()) {
2443 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2444 } else if (location.IsDoubleStackSlot()) {
2445 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002446 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002447 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002448}
2449
2450void InstructionCodeGeneratorARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002451 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002452 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002453}
2454
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002455void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002456 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002457 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002458 locations->SetInAt(0, Location::RequiresRegister());
2459 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002460}
2461
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002462void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
2463 LocationSummary* locations = not_->GetLocations();
2464 Location out = locations->Out();
2465 Location in = locations->InAt(0);
2466 switch (not_->InputAt(0)->GetType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002467 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002468 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002469 break;
2470
2471 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01002472 __ mvn(out.AsRegisterPairLow<Register>(),
2473 ShifterOperand(in.AsRegisterPairLow<Register>()));
2474 __ mvn(out.AsRegisterPairHigh<Register>(),
2475 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002476 break;
2477
2478 default:
2479 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2480 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002481}
2482
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002483void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002484 LocationSummary* locations =
2485 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00002486 switch (compare->InputAt(0)->GetType()) {
2487 case Primitive::kPrimLong: {
2488 locations->SetInAt(0, Location::RequiresRegister());
2489 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002490 // Output overlaps because it is written before doing the low comparison.
2491 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00002492 break;
2493 }
2494 case Primitive::kPrimFloat:
2495 case Primitive::kPrimDouble: {
2496 locations->SetInAt(0, Location::RequiresFpuRegister());
2497 locations->SetInAt(1, Location::RequiresFpuRegister());
2498 locations->SetOut(Location::RequiresRegister());
2499 break;
2500 }
2501 default:
2502 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
2503 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002504}
2505
2506void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002507 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002508 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00002509 Location left = locations->InAt(0);
2510 Location right = locations->InAt(1);
2511
2512 Label less, greater, done;
2513 Primitive::Type type = compare->InputAt(0)->GetType();
2514 switch (type) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002515 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002516 __ cmp(left.AsRegisterPairHigh<Register>(),
2517 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002518 __ b(&less, LT);
2519 __ b(&greater, GT);
Calin Juravleddb7df22014-11-25 20:56:51 +00002520 // Do LoadImmediate before any `cmp`, as LoadImmediate might affect the status flags.
2521 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002522 __ cmp(left.AsRegisterPairLow<Register>(),
2523 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Calin Juravleddb7df22014-11-25 20:56:51 +00002524 break;
2525 }
2526 case Primitive::kPrimFloat:
2527 case Primitive::kPrimDouble: {
2528 __ LoadImmediate(out, 0);
2529 if (type == Primitive::kPrimFloat) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002530 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00002531 } else {
2532 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
2533 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
2534 }
2535 __ vmstat(); // transfer FP status register to ARM APSR.
2536 __ b(compare->IsGtBias() ? &greater : &less, VS); // VS for unordered.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002537 break;
2538 }
2539 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00002540 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002541 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002542 __ b(&done, EQ);
2543 __ b(&less, CC); // CC is for both: unsigned compare for longs and 'less than' for floats.
2544
2545 __ Bind(&greater);
2546 __ LoadImmediate(out, 1);
2547 __ b(&done);
2548
2549 __ Bind(&less);
2550 __ LoadImmediate(out, -1);
2551
2552 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002553}
2554
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002555void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002556 LocationSummary* locations =
2557 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01002558 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2559 locations->SetInAt(i, Location::Any());
2560 }
2561 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002562}
2563
2564void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002565 UNUSED(instruction);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002566 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002567}
2568
Calin Juravle52c48962014-12-16 17:02:57 +00002569void InstructionCodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
2570 // TODO (ported from quick): revisit Arm barrier kinds
2571 DmbOptions flavour = DmbOptions::ISH; // quiet c++ warnings
2572 switch (kind) {
2573 case MemBarrierKind::kAnyStore:
2574 case MemBarrierKind::kLoadAny:
2575 case MemBarrierKind::kAnyAny: {
2576 flavour = DmbOptions::ISH;
2577 break;
2578 }
2579 case MemBarrierKind::kStoreStore: {
2580 flavour = DmbOptions::ISHST;
2581 break;
2582 }
2583 default:
2584 LOG(FATAL) << "Unexpected memory barrier " << kind;
2585 }
2586 __ dmb(flavour);
2587}
2588
2589void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
2590 uint32_t offset,
2591 Register out_lo,
2592 Register out_hi) {
2593 if (offset != 0) {
2594 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00002595 __ add(IP, addr, ShifterOperand(out_lo));
2596 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00002597 }
2598 __ ldrexd(out_lo, out_hi, addr);
2599}
2600
2601void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
2602 uint32_t offset,
2603 Register value_lo,
2604 Register value_hi,
2605 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00002606 Register temp2,
2607 HInstruction* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00002608 Label fail;
2609 if (offset != 0) {
2610 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00002611 __ add(IP, addr, ShifterOperand(temp1));
2612 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00002613 }
2614 __ Bind(&fail);
2615 // We need a load followed by store. (The address used in a STREX instruction must
2616 // be the same as the address in the most recently executed LDREX instruction.)
2617 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00002618 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002619 __ strexd(temp1, value_lo, value_hi, addr);
2620 __ cmp(temp1, ShifterOperand(0));
2621 __ b(&fail, NE);
2622}
2623
2624void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
2625 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2626
Nicolas Geoffray39468442014-09-02 15:17:15 +01002627 LocationSummary* locations =
2628 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002629 locations->SetInAt(0, Location::RequiresRegister());
2630 locations->SetInAt(1, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00002631
Calin Juravle34166012014-12-19 17:22:29 +00002632
Calin Juravle52c48962014-12-16 17:02:57 +00002633 Primitive::Type field_type = field_info.GetFieldType();
2634 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00002635 bool generate_volatile = field_info.IsVolatile()
2636 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002637 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002638 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00002639 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
2640 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002641 locations->AddTemp(Location::RequiresRegister());
2642 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00002643 } else if (generate_volatile) {
Calin Juravle52c48962014-12-16 17:02:57 +00002644 // Arm encoding have some additional constraints for ldrexd/strexd:
2645 // - registers need to be consecutive
2646 // - the first register should be even but not R14.
2647 // We don't test for Arm yet, and the assertion makes sure that we revisit this if we ever
2648 // enable Arm encoding.
2649 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
2650
2651 locations->AddTemp(Location::RequiresRegister());
2652 locations->AddTemp(Location::RequiresRegister());
2653 if (field_type == Primitive::kPrimDouble) {
2654 // For doubles we need two more registers to copy the value.
2655 locations->AddTemp(Location::RegisterLocation(R2));
2656 locations->AddTemp(Location::RegisterLocation(R3));
2657 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002658 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002659}
2660
Calin Juravle52c48962014-12-16 17:02:57 +00002661void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
2662 const FieldInfo& field_info) {
2663 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2664
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002665 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00002666 Register base = locations->InAt(0).AsRegister<Register>();
2667 Location value = locations->InAt(1);
2668
2669 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002670 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00002671 Primitive::Type field_type = field_info.GetFieldType();
2672 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2673
2674 if (is_volatile) {
2675 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
2676 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002677
2678 switch (field_type) {
2679 case Primitive::kPrimBoolean:
2680 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00002681 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002682 break;
2683 }
2684
2685 case Primitive::kPrimShort:
2686 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00002687 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002688 break;
2689 }
2690
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002691 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002692 case Primitive::kPrimNot: {
Calin Juravle77520bc2015-01-12 18:45:46 +00002693 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002694 break;
2695 }
2696
2697 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00002698 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00002699 GenerateWideAtomicStore(base, offset,
2700 value.AsRegisterPairLow<Register>(),
2701 value.AsRegisterPairHigh<Register>(),
2702 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00002703 locations->GetTemp(1).AsRegister<Register>(),
2704 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002705 } else {
2706 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00002707 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002708 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002709 break;
2710 }
2711
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002712 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00002713 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002714 break;
2715 }
2716
2717 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00002718 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00002719 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00002720 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
2721 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
2722
2723 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
2724
2725 GenerateWideAtomicStore(base, offset,
2726 value_reg_lo,
2727 value_reg_hi,
2728 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00002729 locations->GetTemp(3).AsRegister<Register>(),
2730 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002731 } else {
2732 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00002733 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002734 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002735 break;
2736 }
2737
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002738 case Primitive::kPrimVoid:
2739 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002740 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002741 }
Calin Juravle52c48962014-12-16 17:02:57 +00002742
Calin Juravle77520bc2015-01-12 18:45:46 +00002743 // Longs and doubles are handled in the switch.
2744 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
2745 codegen_->MaybeRecordImplicitNullCheck(instruction);
2746 }
2747
2748 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
2749 Register temp = locations->GetTemp(0).AsRegister<Register>();
2750 Register card = locations->GetTemp(1).AsRegister<Register>();
2751 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>());
2752 }
2753
Calin Juravle52c48962014-12-16 17:02:57 +00002754 if (is_volatile) {
2755 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
2756 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002757}
2758
Calin Juravle52c48962014-12-16 17:02:57 +00002759void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
2760 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002761 LocationSummary* locations =
2762 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002763 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00002764
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002765 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00002766 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002767 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002768 bool overlap = field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong);
2769 locations->SetOut(Location::RequiresRegister(),
2770 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
2771 if (volatile_for_double) {
Calin Juravle52c48962014-12-16 17:02:57 +00002772 // Arm encoding have some additional constraints for ldrexd/strexd:
2773 // - registers need to be consecutive
2774 // - the first register should be even but not R14.
2775 // We don't test for Arm yet, and the assertion makes sure that we revisit this if we ever
2776 // enable Arm encoding.
2777 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
2778 locations->AddTemp(Location::RequiresRegister());
2779 locations->AddTemp(Location::RequiresRegister());
2780 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002781}
2782
Calin Juravle52c48962014-12-16 17:02:57 +00002783void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
2784 const FieldInfo& field_info) {
2785 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002786
Calin Juravle52c48962014-12-16 17:02:57 +00002787 LocationSummary* locations = instruction->GetLocations();
2788 Register base = locations->InAt(0).AsRegister<Register>();
2789 Location out = locations->Out();
2790 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002791 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00002792 Primitive::Type field_type = field_info.GetFieldType();
2793 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2794
2795 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002796 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00002797 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002798 break;
2799 }
2800
2801 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00002802 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002803 break;
2804 }
2805
2806 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00002807 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002808 break;
2809 }
2810
2811 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00002812 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002813 break;
2814 }
2815
2816 case Primitive::kPrimInt:
2817 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00002818 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002819 break;
2820 }
2821
2822 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00002823 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00002824 GenerateWideAtomicLoad(base, offset,
2825 out.AsRegisterPairLow<Register>(),
2826 out.AsRegisterPairHigh<Register>());
2827 } else {
2828 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
2829 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002830 break;
2831 }
2832
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002833 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00002834 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002835 break;
2836 }
2837
2838 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00002839 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00002840 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00002841 Register lo = locations->GetTemp(0).AsRegister<Register>();
2842 Register hi = locations->GetTemp(1).AsRegister<Register>();
2843 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00002844 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002845 __ vmovdrr(out_reg, lo, hi);
2846 } else {
2847 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00002848 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002849 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002850 break;
2851 }
2852
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002853 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00002854 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002855 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002856 }
Calin Juravle52c48962014-12-16 17:02:57 +00002857
Calin Juravle77520bc2015-01-12 18:45:46 +00002858 // Doubles are handled in the switch.
2859 if (field_type != Primitive::kPrimDouble) {
2860 codegen_->MaybeRecordImplicitNullCheck(instruction);
2861 }
2862
Calin Juravle52c48962014-12-16 17:02:57 +00002863 if (is_volatile) {
2864 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
2865 }
2866}
2867
2868void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2869 HandleFieldSet(instruction, instruction->GetFieldInfo());
2870}
2871
2872void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2873 HandleFieldSet(instruction, instruction->GetFieldInfo());
2874}
2875
2876void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2877 HandleFieldGet(instruction, instruction->GetFieldInfo());
2878}
2879
2880void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2881 HandleFieldGet(instruction, instruction->GetFieldInfo());
2882}
2883
2884void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2885 HandleFieldGet(instruction, instruction->GetFieldInfo());
2886}
2887
2888void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2889 HandleFieldGet(instruction, instruction->GetFieldInfo());
2890}
2891
2892void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2893 HandleFieldSet(instruction, instruction->GetFieldInfo());
2894}
2895
2896void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2897 HandleFieldSet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002898}
2899
2900void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002901 LocationSummary* locations =
2902 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle77520bc2015-01-12 18:45:46 +00002903 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002904 if (instruction->HasUses()) {
2905 locations->SetOut(Location::SameAsFirstInput());
2906 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002907}
2908
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002909void InstructionCodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00002910 if (codegen_->CanMoveNullCheckToUser(instruction)) {
2911 return;
2912 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002913 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00002914
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002915 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
2916 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2917}
2918
2919void InstructionCodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002920 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002921 codegen_->AddSlowPath(slow_path);
2922
2923 LocationSummary* locations = instruction->GetLocations();
2924 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002925
Calin Juravle77520bc2015-01-12 18:45:46 +00002926 __ cmp(obj.AsRegister<Register>(), ShifterOperand(0));
2927 __ b(slow_path->GetEntryLabel(), EQ);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002928}
2929
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002930void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
2931 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
2932 GenerateImplicitNullCheck(instruction);
2933 } else {
2934 GenerateExplicitNullCheck(instruction);
2935 }
2936}
2937
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002938void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002939 LocationSummary* locations =
2940 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002941 locations->SetInAt(0, Location::RequiresRegister());
2942 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2943 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002944}
2945
2946void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
2947 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002948 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002949 Location index = locations->InAt(1);
2950
2951 switch (instruction->GetType()) {
2952 case Primitive::kPrimBoolean: {
2953 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002954 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002955 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002956 size_t offset =
2957 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002958 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
2959 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002960 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002961 __ LoadFromOffset(kLoadUnsignedByte, out, IP, data_offset);
2962 }
2963 break;
2964 }
2965
2966 case Primitive::kPrimByte: {
2967 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002968 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002969 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002970 size_t offset =
2971 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002972 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
2973 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002974 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002975 __ LoadFromOffset(kLoadSignedByte, out, IP, data_offset);
2976 }
2977 break;
2978 }
2979
2980 case Primitive::kPrimShort: {
2981 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002982 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002983 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002984 size_t offset =
2985 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002986 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
2987 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002988 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002989 __ LoadFromOffset(kLoadSignedHalfword, out, IP, data_offset);
2990 }
2991 break;
2992 }
2993
2994 case Primitive::kPrimChar: {
2995 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002996 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002997 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002998 size_t offset =
2999 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003000 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
3001 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003002 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003003 __ LoadFromOffset(kLoadUnsignedHalfword, out, IP, data_offset);
3004 }
3005 break;
3006 }
3007
3008 case Primitive::kPrimInt:
3009 case Primitive::kPrimNot: {
3010 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
3011 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003012 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003013 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003014 size_t offset =
3015 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003016 __ LoadFromOffset(kLoadWord, out, obj, offset);
3017 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003018 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003019 __ LoadFromOffset(kLoadWord, out, IP, data_offset);
3020 }
3021 break;
3022 }
3023
3024 case Primitive::kPrimLong: {
3025 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003026 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003027 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003028 size_t offset =
3029 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003030 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003031 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003032 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003033 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003034 }
3035 break;
3036 }
3037
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003038 case Primitive::kPrimFloat: {
3039 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3040 Location out = locations->Out();
3041 DCHECK(out.IsFpuRegister());
3042 if (index.IsConstant()) {
3043 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3044 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), obj, offset);
3045 } else {
3046 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
3047 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), IP, data_offset);
3048 }
3049 break;
3050 }
3051
3052 case Primitive::kPrimDouble: {
3053 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3054 Location out = locations->Out();
3055 DCHECK(out.IsFpuRegisterPair());
3056 if (index.IsConstant()) {
3057 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3058 __ LoadDFromOffset(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()), obj, offset);
3059 } else {
3060 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
3061 __ LoadDFromOffset(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
3062 }
3063 break;
3064 }
3065
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003066 case Primitive::kPrimVoid:
3067 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003068 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003069 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003070 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003071}
3072
3073void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003074 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003075
3076 bool needs_write_barrier =
3077 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3078 bool needs_runtime_call = instruction->NeedsTypeCheck();
3079
Nicolas Geoffray39468442014-09-02 15:17:15 +01003080 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003081 instruction, needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
3082 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003083 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003084 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3085 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3086 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003087 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003088 locations->SetInAt(0, Location::RequiresRegister());
3089 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3090 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003091
3092 if (needs_write_barrier) {
3093 // Temporary registers for the write barrier.
3094 locations->AddTemp(Location::RequiresRegister());
3095 locations->AddTemp(Location::RequiresRegister());
3096 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003097 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003098}
3099
3100void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
3101 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003102 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003103 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003104 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003105 bool needs_runtime_call = locations->WillCall();
3106 bool needs_write_barrier =
3107 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003108
3109 switch (value_type) {
3110 case Primitive::kPrimBoolean:
3111 case Primitive::kPrimByte: {
3112 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003113 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003114 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003115 size_t offset =
3116 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003117 __ StoreToOffset(kStoreByte, value, obj, offset);
3118 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003119 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003120 __ StoreToOffset(kStoreByte, value, IP, data_offset);
3121 }
3122 break;
3123 }
3124
3125 case Primitive::kPrimShort:
3126 case Primitive::kPrimChar: {
3127 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003128 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003129 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003130 size_t offset =
3131 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003132 __ StoreToOffset(kStoreHalfword, value, obj, offset);
3133 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003134 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003135 __ StoreToOffset(kStoreHalfword, value, IP, data_offset);
3136 }
3137 break;
3138 }
3139
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003140 case Primitive::kPrimInt:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003141 case Primitive::kPrimNot: {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003142 if (!needs_runtime_call) {
3143 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003144 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003145 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003146 size_t offset =
3147 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003148 __ StoreToOffset(kStoreWord, value, obj, offset);
3149 } else {
3150 DCHECK(index.IsRegister()) << index;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003151 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003152 __ StoreToOffset(kStoreWord, value, IP, data_offset);
3153 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003154 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003155 if (needs_write_barrier) {
3156 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003157 Register temp = locations->GetTemp(0).AsRegister<Register>();
3158 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003159 codegen_->MarkGCCard(temp, card, obj, value);
3160 }
3161 } else {
3162 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain199f3362014-11-27 17:15:16 +00003163 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
3164 instruction,
3165 instruction->GetDexPc());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003166 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003167 break;
3168 }
3169
3170 case Primitive::kPrimLong: {
3171 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003172 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003173 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003174 size_t offset =
3175 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003176 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003177 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003178 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003179 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003180 }
3181 break;
3182 }
3183
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003184 case Primitive::kPrimFloat: {
3185 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3186 Location value = locations->InAt(2);
3187 DCHECK(value.IsFpuRegister());
3188 if (index.IsConstant()) {
3189 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3190 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), obj, offset);
3191 } else {
3192 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
3193 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
3194 }
3195 break;
3196 }
3197
3198 case Primitive::kPrimDouble: {
3199 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3200 Location value = locations->InAt(2);
3201 DCHECK(value.IsFpuRegisterPair());
3202 if (index.IsConstant()) {
3203 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3204 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), obj, offset);
3205 } else {
3206 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
3207 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
3208 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003209
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003210 break;
3211 }
3212
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003213 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003214 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003215 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003216 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003217
3218 // Ints and objects are handled in the switch.
3219 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
3220 codegen_->MaybeRecordImplicitNullCheck(instruction);
3221 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003222}
3223
3224void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003225 LocationSummary* locations =
3226 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003227 locations->SetInAt(0, Location::RequiresRegister());
3228 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003229}
3230
3231void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
3232 LocationSummary* locations = instruction->GetLocations();
3233 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003234 Register obj = locations->InAt(0).AsRegister<Register>();
3235 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003236 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003237 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003238}
3239
3240void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003241 LocationSummary* locations =
3242 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003243 locations->SetInAt(0, Location::RequiresRegister());
3244 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003245 if (instruction->HasUses()) {
3246 locations->SetOut(Location::SameAsFirstInput());
3247 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003248}
3249
3250void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
3251 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003252 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(
Nicolas Geoffray39468442014-09-02 15:17:15 +01003253 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003254 codegen_->AddSlowPath(slow_path);
3255
Roland Levillain271ab9c2014-11-27 15:23:57 +00003256 Register index = locations->InAt(0).AsRegister<Register>();
3257 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003258
3259 __ cmp(index, ShifterOperand(length));
3260 __ b(slow_path->GetEntryLabel(), CS);
3261}
3262
3263void CodeGeneratorARM::MarkGCCard(Register temp, Register card, Register object, Register value) {
3264 Label is_null;
3265 __ CompareAndBranchIfZero(value, &is_null);
3266 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmWordSize>().Int32Value());
3267 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
3268 __ strb(card, Address(card, temp));
3269 __ Bind(&is_null);
3270}
3271
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003272void LocationsBuilderARM::VisitTemporary(HTemporary* temp) {
3273 temp->SetLocations(nullptr);
3274}
3275
3276void InstructionCodeGeneratorARM::VisitTemporary(HTemporary* temp) {
3277 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003278 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003279}
3280
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003281void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003282 UNUSED(instruction);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003283 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003284}
3285
3286void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003287 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3288}
3289
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003290void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
3291 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3292}
3293
3294void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003295 HBasicBlock* block = instruction->GetBlock();
3296 if (block->GetLoopInformation() != nullptr) {
3297 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3298 // The back edge will generate the suspend check.
3299 return;
3300 }
3301 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3302 // The goto will generate the suspend check.
3303 return;
3304 }
3305 GenerateSuspendCheck(instruction, nullptr);
3306}
3307
3308void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
3309 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003310 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003311 new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003312 codegen_->AddSlowPath(slow_path);
3313
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00003314 __ LoadFromOffset(
3315 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmWordSize>().Int32Value());
3316 __ cmp(IP, ShifterOperand(0));
3317 // TODO: Figure out the branch offsets and use cbz/cbnz.
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003318 if (successor == nullptr) {
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00003319 __ b(slow_path->GetEntryLabel(), NE);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003320 __ Bind(slow_path->GetReturnLabel());
3321 } else {
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00003322 __ b(codegen_->GetLabelOf(successor), EQ);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003323 __ b(slow_path->GetEntryLabel());
3324 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003325}
3326
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003327ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
3328 return codegen_->GetAssembler();
3329}
3330
3331void ParallelMoveResolverARM::EmitMove(size_t index) {
3332 MoveOperands* move = moves_.Get(index);
3333 Location source = move->GetSource();
3334 Location destination = move->GetDestination();
3335
3336 if (source.IsRegister()) {
3337 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003338 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003339 } else {
3340 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003341 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003342 SP, destination.GetStackIndex());
3343 }
3344 } else if (source.IsStackSlot()) {
3345 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003346 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003347 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003348 } else if (destination.IsFpuRegister()) {
3349 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003350 } else {
3351 DCHECK(destination.IsStackSlot());
3352 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
3353 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
3354 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003355 } else if (source.IsFpuRegister()) {
3356 if (destination.IsFpuRegister()) {
3357 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003358 } else {
3359 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003360 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
3361 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003362 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003363 DCHECK(destination.IsDoubleStackSlot()) << destination;
3364 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
3365 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
3366 __ LoadFromOffset(kLoadWord, IP, SP, source.GetHighStackIndex(kArmWordSize));
3367 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003368 } else {
3369 DCHECK(source.IsConstant()) << source;
3370 HInstruction* constant = source.GetConstant();
3371 if (constant->IsIntConstant()) {
3372 int32_t value = constant->AsIntConstant()->GetValue();
3373 if (destination.IsRegister()) {
3374 __ LoadImmediate(destination.AsRegister<Register>(), value);
3375 } else {
3376 DCHECK(destination.IsStackSlot());
3377 __ LoadImmediate(IP, value);
3378 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
3379 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003380 } else if (constant->IsLongConstant()) {
3381 int64_t value = constant->AsLongConstant()->GetValue();
3382 if (destination.IsRegister()) {
3383 // In the presence of long or double constants, the parallel move resolver will
3384 // split the move into two, but keeps the same constant for both moves. Here,
3385 // we use the low or high part depending on which register this move goes to.
3386 if (destination.reg() % 2 == 0) {
3387 __ LoadImmediate(destination.AsRegister<Register>(), Low32Bits(value));
3388 } else {
3389 __ LoadImmediate(destination.AsRegister<Register>(), High32Bits(value));
3390 }
3391 } else {
3392 DCHECK(destination.IsDoubleStackSlot());
3393 __ LoadImmediate(IP, Low32Bits(value));
3394 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
3395 __ LoadImmediate(IP, High32Bits(value));
3396 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
3397 }
3398 } else if (constant->IsDoubleConstant()) {
3399 double value = constant->AsDoubleConstant()->GetValue();
3400 uint64_t int_value = bit_cast<uint64_t, double>(value);
3401 if (destination.IsFpuRegister()) {
3402 // In the presence of long or double constants, the parallel move resolver will
3403 // split the move into two, but keeps the same constant for both moves. Here,
3404 // we use the low or high part depending on which register this move goes to.
3405 if (destination.reg() % 2 == 0) {
3406 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(),
3407 bit_cast<float, uint32_t>(Low32Bits(int_value)));
3408 } else {
3409 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(),
3410 bit_cast<float, uint32_t>(High32Bits(int_value)));
3411 }
3412 } else {
3413 DCHECK(destination.IsDoubleStackSlot());
3414 __ LoadImmediate(IP, Low32Bits(int_value));
3415 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
3416 __ LoadImmediate(IP, High32Bits(int_value));
3417 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
3418 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003419 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003420 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003421 float value = constant->AsFloatConstant()->GetValue();
3422 if (destination.IsFpuRegister()) {
3423 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
3424 } else {
3425 DCHECK(destination.IsStackSlot());
3426 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
3427 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
3428 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003429 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003430 }
3431}
3432
3433void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
3434 __ Mov(IP, reg);
3435 __ LoadFromOffset(kLoadWord, reg, SP, mem);
3436 __ StoreToOffset(kStoreWord, IP, SP, mem);
3437}
3438
3439void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
3440 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
3441 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
3442 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
3443 SP, mem1 + stack_offset);
3444 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
3445 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
3446 SP, mem2 + stack_offset);
3447 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
3448}
3449
3450void ParallelMoveResolverARM::EmitSwap(size_t index) {
3451 MoveOperands* move = moves_.Get(index);
3452 Location source = move->GetSource();
3453 Location destination = move->GetDestination();
3454
3455 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003456 DCHECK_NE(source.AsRegister<Register>(), IP);
3457 DCHECK_NE(destination.AsRegister<Register>(), IP);
3458 __ Mov(IP, source.AsRegister<Register>());
3459 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
3460 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003461 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003462 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003463 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003464 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003465 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
3466 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003467 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00003468 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003469 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00003470 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003471 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
3472 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
3473 : destination.AsFpuRegister<SRegister>();
3474 int mem = source.IsFpuRegister()
3475 ? destination.GetStackIndex()
3476 : source.GetStackIndex();
3477
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00003478 __ vmovrs(IP, reg);
3479 __ LoadFromOffset(kLoadWord, IP, SP, mem);
3480 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00003481 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00003482 Exchange(source.GetStackIndex(), destination.GetStackIndex());
3483 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003484 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00003485 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003486 }
3487}
3488
3489void ParallelMoveResolverARM::SpillScratch(int reg) {
3490 __ Push(static_cast<Register>(reg));
3491}
3492
3493void ParallelMoveResolverARM::RestoreScratch(int reg) {
3494 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003495}
3496
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003497void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003498 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
3499 ? LocationSummary::kCallOnSlowPath
3500 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003501 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003502 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003503 locations->SetOut(Location::RequiresRegister());
3504}
3505
3506void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003507 Register out = cls->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003508 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003509 DCHECK(!cls->CanCallRuntime());
3510 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003511 codegen_->LoadCurrentMethod(out);
3512 __ LoadFromOffset(kLoadWord, out, out, mirror::ArtMethod::DeclaringClassOffset().Int32Value());
3513 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003514 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003515 codegen_->LoadCurrentMethod(out);
3516 __ LoadFromOffset(
3517 kLoadWord, out, out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value());
3518 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003519
3520 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
3521 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3522 codegen_->AddSlowPath(slow_path);
3523 __ cmp(out, ShifterOperand(0));
3524 __ b(slow_path->GetEntryLabel(), EQ);
3525 if (cls->MustGenerateClinitCheck()) {
3526 GenerateClassInitializationCheck(slow_path, out);
3527 } else {
3528 __ Bind(slow_path->GetExitLabel());
3529 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003530 }
3531}
3532
3533void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
3534 LocationSummary* locations =
3535 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3536 locations->SetInAt(0, Location::RequiresRegister());
3537 if (check->HasUses()) {
3538 locations->SetOut(Location::SameAsFirstInput());
3539 }
3540}
3541
3542void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003543 // We assume the class is not null.
3544 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
3545 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003546 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00003547 GenerateClassInitializationCheck(slow_path,
3548 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003549}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003550
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003551void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
3552 SlowPathCodeARM* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003553 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
3554 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
3555 __ b(slow_path->GetEntryLabel(), LT);
3556 // Even if the initialized flag is set, we may be in a situation where caches are not synced
3557 // properly. Therefore, we do a memory fence.
3558 __ dmb(ISH);
3559 __ Bind(slow_path->GetExitLabel());
3560}
3561
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003562void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
3563 LocationSummary* locations =
3564 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3565 locations->SetOut(Location::RequiresRegister());
3566}
3567
3568void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) {
3569 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
3570 codegen_->AddSlowPath(slow_path);
3571
Roland Levillain271ab9c2014-11-27 15:23:57 +00003572 Register out = load->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003573 codegen_->LoadCurrentMethod(out);
Mathieu Chartiereace4582014-11-24 18:29:54 -08003574 __ LoadFromOffset(kLoadWord, out, out, mirror::ArtMethod::DeclaringClassOffset().Int32Value());
3575 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003576 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
3577 __ cmp(out, ShifterOperand(0));
3578 __ b(slow_path->GetEntryLabel(), EQ);
3579 __ Bind(slow_path->GetExitLabel());
3580}
3581
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003582void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
3583 LocationSummary* locations =
3584 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3585 locations->SetOut(Location::RequiresRegister());
3586}
3587
3588void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003589 Register out = load->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003590 int32_t offset = Thread::ExceptionOffset<kArmWordSize>().Int32Value();
3591 __ LoadFromOffset(kLoadWord, out, TR, offset);
3592 __ LoadImmediate(IP, 0);
3593 __ StoreToOffset(kStoreWord, IP, TR, offset);
3594}
3595
3596void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
3597 LocationSummary* locations =
3598 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3599 InvokeRuntimeCallingConvention calling_convention;
3600 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3601}
3602
3603void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
3604 codegen_->InvokeRuntime(
3605 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc());
3606}
3607
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003608void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003609 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
3610 ? LocationSummary::kNoCall
3611 : LocationSummary::kCallOnSlowPath;
3612 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3613 locations->SetInAt(0, Location::RequiresRegister());
3614 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003615 // The out register is used as a temporary, so it overlaps with the inputs.
3616 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003617}
3618
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003619void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003620 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003621 Register obj = locations->InAt(0).AsRegister<Register>();
3622 Register cls = locations->InAt(1).AsRegister<Register>();
3623 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003624 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3625 Label done, zero;
3626 SlowPathCodeARM* slow_path = nullptr;
3627
3628 // Return 0 if `obj` is null.
3629 // TODO: avoid this check if we know obj is not null.
3630 __ cmp(obj, ShifterOperand(0));
3631 __ b(&zero, EQ);
3632 // Compare the class of `obj` with `cls`.
3633 __ LoadFromOffset(kLoadWord, out, obj, class_offset);
3634 __ cmp(out, ShifterOperand(cls));
3635 if (instruction->IsClassFinal()) {
3636 // Classes must be equal for the instanceof to succeed.
3637 __ b(&zero, NE);
3638 __ LoadImmediate(out, 1);
3639 __ b(&done);
3640 } else {
3641 // If the classes are not equal, we go into a slow path.
3642 DCHECK(locations->OnlyCallsOnSlowPath());
3643 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003644 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003645 codegen_->AddSlowPath(slow_path);
3646 __ b(slow_path->GetEntryLabel(), NE);
3647 __ LoadImmediate(out, 1);
3648 __ b(&done);
3649 }
3650 __ Bind(&zero);
3651 __ LoadImmediate(out, 0);
3652 if (slow_path != nullptr) {
3653 __ Bind(slow_path->GetExitLabel());
3654 }
3655 __ Bind(&done);
3656}
3657
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003658void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
3659 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3660 instruction, LocationSummary::kCallOnSlowPath);
3661 locations->SetInAt(0, Location::RequiresRegister());
3662 locations->SetInAt(1, Location::RequiresRegister());
3663 locations->AddTemp(Location::RequiresRegister());
3664}
3665
3666void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
3667 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003668 Register obj = locations->InAt(0).AsRegister<Register>();
3669 Register cls = locations->InAt(1).AsRegister<Register>();
3670 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003671 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3672
3673 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(
3674 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
3675 codegen_->AddSlowPath(slow_path);
3676
3677 // TODO: avoid this check if we know obj is not null.
3678 __ cmp(obj, ShifterOperand(0));
3679 __ b(slow_path->GetExitLabel(), EQ);
3680 // Compare the class of `obj` with `cls`.
3681 __ LoadFromOffset(kLoadWord, temp, obj, class_offset);
3682 __ cmp(temp, ShifterOperand(cls));
3683 __ b(slow_path->GetEntryLabel(), NE);
3684 __ Bind(slow_path->GetExitLabel());
3685}
3686
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003687void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
3688 LocationSummary* locations =
3689 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3690 InvokeRuntimeCallingConvention calling_convention;
3691 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3692}
3693
3694void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
3695 codegen_->InvokeRuntime(instruction->IsEnter()
3696 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
3697 instruction,
3698 instruction->GetDexPc());
3699}
3700
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003701void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
3702void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
3703void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
3704
3705void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
3706 LocationSummary* locations =
3707 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3708 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
3709 || instruction->GetResultType() == Primitive::kPrimLong);
3710 locations->SetInAt(0, Location::RequiresRegister());
3711 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003712 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003713}
3714
3715void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
3716 HandleBitwiseOperation(instruction);
3717}
3718
3719void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
3720 HandleBitwiseOperation(instruction);
3721}
3722
3723void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
3724 HandleBitwiseOperation(instruction);
3725}
3726
3727void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
3728 LocationSummary* locations = instruction->GetLocations();
3729
3730 if (instruction->GetResultType() == Primitive::kPrimInt) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003731 Register first = locations->InAt(0).AsRegister<Register>();
3732 Register second = locations->InAt(1).AsRegister<Register>();
3733 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003734 if (instruction->IsAnd()) {
3735 __ and_(out, first, ShifterOperand(second));
3736 } else if (instruction->IsOr()) {
3737 __ orr(out, first, ShifterOperand(second));
3738 } else {
3739 DCHECK(instruction->IsXor());
3740 __ eor(out, first, ShifterOperand(second));
3741 }
3742 } else {
3743 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3744 Location first = locations->InAt(0);
3745 Location second = locations->InAt(1);
3746 Location out = locations->Out();
3747 if (instruction->IsAnd()) {
3748 __ and_(out.AsRegisterPairLow<Register>(),
3749 first.AsRegisterPairLow<Register>(),
3750 ShifterOperand(second.AsRegisterPairLow<Register>()));
3751 __ and_(out.AsRegisterPairHigh<Register>(),
3752 first.AsRegisterPairHigh<Register>(),
3753 ShifterOperand(second.AsRegisterPairHigh<Register>()));
3754 } else if (instruction->IsOr()) {
3755 __ orr(out.AsRegisterPairLow<Register>(),
3756 first.AsRegisterPairLow<Register>(),
3757 ShifterOperand(second.AsRegisterPairLow<Register>()));
3758 __ orr(out.AsRegisterPairHigh<Register>(),
3759 first.AsRegisterPairHigh<Register>(),
3760 ShifterOperand(second.AsRegisterPairHigh<Register>()));
3761 } else {
3762 DCHECK(instruction->IsXor());
3763 __ eor(out.AsRegisterPairLow<Register>(),
3764 first.AsRegisterPairLow<Register>(),
3765 ShifterOperand(second.AsRegisterPairLow<Register>()));
3766 __ eor(out.AsRegisterPairHigh<Register>(),
3767 first.AsRegisterPairHigh<Register>(),
3768 ShifterOperand(second.AsRegisterPairHigh<Register>()));
3769 }
3770 }
3771}
3772
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08003773void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Register temp) {
3774 DCHECK_EQ(temp, kArtMethodRegister);
3775
3776 // TODO: Implement all kinds of calls:
3777 // 1) boot -> boot
3778 // 2) app -> boot
3779 // 3) app -> app
3780 //
3781 // Currently we implement the app -> app logic, which looks up in the resolve cache.
3782
3783 // temp = method;
3784 LoadCurrentMethod(temp);
3785 if (!invoke->IsRecursive()) {
3786 // temp = temp->dex_cache_resolved_methods_;
3787 __ LoadFromOffset(
3788 kLoadWord, temp, temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value());
3789 // temp = temp[index_in_cache]
3790 __ LoadFromOffset(
3791 kLoadWord, temp, temp, CodeGenerator::GetCacheOffset(invoke->GetDexMethodIndex()));
3792 // LR = temp[offset_of_quick_compiled_code]
3793 __ LoadFromOffset(kLoadWord, LR, temp,
3794 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
3795 kArmWordSize).Int32Value());
3796 // LR()
3797 __ blx(LR);
3798 } else {
3799 __ bl(GetFrameEntryLabel());
3800 }
3801
3802 RecordPcInfo(invoke, invoke->GetDexPc());
3803 DCHECK(!IsLeafMethod());
3804}
3805
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003806} // namespace arm
3807} // namespace art