blob: 0fa38c00e2abe76445d3e029d304bc99f5e68c8d [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"
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "art_method.h"
Zheng Xuc6667102015-05-15 16:08:45 +080021#include "code_generator_utils.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070022#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010023#include "gc/accounting/card_table.h"
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -080024#include "intrinsics.h"
25#include "intrinsics_arm.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070026#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070027#include "mirror/class-inl.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070028#include "thread.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010029#include "utils/arm/assembler_arm.h"
30#include "utils/arm/managed_register_arm.h"
Roland Levillain946e1432014-11-11 17:35:19 +000031#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010032#include "utils/stack_checks.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000033
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000034namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010035
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000036namespace arm {
37
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +000038static bool ExpectedPairLayout(Location location) {
39 // We expected this for both core and fpu register pairs.
40 return ((location.low() & 1) == 0) && (location.low() + 1 == location.high());
41}
42
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010043static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010044static constexpr Register kMethodRegisterArgument = R0;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010045
Nicolas Geoffray4dee6362015-01-23 18:23:14 +000046// We unconditionally allocate R5 to ensure we can do long operations
47// with baseline.
48static constexpr Register kCoreSavedRegisterForBaseline = R5;
49static constexpr Register kCoreCalleeSaves[] =
50 { R5, R6, R7, R8, R10, R11, PC };
51static constexpr SRegister kFpuCalleeSaves[] =
52 { S16, S17, S18, S19, S20, S21, S22, S23, S24, S25, S26, S27, S28, S29, S30, S31 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010053
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +000054// D31 cannot be split into two S registers, and the register allocator only works on
55// S registers. Therefore there is no need to block it.
56static constexpr DRegister DTMP = D31;
57
Roland Levillain62a46b22015-06-01 18:24:13 +010058#define __ down_cast<ArmAssembler*>(codegen->GetAssembler())->
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010059#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmWordSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010060
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010061class NullCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010062 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010063 explicit NullCheckSlowPathARM(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010064
Alexandre Rames67555f72014-11-18 10:55:16 +000065 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010066 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010067 __ Bind(GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010068 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +000069 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010070 }
71
Alexandre Rames9931f312015-06-19 14:47:01 +010072 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM"; }
73
Nicolas Geoffraye5038322014-07-04 09:41:32 +010074 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010075 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010076 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM);
77};
78
Calin Juravled0d48522014-11-04 16:40:20 +000079class DivZeroCheckSlowPathARM : public SlowPathCodeARM {
80 public:
81 explicit DivZeroCheckSlowPathARM(HDivZeroCheck* instruction) : instruction_(instruction) {}
82
Alexandre Rames67555f72014-11-18 10:55:16 +000083 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000084 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
85 __ Bind(GetEntryLabel());
86 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +000087 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Calin Juravled0d48522014-11-04 16:40:20 +000088 }
89
Alexandre Rames9931f312015-06-19 14:47:01 +010090 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM"; }
91
Calin Juravled0d48522014-11-04 16:40:20 +000092 private:
93 HDivZeroCheck* const instruction_;
94 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM);
95};
96
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010097class SuspendCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000098 public:
Alexandre Rames67555f72014-11-18 10:55:16 +000099 SuspendCheckSlowPathARM(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100100 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000101
Alexandre Rames67555f72014-11-18 10:55:16 +0000102 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100103 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000104 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000105 SaveLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100106 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000107 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000108 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100109 if (successor_ == nullptr) {
110 __ b(GetReturnLabel());
111 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100112 __ b(arm_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100113 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000114 }
115
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100116 Label* GetReturnLabel() {
117 DCHECK(successor_ == nullptr);
118 return &return_label_;
119 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000120
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100121 HBasicBlock* GetSuccessor() const {
122 return successor_;
123 }
124
Alexandre Rames9931f312015-06-19 14:47:01 +0100125 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM"; }
126
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000127 private:
128 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100129 // If not null, the block to branch to after the suspend check.
130 HBasicBlock* const successor_;
131
132 // If `successor_` is null, the label to branch to after the suspend check.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000133 Label return_label_;
134
135 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM);
136};
137
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100138class BoundsCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100139 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100140 BoundsCheckSlowPathARM(HBoundsCheck* instruction,
141 Location index_location,
142 Location length_location)
Nicolas Geoffray39468442014-09-02 15:17:15 +0100143 : instruction_(instruction),
144 index_location_(index_location),
145 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100146
Alexandre Rames67555f72014-11-18 10:55:16 +0000147 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100148 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100149 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000150 // We're moving two locations to locations that could overlap, so we need a parallel
151 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100152 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000153 codegen->EmitParallelMoves(
154 index_location_,
155 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100156 Primitive::kPrimInt,
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000157 length_location_,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100158 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
159 Primitive::kPrimInt);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100160 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000161 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100162 }
163
Alexandre Rames9931f312015-06-19 14:47:01 +0100164 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM"; }
165
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100166 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100167 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100168 const Location index_location_;
169 const Location length_location_;
170
171 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM);
172};
173
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000174class LoadClassSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100175 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000176 LoadClassSlowPathARM(HLoadClass* cls,
177 HInstruction* at,
178 uint32_t dex_pc,
179 bool do_clinit)
180 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
181 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
182 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100183
Alexandre Rames67555f72014-11-18 10:55:16 +0000184 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000185 LocationSummary* locations = at_->GetLocations();
186
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100187 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
188 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000189 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100190
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100191 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000192 __ LoadImmediate(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000193 int32_t entry_point_offset = do_clinit_
194 ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
195 : QUICK_ENTRY_POINT(pInitializeType);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000196 arm_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000197
198 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000199 Location out = locations->Out();
200 if (out.IsValid()) {
201 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000202 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
203 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000204 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100205 __ b(GetExitLabel());
206 }
207
Alexandre Rames9931f312015-06-19 14:47:01 +0100208 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM"; }
209
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100210 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000211 // The class this slow path will load.
212 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100213
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000214 // The instruction where this slow path is happening.
215 // (Might be the load class or an initialization check).
216 HInstruction* const at_;
217
218 // The dex PC of `at_`.
219 const uint32_t dex_pc_;
220
221 // Whether to initialize the class.
222 const bool do_clinit_;
223
224 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100225};
226
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000227class LoadStringSlowPathARM : public SlowPathCodeARM {
228 public:
229 explicit LoadStringSlowPathARM(HLoadString* instruction) : instruction_(instruction) {}
230
Alexandre Rames67555f72014-11-18 10:55:16 +0000231 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000232 LocationSummary* locations = instruction_->GetLocations();
233 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
234
235 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
236 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000237 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000238
239 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800240 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction_->GetStringIndex());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000241 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000242 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000243 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
244
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000245 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000246 __ b(GetExitLabel());
247 }
248
Alexandre Rames9931f312015-06-19 14:47:01 +0100249 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM"; }
250
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000251 private:
252 HLoadString* const instruction_;
253
254 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM);
255};
256
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000257class TypeCheckSlowPathARM : public SlowPathCodeARM {
258 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000259 TypeCheckSlowPathARM(HInstruction* instruction,
260 Location class_to_check,
261 Location object_class,
262 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000263 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000264 class_to_check_(class_to_check),
265 object_class_(object_class),
266 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000267
Alexandre Rames67555f72014-11-18 10:55:16 +0000268 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000269 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000270 DCHECK(instruction_->IsCheckCast()
271 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000272
273 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
274 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000275 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000276
277 // We're moving two locations to locations that could overlap, so we need a parallel
278 // move resolver.
279 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000280 codegen->EmitParallelMoves(
281 class_to_check_,
282 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100283 Primitive::kPrimNot,
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000284 object_class_,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100285 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
286 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000287
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000288 if (instruction_->IsInstanceOf()) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000289 arm_codegen->InvokeRuntime(
290 QUICK_ENTRY_POINT(pInstanceofNonTrivial), instruction_, dex_pc_, this);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000291 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
292 } else {
293 DCHECK(instruction_->IsCheckCast());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000294 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc_, this);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000295 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000296
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000297 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000298 __ b(GetExitLabel());
299 }
300
Alexandre Rames9931f312015-06-19 14:47:01 +0100301 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM"; }
302
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000303 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000304 HInstruction* const instruction_;
305 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000306 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000307 uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000308
309 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM);
310};
311
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700312class DeoptimizationSlowPathARM : public SlowPathCodeARM {
313 public:
314 explicit DeoptimizationSlowPathARM(HInstruction* instruction)
315 : instruction_(instruction) {}
316
317 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
318 __ Bind(GetEntryLabel());
319 SaveLiveRegisters(codegen, instruction_->GetLocations());
320 DCHECK(instruction_->IsDeoptimize());
321 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
322 uint32_t dex_pc = deoptimize->GetDexPc();
323 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
324 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize), instruction_, dex_pc, this);
325 }
326
Alexandre Rames9931f312015-06-19 14:47:01 +0100327 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM"; }
328
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700329 private:
330 HInstruction* const instruction_;
331 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM);
332};
333
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000334#undef __
335
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100336#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100337#define __ down_cast<ArmAssembler*>(GetAssembler())->
Dave Allison20dfc792014-06-16 20:44:29 -0700338
339inline Condition ARMCondition(IfCondition cond) {
340 switch (cond) {
341 case kCondEQ: return EQ;
342 case kCondNE: return NE;
343 case kCondLT: return LT;
344 case kCondLE: return LE;
345 case kCondGT: return GT;
346 case kCondGE: return GE;
347 default:
348 LOG(FATAL) << "Unknown if condition";
349 }
350 return EQ; // Unreachable.
351}
352
353inline Condition ARMOppositeCondition(IfCondition cond) {
354 switch (cond) {
355 case kCondEQ: return NE;
356 case kCondNE: return EQ;
357 case kCondLT: return GE;
358 case kCondLE: return GT;
359 case kCondGT: return LE;
360 case kCondGE: return LT;
361 default:
362 LOG(FATAL) << "Unknown if condition";
363 }
364 return EQ; // Unreachable.
365}
366
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100367void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100368 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100369}
370
371void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100372 stream << SRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100373}
374
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100375size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
376 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
377 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100378}
379
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100380size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
381 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
382 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100383}
384
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000385size_t CodeGeneratorARM::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
386 __ StoreSToOffset(static_cast<SRegister>(reg_id), SP, stack_index);
387 return kArmWordSize;
388}
389
390size_t CodeGeneratorARM::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
391 __ LoadSFromOffset(static_cast<SRegister>(reg_id), SP, stack_index);
392 return kArmWordSize;
393}
394
Calin Juravle34166012014-12-19 17:22:29 +0000395CodeGeneratorARM::CodeGeneratorARM(HGraph* graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000396 const ArmInstructionSetFeatures& isa_features,
397 const CompilerOptions& compiler_options)
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000398 : CodeGenerator(graph,
399 kNumberOfCoreRegisters,
400 kNumberOfSRegisters,
401 kNumberOfRegisterPairs,
402 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
403 arraysize(kCoreCalleeSaves)),
404 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
405 arraysize(kFpuCalleeSaves)),
406 compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100407 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100408 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100409 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100410 move_resolver_(graph->GetArena(), this),
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000411 assembler_(),
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000412 isa_features_(isa_features) {
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000413 // Save the PC register to mimic Quick.
414 AddAllocatedRegister(Location::RegisterLocation(PC));
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100415}
416
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000417void CodeGeneratorARM::Finalize(CodeAllocator* allocator) {
418 // Ensure that we fix up branches and literal loads and emit the literal pool.
419 __ FinalizeCode();
420
421 // Adjust native pc offsets in stack maps.
422 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
423 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
424 uint32_t new_position = __ GetAdjustedPosition(old_position);
425 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
426 }
427 // Adjust native pc offsets of block labels.
428 for (size_t block_idx = 0u, end = block_order_->Size(); block_idx != end; ++block_idx) {
429 HBasicBlock* block = block_order_->Get(block_idx);
430 // Get the label directly from block_labels_ rather than through GetLabelOf() to avoid
431 // FirstNonEmptyBlock() which could lead to adjusting a label more than once.
432 DCHECK_LT(static_cast<size_t>(block->GetBlockId()), block_labels_.Size());
433 Label* block_label = &block_labels_.GetRawStorage()[block->GetBlockId()];
434 DCHECK_EQ(block_label->IsBound(), !block->IsSingleGoto());
435 if (block_label->IsBound()) {
436 __ AdjustLabelPosition(block_label);
437 }
438 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100439 // Adjust pc offsets for the disassembly information.
440 if (disasm_info_ != nullptr) {
441 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
442 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
443 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
444 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
445 it.second.start = __ GetAdjustedPosition(it.second.start);
446 it.second.end = __ GetAdjustedPosition(it.second.end);
447 }
448 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
449 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
450 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
451 }
452 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000453
454 CodeGenerator::Finalize(allocator);
455}
456
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100457Location CodeGeneratorARM::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100458 switch (type) {
459 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100460 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100461 ArmManagedRegister pair =
462 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100463 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
464 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
465
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100466 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
467 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100468 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100469 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100470 }
471
472 case Primitive::kPrimByte:
473 case Primitive::kPrimBoolean:
474 case Primitive::kPrimChar:
475 case Primitive::kPrimShort:
476 case Primitive::kPrimInt:
477 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100478 int reg = FindFreeEntry(blocked_core_registers_, kNumberOfCoreRegisters);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100479 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100480 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
481 ArmManagedRegister current =
482 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
483 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100484 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100485 }
486 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100487 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100488 }
489
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000490 case Primitive::kPrimFloat: {
491 int reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfSRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100492 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100493 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100494
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000495 case Primitive::kPrimDouble: {
Nicolas Geoffray3c035032014-10-28 10:46:40 +0000496 int reg = FindTwoFreeConsecutiveAlignedEntries(blocked_fpu_registers_, kNumberOfSRegisters);
497 DCHECK_EQ(reg % 2, 0);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000498 return Location::FpuRegisterPairLocation(reg, reg + 1);
499 }
500
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100501 case Primitive::kPrimVoid:
502 LOG(FATAL) << "Unreachable type " << type;
503 }
504
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100505 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100506}
507
Nicolas Geoffraya0bb2bd2015-01-26 12:49:35 +0000508void CodeGeneratorARM::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100509 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100510 blocked_register_pairs_[R1_R2] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100511
512 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100513 blocked_core_registers_[SP] = true;
514 blocked_core_registers_[LR] = true;
515 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100516
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100517 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100518 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100519
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100520 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100521 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100522
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000523 if (is_baseline) {
524 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
525 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
526 }
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000527
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000528 blocked_core_registers_[kCoreSavedRegisterForBaseline] = false;
529
530 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
531 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
532 }
533 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100534
535 UpdateBlockedPairRegisters();
536}
537
538void CodeGeneratorARM::UpdateBlockedPairRegisters() const {
539 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
540 ArmManagedRegister current =
541 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
542 if (blocked_core_registers_[current.AsRegisterPairLow()]
543 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
544 blocked_register_pairs_[i] = true;
545 }
546 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100547}
548
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100549InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
550 : HGraphVisitor(graph),
551 assembler_(codegen->GetAssembler()),
552 codegen_(codegen) {}
553
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000554void CodeGeneratorARM::ComputeSpillMask() {
555 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000556 // Save one extra register for baseline. Note that on thumb2, there is no easy
557 // instruction to restore just the PC, so this actually helps both baseline
558 // and non-baseline to save and restore at least two registers at entry and exit.
559 core_spill_mask_ |= (1 << kCoreSavedRegisterForBaseline);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000560 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
561 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
562 // We use vpush and vpop for saving and restoring floating point registers, which take
563 // a SRegister and the number of registers to save/restore after that SRegister. We
564 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
565 // but in the range.
566 if (fpu_spill_mask_ != 0) {
567 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
568 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
569 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
570 fpu_spill_mask_ |= (1 << i);
571 }
572 }
573}
574
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100575static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100576 return dwarf::Reg::ArmCore(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100577}
578
579static dwarf::Reg DWARFReg(SRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100580 return dwarf::Reg::ArmFp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100581}
582
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000583void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +0000584 bool skip_overflow_check =
585 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000586 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000587 __ Bind(&frame_entry_label_);
588
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000589 if (HasEmptyFrame()) {
590 return;
591 }
592
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100593 if (!skip_overflow_check) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000594 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
595 __ LoadFromOffset(kLoadWord, IP, IP, 0);
596 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100597 }
598
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000599 // PC is in the list of callee-save to mimic Quick, but we need to push
600 // LR at entry instead.
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100601 uint32_t push_mask = (core_spill_mask_ & (~(1 << PC))) | 1 << LR;
602 __ PushList(push_mask);
603 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(push_mask));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100604 __ cfi().RelOffsetForMany(DWARFReg(kMethodRegisterArgument), 0, push_mask, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000605 if (fpu_spill_mask_ != 0) {
606 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
607 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100608 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
David Srbecky9d8606d2015-04-12 09:35:32 +0100609 __ cfi().RelOffsetForMany(DWARFReg(S0), 0, fpu_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000610 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100611 int adjust = GetFrameSize() - FrameEntrySpillSize();
612 __ AddConstant(SP, -adjust);
613 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100614 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, 0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000615}
616
617void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000618 if (HasEmptyFrame()) {
619 __ bx(LR);
620 return;
621 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100622 __ cfi().RememberState();
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100623 int adjust = GetFrameSize() - FrameEntrySpillSize();
624 __ AddConstant(SP, adjust);
625 __ cfi().AdjustCFAOffset(-adjust);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000626 if (fpu_spill_mask_ != 0) {
627 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
628 __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100629 __ cfi().AdjustCFAOffset(-kArmPointerSize * POPCOUNT(fpu_spill_mask_));
630 __ cfi().RestoreMany(DWARFReg(SRegister(0)), fpu_spill_mask_);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000631 }
632 __ PopList(core_spill_mask_);
David Srbeckyc34dc932015-04-12 09:27:43 +0100633 __ cfi().RestoreState();
634 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000635}
636
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100637void CodeGeneratorARM::Bind(HBasicBlock* block) {
638 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000639}
640
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100641Location CodeGeneratorARM::GetStackLocation(HLoadLocal* load) const {
642 switch (load->GetType()) {
643 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100644 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100645 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100646
647 case Primitive::kPrimInt:
648 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100649 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100650 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100651
652 case Primitive::kPrimBoolean:
653 case Primitive::kPrimByte:
654 case Primitive::kPrimChar:
655 case Primitive::kPrimShort:
656 case Primitive::kPrimVoid:
657 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700658 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100659 }
660
661 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700662 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100663}
664
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100665Location InvokeDexCallingConventionVisitorARM::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100666 switch (type) {
667 case Primitive::kPrimBoolean:
668 case Primitive::kPrimByte:
669 case Primitive::kPrimChar:
670 case Primitive::kPrimShort:
671 case Primitive::kPrimInt:
672 case Primitive::kPrimNot: {
673 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000674 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100675 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100676 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100677 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000678 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100679 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100680 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100681
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000682 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100683 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000684 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100685 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000686 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100687 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000688 if (calling_convention.GetRegisterAt(index) == R1) {
689 // Skip R1, and use R2_R3 instead.
690 gp_index_++;
691 index++;
692 }
693 }
694 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
695 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +0000696 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000697 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +0000698 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100699 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000700 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
701 }
702 }
703
704 case Primitive::kPrimFloat: {
705 uint32_t stack_index = stack_index_++;
706 if (float_index_ % 2 == 0) {
707 float_index_ = std::max(double_index_, float_index_);
708 }
709 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
710 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
711 } else {
712 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
713 }
714 }
715
716 case Primitive::kPrimDouble: {
717 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
718 uint32_t stack_index = stack_index_;
719 stack_index_ += 2;
720 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
721 uint32_t index = double_index_;
722 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000723 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000724 calling_convention.GetFpuRegisterAt(index),
725 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000726 DCHECK(ExpectedPairLayout(result));
727 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000728 } else {
729 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100730 }
731 }
732
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100733 case Primitive::kPrimVoid:
734 LOG(FATAL) << "Unexpected parameter type " << type;
735 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100736 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100737 return Location();
738}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100739
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100740Location InvokeDexCallingConventionVisitorARM::GetReturnLocation(Primitive::Type type) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000741 switch (type) {
742 case Primitive::kPrimBoolean:
743 case Primitive::kPrimByte:
744 case Primitive::kPrimChar:
745 case Primitive::kPrimShort:
746 case Primitive::kPrimInt:
747 case Primitive::kPrimNot: {
748 return Location::RegisterLocation(R0);
749 }
750
751 case Primitive::kPrimFloat: {
752 return Location::FpuRegisterLocation(S0);
753 }
754
755 case Primitive::kPrimLong: {
756 return Location::RegisterPairLocation(R0, R1);
757 }
758
759 case Primitive::kPrimDouble: {
760 return Location::FpuRegisterPairLocation(S0, S1);
761 }
762
763 case Primitive::kPrimVoid:
764 return Location();
765 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100766
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000767 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000768}
769
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100770Location InvokeDexCallingConventionVisitorARM::GetMethodLocation() const {
771 return Location::RegisterLocation(kMethodRegisterArgument);
772}
773
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100774void CodeGeneratorARM::Move32(Location destination, Location source) {
775 if (source.Equals(destination)) {
776 return;
777 }
778 if (destination.IsRegister()) {
779 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000780 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100781 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000782 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100783 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000784 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100785 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100786 } else if (destination.IsFpuRegister()) {
787 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000788 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100789 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000790 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100791 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000792 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100793 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100794 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +0000795 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100796 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000797 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100798 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000799 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100800 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +0000801 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100802 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
803 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100804 }
805 }
806}
807
808void CodeGeneratorARM::Move64(Location destination, Location source) {
809 if (source.Equals(destination)) {
810 return;
811 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100812 if (destination.IsRegisterPair()) {
813 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000814 EmitParallelMoves(
815 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
816 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100817 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000818 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100819 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
820 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100821 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000822 UNIMPLEMENTED(FATAL);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100823 } else {
824 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000825 DCHECK(ExpectedPairLayout(destination));
826 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
827 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100828 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000829 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100830 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000831 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
832 SP,
833 source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100834 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000835 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100836 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100837 } else {
838 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100839 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000840 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100841 if (source.AsRegisterPairLow<Register>() == R1) {
842 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100843 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
844 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100845 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100846 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100847 SP, destination.GetStackIndex());
848 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000849 } else if (source.IsFpuRegisterPair()) {
850 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
851 SP,
852 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100853 } else {
854 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000855 EmitParallelMoves(
856 Location::StackSlot(source.GetStackIndex()),
857 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100858 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000859 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100860 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)),
861 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100862 }
863 }
864}
865
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100866void CodeGeneratorARM::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100867 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100868 if (instruction->IsCurrentMethod()) {
869 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
870 } else if (locations != nullptr && locations->Out().Equals(location)) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100871 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100872 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000873 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000874 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
875 int32_t value = GetInt32ValueOf(const_to_move);
Calin Juravlea21f5982014-11-13 15:53:04 +0000876 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000877 __ LoadImmediate(location.AsRegister<Register>(), value);
Calin Juravlea21f5982014-11-13 15:53:04 +0000878 } else {
879 DCHECK(location.IsStackSlot());
880 __ LoadImmediate(IP, value);
881 __ StoreToOffset(kStoreWord, IP, SP, location.GetStackIndex());
882 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000883 } else {
Nicolas Geoffray3747b482015-01-19 17:17:16 +0000884 DCHECK(const_to_move->IsLongConstant()) << const_to_move->DebugName();
Calin Juravlea21f5982014-11-13 15:53:04 +0000885 int64_t value = const_to_move->AsLongConstant()->GetValue();
886 if (location.IsRegisterPair()) {
887 __ LoadImmediate(location.AsRegisterPairLow<Register>(), Low32Bits(value));
888 __ LoadImmediate(location.AsRegisterPairHigh<Register>(), High32Bits(value));
889 } else {
890 DCHECK(location.IsDoubleStackSlot());
891 __ LoadImmediate(IP, Low32Bits(value));
892 __ StoreToOffset(kStoreWord, IP, SP, location.GetStackIndex());
893 __ LoadImmediate(IP, High32Bits(value));
894 __ StoreToOffset(kStoreWord, IP, SP, location.GetHighStackIndex(kArmWordSize));
895 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100896 }
Roland Levillain476df552014-10-09 17:51:36 +0100897 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100898 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
899 switch (instruction->GetType()) {
900 case Primitive::kPrimBoolean:
901 case Primitive::kPrimByte:
902 case Primitive::kPrimChar:
903 case Primitive::kPrimShort:
904 case Primitive::kPrimInt:
905 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100906 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100907 Move32(location, Location::StackSlot(stack_slot));
908 break;
909
910 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100911 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100912 Move64(location, Location::DoubleStackSlot(stack_slot));
913 break;
914
915 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100916 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100917 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000918 } else if (instruction->IsTemporary()) {
919 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000920 if (temp_location.IsStackSlot()) {
921 Move32(location, temp_location);
922 } else {
923 DCHECK(temp_location.IsDoubleStackSlot());
924 Move64(location, temp_location);
925 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000926 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100927 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100928 switch (instruction->GetType()) {
929 case Primitive::kPrimBoolean:
930 case Primitive::kPrimByte:
931 case Primitive::kPrimChar:
932 case Primitive::kPrimShort:
933 case Primitive::kPrimNot:
934 case Primitive::kPrimInt:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100935 case Primitive::kPrimFloat:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100936 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100937 break;
938
939 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100940 case Primitive::kPrimDouble:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100941 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100942 break;
943
944 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100945 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100946 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000947 }
948}
949
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100950void CodeGeneratorARM::InvokeRuntime(int32_t entry_point_offset,
951 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000952 uint32_t dex_pc,
953 SlowPathCode* slow_path) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100954 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
955 __ blx(LR);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000956 RecordPcInfo(instruction, dex_pc, slow_path);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100957 DCHECK(instruction->IsSuspendCheck()
958 || instruction->IsBoundsCheck()
959 || instruction->IsNullCheck()
Calin Juravled0d48522014-11-04 16:40:20 +0000960 || instruction->IsDivZeroCheck()
Roland Levillain624279f2014-12-04 11:54:28 +0000961 || instruction->GetLocations()->CanCall()
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100962 || !IsLeafMethod());
963}
964
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000965void LocationsBuilderARM::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000966 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000967}
968
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000969void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000970 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100971 DCHECK(!successor->IsExitBlock());
972
973 HBasicBlock* block = got->GetBlock();
974 HInstruction* previous = got->GetPrevious();
975
976 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000977 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100978 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
979 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
980 return;
981 }
982
983 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
984 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
985 }
986 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000987 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000988 }
989}
990
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000991void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000992 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000993}
994
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000995void InstructionCodeGeneratorARM::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700996 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000997}
998
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700999void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
1000 Label* true_target,
1001 Label* false_target,
1002 Label* always_true_target) {
1003 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001004 if (cond->IsIntConstant()) {
1005 // Constant condition, statically compared against 1.
1006 int32_t cond_value = cond->AsIntConstant()->GetValue();
1007 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001008 if (always_true_target != nullptr) {
1009 __ b(always_true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001010 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001011 return;
1012 } else {
1013 DCHECK_EQ(cond_value, 0);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001014 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001015 } else {
1016 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1017 // Condition has been materialized, compare the output to 0
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001018 DCHECK(instruction->GetLocations()->InAt(0).IsRegister());
1019 __ cmp(instruction->GetLocations()->InAt(0).AsRegister<Register>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001020 ShifterOperand(0));
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001021 __ b(true_target, NE);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001022 } else {
1023 // Condition has not been materialized, use its inputs as the
1024 // comparison and its condition as the branch condition.
1025 LocationSummary* locations = cond->GetLocations();
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001026 DCHECK(locations->InAt(0).IsRegister()) << locations->InAt(0);
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +00001027 Register left = locations->InAt(0).AsRegister<Register>();
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001028 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +00001029 __ cmp(left, ShifterOperand(locations->InAt(1).AsRegister<Register>()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001030 } else {
1031 DCHECK(locations->InAt(1).IsConstant());
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001032 HConstant* constant = locations->InAt(1).GetConstant();
1033 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001034 ShifterOperand operand;
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +00001035 if (GetAssembler()->ShifterOperandCanHold(R0, left, CMP, value, &operand)) {
1036 __ cmp(left, operand);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001037 } else {
1038 Register temp = IP;
1039 __ LoadImmediate(temp, value);
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +00001040 __ cmp(left, ShifterOperand(temp));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001041 }
1042 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001043 __ b(true_target, ARMCondition(cond->AsCondition()->GetCondition()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001044 }
Dave Allison20dfc792014-06-16 20:44:29 -07001045 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001046 if (false_target != nullptr) {
1047 __ b(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001048 }
1049}
1050
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001051void LocationsBuilderARM::VisitIf(HIf* if_instr) {
1052 LocationSummary* locations =
1053 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
1054 HInstruction* cond = if_instr->InputAt(0);
1055 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1056 locations->SetInAt(0, Location::RequiresRegister());
1057 }
1058}
1059
1060void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
1061 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1062 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1063 Label* always_true_target = true_target;
1064 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1065 if_instr->IfTrueSuccessor())) {
1066 always_true_target = nullptr;
1067 }
1068 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1069 if_instr->IfFalseSuccessor())) {
1070 false_target = nullptr;
1071 }
1072 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
1073}
1074
1075void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
1076 LocationSummary* locations = new (GetGraph()->GetArena())
1077 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1078 HInstruction* cond = deoptimize->InputAt(0);
1079 DCHECK(cond->IsCondition());
1080 if (cond->AsCondition()->NeedsMaterialization()) {
1081 locations->SetInAt(0, Location::RequiresRegister());
1082 }
1083}
1084
1085void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) {
1086 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena())
1087 DeoptimizationSlowPathARM(deoptimize);
1088 codegen_->AddSlowPath(slow_path);
1089 Label* slow_path_entry = slow_path->GetEntryLabel();
1090 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
1091}
Dave Allison20dfc792014-06-16 20:44:29 -07001092
Roland Levillain0d37cd02015-05-27 16:39:19 +01001093void LocationsBuilderARM::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001094 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001095 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001096 locations->SetInAt(0, Location::RequiresRegister());
Roland Levillain0d37cd02015-05-27 16:39:19 +01001097 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1098 if (cond->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001099 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001100 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001101}
1102
Roland Levillain0d37cd02015-05-27 16:39:19 +01001103void InstructionCodeGeneratorARM::VisitCondition(HCondition* cond) {
1104 if (!cond->NeedsMaterialization()) return;
1105 LocationSummary* locations = cond->GetLocations();
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +00001106 Register left = locations->InAt(0).AsRegister<Register>();
1107
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001108 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +00001109 __ cmp(left, ShifterOperand(locations->InAt(1).AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001110 } else {
1111 DCHECK(locations->InAt(1).IsConstant());
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001112 int32_t value = CodeGenerator::GetInt32ValueOf(locations->InAt(1).GetConstant());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001113 ShifterOperand operand;
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +00001114 if (GetAssembler()->ShifterOperandCanHold(R0, left, CMP, value, &operand)) {
1115 __ cmp(left, operand);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001116 } else {
1117 Register temp = IP;
1118 __ LoadImmediate(temp, value);
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +00001119 __ cmp(left, ShifterOperand(temp));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001120 }
Dave Allison20dfc792014-06-16 20:44:29 -07001121 }
Roland Levillain0d37cd02015-05-27 16:39:19 +01001122 __ it(ARMCondition(cond->GetCondition()), kItElse);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001123 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(1),
Roland Levillain0d37cd02015-05-27 16:39:19 +01001124 ARMCondition(cond->GetCondition()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001125 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(0),
Roland Levillain0d37cd02015-05-27 16:39:19 +01001126 ARMOppositeCondition(cond->GetCondition()));
Dave Allison20dfc792014-06-16 20:44:29 -07001127}
1128
1129void LocationsBuilderARM::VisitEqual(HEqual* comp) {
1130 VisitCondition(comp);
1131}
1132
1133void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
1134 VisitCondition(comp);
1135}
1136
1137void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
1138 VisitCondition(comp);
1139}
1140
1141void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
1142 VisitCondition(comp);
1143}
1144
1145void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
1146 VisitCondition(comp);
1147}
1148
1149void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
1150 VisitCondition(comp);
1151}
1152
1153void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1154 VisitCondition(comp);
1155}
1156
1157void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1158 VisitCondition(comp);
1159}
1160
1161void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
1162 VisitCondition(comp);
1163}
1164
1165void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
1166 VisitCondition(comp);
1167}
1168
1169void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1170 VisitCondition(comp);
1171}
1172
1173void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1174 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001175}
1176
1177void LocationsBuilderARM::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001178 local->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001179}
1180
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001181void InstructionCodeGeneratorARM::VisitLocal(HLocal* local) {
1182 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001183}
1184
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001185void LocationsBuilderARM::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001186 load->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001187}
1188
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001189void InstructionCodeGeneratorARM::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001190 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001191 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001192}
1193
1194void LocationsBuilderARM::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001195 LocationSummary* locations =
1196 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001197 switch (store->InputAt(1)->GetType()) {
1198 case Primitive::kPrimBoolean:
1199 case Primitive::kPrimByte:
1200 case Primitive::kPrimChar:
1201 case Primitive::kPrimShort:
1202 case Primitive::kPrimInt:
1203 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001204 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001205 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1206 break;
1207
1208 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001209 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001210 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1211 break;
1212
1213 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001214 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001215 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001216}
1217
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001218void InstructionCodeGeneratorARM::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001219 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001220}
1221
1222void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001223 LocationSummary* locations =
1224 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001225 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001226}
1227
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001228void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001229 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001230 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001231}
1232
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001233void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
1234 LocationSummary* locations =
1235 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1236 locations->SetOut(Location::ConstantLocation(constant));
1237}
1238
1239void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant) {
1240 // Will be generated at use site.
1241 UNUSED(constant);
1242}
1243
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001244void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001245 LocationSummary* locations =
1246 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001247 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001248}
1249
1250void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant) {
1251 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001252 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001253}
1254
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001255void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
1256 LocationSummary* locations =
1257 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1258 locations->SetOut(Location::ConstantLocation(constant));
1259}
1260
1261void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant) {
1262 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001263 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001264}
1265
1266void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
1267 LocationSummary* locations =
1268 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1269 locations->SetOut(Location::ConstantLocation(constant));
1270}
1271
1272void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant) {
1273 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001274 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001275}
1276
Calin Juravle27df7582015-04-17 19:12:31 +01001277void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1278 memory_barrier->SetLocations(nullptr);
1279}
1280
1281void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1282 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1283}
1284
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001285void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001286 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001287}
1288
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001289void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001290 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001291 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001292}
1293
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001294void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001295 LocationSummary* locations =
1296 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001297 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001298}
1299
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001300void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001301 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001302 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001303}
1304
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001305void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001306 // When we do not run baseline, explicit clinit checks triggered by static
1307 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1308 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001309
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001310 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
1311 codegen_->GetInstructionSetFeatures());
1312 if (intrinsic.TryDispatch(invoke)) {
1313 return;
1314 }
1315
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001316 HandleInvoke(invoke);
1317}
1318
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001319static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
1320 if (invoke->GetLocations()->Intrinsified()) {
1321 IntrinsicCodeGeneratorARM intrinsic(codegen);
1322 intrinsic.Dispatch(invoke);
1323 return true;
1324 }
1325 return false;
1326}
1327
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001328void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001329 // When we do not run baseline, explicit clinit checks triggered by static
1330 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1331 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001332
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001333 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1334 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001335 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001336
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001337 LocationSummary* locations = invoke->GetLocations();
1338 codegen_->GenerateStaticOrDirectCall(
1339 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001340 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001341}
1342
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001343void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001344 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001345 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001346}
1347
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001348void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001349 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
1350 codegen_->GetInstructionSetFeatures());
1351 if (intrinsic.TryDispatch(invoke)) {
1352 return;
1353 }
1354
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001355 HandleInvoke(invoke);
1356}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001357
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001358void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001359 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1360 return;
1361 }
1362
Roland Levillain271ab9c2014-11-27 15:23:57 +00001363 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001364 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
1365 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001366 LocationSummary* locations = invoke->GetLocations();
1367 Location receiver = locations->InAt(0);
1368 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1369 // temp = object->GetClass();
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001370 DCHECK(receiver.IsRegister());
1371 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00001372 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001373 // temp = temp->GetMethodAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001374 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001375 kArmWordSize).Int32Value();
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001376 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001377 // LR = temp->GetEntryPoint();
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001378 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001379 // LR();
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001380 __ blx(LR);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001381 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001382 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001383}
1384
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001385void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1386 HandleInvoke(invoke);
1387 // Add the hidden argument.
1388 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
1389}
1390
1391void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1392 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001393 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001394 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1395 invoke->GetImtIndex() % mirror::Class::kImtSize, kArmPointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001396 LocationSummary* locations = invoke->GetLocations();
1397 Location receiver = locations->InAt(0);
1398 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1399
1400 // Set the hidden argument.
Roland Levillain199f3362014-11-27 17:15:16 +00001401 __ LoadImmediate(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
1402 invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001403
1404 // temp = object->GetClass();
1405 if (receiver.IsStackSlot()) {
1406 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
1407 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
1408 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001409 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001410 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001411 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001412 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001413 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001414 kArmWordSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001415 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
1416 // LR = temp->GetEntryPoint();
1417 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
1418 // LR();
1419 __ blx(LR);
1420 DCHECK(!codegen_->IsLeafMethod());
1421 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1422}
1423
Roland Levillain88cb1752014-10-20 16:36:47 +01001424void LocationsBuilderARM::VisitNeg(HNeg* neg) {
1425 LocationSummary* locations =
1426 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1427 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001428 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01001429 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001430 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1431 break;
1432 }
1433 case Primitive::kPrimLong: {
1434 locations->SetInAt(0, Location::RequiresRegister());
1435 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01001436 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001437 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001438
Roland Levillain88cb1752014-10-20 16:36:47 +01001439 case Primitive::kPrimFloat:
1440 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001441 locations->SetInAt(0, Location::RequiresFpuRegister());
1442 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01001443 break;
1444
1445 default:
1446 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1447 }
1448}
1449
1450void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
1451 LocationSummary* locations = neg->GetLocations();
1452 Location out = locations->Out();
1453 Location in = locations->InAt(0);
1454 switch (neg->GetResultType()) {
1455 case Primitive::kPrimInt:
1456 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001457 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01001458 break;
1459
1460 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001461 DCHECK(in.IsRegisterPair());
1462 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
1463 __ rsbs(out.AsRegisterPairLow<Register>(),
1464 in.AsRegisterPairLow<Register>(),
1465 ShifterOperand(0));
1466 // We cannot emit an RSC (Reverse Subtract with Carry)
1467 // instruction here, as it does not exist in the Thumb-2
1468 // instruction set. We use the following approach
1469 // using SBC and SUB instead.
1470 //
1471 // out.hi = -C
1472 __ sbc(out.AsRegisterPairHigh<Register>(),
1473 out.AsRegisterPairHigh<Register>(),
1474 ShifterOperand(out.AsRegisterPairHigh<Register>()));
1475 // out.hi = out.hi - in.hi
1476 __ sub(out.AsRegisterPairHigh<Register>(),
1477 out.AsRegisterPairHigh<Register>(),
1478 ShifterOperand(in.AsRegisterPairHigh<Register>()));
1479 break;
1480
Roland Levillain88cb1752014-10-20 16:36:47 +01001481 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001482 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001483 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001484 break;
1485
Roland Levillain88cb1752014-10-20 16:36:47 +01001486 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001487 DCHECK(in.IsFpuRegisterPair());
1488 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
1489 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01001490 break;
1491
1492 default:
1493 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1494 }
1495}
1496
Roland Levillaindff1f282014-11-05 14:15:05 +00001497void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001498 Primitive::Type result_type = conversion->GetResultType();
1499 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001500 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001501
Roland Levillain5b3ee562015-04-14 16:02:41 +01001502 // The float-to-long, double-to-long and long-to-float type conversions
1503 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001504 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01001505 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1506 && result_type == Primitive::kPrimLong)
1507 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Roland Levillain624279f2014-12-04 11:54:28 +00001508 ? LocationSummary::kCall
1509 : LocationSummary::kNoCall;
1510 LocationSummary* locations =
1511 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1512
David Brazdilb2bd1c52015-03-25 11:17:37 +00001513 // The Java language does not allow treating boolean as an integral type but
1514 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001515
Roland Levillaindff1f282014-11-05 14:15:05 +00001516 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001517 case Primitive::kPrimByte:
1518 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001519 case Primitive::kPrimBoolean:
1520 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001521 case Primitive::kPrimShort:
1522 case Primitive::kPrimInt:
1523 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001524 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001525 locations->SetInAt(0, Location::RequiresRegister());
1526 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1527 break;
1528
1529 default:
1530 LOG(FATAL) << "Unexpected type conversion from " << input_type
1531 << " to " << result_type;
1532 }
1533 break;
1534
Roland Levillain01a8d712014-11-14 16:27:39 +00001535 case Primitive::kPrimShort:
1536 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001537 case Primitive::kPrimBoolean:
1538 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001539 case Primitive::kPrimByte:
1540 case Primitive::kPrimInt:
1541 case Primitive::kPrimChar:
1542 // Processing a Dex `int-to-short' instruction.
1543 locations->SetInAt(0, Location::RequiresRegister());
1544 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1545 break;
1546
1547 default:
1548 LOG(FATAL) << "Unexpected type conversion from " << input_type
1549 << " to " << result_type;
1550 }
1551 break;
1552
Roland Levillain946e1432014-11-11 17:35:19 +00001553 case Primitive::kPrimInt:
1554 switch (input_type) {
1555 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001556 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001557 locations->SetInAt(0, Location::Any());
1558 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1559 break;
1560
1561 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001562 // Processing a Dex `float-to-int' instruction.
1563 locations->SetInAt(0, Location::RequiresFpuRegister());
1564 locations->SetOut(Location::RequiresRegister());
1565 locations->AddTemp(Location::RequiresFpuRegister());
1566 break;
1567
Roland Levillain946e1432014-11-11 17:35:19 +00001568 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001569 // Processing a Dex `double-to-int' instruction.
1570 locations->SetInAt(0, Location::RequiresFpuRegister());
1571 locations->SetOut(Location::RequiresRegister());
1572 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001573 break;
1574
1575 default:
1576 LOG(FATAL) << "Unexpected type conversion from " << input_type
1577 << " to " << result_type;
1578 }
1579 break;
1580
Roland Levillaindff1f282014-11-05 14:15:05 +00001581 case Primitive::kPrimLong:
1582 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001583 case Primitive::kPrimBoolean:
1584 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001585 case Primitive::kPrimByte:
1586 case Primitive::kPrimShort:
1587 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001588 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001589 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001590 locations->SetInAt(0, Location::RequiresRegister());
1591 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1592 break;
1593
Roland Levillain624279f2014-12-04 11:54:28 +00001594 case Primitive::kPrimFloat: {
1595 // Processing a Dex `float-to-long' instruction.
1596 InvokeRuntimeCallingConvention calling_convention;
1597 locations->SetInAt(0, Location::FpuRegisterLocation(
1598 calling_convention.GetFpuRegisterAt(0)));
1599 locations->SetOut(Location::RegisterPairLocation(R0, R1));
1600 break;
1601 }
1602
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001603 case Primitive::kPrimDouble: {
1604 // Processing a Dex `double-to-long' instruction.
1605 InvokeRuntimeCallingConvention calling_convention;
1606 locations->SetInAt(0, Location::FpuRegisterPairLocation(
1607 calling_convention.GetFpuRegisterAt(0),
1608 calling_convention.GetFpuRegisterAt(1)));
1609 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00001610 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001611 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001612
1613 default:
1614 LOG(FATAL) << "Unexpected type conversion from " << input_type
1615 << " to " << result_type;
1616 }
1617 break;
1618
Roland Levillain981e4542014-11-14 11:47:14 +00001619 case Primitive::kPrimChar:
1620 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001621 case Primitive::kPrimBoolean:
1622 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001623 case Primitive::kPrimByte:
1624 case Primitive::kPrimShort:
1625 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001626 // Processing a Dex `int-to-char' instruction.
1627 locations->SetInAt(0, Location::RequiresRegister());
1628 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1629 break;
1630
1631 default:
1632 LOG(FATAL) << "Unexpected type conversion from " << input_type
1633 << " to " << result_type;
1634 }
1635 break;
1636
Roland Levillaindff1f282014-11-05 14:15:05 +00001637 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001638 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001639 case Primitive::kPrimBoolean:
1640 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001641 case Primitive::kPrimByte:
1642 case Primitive::kPrimShort:
1643 case Primitive::kPrimInt:
1644 case Primitive::kPrimChar:
1645 // Processing a Dex `int-to-float' instruction.
1646 locations->SetInAt(0, Location::RequiresRegister());
1647 locations->SetOut(Location::RequiresFpuRegister());
1648 break;
1649
Roland Levillain5b3ee562015-04-14 16:02:41 +01001650 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00001651 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01001652 InvokeRuntimeCallingConvention calling_convention;
1653 locations->SetInAt(0, Location::RegisterPairLocation(
1654 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
1655 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00001656 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01001657 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00001658
Roland Levillaincff13742014-11-17 14:32:17 +00001659 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001660 // Processing a Dex `double-to-float' instruction.
1661 locations->SetInAt(0, Location::RequiresFpuRegister());
1662 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001663 break;
1664
1665 default:
1666 LOG(FATAL) << "Unexpected type conversion from " << input_type
1667 << " to " << result_type;
1668 };
1669 break;
1670
Roland Levillaindff1f282014-11-05 14:15:05 +00001671 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001672 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001673 case Primitive::kPrimBoolean:
1674 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001675 case Primitive::kPrimByte:
1676 case Primitive::kPrimShort:
1677 case Primitive::kPrimInt:
1678 case Primitive::kPrimChar:
1679 // Processing a Dex `int-to-double' instruction.
1680 locations->SetInAt(0, Location::RequiresRegister());
1681 locations->SetOut(Location::RequiresFpuRegister());
1682 break;
1683
1684 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001685 // Processing a Dex `long-to-double' instruction.
1686 locations->SetInAt(0, Location::RequiresRegister());
1687 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01001688 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00001689 locations->AddTemp(Location::RequiresFpuRegister());
1690 break;
1691
Roland Levillaincff13742014-11-17 14:32:17 +00001692 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001693 // Processing a Dex `float-to-double' instruction.
1694 locations->SetInAt(0, Location::RequiresFpuRegister());
1695 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001696 break;
1697
1698 default:
1699 LOG(FATAL) << "Unexpected type conversion from " << input_type
1700 << " to " << result_type;
1701 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001702 break;
1703
1704 default:
1705 LOG(FATAL) << "Unexpected type conversion from " << input_type
1706 << " to " << result_type;
1707 }
1708}
1709
1710void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
1711 LocationSummary* locations = conversion->GetLocations();
1712 Location out = locations->Out();
1713 Location in = locations->InAt(0);
1714 Primitive::Type result_type = conversion->GetResultType();
1715 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001716 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001717 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001718 case Primitive::kPrimByte:
1719 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001720 case Primitive::kPrimBoolean:
1721 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001722 case Primitive::kPrimShort:
1723 case Primitive::kPrimInt:
1724 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001725 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001726 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001727 break;
1728
1729 default:
1730 LOG(FATAL) << "Unexpected type conversion from " << input_type
1731 << " to " << result_type;
1732 }
1733 break;
1734
Roland Levillain01a8d712014-11-14 16:27:39 +00001735 case Primitive::kPrimShort:
1736 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001737 case Primitive::kPrimBoolean:
1738 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001739 case Primitive::kPrimByte:
1740 case Primitive::kPrimInt:
1741 case Primitive::kPrimChar:
1742 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001743 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00001744 break;
1745
1746 default:
1747 LOG(FATAL) << "Unexpected type conversion from " << input_type
1748 << " to " << result_type;
1749 }
1750 break;
1751
Roland Levillain946e1432014-11-11 17:35:19 +00001752 case Primitive::kPrimInt:
1753 switch (input_type) {
1754 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001755 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001756 DCHECK(out.IsRegister());
1757 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001758 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001759 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001760 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00001761 } else {
1762 DCHECK(in.IsConstant());
1763 DCHECK(in.GetConstant()->IsLongConstant());
1764 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001765 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00001766 }
1767 break;
1768
Roland Levillain3f8f9362014-12-02 17:45:01 +00001769 case Primitive::kPrimFloat: {
1770 // Processing a Dex `float-to-int' instruction.
1771 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
1772 __ vmovs(temp, in.AsFpuRegister<SRegister>());
1773 __ vcvtis(temp, temp);
1774 __ vmovrs(out.AsRegister<Register>(), temp);
1775 break;
1776 }
1777
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001778 case Primitive::kPrimDouble: {
1779 // Processing a Dex `double-to-int' instruction.
1780 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
1781 DRegister temp_d = FromLowSToD(temp_s);
1782 __ vmovd(temp_d, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
1783 __ vcvtid(temp_s, temp_d);
1784 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00001785 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001786 }
Roland Levillain946e1432014-11-11 17:35:19 +00001787
1788 default:
1789 LOG(FATAL) << "Unexpected type conversion from " << input_type
1790 << " to " << result_type;
1791 }
1792 break;
1793
Roland Levillaindff1f282014-11-05 14:15:05 +00001794 case Primitive::kPrimLong:
1795 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001796 case Primitive::kPrimBoolean:
1797 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001798 case Primitive::kPrimByte:
1799 case Primitive::kPrimShort:
1800 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001801 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001802 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001803 DCHECK(out.IsRegisterPair());
1804 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001805 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00001806 // Sign extension.
1807 __ Asr(out.AsRegisterPairHigh<Register>(),
1808 out.AsRegisterPairLow<Register>(),
1809 31);
1810 break;
1811
1812 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001813 // Processing a Dex `float-to-long' instruction.
Roland Levillain624279f2014-12-04 11:54:28 +00001814 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
1815 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001816 conversion->GetDexPc(),
1817 nullptr);
Roland Levillain624279f2014-12-04 11:54:28 +00001818 break;
1819
Roland Levillaindff1f282014-11-05 14:15:05 +00001820 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001821 // Processing a Dex `double-to-long' instruction.
1822 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
1823 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001824 conversion->GetDexPc(),
1825 nullptr);
Roland Levillaindff1f282014-11-05 14:15:05 +00001826 break;
1827
1828 default:
1829 LOG(FATAL) << "Unexpected type conversion from " << input_type
1830 << " to " << result_type;
1831 }
1832 break;
1833
Roland Levillain981e4542014-11-14 11:47:14 +00001834 case Primitive::kPrimChar:
1835 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001836 case Primitive::kPrimBoolean:
1837 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001838 case Primitive::kPrimByte:
1839 case Primitive::kPrimShort:
1840 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001841 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001842 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00001843 break;
1844
1845 default:
1846 LOG(FATAL) << "Unexpected type conversion from " << input_type
1847 << " to " << result_type;
1848 }
1849 break;
1850
Roland Levillaindff1f282014-11-05 14:15:05 +00001851 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001852 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001853 case Primitive::kPrimBoolean:
1854 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001855 case Primitive::kPrimByte:
1856 case Primitive::kPrimShort:
1857 case Primitive::kPrimInt:
1858 case Primitive::kPrimChar: {
1859 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001860 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
1861 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001862 break;
1863 }
1864
Roland Levillain5b3ee562015-04-14 16:02:41 +01001865 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001866 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01001867 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pL2f),
1868 conversion,
1869 conversion->GetDexPc(),
1870 nullptr);
Roland Levillain6d0e4832014-11-27 18:31:21 +00001871 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00001872
Roland Levillaincff13742014-11-17 14:32:17 +00001873 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001874 // Processing a Dex `double-to-float' instruction.
1875 __ vcvtsd(out.AsFpuRegister<SRegister>(),
1876 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00001877 break;
1878
1879 default:
1880 LOG(FATAL) << "Unexpected type conversion from " << input_type
1881 << " to " << result_type;
1882 };
1883 break;
1884
Roland Levillaindff1f282014-11-05 14:15:05 +00001885 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001886 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001887 case Primitive::kPrimBoolean:
1888 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001889 case Primitive::kPrimByte:
1890 case Primitive::kPrimShort:
1891 case Primitive::kPrimInt:
1892 case Primitive::kPrimChar: {
1893 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001894 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001895 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
1896 out.AsFpuRegisterPairLow<SRegister>());
1897 break;
1898 }
1899
Roland Levillain647b9ed2014-11-27 12:06:00 +00001900 case Primitive::kPrimLong: {
1901 // Processing a Dex `long-to-double' instruction.
1902 Register low = in.AsRegisterPairLow<Register>();
1903 Register high = in.AsRegisterPairHigh<Register>();
1904 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
1905 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01001906 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00001907 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01001908 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
1909 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001910
Roland Levillain682393c2015-04-14 15:57:52 +01001911 // temp_d = int-to-double(high)
1912 __ vmovsr(temp_s, high);
1913 __ vcvtdi(temp_d, temp_s);
1914 // constant_d = k2Pow32EncodingForDouble
1915 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
1916 // out_d = unsigned-to-double(low)
1917 __ vmovsr(out_s, low);
1918 __ vcvtdu(out_d, out_s);
1919 // out_d += temp_d * constant_d
1920 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001921 break;
1922 }
1923
Roland Levillaincff13742014-11-17 14:32:17 +00001924 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001925 // Processing a Dex `float-to-double' instruction.
1926 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
1927 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001928 break;
1929
1930 default:
1931 LOG(FATAL) << "Unexpected type conversion from " << input_type
1932 << " to " << result_type;
1933 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001934 break;
1935
1936 default:
1937 LOG(FATAL) << "Unexpected type conversion from " << input_type
1938 << " to " << result_type;
1939 }
1940}
1941
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001942void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001943 LocationSummary* locations =
1944 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001945 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001946 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001947 locations->SetInAt(0, Location::RequiresRegister());
1948 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001949 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1950 break;
1951 }
1952
1953 case Primitive::kPrimLong: {
1954 locations->SetInAt(0, Location::RequiresRegister());
1955 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001956 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001957 break;
1958 }
1959
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001960 case Primitive::kPrimFloat:
1961 case Primitive::kPrimDouble: {
1962 locations->SetInAt(0, Location::RequiresFpuRegister());
1963 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00001964 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001965 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001966 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001967
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001968 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001969 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001970 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001971}
1972
1973void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
1974 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001975 Location out = locations->Out();
1976 Location first = locations->InAt(0);
1977 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001978 switch (add->GetResultType()) {
1979 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001980 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00001981 __ add(out.AsRegister<Register>(),
1982 first.AsRegister<Register>(),
1983 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001984 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001985 __ AddConstant(out.AsRegister<Register>(),
1986 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001987 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001988 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001989 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001990
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001991 case Primitive::kPrimLong: {
1992 DCHECK(second.IsRegisterPair());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001993 __ adds(out.AsRegisterPairLow<Register>(),
1994 first.AsRegisterPairLow<Register>(),
1995 ShifterOperand(second.AsRegisterPairLow<Register>()));
1996 __ adc(out.AsRegisterPairHigh<Register>(),
1997 first.AsRegisterPairHigh<Register>(),
1998 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001999 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002000 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002001
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002002 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00002003 __ vadds(out.AsFpuRegister<SRegister>(),
2004 first.AsFpuRegister<SRegister>(),
2005 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002006 break;
2007
2008 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002009 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2010 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2011 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002012 break;
2013
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002014 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002015 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002016 }
2017}
2018
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002019void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002020 LocationSummary* locations =
2021 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002022 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002023 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002024 locations->SetInAt(0, Location::RequiresRegister());
2025 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002026 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2027 break;
2028 }
2029
2030 case Primitive::kPrimLong: {
2031 locations->SetInAt(0, Location::RequiresRegister());
2032 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002033 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002034 break;
2035 }
Calin Juravle11351682014-10-23 15:38:15 +01002036 case Primitive::kPrimFloat:
2037 case Primitive::kPrimDouble: {
2038 locations->SetInAt(0, Location::RequiresFpuRegister());
2039 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002040 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002041 break;
Calin Juravle11351682014-10-23 15:38:15 +01002042 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002043 default:
Calin Juravle11351682014-10-23 15:38:15 +01002044 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002045 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002046}
2047
2048void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
2049 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002050 Location out = locations->Out();
2051 Location first = locations->InAt(0);
2052 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002053 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002054 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002055 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002056 __ sub(out.AsRegister<Register>(),
2057 first.AsRegister<Register>(),
2058 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002059 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002060 __ AddConstant(out.AsRegister<Register>(),
2061 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01002062 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002063 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002064 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002065 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002066
Calin Juravle11351682014-10-23 15:38:15 +01002067 case Primitive::kPrimLong: {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002068 DCHECK(second.IsRegisterPair());
Calin Juravle11351682014-10-23 15:38:15 +01002069 __ subs(out.AsRegisterPairLow<Register>(),
2070 first.AsRegisterPairLow<Register>(),
2071 ShifterOperand(second.AsRegisterPairLow<Register>()));
2072 __ sbc(out.AsRegisterPairHigh<Register>(),
2073 first.AsRegisterPairHigh<Register>(),
2074 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002075 break;
Calin Juravle11351682014-10-23 15:38:15 +01002076 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002077
Calin Juravle11351682014-10-23 15:38:15 +01002078 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002079 __ vsubs(out.AsFpuRegister<SRegister>(),
2080 first.AsFpuRegister<SRegister>(),
2081 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002082 break;
Calin Juravle11351682014-10-23 15:38:15 +01002083 }
2084
2085 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002086 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2087 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2088 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01002089 break;
2090 }
2091
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002092
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002093 default:
Calin Juravle11351682014-10-23 15:38:15 +01002094 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002095 }
2096}
2097
Calin Juravle34bacdf2014-10-07 20:23:36 +01002098void LocationsBuilderARM::VisitMul(HMul* mul) {
2099 LocationSummary* locations =
2100 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2101 switch (mul->GetResultType()) {
2102 case Primitive::kPrimInt:
2103 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002104 locations->SetInAt(0, Location::RequiresRegister());
2105 locations->SetInAt(1, Location::RequiresRegister());
2106 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002107 break;
2108 }
2109
Calin Juravleb5bfa962014-10-21 18:02:24 +01002110 case Primitive::kPrimFloat:
2111 case Primitive::kPrimDouble: {
2112 locations->SetInAt(0, Location::RequiresFpuRegister());
2113 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002114 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002115 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002116 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002117
2118 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002119 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002120 }
2121}
2122
2123void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
2124 LocationSummary* locations = mul->GetLocations();
2125 Location out = locations->Out();
2126 Location first = locations->InAt(0);
2127 Location second = locations->InAt(1);
2128 switch (mul->GetResultType()) {
2129 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00002130 __ mul(out.AsRegister<Register>(),
2131 first.AsRegister<Register>(),
2132 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002133 break;
2134 }
2135 case Primitive::kPrimLong: {
2136 Register out_hi = out.AsRegisterPairHigh<Register>();
2137 Register out_lo = out.AsRegisterPairLow<Register>();
2138 Register in1_hi = first.AsRegisterPairHigh<Register>();
2139 Register in1_lo = first.AsRegisterPairLow<Register>();
2140 Register in2_hi = second.AsRegisterPairHigh<Register>();
2141 Register in2_lo = second.AsRegisterPairLow<Register>();
2142
2143 // Extra checks to protect caused by the existence of R1_R2.
2144 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
2145 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
2146 DCHECK_NE(out_hi, in1_lo);
2147 DCHECK_NE(out_hi, in2_lo);
2148
2149 // input: in1 - 64 bits, in2 - 64 bits
2150 // output: out
2151 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2152 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2153 // parts: out.lo = (in1.lo * in2.lo)[31:0]
2154
2155 // IP <- in1.lo * in2.hi
2156 __ mul(IP, in1_lo, in2_hi);
2157 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2158 __ mla(out_hi, in1_hi, in2_lo, IP);
2159 // out.lo <- (in1.lo * in2.lo)[31:0];
2160 __ umull(out_lo, IP, in1_lo, in2_lo);
2161 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2162 __ add(out_hi, out_hi, ShifterOperand(IP));
2163 break;
2164 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002165
2166 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002167 __ vmuls(out.AsFpuRegister<SRegister>(),
2168 first.AsFpuRegister<SRegister>(),
2169 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002170 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002171 }
2172
2173 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002174 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2175 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2176 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01002177 break;
2178 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002179
2180 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002181 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002182 }
2183}
2184
Zheng Xuc6667102015-05-15 16:08:45 +08002185void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2186 DCHECK(instruction->IsDiv() || instruction->IsRem());
2187 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2188
2189 LocationSummary* locations = instruction->GetLocations();
2190 Location second = locations->InAt(1);
2191 DCHECK(second.IsConstant());
2192
2193 Register out = locations->Out().AsRegister<Register>();
2194 Register dividend = locations->InAt(0).AsRegister<Register>();
2195 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2196 DCHECK(imm == 1 || imm == -1);
2197
2198 if (instruction->IsRem()) {
2199 __ LoadImmediate(out, 0);
2200 } else {
2201 if (imm == 1) {
2202 __ Mov(out, dividend);
2203 } else {
2204 __ rsb(out, dividend, ShifterOperand(0));
2205 }
2206 }
2207}
2208
2209void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2210 DCHECK(instruction->IsDiv() || instruction->IsRem());
2211 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2212
2213 LocationSummary* locations = instruction->GetLocations();
2214 Location second = locations->InAt(1);
2215 DCHECK(second.IsConstant());
2216
2217 Register out = locations->Out().AsRegister<Register>();
2218 Register dividend = locations->InAt(0).AsRegister<Register>();
2219 Register temp = locations->GetTemp(0).AsRegister<Register>();
2220 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Vladimir Marko80afd022015-05-19 18:08:00 +01002221 uint32_t abs_imm = static_cast<uint32_t>(std::abs(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002222 DCHECK(IsPowerOfTwo(abs_imm));
2223 int ctz_imm = CTZ(abs_imm);
2224
2225 if (ctz_imm == 1) {
2226 __ Lsr(temp, dividend, 32 - ctz_imm);
2227 } else {
2228 __ Asr(temp, dividend, 31);
2229 __ Lsr(temp, temp, 32 - ctz_imm);
2230 }
2231 __ add(out, temp, ShifterOperand(dividend));
2232
2233 if (instruction->IsDiv()) {
2234 __ Asr(out, out, ctz_imm);
2235 if (imm < 0) {
2236 __ rsb(out, out, ShifterOperand(0));
2237 }
2238 } else {
2239 __ ubfx(out, out, 0, ctz_imm);
2240 __ sub(out, out, ShifterOperand(temp));
2241 }
2242}
2243
2244void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2245 DCHECK(instruction->IsDiv() || instruction->IsRem());
2246 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2247
2248 LocationSummary* locations = instruction->GetLocations();
2249 Location second = locations->InAt(1);
2250 DCHECK(second.IsConstant());
2251
2252 Register out = locations->Out().AsRegister<Register>();
2253 Register dividend = locations->InAt(0).AsRegister<Register>();
2254 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2255 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
2256 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2257
2258 int64_t magic;
2259 int shift;
2260 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2261
2262 __ LoadImmediate(temp1, magic);
2263 __ smull(temp2, temp1, dividend, temp1);
2264
2265 if (imm > 0 && magic < 0) {
2266 __ add(temp1, temp1, ShifterOperand(dividend));
2267 } else if (imm < 0 && magic > 0) {
2268 __ sub(temp1, temp1, ShifterOperand(dividend));
2269 }
2270
2271 if (shift != 0) {
2272 __ Asr(temp1, temp1, shift);
2273 }
2274
2275 if (instruction->IsDiv()) {
2276 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
2277 } else {
2278 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
2279 // TODO: Strength reduction for mls.
2280 __ LoadImmediate(temp2, imm);
2281 __ mls(out, temp1, temp2, dividend);
2282 }
2283}
2284
2285void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
2286 DCHECK(instruction->IsDiv() || instruction->IsRem());
2287 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2288
2289 LocationSummary* locations = instruction->GetLocations();
2290 Location second = locations->InAt(1);
2291 DCHECK(second.IsConstant());
2292
2293 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2294 if (imm == 0) {
2295 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2296 } else if (imm == 1 || imm == -1) {
2297 DivRemOneOrMinusOne(instruction);
2298 } else if (IsPowerOfTwo(std::abs(imm))) {
2299 DivRemByPowerOfTwo(instruction);
2300 } else {
2301 DCHECK(imm <= -2 || imm >= 2);
2302 GenerateDivRemWithAnyConstant(instruction);
2303 }
2304}
2305
Calin Juravle7c4954d2014-10-28 16:57:40 +00002306void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002307 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2308 if (div->GetResultType() == Primitive::kPrimLong) {
2309 // pLdiv runtime call.
2310 call_kind = LocationSummary::kCall;
Zheng Xuc6667102015-05-15 16:08:45 +08002311 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
2312 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002313 } else if (div->GetResultType() == Primitive::kPrimInt &&
2314 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
2315 // pIdivmod runtime call.
2316 call_kind = LocationSummary::kCall;
2317 }
2318
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002319 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2320
Calin Juravle7c4954d2014-10-28 16:57:40 +00002321 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002322 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002323 if (div->InputAt(1)->IsConstant()) {
2324 locations->SetInAt(0, Location::RequiresRegister());
2325 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
2326 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2327 int32_t abs_imm = std::abs(div->InputAt(1)->AsIntConstant()->GetValue());
2328 if (abs_imm <= 1) {
2329 // No temp register required.
2330 } else {
2331 locations->AddTemp(Location::RequiresRegister());
2332 if (!IsPowerOfTwo(abs_imm)) {
2333 locations->AddTemp(Location::RequiresRegister());
2334 }
2335 }
2336 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002337 locations->SetInAt(0, Location::RequiresRegister());
2338 locations->SetInAt(1, Location::RequiresRegister());
2339 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2340 } else {
2341 InvokeRuntimeCallingConvention calling_convention;
2342 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2343 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2344 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
2345 // we only need the former.
2346 locations->SetOut(Location::RegisterLocation(R0));
2347 }
Calin Juravled0d48522014-11-04 16:40:20 +00002348 break;
2349 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002350 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002351 InvokeRuntimeCallingConvention calling_convention;
2352 locations->SetInAt(0, Location::RegisterPairLocation(
2353 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2354 locations->SetInAt(1, Location::RegisterPairLocation(
2355 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002356 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002357 break;
2358 }
2359 case Primitive::kPrimFloat:
2360 case Primitive::kPrimDouble: {
2361 locations->SetInAt(0, Location::RequiresFpuRegister());
2362 locations->SetInAt(1, Location::RequiresFpuRegister());
2363 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2364 break;
2365 }
2366
2367 default:
2368 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2369 }
2370}
2371
2372void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
2373 LocationSummary* locations = div->GetLocations();
2374 Location out = locations->Out();
2375 Location first = locations->InAt(0);
2376 Location second = locations->InAt(1);
2377
2378 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002379 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002380 if (second.IsConstant()) {
2381 GenerateDivRemConstantIntegral(div);
2382 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002383 __ sdiv(out.AsRegister<Register>(),
2384 first.AsRegister<Register>(),
2385 second.AsRegister<Register>());
2386 } else {
2387 InvokeRuntimeCallingConvention calling_convention;
2388 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
2389 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
2390 DCHECK_EQ(R0, out.AsRegister<Register>());
2391
2392 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), div, div->GetDexPc(), nullptr);
2393 }
Calin Juravled0d48522014-11-04 16:40:20 +00002394 break;
2395 }
2396
Calin Juravle7c4954d2014-10-28 16:57:40 +00002397 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002398 InvokeRuntimeCallingConvention calling_convention;
2399 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2400 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2401 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2402 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2403 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002404 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002405
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002406 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv), div, div->GetDexPc(), nullptr);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002407 break;
2408 }
2409
2410 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002411 __ vdivs(out.AsFpuRegister<SRegister>(),
2412 first.AsFpuRegister<SRegister>(),
2413 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002414 break;
2415 }
2416
2417 case Primitive::kPrimDouble: {
2418 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2419 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2420 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
2421 break;
2422 }
2423
2424 default:
2425 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2426 }
2427}
2428
Calin Juravlebacfec32014-11-14 15:54:36 +00002429void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002430 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002431
2432 // Most remainders are implemented in the runtime.
2433 LocationSummary::CallKind call_kind = LocationSummary::kCall;
Zheng Xuc6667102015-05-15 16:08:45 +08002434 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
2435 // sdiv will be replaced by other instruction sequence.
2436 call_kind = LocationSummary::kNoCall;
2437 } else if ((rem->GetResultType() == Primitive::kPrimInt)
2438 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002439 // Have hardware divide instruction for int, do it with three instructions.
2440 call_kind = LocationSummary::kNoCall;
2441 }
2442
Calin Juravlebacfec32014-11-14 15:54:36 +00002443 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2444
Calin Juravled2ec87d2014-12-08 14:24:46 +00002445 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002446 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002447 if (rem->InputAt(1)->IsConstant()) {
2448 locations->SetInAt(0, Location::RequiresRegister());
2449 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
2450 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2451 int32_t abs_imm = std::abs(rem->InputAt(1)->AsIntConstant()->GetValue());
2452 if (abs_imm <= 1) {
2453 // No temp register required.
2454 } else {
2455 locations->AddTemp(Location::RequiresRegister());
2456 if (!IsPowerOfTwo(abs_imm)) {
2457 locations->AddTemp(Location::RequiresRegister());
2458 }
2459 }
2460 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002461 locations->SetInAt(0, Location::RequiresRegister());
2462 locations->SetInAt(1, Location::RequiresRegister());
2463 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2464 locations->AddTemp(Location::RequiresRegister());
2465 } else {
2466 InvokeRuntimeCallingConvention calling_convention;
2467 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2468 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2469 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
2470 // we only need the latter.
2471 locations->SetOut(Location::RegisterLocation(R1));
2472 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002473 break;
2474 }
2475 case Primitive::kPrimLong: {
2476 InvokeRuntimeCallingConvention calling_convention;
2477 locations->SetInAt(0, Location::RegisterPairLocation(
2478 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2479 locations->SetInAt(1, Location::RegisterPairLocation(
2480 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2481 // The runtime helper puts the output in R2,R3.
2482 locations->SetOut(Location::RegisterPairLocation(R2, R3));
2483 break;
2484 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00002485 case Primitive::kPrimFloat: {
2486 InvokeRuntimeCallingConvention calling_convention;
2487 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
2488 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
2489 locations->SetOut(Location::FpuRegisterLocation(S0));
2490 break;
2491 }
2492
Calin Juravlebacfec32014-11-14 15:54:36 +00002493 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002494 InvokeRuntimeCallingConvention calling_convention;
2495 locations->SetInAt(0, Location::FpuRegisterPairLocation(
2496 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
2497 locations->SetInAt(1, Location::FpuRegisterPairLocation(
2498 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
2499 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00002500 break;
2501 }
2502
2503 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002504 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002505 }
2506}
2507
2508void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
2509 LocationSummary* locations = rem->GetLocations();
2510 Location out = locations->Out();
2511 Location first = locations->InAt(0);
2512 Location second = locations->InAt(1);
2513
Calin Juravled2ec87d2014-12-08 14:24:46 +00002514 Primitive::Type type = rem->GetResultType();
2515 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002516 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002517 if (second.IsConstant()) {
2518 GenerateDivRemConstantIntegral(rem);
2519 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002520 Register reg1 = first.AsRegister<Register>();
2521 Register reg2 = second.AsRegister<Register>();
2522 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00002523
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002524 // temp = reg1 / reg2 (integer division)
2525 // temp = temp * reg2
2526 // dest = reg1 - temp
2527 __ sdiv(temp, reg1, reg2);
2528 __ mul(temp, temp, reg2);
2529 __ sub(out.AsRegister<Register>(), reg1, ShifterOperand(temp));
2530 } else {
2531 InvokeRuntimeCallingConvention calling_convention;
2532 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
2533 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
2534 DCHECK_EQ(R1, out.AsRegister<Register>());
2535
2536 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), rem, rem->GetDexPc(), nullptr);
2537 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002538 break;
2539 }
2540
2541 case Primitive::kPrimLong: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002542 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod), rem, rem->GetDexPc(), nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00002543 break;
2544 }
2545
Calin Juravled2ec87d2014-12-08 14:24:46 +00002546 case Primitive::kPrimFloat: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002547 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf), rem, rem->GetDexPc(), nullptr);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002548 break;
2549 }
2550
Calin Juravlebacfec32014-11-14 15:54:36 +00002551 case Primitive::kPrimDouble: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002552 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod), rem, rem->GetDexPc(), nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00002553 break;
2554 }
2555
2556 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002557 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002558 }
2559}
2560
Calin Juravled0d48522014-11-04 16:40:20 +00002561void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2562 LocationSummary* locations =
2563 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002564 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00002565 if (instruction->HasUses()) {
2566 locations->SetOut(Location::SameAsFirstInput());
2567 }
2568}
2569
2570void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2571 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
2572 codegen_->AddSlowPath(slow_path);
2573
2574 LocationSummary* locations = instruction->GetLocations();
2575 Location value = locations->InAt(0);
2576
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002577 switch (instruction->GetType()) {
2578 case Primitive::kPrimInt: {
2579 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002580 __ cmp(value.AsRegister<Register>(), ShifterOperand(0));
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002581 __ b(slow_path->GetEntryLabel(), EQ);
2582 } else {
2583 DCHECK(value.IsConstant()) << value;
2584 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2585 __ b(slow_path->GetEntryLabel());
2586 }
2587 }
2588 break;
2589 }
2590 case Primitive::kPrimLong: {
2591 if (value.IsRegisterPair()) {
2592 __ orrs(IP,
2593 value.AsRegisterPairLow<Register>(),
2594 ShifterOperand(value.AsRegisterPairHigh<Register>()));
2595 __ b(slow_path->GetEntryLabel(), EQ);
2596 } else {
2597 DCHECK(value.IsConstant()) << value;
2598 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2599 __ b(slow_path->GetEntryLabel());
2600 }
2601 }
2602 break;
2603 default:
2604 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2605 }
2606 }
Calin Juravled0d48522014-11-04 16:40:20 +00002607}
2608
Calin Juravle9aec02f2014-11-18 23:06:35 +00002609void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
2610 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2611
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002612 LocationSummary* locations =
2613 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002614
2615 switch (op->GetResultType()) {
2616 case Primitive::kPrimInt: {
2617 locations->SetInAt(0, Location::RequiresRegister());
2618 locations->SetInAt(1, Location::RegisterOrConstant(op->InputAt(1)));
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002619 // Make the output overlap, as it will be used to hold the masked
2620 // second input.
2621 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002622 break;
2623 }
2624 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002625 locations->SetInAt(0, Location::RequiresRegister());
2626 locations->SetInAt(1, Location::RequiresRegister());
2627 locations->AddTemp(Location::RequiresRegister());
2628 locations->SetOut(Location::RequiresRegister());
Calin Juravle9aec02f2014-11-18 23:06:35 +00002629 break;
2630 }
2631 default:
2632 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2633 }
2634}
2635
2636void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
2637 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2638
2639 LocationSummary* locations = op->GetLocations();
2640 Location out = locations->Out();
2641 Location first = locations->InAt(0);
2642 Location second = locations->InAt(1);
2643
2644 Primitive::Type type = op->GetResultType();
2645 switch (type) {
2646 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002647 Register out_reg = out.AsRegister<Register>();
2648 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002649 // Arm doesn't mask the shift count so we need to do it ourselves.
2650 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002651 Register second_reg = second.AsRegister<Register>();
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002652 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftValue));
Calin Juravle9aec02f2014-11-18 23:06:35 +00002653 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002654 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002655 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002656 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002657 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002658 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002659 }
2660 } else {
2661 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
2662 uint32_t shift_value = static_cast<uint32_t>(cst & kMaxIntShiftValue);
2663 if (shift_value == 0) { // arm does not support shifting with 0 immediate.
2664 __ Mov(out_reg, first_reg);
2665 } else if (op->IsShl()) {
2666 __ Lsl(out_reg, first_reg, shift_value);
2667 } else if (op->IsShr()) {
2668 __ Asr(out_reg, first_reg, shift_value);
2669 } else {
2670 __ Lsr(out_reg, first_reg, shift_value);
2671 }
2672 }
2673 break;
2674 }
2675 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002676 Register o_h = out.AsRegisterPairHigh<Register>();
2677 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002678
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002679 Register temp = locations->GetTemp(0).AsRegister<Register>();
2680
2681 Register high = first.AsRegisterPairHigh<Register>();
2682 Register low = first.AsRegisterPairLow<Register>();
2683
2684 Register second_reg = second.AsRegister<Register>();
2685
Calin Juravle9aec02f2014-11-18 23:06:35 +00002686 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002687 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftValue));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002688 // Shift the high part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002689 __ Lsl(o_h, high, o_l);
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002690 // Shift the low part and `or` what overflew on the high part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002691 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002692 __ Lsr(temp, low, temp);
2693 __ orr(o_h, o_h, ShifterOperand(temp));
2694 // If the shift is > 32 bits, override the high part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002695 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002696 __ it(PL);
2697 __ Lsl(o_h, low, temp, false, PL);
2698 // Shift the low part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002699 __ Lsl(o_l, low, o_l);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002700 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002701 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftValue));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002702 // Shift the low part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002703 __ Lsr(o_l, low, o_h);
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002704 // Shift the high part and `or` what underflew on the low part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002705 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002706 __ Lsl(temp, high, temp);
2707 __ orr(o_l, o_l, ShifterOperand(temp));
2708 // If the shift is > 32 bits, override the low part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002709 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002710 __ it(PL);
2711 __ Asr(o_l, high, temp, false, PL);
2712 // Shift the high part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002713 __ Asr(o_h, high, o_h);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002714 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002715 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftValue));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002716 // same as Shr except we use `Lsr`s and not `Asr`s
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002717 __ Lsr(o_l, low, o_h);
2718 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002719 __ Lsl(temp, high, temp);
2720 __ orr(o_l, o_l, ShifterOperand(temp));
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002721 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002722 __ it(PL);
2723 __ Lsr(o_l, high, temp, false, PL);
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002724 __ Lsr(o_h, high, o_h);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002725 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00002726 break;
2727 }
2728 default:
2729 LOG(FATAL) << "Unexpected operation type " << type;
2730 }
2731}
2732
2733void LocationsBuilderARM::VisitShl(HShl* shl) {
2734 HandleShift(shl);
2735}
2736
2737void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
2738 HandleShift(shl);
2739}
2740
2741void LocationsBuilderARM::VisitShr(HShr* shr) {
2742 HandleShift(shr);
2743}
2744
2745void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
2746 HandleShift(shr);
2747}
2748
2749void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
2750 HandleShift(ushr);
2751}
2752
2753void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
2754 HandleShift(ushr);
2755}
2756
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002757void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002758 LocationSummary* locations =
2759 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002760 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002761 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002762 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002763 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002764}
2765
2766void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
2767 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002768 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002769 codegen_->InvokeRuntime(GetThreadOffset<kArmWordSize>(instruction->GetEntrypoint()).Int32Value(),
2770 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002771 instruction->GetDexPc(),
2772 nullptr);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002773}
2774
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002775void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
2776 LocationSummary* locations =
2777 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2778 InvokeRuntimeCallingConvention calling_convention;
2779 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002780 locations->SetOut(Location::RegisterLocation(R0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002781 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002782 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002783}
2784
2785void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
2786 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002787 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002788 codegen_->InvokeRuntime(GetThreadOffset<kArmWordSize>(instruction->GetEntrypoint()).Int32Value(),
2789 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002790 instruction->GetDexPc(),
2791 nullptr);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002792}
2793
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002794void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002795 LocationSummary* locations =
2796 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002797 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2798 if (location.IsStackSlot()) {
2799 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2800 } else if (location.IsDoubleStackSlot()) {
2801 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002802 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002803 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002804}
2805
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002806void InstructionCodeGeneratorARM::VisitParameterValue(
2807 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002808 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002809}
2810
2811void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
2812 LocationSummary* locations =
2813 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2814 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
2815}
2816
2817void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
2818 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002819}
2820
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002821void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002822 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002823 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002824 locations->SetInAt(0, Location::RequiresRegister());
2825 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002826}
2827
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002828void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
2829 LocationSummary* locations = not_->GetLocations();
2830 Location out = locations->Out();
2831 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00002832 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002833 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002834 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002835 break;
2836
2837 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01002838 __ mvn(out.AsRegisterPairLow<Register>(),
2839 ShifterOperand(in.AsRegisterPairLow<Register>()));
2840 __ mvn(out.AsRegisterPairHigh<Register>(),
2841 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002842 break;
2843
2844 default:
2845 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2846 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002847}
2848
David Brazdil66d126e2015-04-03 16:02:44 +01002849void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
2850 LocationSummary* locations =
2851 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
2852 locations->SetInAt(0, Location::RequiresRegister());
2853 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2854}
2855
2856void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01002857 LocationSummary* locations = bool_not->GetLocations();
2858 Location out = locations->Out();
2859 Location in = locations->InAt(0);
2860 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
2861}
2862
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002863void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002864 LocationSummary* locations =
2865 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00002866 switch (compare->InputAt(0)->GetType()) {
2867 case Primitive::kPrimLong: {
2868 locations->SetInAt(0, Location::RequiresRegister());
2869 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002870 // Output overlaps because it is written before doing the low comparison.
2871 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00002872 break;
2873 }
2874 case Primitive::kPrimFloat:
2875 case Primitive::kPrimDouble: {
2876 locations->SetInAt(0, Location::RequiresFpuRegister());
2877 locations->SetInAt(1, Location::RequiresFpuRegister());
2878 locations->SetOut(Location::RequiresRegister());
2879 break;
2880 }
2881 default:
2882 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
2883 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002884}
2885
2886void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002887 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002888 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00002889 Location left = locations->InAt(0);
2890 Location right = locations->InAt(1);
2891
Vladimir Markocf93a5c2015-06-16 11:33:24 +00002892 Label less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00002893 Primitive::Type type = compare->InputAt(0)->GetType();
2894 switch (type) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002895 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002896 __ cmp(left.AsRegisterPairHigh<Register>(),
2897 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002898 __ b(&less, LT);
2899 __ b(&greater, GT);
Calin Juravleddb7df22014-11-25 20:56:51 +00002900 // Do LoadImmediate before any `cmp`, as LoadImmediate might affect the status flags.
2901 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002902 __ cmp(left.AsRegisterPairLow<Register>(),
2903 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Calin Juravleddb7df22014-11-25 20:56:51 +00002904 break;
2905 }
2906 case Primitive::kPrimFloat:
2907 case Primitive::kPrimDouble: {
2908 __ LoadImmediate(out, 0);
2909 if (type == Primitive::kPrimFloat) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002910 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00002911 } else {
2912 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
2913 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
2914 }
2915 __ vmstat(); // transfer FP status register to ARM APSR.
2916 __ b(compare->IsGtBias() ? &greater : &less, VS); // VS for unordered.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002917 break;
2918 }
2919 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00002920 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002921 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002922 __ b(&done, EQ);
2923 __ b(&less, CC); // CC is for both: unsigned compare for longs and 'less than' for floats.
2924
2925 __ Bind(&greater);
2926 __ LoadImmediate(out, 1);
2927 __ b(&done);
2928
2929 __ Bind(&less);
2930 __ LoadImmediate(out, -1);
2931
2932 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002933}
2934
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002935void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002936 LocationSummary* locations =
2937 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01002938 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2939 locations->SetInAt(i, Location::Any());
2940 }
2941 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002942}
2943
2944void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002945 UNUSED(instruction);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002946 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002947}
2948
Calin Juravle52c48962014-12-16 17:02:57 +00002949void InstructionCodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
2950 // TODO (ported from quick): revisit Arm barrier kinds
Kenny Root1d8199d2015-06-02 11:01:10 -07002951 DmbOptions flavor = DmbOptions::ISH; // quiet c++ warnings
Calin Juravle52c48962014-12-16 17:02:57 +00002952 switch (kind) {
2953 case MemBarrierKind::kAnyStore:
2954 case MemBarrierKind::kLoadAny:
2955 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07002956 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00002957 break;
2958 }
2959 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07002960 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00002961 break;
2962 }
2963 default:
2964 LOG(FATAL) << "Unexpected memory barrier " << kind;
2965 }
Kenny Root1d8199d2015-06-02 11:01:10 -07002966 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00002967}
2968
2969void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
2970 uint32_t offset,
2971 Register out_lo,
2972 Register out_hi) {
2973 if (offset != 0) {
2974 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00002975 __ add(IP, addr, ShifterOperand(out_lo));
2976 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00002977 }
2978 __ ldrexd(out_lo, out_hi, addr);
2979}
2980
2981void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
2982 uint32_t offset,
2983 Register value_lo,
2984 Register value_hi,
2985 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00002986 Register temp2,
2987 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00002988 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00002989 if (offset != 0) {
2990 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00002991 __ add(IP, addr, ShifterOperand(temp1));
2992 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00002993 }
2994 __ Bind(&fail);
2995 // We need a load followed by store. (The address used in a STREX instruction must
2996 // be the same as the address in the most recently executed LDREX instruction.)
2997 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00002998 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002999 __ strexd(temp1, value_lo, value_hi, addr);
3000 __ cmp(temp1, ShifterOperand(0));
3001 __ b(&fail, NE);
3002}
3003
3004void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3005 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3006
Nicolas Geoffray39468442014-09-02 15:17:15 +01003007 LocationSummary* locations =
3008 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003009 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003010
Calin Juravle52c48962014-12-16 17:02:57 +00003011 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003012 if (Primitive::IsFloatingPointType(field_type)) {
3013 locations->SetInAt(1, Location::RequiresFpuRegister());
3014 } else {
3015 locations->SetInAt(1, Location::RequiresRegister());
3016 }
3017
Calin Juravle52c48962014-12-16 17:02:57 +00003018 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00003019 bool generate_volatile = field_info.IsVolatile()
3020 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003021 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003022 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00003023 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
3024 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003025 locations->AddTemp(Location::RequiresRegister());
3026 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003027 } else if (generate_volatile) {
Calin Juravle52c48962014-12-16 17:02:57 +00003028 // Arm encoding have some additional constraints for ldrexd/strexd:
3029 // - registers need to be consecutive
3030 // - the first register should be even but not R14.
3031 // We don't test for Arm yet, and the assertion makes sure that we revisit this if we ever
3032 // enable Arm encoding.
3033 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3034
3035 locations->AddTemp(Location::RequiresRegister());
3036 locations->AddTemp(Location::RequiresRegister());
3037 if (field_type == Primitive::kPrimDouble) {
3038 // For doubles we need two more registers to copy the value.
3039 locations->AddTemp(Location::RegisterLocation(R2));
3040 locations->AddTemp(Location::RegisterLocation(R3));
3041 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003042 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003043}
3044
Calin Juravle52c48962014-12-16 17:02:57 +00003045void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003046 const FieldInfo& field_info,
3047 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003048 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3049
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003050 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003051 Register base = locations->InAt(0).AsRegister<Register>();
3052 Location value = locations->InAt(1);
3053
3054 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003055 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00003056 Primitive::Type field_type = field_info.GetFieldType();
3057 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3058
3059 if (is_volatile) {
3060 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3061 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003062
3063 switch (field_type) {
3064 case Primitive::kPrimBoolean:
3065 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003066 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003067 break;
3068 }
3069
3070 case Primitive::kPrimShort:
3071 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003072 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003073 break;
3074 }
3075
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003076 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003077 case Primitive::kPrimNot: {
Calin Juravle77520bc2015-01-12 18:45:46 +00003078 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003079 break;
3080 }
3081
3082 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00003083 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003084 GenerateWideAtomicStore(base, offset,
3085 value.AsRegisterPairLow<Register>(),
3086 value.AsRegisterPairHigh<Register>(),
3087 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003088 locations->GetTemp(1).AsRegister<Register>(),
3089 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003090 } else {
3091 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003092 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003093 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003094 break;
3095 }
3096
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003097 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003098 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003099 break;
3100 }
3101
3102 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003103 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00003104 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003105 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
3106 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
3107
3108 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
3109
3110 GenerateWideAtomicStore(base, offset,
3111 value_reg_lo,
3112 value_reg_hi,
3113 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003114 locations->GetTemp(3).AsRegister<Register>(),
3115 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003116 } else {
3117 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003118 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003119 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003120 break;
3121 }
3122
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003123 case Primitive::kPrimVoid:
3124 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003125 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003126 }
Calin Juravle52c48962014-12-16 17:02:57 +00003127
Calin Juravle77520bc2015-01-12 18:45:46 +00003128 // Longs and doubles are handled in the switch.
3129 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
3130 codegen_->MaybeRecordImplicitNullCheck(instruction);
3131 }
3132
3133 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3134 Register temp = locations->GetTemp(0).AsRegister<Register>();
3135 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003136 codegen_->MarkGCCard(
3137 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003138 }
3139
Calin Juravle52c48962014-12-16 17:02:57 +00003140 if (is_volatile) {
3141 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3142 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003143}
3144
Calin Juravle52c48962014-12-16 17:02:57 +00003145void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3146 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003147 LocationSummary* locations =
3148 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003149 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003150
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003151 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00003152 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003153 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003154 bool overlap = field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong);
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01003155
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003156 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3157 locations->SetOut(Location::RequiresFpuRegister());
3158 } else {
3159 locations->SetOut(Location::RequiresRegister(),
3160 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
3161 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003162 if (volatile_for_double) {
Calin Juravle52c48962014-12-16 17:02:57 +00003163 // Arm encoding have some additional constraints for ldrexd/strexd:
3164 // - registers need to be consecutive
3165 // - the first register should be even but not R14.
3166 // We don't test for Arm yet, and the assertion makes sure that we revisit this if we ever
3167 // enable Arm encoding.
3168 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3169 locations->AddTemp(Location::RequiresRegister());
3170 locations->AddTemp(Location::RequiresRegister());
3171 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003172}
3173
Calin Juravle52c48962014-12-16 17:02:57 +00003174void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
3175 const FieldInfo& field_info) {
3176 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003177
Calin Juravle52c48962014-12-16 17:02:57 +00003178 LocationSummary* locations = instruction->GetLocations();
3179 Register base = locations->InAt(0).AsRegister<Register>();
3180 Location out = locations->Out();
3181 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003182 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00003183 Primitive::Type field_type = field_info.GetFieldType();
3184 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3185
3186 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003187 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003188 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003189 break;
3190 }
3191
3192 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003193 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003194 break;
3195 }
3196
3197 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003198 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003199 break;
3200 }
3201
3202 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003203 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003204 break;
3205 }
3206
3207 case Primitive::kPrimInt:
3208 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003209 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003210 break;
3211 }
3212
3213 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00003214 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003215 GenerateWideAtomicLoad(base, offset,
3216 out.AsRegisterPairLow<Register>(),
3217 out.AsRegisterPairHigh<Register>());
3218 } else {
3219 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
3220 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003221 break;
3222 }
3223
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003224 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003225 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003226 break;
3227 }
3228
3229 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003230 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00003231 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003232 Register lo = locations->GetTemp(0).AsRegister<Register>();
3233 Register hi = locations->GetTemp(1).AsRegister<Register>();
3234 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00003235 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003236 __ vmovdrr(out_reg, lo, hi);
3237 } else {
3238 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003239 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003240 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003241 break;
3242 }
3243
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003244 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003245 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003246 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003247 }
Calin Juravle52c48962014-12-16 17:02:57 +00003248
Calin Juravle77520bc2015-01-12 18:45:46 +00003249 // Doubles are handled in the switch.
3250 if (field_type != Primitive::kPrimDouble) {
3251 codegen_->MaybeRecordImplicitNullCheck(instruction);
3252 }
3253
Calin Juravle52c48962014-12-16 17:02:57 +00003254 if (is_volatile) {
3255 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3256 }
3257}
3258
3259void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3260 HandleFieldSet(instruction, instruction->GetFieldInfo());
3261}
3262
3263void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003264 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003265}
3266
3267void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3268 HandleFieldGet(instruction, instruction->GetFieldInfo());
3269}
3270
3271void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3272 HandleFieldGet(instruction, instruction->GetFieldInfo());
3273}
3274
3275void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3276 HandleFieldGet(instruction, instruction->GetFieldInfo());
3277}
3278
3279void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3280 HandleFieldGet(instruction, instruction->GetFieldInfo());
3281}
3282
3283void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3284 HandleFieldSet(instruction, instruction->GetFieldInfo());
3285}
3286
3287void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003288 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003289}
3290
3291void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003292 LocationSummary* locations =
3293 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle77520bc2015-01-12 18:45:46 +00003294 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003295 if (instruction->HasUses()) {
3296 locations->SetOut(Location::SameAsFirstInput());
3297 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003298}
3299
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003300void InstructionCodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003301 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3302 return;
3303 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003304 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003305
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003306 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
3307 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3308}
3309
3310void InstructionCodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003311 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003312 codegen_->AddSlowPath(slow_path);
3313
3314 LocationSummary* locations = instruction->GetLocations();
3315 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003316
Calin Juravle77520bc2015-01-12 18:45:46 +00003317 __ cmp(obj.AsRegister<Register>(), ShifterOperand(0));
3318 __ b(slow_path->GetEntryLabel(), EQ);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003319}
3320
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003321void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
3322 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3323 GenerateImplicitNullCheck(instruction);
3324 } else {
3325 GenerateExplicitNullCheck(instruction);
3326 }
3327}
3328
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003329void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003330 LocationSummary* locations =
3331 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003332 locations->SetInAt(0, Location::RequiresRegister());
3333 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003334 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3335 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3336 } else {
3337 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3338 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003339}
3340
3341void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
3342 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003343 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003344 Location index = locations->InAt(1);
3345
3346 switch (instruction->GetType()) {
3347 case Primitive::kPrimBoolean: {
3348 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003349 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003350 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003351 size_t offset =
3352 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003353 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
3354 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003355 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003356 __ LoadFromOffset(kLoadUnsignedByte, out, IP, data_offset);
3357 }
3358 break;
3359 }
3360
3361 case Primitive::kPrimByte: {
3362 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003363 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003364 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003365 size_t offset =
3366 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003367 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
3368 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003369 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003370 __ LoadFromOffset(kLoadSignedByte, out, IP, data_offset);
3371 }
3372 break;
3373 }
3374
3375 case Primitive::kPrimShort: {
3376 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003377 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003378 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003379 size_t offset =
3380 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003381 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
3382 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003383 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003384 __ LoadFromOffset(kLoadSignedHalfword, out, IP, data_offset);
3385 }
3386 break;
3387 }
3388
3389 case Primitive::kPrimChar: {
3390 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003391 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003392 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003393 size_t offset =
3394 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003395 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
3396 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003397 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003398 __ LoadFromOffset(kLoadUnsignedHalfword, out, IP, data_offset);
3399 }
3400 break;
3401 }
3402
3403 case Primitive::kPrimInt:
3404 case Primitive::kPrimNot: {
3405 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
3406 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003407 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003408 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003409 size_t offset =
3410 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003411 __ LoadFromOffset(kLoadWord, out, obj, offset);
3412 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003413 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003414 __ LoadFromOffset(kLoadWord, out, IP, data_offset);
3415 }
3416 break;
3417 }
3418
3419 case Primitive::kPrimLong: {
3420 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003421 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003422 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003423 size_t offset =
3424 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003425 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003426 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003427 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003428 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003429 }
3430 break;
3431 }
3432
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003433 case Primitive::kPrimFloat: {
3434 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3435 Location out = locations->Out();
3436 DCHECK(out.IsFpuRegister());
3437 if (index.IsConstant()) {
3438 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3439 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), obj, offset);
3440 } else {
3441 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
3442 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), IP, data_offset);
3443 }
3444 break;
3445 }
3446
3447 case Primitive::kPrimDouble: {
3448 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3449 Location out = locations->Out();
3450 DCHECK(out.IsFpuRegisterPair());
3451 if (index.IsConstant()) {
3452 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3453 __ LoadDFromOffset(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()), obj, offset);
3454 } else {
3455 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
3456 __ LoadDFromOffset(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
3457 }
3458 break;
3459 }
3460
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003461 case Primitive::kPrimVoid:
3462 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003463 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003464 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003465 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003466}
3467
3468void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003469 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003470
3471 bool needs_write_barrier =
3472 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3473 bool needs_runtime_call = instruction->NeedsTypeCheck();
3474
Nicolas Geoffray39468442014-09-02 15:17:15 +01003475 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003476 instruction, needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
3477 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003478 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003479 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3480 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3481 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003482 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003483 locations->SetInAt(0, Location::RequiresRegister());
3484 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003485 if (Primitive::IsFloatingPointType(value_type)) {
3486 locations->SetInAt(2, Location::RequiresFpuRegister());
3487 } else {
3488 locations->SetInAt(2, Location::RequiresRegister());
3489 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003490
3491 if (needs_write_barrier) {
3492 // Temporary registers for the write barrier.
3493 locations->AddTemp(Location::RequiresRegister());
3494 locations->AddTemp(Location::RequiresRegister());
3495 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003496 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003497}
3498
3499void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
3500 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003501 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003502 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003503 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003504 bool needs_runtime_call = locations->WillCall();
3505 bool needs_write_barrier =
3506 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003507
3508 switch (value_type) {
3509 case Primitive::kPrimBoolean:
3510 case Primitive::kPrimByte: {
3511 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003512 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003513 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003514 size_t offset =
3515 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003516 __ StoreToOffset(kStoreByte, value, obj, offset);
3517 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003518 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003519 __ StoreToOffset(kStoreByte, value, IP, data_offset);
3520 }
3521 break;
3522 }
3523
3524 case Primitive::kPrimShort:
3525 case Primitive::kPrimChar: {
3526 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003527 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003528 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003529 size_t offset =
3530 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003531 __ StoreToOffset(kStoreHalfword, value, obj, offset);
3532 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003533 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003534 __ StoreToOffset(kStoreHalfword, value, IP, data_offset);
3535 }
3536 break;
3537 }
3538
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003539 case Primitive::kPrimInt:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003540 case Primitive::kPrimNot: {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003541 if (!needs_runtime_call) {
3542 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003543 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003544 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003545 size_t offset =
3546 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003547 __ StoreToOffset(kStoreWord, value, obj, offset);
3548 } else {
3549 DCHECK(index.IsRegister()) << index;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003550 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003551 __ StoreToOffset(kStoreWord, value, IP, data_offset);
3552 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003553 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003554 if (needs_write_barrier) {
3555 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003556 Register temp = locations->GetTemp(0).AsRegister<Register>();
3557 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003558 codegen_->MarkGCCard(temp, card, obj, value, instruction->GetValueCanBeNull());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003559 }
3560 } else {
3561 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain199f3362014-11-27 17:15:16 +00003562 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
3563 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003564 instruction->GetDexPc(),
3565 nullptr);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003566 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003567 break;
3568 }
3569
3570 case Primitive::kPrimLong: {
3571 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003572 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003573 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003574 size_t offset =
3575 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003576 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003577 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003578 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003579 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003580 }
3581 break;
3582 }
3583
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003584 case Primitive::kPrimFloat: {
3585 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3586 Location value = locations->InAt(2);
3587 DCHECK(value.IsFpuRegister());
3588 if (index.IsConstant()) {
3589 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3590 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), obj, offset);
3591 } else {
3592 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
3593 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
3594 }
3595 break;
3596 }
3597
3598 case Primitive::kPrimDouble: {
3599 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3600 Location value = locations->InAt(2);
3601 DCHECK(value.IsFpuRegisterPair());
3602 if (index.IsConstant()) {
3603 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3604 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), obj, offset);
3605 } else {
3606 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
3607 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
3608 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003609
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003610 break;
3611 }
3612
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003613 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003614 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003615 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003616 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003617
3618 // Ints and objects are handled in the switch.
3619 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
3620 codegen_->MaybeRecordImplicitNullCheck(instruction);
3621 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003622}
3623
3624void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003625 LocationSummary* locations =
3626 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003627 locations->SetInAt(0, Location::RequiresRegister());
3628 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003629}
3630
3631void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
3632 LocationSummary* locations = instruction->GetLocations();
3633 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003634 Register obj = locations->InAt(0).AsRegister<Register>();
3635 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003636 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003637 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003638}
3639
3640void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003641 LocationSummary* locations =
3642 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003643 locations->SetInAt(0, Location::RequiresRegister());
3644 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003645 if (instruction->HasUses()) {
3646 locations->SetOut(Location::SameAsFirstInput());
3647 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003648}
3649
3650void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
3651 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003652 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(
Nicolas Geoffray39468442014-09-02 15:17:15 +01003653 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003654 codegen_->AddSlowPath(slow_path);
3655
Roland Levillain271ab9c2014-11-27 15:23:57 +00003656 Register index = locations->InAt(0).AsRegister<Register>();
3657 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003658
3659 __ cmp(index, ShifterOperand(length));
3660 __ b(slow_path->GetEntryLabel(), CS);
3661}
3662
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003663void CodeGeneratorARM::MarkGCCard(Register temp,
3664 Register card,
3665 Register object,
3666 Register value,
3667 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003668 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003669 if (can_be_null) {
3670 __ CompareAndBranchIfZero(value, &is_null);
3671 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003672 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmWordSize>().Int32Value());
3673 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
3674 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003675 if (can_be_null) {
3676 __ Bind(&is_null);
3677 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003678}
3679
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003680void LocationsBuilderARM::VisitTemporary(HTemporary* temp) {
3681 temp->SetLocations(nullptr);
3682}
3683
3684void InstructionCodeGeneratorARM::VisitTemporary(HTemporary* temp) {
3685 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003686 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003687}
3688
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003689void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003690 UNUSED(instruction);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003691 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003692}
3693
3694void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003695 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3696}
3697
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003698void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
3699 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3700}
3701
3702void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003703 HBasicBlock* block = instruction->GetBlock();
3704 if (block->GetLoopInformation() != nullptr) {
3705 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3706 // The back edge will generate the suspend check.
3707 return;
3708 }
3709 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3710 // The goto will generate the suspend check.
3711 return;
3712 }
3713 GenerateSuspendCheck(instruction, nullptr);
3714}
3715
3716void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
3717 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003718 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003719 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
3720 if (slow_path == nullptr) {
3721 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
3722 instruction->SetSlowPath(slow_path);
3723 codegen_->AddSlowPath(slow_path);
3724 if (successor != nullptr) {
3725 DCHECK(successor->IsLoopHeader());
3726 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
3727 }
3728 } else {
3729 DCHECK_EQ(slow_path->GetSuccessor(), successor);
3730 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003731
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00003732 __ LoadFromOffset(
3733 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmWordSize>().Int32Value());
3734 __ cmp(IP, ShifterOperand(0));
3735 // TODO: Figure out the branch offsets and use cbz/cbnz.
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003736 if (successor == nullptr) {
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00003737 __ b(slow_path->GetEntryLabel(), NE);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003738 __ Bind(slow_path->GetReturnLabel());
3739 } else {
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00003740 __ b(codegen_->GetLabelOf(successor), EQ);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003741 __ b(slow_path->GetEntryLabel());
3742 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003743}
3744
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003745ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
3746 return codegen_->GetAssembler();
3747}
3748
3749void ParallelMoveResolverARM::EmitMove(size_t index) {
3750 MoveOperands* move = moves_.Get(index);
3751 Location source = move->GetSource();
3752 Location destination = move->GetDestination();
3753
3754 if (source.IsRegister()) {
3755 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003756 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003757 } else {
3758 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003759 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003760 SP, destination.GetStackIndex());
3761 }
3762 } else if (source.IsStackSlot()) {
3763 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003764 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003765 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003766 } else if (destination.IsFpuRegister()) {
3767 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003768 } else {
3769 DCHECK(destination.IsStackSlot());
3770 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
3771 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
3772 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003773 } else if (source.IsFpuRegister()) {
3774 if (destination.IsFpuRegister()) {
3775 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003776 } else {
3777 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003778 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
3779 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003780 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003781 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00003782 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
3783 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003784 } else if (destination.IsRegisterPair()) {
3785 DCHECK(ExpectedPairLayout(destination));
3786 __ LoadFromOffset(
3787 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
3788 } else {
3789 DCHECK(destination.IsFpuRegisterPair()) << destination;
3790 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
3791 SP,
3792 source.GetStackIndex());
3793 }
3794 } else if (source.IsRegisterPair()) {
3795 if (destination.IsRegisterPair()) {
3796 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
3797 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
3798 } else {
3799 DCHECK(destination.IsDoubleStackSlot()) << destination;
3800 DCHECK(ExpectedPairLayout(source));
3801 __ StoreToOffset(
3802 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
3803 }
3804 } else if (source.IsFpuRegisterPair()) {
3805 if (destination.IsFpuRegisterPair()) {
3806 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
3807 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
3808 } else {
3809 DCHECK(destination.IsDoubleStackSlot()) << destination;
3810 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
3811 SP,
3812 destination.GetStackIndex());
3813 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003814 } else {
3815 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003816 HConstant* constant = source.GetConstant();
3817 if (constant->IsIntConstant() || constant->IsNullConstant()) {
3818 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003819 if (destination.IsRegister()) {
3820 __ LoadImmediate(destination.AsRegister<Register>(), value);
3821 } else {
3822 DCHECK(destination.IsStackSlot());
3823 __ LoadImmediate(IP, value);
3824 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
3825 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003826 } else if (constant->IsLongConstant()) {
3827 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003828 if (destination.IsRegisterPair()) {
3829 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
3830 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003831 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003832 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003833 __ LoadImmediate(IP, Low32Bits(value));
3834 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
3835 __ LoadImmediate(IP, High32Bits(value));
3836 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
3837 }
3838 } else if (constant->IsDoubleConstant()) {
3839 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003840 if (destination.IsFpuRegisterPair()) {
3841 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003842 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003843 DCHECK(destination.IsDoubleStackSlot()) << destination;
3844 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003845 __ LoadImmediate(IP, Low32Bits(int_value));
3846 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
3847 __ LoadImmediate(IP, High32Bits(int_value));
3848 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
3849 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003850 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003851 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003852 float value = constant->AsFloatConstant()->GetValue();
3853 if (destination.IsFpuRegister()) {
3854 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
3855 } else {
3856 DCHECK(destination.IsStackSlot());
3857 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
3858 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
3859 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003860 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003861 }
3862}
3863
3864void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
3865 __ Mov(IP, reg);
3866 __ LoadFromOffset(kLoadWord, reg, SP, mem);
3867 __ StoreToOffset(kStoreWord, IP, SP, mem);
3868}
3869
3870void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
3871 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
3872 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
3873 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
3874 SP, mem1 + stack_offset);
3875 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
3876 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
3877 SP, mem2 + stack_offset);
3878 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
3879}
3880
3881void ParallelMoveResolverARM::EmitSwap(size_t index) {
3882 MoveOperands* move = moves_.Get(index);
3883 Location source = move->GetSource();
3884 Location destination = move->GetDestination();
3885
3886 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003887 DCHECK_NE(source.AsRegister<Register>(), IP);
3888 DCHECK_NE(destination.AsRegister<Register>(), IP);
3889 __ Mov(IP, source.AsRegister<Register>());
3890 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
3891 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003892 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003893 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003894 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003895 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003896 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
3897 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003898 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00003899 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003900 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00003901 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003902 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00003903 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003904 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003905 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00003906 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
3907 destination.AsRegisterPairHigh<Register>(),
3908 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003909 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003910 Register low_reg = source.IsRegisterPair()
3911 ? source.AsRegisterPairLow<Register>()
3912 : destination.AsRegisterPairLow<Register>();
3913 int mem = source.IsRegisterPair()
3914 ? destination.GetStackIndex()
3915 : source.GetStackIndex();
3916 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00003917 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003918 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00003919 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003920 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003921 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
3922 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00003923 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003924 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00003925 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003926 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
3927 DRegister reg = source.IsFpuRegisterPair()
3928 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
3929 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
3930 int mem = source.IsFpuRegisterPair()
3931 ? destination.GetStackIndex()
3932 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00003933 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003934 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00003935 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003936 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
3937 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
3938 : destination.AsFpuRegister<SRegister>();
3939 int mem = source.IsFpuRegister()
3940 ? destination.GetStackIndex()
3941 : source.GetStackIndex();
3942
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00003943 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003944 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00003945 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00003946 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00003947 Exchange(source.GetStackIndex(), destination.GetStackIndex());
3948 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003949 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00003950 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003951 }
3952}
3953
3954void ParallelMoveResolverARM::SpillScratch(int reg) {
3955 __ Push(static_cast<Register>(reg));
3956}
3957
3958void ParallelMoveResolverARM::RestoreScratch(int reg) {
3959 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003960}
3961
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003962void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003963 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
3964 ? LocationSummary::kCallOnSlowPath
3965 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003966 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003967 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003968 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003969 locations->SetOut(Location::RequiresRegister());
3970}
3971
3972void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003973 LocationSummary* locations = cls->GetLocations();
3974 Register out = locations->Out().AsRegister<Register>();
3975 Register current_method = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003976 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003977 DCHECK(!cls->CanCallRuntime());
3978 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003979 __ LoadFromOffset(
Mathieu Chartiere401d142015-04-22 13:56:20 -07003980 kLoadWord, out, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003981 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003982 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003983 __ LoadFromOffset(kLoadWord,
3984 out,
3985 current_method,
Mathieu Chartiere401d142015-04-22 13:56:20 -07003986 ArtMethod::DexCacheResolvedTypesOffset().Int32Value());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003987 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003988
3989 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
3990 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3991 codegen_->AddSlowPath(slow_path);
3992 __ cmp(out, ShifterOperand(0));
3993 __ b(slow_path->GetEntryLabel(), EQ);
3994 if (cls->MustGenerateClinitCheck()) {
3995 GenerateClassInitializationCheck(slow_path, out);
3996 } else {
3997 __ Bind(slow_path->GetExitLabel());
3998 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003999 }
4000}
4001
4002void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
4003 LocationSummary* locations =
4004 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4005 locations->SetInAt(0, Location::RequiresRegister());
4006 if (check->HasUses()) {
4007 locations->SetOut(Location::SameAsFirstInput());
4008 }
4009}
4010
4011void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004012 // We assume the class is not null.
4013 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
4014 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004015 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004016 GenerateClassInitializationCheck(slow_path,
4017 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004018}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004019
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004020void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
4021 SlowPathCodeARM* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004022 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
4023 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
4024 __ b(slow_path->GetEntryLabel(), LT);
4025 // Even if the initialized flag is set, we may be in a situation where caches are not synced
4026 // properly. Therefore, we do a memory fence.
4027 __ dmb(ISH);
4028 __ Bind(slow_path->GetExitLabel());
4029}
4030
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004031void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
4032 LocationSummary* locations =
4033 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004034 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004035 locations->SetOut(Location::RequiresRegister());
4036}
4037
4038void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) {
4039 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
4040 codegen_->AddSlowPath(slow_path);
4041
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004042 LocationSummary* locations = load->GetLocations();
4043 Register out = locations->Out().AsRegister<Register>();
4044 Register current_method = locations->InAt(0).AsRegister<Register>();
4045 __ LoadFromOffset(
Mathieu Chartiere401d142015-04-22 13:56:20 -07004046 kLoadWord, out, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Mathieu Chartiereace4582014-11-24 18:29:54 -08004047 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004048 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
4049 __ cmp(out, ShifterOperand(0));
4050 __ b(slow_path->GetEntryLabel(), EQ);
4051 __ Bind(slow_path->GetExitLabel());
4052}
4053
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004054void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
4055 LocationSummary* locations =
4056 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4057 locations->SetOut(Location::RequiresRegister());
4058}
4059
4060void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004061 Register out = load->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004062 int32_t offset = Thread::ExceptionOffset<kArmWordSize>().Int32Value();
4063 __ LoadFromOffset(kLoadWord, out, TR, offset);
4064 __ LoadImmediate(IP, 0);
4065 __ StoreToOffset(kStoreWord, IP, TR, offset);
4066}
4067
4068void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
4069 LocationSummary* locations =
4070 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4071 InvokeRuntimeCallingConvention calling_convention;
4072 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4073}
4074
4075void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
4076 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004077 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004078}
4079
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004080void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004081 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4082 ? LocationSummary::kNoCall
4083 : LocationSummary::kCallOnSlowPath;
4084 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4085 locations->SetInAt(0, Location::RequiresRegister());
4086 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004087 // The out register is used as a temporary, so it overlaps with the inputs.
4088 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004089}
4090
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004091void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004092 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004093 Register obj = locations->InAt(0).AsRegister<Register>();
4094 Register cls = locations->InAt(1).AsRegister<Register>();
4095 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004096 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004097 Label done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004098 SlowPathCodeARM* slow_path = nullptr;
4099
4100 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004101 // avoid null check if we know obj is not null.
4102 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00004103 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004104 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004105 // Compare the class of `obj` with `cls`.
4106 __ LoadFromOffset(kLoadWord, out, obj, class_offset);
4107 __ cmp(out, ShifterOperand(cls));
4108 if (instruction->IsClassFinal()) {
4109 // Classes must be equal for the instanceof to succeed.
4110 __ b(&zero, NE);
4111 __ LoadImmediate(out, 1);
4112 __ b(&done);
4113 } else {
4114 // If the classes are not equal, we go into a slow path.
4115 DCHECK(locations->OnlyCallsOnSlowPath());
4116 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004117 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004118 codegen_->AddSlowPath(slow_path);
4119 __ b(slow_path->GetEntryLabel(), NE);
4120 __ LoadImmediate(out, 1);
4121 __ b(&done);
4122 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004123
4124 if (instruction->MustDoNullCheck() || instruction->IsClassFinal()) {
4125 __ Bind(&zero);
4126 __ LoadImmediate(out, 0);
4127 }
4128
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004129 if (slow_path != nullptr) {
4130 __ Bind(slow_path->GetExitLabel());
4131 }
4132 __ Bind(&done);
4133}
4134
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004135void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
4136 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4137 instruction, LocationSummary::kCallOnSlowPath);
4138 locations->SetInAt(0, Location::RequiresRegister());
4139 locations->SetInAt(1, Location::RequiresRegister());
4140 locations->AddTemp(Location::RequiresRegister());
4141}
4142
4143void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
4144 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004145 Register obj = locations->InAt(0).AsRegister<Register>();
4146 Register cls = locations->InAt(1).AsRegister<Register>();
4147 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004148 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4149
4150 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(
4151 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
4152 codegen_->AddSlowPath(slow_path);
4153
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004154 // avoid null check if we know obj is not null.
4155 if (instruction->MustDoNullCheck()) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004156 __ CompareAndBranchIfZero(obj, slow_path->GetExitLabel());
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004157 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004158 // Compare the class of `obj` with `cls`.
4159 __ LoadFromOffset(kLoadWord, temp, obj, class_offset);
4160 __ cmp(temp, ShifterOperand(cls));
4161 __ b(slow_path->GetEntryLabel(), NE);
4162 __ Bind(slow_path->GetExitLabel());
4163}
4164
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004165void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
4166 LocationSummary* locations =
4167 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4168 InvokeRuntimeCallingConvention calling_convention;
4169 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4170}
4171
4172void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
4173 codegen_->InvokeRuntime(instruction->IsEnter()
4174 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
4175 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004176 instruction->GetDexPc(),
4177 nullptr);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004178}
4179
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004180void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4181void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4182void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4183
4184void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
4185 LocationSummary* locations =
4186 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4187 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4188 || instruction->GetResultType() == Primitive::kPrimLong);
4189 locations->SetInAt(0, Location::RequiresRegister());
4190 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004191 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004192}
4193
4194void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
4195 HandleBitwiseOperation(instruction);
4196}
4197
4198void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
4199 HandleBitwiseOperation(instruction);
4200}
4201
4202void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
4203 HandleBitwiseOperation(instruction);
4204}
4205
4206void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
4207 LocationSummary* locations = instruction->GetLocations();
4208
4209 if (instruction->GetResultType() == Primitive::kPrimInt) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004210 Register first = locations->InAt(0).AsRegister<Register>();
4211 Register second = locations->InAt(1).AsRegister<Register>();
4212 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004213 if (instruction->IsAnd()) {
4214 __ and_(out, first, ShifterOperand(second));
4215 } else if (instruction->IsOr()) {
4216 __ orr(out, first, ShifterOperand(second));
4217 } else {
4218 DCHECK(instruction->IsXor());
4219 __ eor(out, first, ShifterOperand(second));
4220 }
4221 } else {
4222 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
4223 Location first = locations->InAt(0);
4224 Location second = locations->InAt(1);
4225 Location out = locations->Out();
4226 if (instruction->IsAnd()) {
4227 __ and_(out.AsRegisterPairLow<Register>(),
4228 first.AsRegisterPairLow<Register>(),
4229 ShifterOperand(second.AsRegisterPairLow<Register>()));
4230 __ and_(out.AsRegisterPairHigh<Register>(),
4231 first.AsRegisterPairHigh<Register>(),
4232 ShifterOperand(second.AsRegisterPairHigh<Register>()));
4233 } else if (instruction->IsOr()) {
4234 __ orr(out.AsRegisterPairLow<Register>(),
4235 first.AsRegisterPairLow<Register>(),
4236 ShifterOperand(second.AsRegisterPairLow<Register>()));
4237 __ orr(out.AsRegisterPairHigh<Register>(),
4238 first.AsRegisterPairHigh<Register>(),
4239 ShifterOperand(second.AsRegisterPairHigh<Register>()));
4240 } else {
4241 DCHECK(instruction->IsXor());
4242 __ eor(out.AsRegisterPairLow<Register>(),
4243 first.AsRegisterPairLow<Register>(),
4244 ShifterOperand(second.AsRegisterPairLow<Register>()));
4245 __ eor(out.AsRegisterPairHigh<Register>(),
4246 first.AsRegisterPairHigh<Register>(),
4247 ShifterOperand(second.AsRegisterPairHigh<Register>()));
4248 }
4249 }
4250}
4251
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004252void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08004253 // TODO: Implement all kinds of calls:
4254 // 1) boot -> boot
4255 // 2) app -> boot
4256 // 3) app -> app
4257 //
4258 // Currently we implement the app -> app logic, which looks up in the resolve cache.
4259
Jeff Hao848f70a2014-01-15 13:49:50 -08004260 if (invoke->IsStringInit()) {
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004261 Register reg = temp.AsRegister<Register>();
Jeff Hao848f70a2014-01-15 13:49:50 -08004262 // temp = thread->string_init_entrypoint
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004263 __ LoadFromOffset(kLoadWord, reg, TR, invoke->GetStringInitOffset());
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08004264 // LR = temp[offset_of_quick_compiled_code]
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004265 __ LoadFromOffset(kLoadWord, LR, reg,
Mathieu Chartiere401d142015-04-22 13:56:20 -07004266 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08004267 kArmWordSize).Int32Value());
4268 // LR()
4269 __ blx(LR);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004270 } else if (invoke->IsRecursive()) {
4271 __ bl(GetFrameEntryLabel());
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08004272 } else {
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01004273 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
4274 Register method_reg;
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004275 Register reg = temp.AsRegister<Register>();
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01004276 if (current_method.IsRegister()) {
4277 method_reg = current_method.AsRegister<Register>();
4278 } else {
4279 DCHECK(invoke->GetLocations()->Intrinsified());
4280 DCHECK(!current_method.IsValid());
4281 method_reg = reg;
4282 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
4283 }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004284 // reg = current_method->dex_cache_resolved_methods_;
4285 __ LoadFromOffset(
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01004286 kLoadWord, reg, method_reg, ArtMethod::DexCacheResolvedMethodsOffset().Int32Value());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004287 // reg = reg[index_in_cache]
4288 __ LoadFromOffset(
4289 kLoadWord, reg, reg, CodeGenerator::GetCacheOffset(invoke->GetDexMethodIndex()));
4290 // LR = reg[offset_of_quick_compiled_code]
4291 __ LoadFromOffset(kLoadWord, LR, reg, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
4292 kArmWordSize).Int32Value());
4293 // LR()
4294 __ blx(LR);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08004295 }
4296
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08004297 DCHECK(!IsLeafMethod());
4298}
4299
Calin Juravleb1498f62015-02-16 13:13:29 +00004300void LocationsBuilderARM::VisitBoundType(HBoundType* instruction) {
4301 // Nothing to do, this should be removed during prepare for register allocator.
4302 UNUSED(instruction);
4303 LOG(FATAL) << "Unreachable";
4304}
4305
4306void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction) {
4307 // Nothing to do, this should be removed during prepare for register allocator.
4308 UNUSED(instruction);
4309 LOG(FATAL) << "Unreachable";
4310}
4311
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004312} // namespace arm
4313} // namespace art