blob: 12ab68e8cd8dce77c325ca852b1dafe4df989a82 [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"
Vladimir Marko58155012015-08-19 12:49:41 +000022#include "compiled_method.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070023#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010024#include "gc/accounting/card_table.h"
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -080025#include "intrinsics.h"
26#include "intrinsics_arm.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070027#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070028#include "mirror/class-inl.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070029#include "thread.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010030#include "utils/arm/assembler_arm.h"
31#include "utils/arm/managed_register_arm.h"
Roland Levillain946e1432014-11-11 17:35:19 +000032#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010033#include "utils/stack_checks.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000034
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000035namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010036
Roland Levillain3b359c72015-11-17 19:35:12 +000037template<class MirrorType>
38class GcRoot;
39
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000040namespace arm {
41
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +000042static bool ExpectedPairLayout(Location location) {
43 // We expected this for both core and fpu register pairs.
44 return ((location.low() & 1) == 0) && (location.low() + 1 == location.high());
45}
46
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010047static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010048static constexpr Register kMethodRegisterArgument = R0;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010049
Nicolas Geoffray4dee6362015-01-23 18:23:14 +000050// We unconditionally allocate R5 to ensure we can do long operations
51// with baseline.
52static constexpr Register kCoreSavedRegisterForBaseline = R5;
53static constexpr Register kCoreCalleeSaves[] =
Andreas Gampe501fd632015-09-10 16:11:06 -070054 { R5, R6, R7, R8, R10, R11, LR };
Nicolas Geoffray4dee6362015-01-23 18:23:14 +000055static constexpr SRegister kFpuCalleeSaves[] =
56 { S16, S17, S18, S19, S20, S21, S22, S23, S24, S25, S26, S27, S28, S29, S30, S31 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010057
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +000058// D31 cannot be split into two S registers, and the register allocator only works on
59// S registers. Therefore there is no need to block it.
60static constexpr DRegister DTMP = D31;
61
Andreas Gampe7cffc3b2015-10-19 21:31:53 -070062static constexpr uint32_t kPackedSwitchJumpTableThreshold = 6;
63
Roland Levillain62a46b22015-06-01 18:24:13 +010064#define __ down_cast<ArmAssembler*>(codegen->GetAssembler())->
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010065#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmWordSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010066
Andreas Gampe85b62f22015-09-09 13:15:38 -070067class NullCheckSlowPathARM : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010068 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010069 explicit NullCheckSlowPathARM(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010070
Alexandre Rames67555f72014-11-18 10:55:16 +000071 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010072 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010073 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000074 if (instruction_->CanThrowIntoCatchBlock()) {
75 // Live registers will be restored in the catch block if caught.
76 SaveLiveRegisters(codegen, instruction_->GetLocations());
77 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010078 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +000079 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010080 }
81
Alexandre Rames8158f282015-08-07 10:26:17 +010082 bool IsFatal() const OVERRIDE { return true; }
83
Alexandre Rames9931f312015-06-19 14:47:01 +010084 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM"; }
85
Nicolas Geoffraye5038322014-07-04 09:41:32 +010086 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010087 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010088 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM);
89};
90
Andreas Gampe85b62f22015-09-09 13:15:38 -070091class DivZeroCheckSlowPathARM : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000092 public:
93 explicit DivZeroCheckSlowPathARM(HDivZeroCheck* instruction) : instruction_(instruction) {}
94
Alexandre Rames67555f72014-11-18 10:55:16 +000095 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000096 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
97 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000098 if (instruction_->CanThrowIntoCatchBlock()) {
99 // Live registers will be restored in the catch block if caught.
100 SaveLiveRegisters(codegen, instruction_->GetLocations());
101 }
Calin Juravled0d48522014-11-04 16:40:20 +0000102 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000103 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Calin Juravled0d48522014-11-04 16:40:20 +0000104 }
105
Alexandre Rames8158f282015-08-07 10:26:17 +0100106 bool IsFatal() const OVERRIDE { return true; }
107
Alexandre Rames9931f312015-06-19 14:47:01 +0100108 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM"; }
109
Calin Juravled0d48522014-11-04 16:40:20 +0000110 private:
111 HDivZeroCheck* const instruction_;
112 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM);
113};
114
Andreas Gampe85b62f22015-09-09 13:15:38 -0700115class SuspendCheckSlowPathARM : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000116 public:
Alexandre Rames67555f72014-11-18 10:55:16 +0000117 SuspendCheckSlowPathARM(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100118 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000119
Alexandre Rames67555f72014-11-18 10:55:16 +0000120 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100121 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000122 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000123 SaveLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100124 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000125 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000126 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100127 if (successor_ == nullptr) {
128 __ b(GetReturnLabel());
129 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100130 __ b(arm_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100131 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000132 }
133
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100134 Label* GetReturnLabel() {
135 DCHECK(successor_ == nullptr);
136 return &return_label_;
137 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000138
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100139 HBasicBlock* GetSuccessor() const {
140 return successor_;
141 }
142
Alexandre Rames9931f312015-06-19 14:47:01 +0100143 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM"; }
144
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000145 private:
146 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100147 // If not null, the block to branch to after the suspend check.
148 HBasicBlock* const successor_;
149
150 // If `successor_` is null, the label to branch to after the suspend check.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000151 Label return_label_;
152
153 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM);
154};
155
Andreas Gampe85b62f22015-09-09 13:15:38 -0700156class BoundsCheckSlowPathARM : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100157 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100158 explicit BoundsCheckSlowPathARM(HBoundsCheck* instruction)
159 : instruction_(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100160
Alexandre Rames67555f72014-11-18 10:55:16 +0000161 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100162 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100163 LocationSummary* locations = instruction_->GetLocations();
164
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100165 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000166 if (instruction_->CanThrowIntoCatchBlock()) {
167 // Live registers will be restored in the catch block if caught.
168 SaveLiveRegisters(codegen, instruction_->GetLocations());
169 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000170 // We're moving two locations to locations that could overlap, so we need a parallel
171 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100172 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000173 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100174 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000175 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100176 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100177 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100178 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
179 Primitive::kPrimInt);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100180 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000181 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100182 }
183
Alexandre Rames8158f282015-08-07 10:26:17 +0100184 bool IsFatal() const OVERRIDE { return true; }
185
Alexandre Rames9931f312015-06-19 14:47:01 +0100186 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM"; }
187
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100188 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100189 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100190
191 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM);
192};
193
Andreas Gampe85b62f22015-09-09 13:15:38 -0700194class LoadClassSlowPathARM : public SlowPathCode {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100195 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000196 LoadClassSlowPathARM(HLoadClass* cls,
197 HInstruction* at,
198 uint32_t dex_pc,
199 bool do_clinit)
200 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
201 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
202 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100203
Alexandre Rames67555f72014-11-18 10:55:16 +0000204 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000205 LocationSummary* locations = at_->GetLocations();
206
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100207 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
208 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000209 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100210
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100211 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000212 __ LoadImmediate(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000213 int32_t entry_point_offset = do_clinit_
214 ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
215 : QUICK_ENTRY_POINT(pInitializeType);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000216 arm_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000217
218 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000219 Location out = locations->Out();
220 if (out.IsValid()) {
221 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000222 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
223 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000224 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100225 __ b(GetExitLabel());
226 }
227
Alexandre Rames9931f312015-06-19 14:47:01 +0100228 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM"; }
229
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100230 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000231 // The class this slow path will load.
232 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100233
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000234 // The instruction where this slow path is happening.
235 // (Might be the load class or an initialization check).
236 HInstruction* const at_;
237
238 // The dex PC of `at_`.
239 const uint32_t dex_pc_;
240
241 // Whether to initialize the class.
242 const bool do_clinit_;
243
244 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100245};
246
Andreas Gampe85b62f22015-09-09 13:15:38 -0700247class LoadStringSlowPathARM : public SlowPathCode {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000248 public:
249 explicit LoadStringSlowPathARM(HLoadString* instruction) : instruction_(instruction) {}
250
Alexandre Rames67555f72014-11-18 10:55:16 +0000251 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000252 LocationSummary* locations = instruction_->GetLocations();
253 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
254
255 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
256 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000257 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000258
259 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800260 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction_->GetStringIndex());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000261 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000262 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000263 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
264
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000265 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000266 __ b(GetExitLabel());
267 }
268
Alexandre Rames9931f312015-06-19 14:47:01 +0100269 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM"; }
270
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000271 private:
272 HLoadString* const instruction_;
273
274 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM);
275};
276
Andreas Gampe85b62f22015-09-09 13:15:38 -0700277class TypeCheckSlowPathARM : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000278 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000279 TypeCheckSlowPathARM(HInstruction* instruction, bool is_fatal)
280 : instruction_(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000281
Alexandre Rames67555f72014-11-18 10:55:16 +0000282 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000283 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100284 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
285 : locations->Out();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000286 DCHECK(instruction_->IsCheckCast()
287 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000288
289 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
290 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000291
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000292 if (!is_fatal_) {
293 SaveLiveRegisters(codegen, locations);
294 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000295
296 // We're moving two locations to locations that could overlap, so we need a parallel
297 // move resolver.
298 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000299 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100300 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000301 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100302 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100303 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100304 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
305 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000306
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000307 if (instruction_->IsInstanceOf()) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100308 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
309 instruction_,
310 instruction_->GetDexPc(),
311 this);
Roland Levillain3b359c72015-11-17 19:35:12 +0000312 CheckEntrypointTypes<
313 kQuickInstanceofNonTrivial, uint32_t, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000314 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
315 } else {
316 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100317 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
318 instruction_,
319 instruction_->GetDexPc(),
320 this);
Roland Levillain3b359c72015-11-17 19:35:12 +0000321 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000322 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000323
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000324 if (!is_fatal_) {
325 RestoreLiveRegisters(codegen, locations);
326 __ b(GetExitLabel());
327 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000328 }
329
Alexandre Rames9931f312015-06-19 14:47:01 +0100330 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM"; }
331
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000332 bool IsFatal() const OVERRIDE { return is_fatal_; }
333
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000334 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000335 HInstruction* const instruction_;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000336 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000337
338 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM);
339};
340
Andreas Gampe85b62f22015-09-09 13:15:38 -0700341class DeoptimizationSlowPathARM : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700342 public:
343 explicit DeoptimizationSlowPathARM(HInstruction* instruction)
344 : instruction_(instruction) {}
345
346 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
347 __ Bind(GetEntryLabel());
348 SaveLiveRegisters(codegen, instruction_->GetLocations());
349 DCHECK(instruction_->IsDeoptimize());
350 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
351 uint32_t dex_pc = deoptimize->GetDexPc();
352 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
353 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize), instruction_, dex_pc, this);
354 }
355
Alexandre Rames9931f312015-06-19 14:47:01 +0100356 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM"; }
357
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700358 private:
359 HInstruction* const instruction_;
360 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM);
361};
362
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100363class ArraySetSlowPathARM : public SlowPathCode {
364 public:
365 explicit ArraySetSlowPathARM(HInstruction* instruction) : instruction_(instruction) {}
366
367 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
368 LocationSummary* locations = instruction_->GetLocations();
369 __ Bind(GetEntryLabel());
370 SaveLiveRegisters(codegen, locations);
371
372 InvokeRuntimeCallingConvention calling_convention;
373 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
374 parallel_move.AddMove(
375 locations->InAt(0),
376 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
377 Primitive::kPrimNot,
378 nullptr);
379 parallel_move.AddMove(
380 locations->InAt(1),
381 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
382 Primitive::kPrimInt,
383 nullptr);
384 parallel_move.AddMove(
385 locations->InAt(2),
386 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
387 Primitive::kPrimNot,
388 nullptr);
389 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
390
391 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
392 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
393 instruction_,
394 instruction_->GetDexPc(),
395 this);
396 RestoreLiveRegisters(codegen, locations);
397 __ b(GetExitLabel());
398 }
399
400 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM"; }
401
402 private:
403 HInstruction* const instruction_;
404
405 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM);
406};
407
Roland Levillain3b359c72015-11-17 19:35:12 +0000408// Slow path generating a read barrier for a heap reference.
409class ReadBarrierForHeapReferenceSlowPathARM : public SlowPathCode {
410 public:
411 ReadBarrierForHeapReferenceSlowPathARM(HInstruction* instruction,
412 Location out,
413 Location ref,
414 Location obj,
415 uint32_t offset,
416 Location index)
417 : instruction_(instruction),
418 out_(out),
419 ref_(ref),
420 obj_(obj),
421 offset_(offset),
422 index_(index) {
423 DCHECK(kEmitCompilerReadBarrier);
424 // If `obj` is equal to `out` or `ref`, it means the initial object
425 // has been overwritten by (or after) the heap object reference load
426 // to be instrumented, e.g.:
427 //
428 // __ LoadFromOffset(kLoadWord, out, out, offset);
429 // codegen_->GenerateReadBarrier(instruction, out_loc, out_loc, out_loc, offset);
430 //
431 // In that case, we have lost the information about the original
432 // object, and the emitted read barrier cannot work properly.
433 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
434 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
435 }
436
437 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
438 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
439 LocationSummary* locations = instruction_->GetLocations();
440 Register reg_out = out_.AsRegister<Register>();
441 DCHECK(locations->CanCall());
442 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
443 DCHECK(!instruction_->IsInvoke() ||
444 (instruction_->IsInvokeStaticOrDirect() &&
445 instruction_->GetLocations()->Intrinsified()));
446
447 __ Bind(GetEntryLabel());
448 SaveLiveRegisters(codegen, locations);
449
450 // We may have to change the index's value, but as `index_` is a
451 // constant member (like other "inputs" of this slow path),
452 // introduce a copy of it, `index`.
453 Location index = index_;
454 if (index_.IsValid()) {
455 // Handle `index_` for HArrayGet and intrinsic UnsafeGetObject.
456 if (instruction_->IsArrayGet()) {
457 // Compute the actual memory offset and store it in `index`.
458 Register index_reg = index_.AsRegister<Register>();
459 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
460 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
461 // We are about to change the value of `index_reg` (see the
462 // calls to art::arm::Thumb2Assembler::Lsl and
463 // art::arm::Thumb2Assembler::AddConstant below), but it has
464 // not been saved by the previous call to
465 // art::SlowPathCode::SaveLiveRegisters, as it is a
466 // callee-save register --
467 // art::SlowPathCode::SaveLiveRegisters does not consider
468 // callee-save registers, as it has been designed with the
469 // assumption that callee-save registers are supposed to be
470 // handled by the called function. So, as a callee-save
471 // register, `index_reg` _would_ eventually be saved onto
472 // the stack, but it would be too late: we would have
473 // changed its value earlier. Therefore, we manually save
474 // it here into another freely available register,
475 // `free_reg`, chosen of course among the caller-save
476 // registers (as a callee-save `free_reg` register would
477 // exhibit the same problem).
478 //
479 // Note we could have requested a temporary register from
480 // the register allocator instead; but we prefer not to, as
481 // this is a slow path, and we know we can find a
482 // caller-save register that is available.
483 Register free_reg = FindAvailableCallerSaveRegister(codegen);
484 __ Mov(free_reg, index_reg);
485 index_reg = free_reg;
486 index = Location::RegisterLocation(index_reg);
487 } else {
488 // The initial register stored in `index_` has already been
489 // saved in the call to art::SlowPathCode::SaveLiveRegisters
490 // (as it is not a callee-save register), so we can freely
491 // use it.
492 }
493 // Shifting the index value contained in `index_reg` by the scale
494 // factor (2) cannot overflow in practice, as the runtime is
495 // unable to allocate object arrays with a size larger than
496 // 2^26 - 1 (that is, 2^28 - 4 bytes).
497 __ Lsl(index_reg, index_reg, TIMES_4);
498 static_assert(
499 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
500 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
501 __ AddConstant(index_reg, index_reg, offset_);
502 } else {
503 DCHECK(instruction_->IsInvoke());
504 DCHECK(instruction_->GetLocations()->Intrinsified());
505 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
506 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
507 << instruction_->AsInvoke()->GetIntrinsic();
508 DCHECK_EQ(offset_, 0U);
509 DCHECK(index_.IsRegisterPair());
510 // UnsafeGet's offset location is a register pair, the low
511 // part contains the correct offset.
512 index = index_.ToLow();
513 }
514 }
515
516 // We're moving two or three locations to locations that could
517 // overlap, so we need a parallel move resolver.
518 InvokeRuntimeCallingConvention calling_convention;
519 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
520 parallel_move.AddMove(ref_,
521 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
522 Primitive::kPrimNot,
523 nullptr);
524 parallel_move.AddMove(obj_,
525 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
526 Primitive::kPrimNot,
527 nullptr);
528 if (index.IsValid()) {
529 parallel_move.AddMove(index,
530 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
531 Primitive::kPrimInt,
532 nullptr);
533 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
534 } else {
535 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
536 __ LoadImmediate(calling_convention.GetRegisterAt(2), offset_);
537 }
538 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
539 instruction_,
540 instruction_->GetDexPc(),
541 this);
542 CheckEntrypointTypes<
543 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
544 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
545
546 RestoreLiveRegisters(codegen, locations);
547 __ b(GetExitLabel());
548 }
549
550 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM"; }
551
552 private:
553 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
554 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
555 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
556 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
557 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
558 return static_cast<Register>(i);
559 }
560 }
561 // We shall never fail to find a free caller-save register, as
562 // there are more than two core caller-save registers on ARM
563 // (meaning it is possible to find one which is different from
564 // `ref` and `obj`).
565 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
566 LOG(FATAL) << "Could not find a free caller-save register";
567 UNREACHABLE();
568 }
569
570 HInstruction* const instruction_;
571 const Location out_;
572 const Location ref_;
573 const Location obj_;
574 const uint32_t offset_;
575 // An additional location containing an index to an array.
576 // Only used for HArrayGet and the UnsafeGetObject &
577 // UnsafeGetObjectVolatile intrinsics.
578 const Location index_;
579
580 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM);
581};
582
583// Slow path generating a read barrier for a GC root.
584class ReadBarrierForRootSlowPathARM : public SlowPathCode {
585 public:
586 ReadBarrierForRootSlowPathARM(HInstruction* instruction, Location out, Location root)
587 : instruction_(instruction), out_(out), root_(root) {}
588
589 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
590 LocationSummary* locations = instruction_->GetLocations();
591 Register reg_out = out_.AsRegister<Register>();
592 DCHECK(locations->CanCall());
593 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
594 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString());
595
596 __ Bind(GetEntryLabel());
597 SaveLiveRegisters(codegen, locations);
598
599 InvokeRuntimeCallingConvention calling_convention;
600 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
601 arm_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
602 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
603 instruction_,
604 instruction_->GetDexPc(),
605 this);
606 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
607 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
608
609 RestoreLiveRegisters(codegen, locations);
610 __ b(GetExitLabel());
611 }
612
613 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM"; }
614
615 private:
616 HInstruction* const instruction_;
617 const Location out_;
618 const Location root_;
619
620 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM);
621};
622
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000623#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100624#define __ down_cast<ArmAssembler*>(GetAssembler())->
Dave Allison20dfc792014-06-16 20:44:29 -0700625
Aart Bike9f37602015-10-09 11:15:55 -0700626inline Condition ARMCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700627 switch (cond) {
628 case kCondEQ: return EQ;
629 case kCondNE: return NE;
630 case kCondLT: return LT;
631 case kCondLE: return LE;
632 case kCondGT: return GT;
633 case kCondGE: return GE;
Aart Bike9f37602015-10-09 11:15:55 -0700634 case kCondB: return LO;
635 case kCondBE: return LS;
636 case kCondA: return HI;
637 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -0700638 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100639 LOG(FATAL) << "Unreachable";
640 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700641}
642
Aart Bike9f37602015-10-09 11:15:55 -0700643// Maps signed condition to unsigned condition.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100644inline Condition ARMUnsignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700645 switch (cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100646 case kCondEQ: return EQ;
647 case kCondNE: return NE;
Aart Bike9f37602015-10-09 11:15:55 -0700648 // Signed to unsigned.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100649 case kCondLT: return LO;
650 case kCondLE: return LS;
651 case kCondGT: return HI;
652 case kCondGE: return HS;
Aart Bike9f37602015-10-09 11:15:55 -0700653 // Unsigned remain unchanged.
654 case kCondB: return LO;
655 case kCondBE: return LS;
656 case kCondA: return HI;
657 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -0700658 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100659 LOG(FATAL) << "Unreachable";
660 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700661}
662
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100663void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100664 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100665}
666
667void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100668 stream << SRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100669}
670
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100671size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
672 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
673 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100674}
675
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100676size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
677 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
678 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100679}
680
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000681size_t CodeGeneratorARM::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
682 __ StoreSToOffset(static_cast<SRegister>(reg_id), SP, stack_index);
683 return kArmWordSize;
684}
685
686size_t CodeGeneratorARM::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
687 __ LoadSFromOffset(static_cast<SRegister>(reg_id), SP, stack_index);
688 return kArmWordSize;
689}
690
Calin Juravle34166012014-12-19 17:22:29 +0000691CodeGeneratorARM::CodeGeneratorARM(HGraph* graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000692 const ArmInstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100693 const CompilerOptions& compiler_options,
694 OptimizingCompilerStats* stats)
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000695 : CodeGenerator(graph,
696 kNumberOfCoreRegisters,
697 kNumberOfSRegisters,
698 kNumberOfRegisterPairs,
699 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
700 arraysize(kCoreCalleeSaves)),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000701 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
702 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100703 compiler_options,
704 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100705 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100706 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100707 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100708 move_resolver_(graph->GetArena(), this),
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000709 assembler_(),
Vladimir Marko58155012015-08-19 12:49:41 +0000710 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100711 method_patches_(MethodReferenceComparator(),
712 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
713 call_patches_(MethodReferenceComparator(),
714 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
715 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Andreas Gampe501fd632015-09-10 16:11:06 -0700716 // Always save the LR register to mimic Quick.
717 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100718}
719
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000720void CodeGeneratorARM::Finalize(CodeAllocator* allocator) {
721 // Ensure that we fix up branches and literal loads and emit the literal pool.
722 __ FinalizeCode();
723
724 // Adjust native pc offsets in stack maps.
725 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
726 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
727 uint32_t new_position = __ GetAdjustedPosition(old_position);
728 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
729 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100730 // Adjust pc offsets for the disassembly information.
731 if (disasm_info_ != nullptr) {
732 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
733 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
734 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
735 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
736 it.second.start = __ GetAdjustedPosition(it.second.start);
737 it.second.end = __ GetAdjustedPosition(it.second.end);
738 }
739 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
740 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
741 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
742 }
743 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000744
745 CodeGenerator::Finalize(allocator);
746}
747
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100748Location CodeGeneratorARM::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100749 switch (type) {
750 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100751 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100752 ArmManagedRegister pair =
753 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100754 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
755 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
756
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100757 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
758 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100759 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100760 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100761 }
762
763 case Primitive::kPrimByte:
764 case Primitive::kPrimBoolean:
765 case Primitive::kPrimChar:
766 case Primitive::kPrimShort:
767 case Primitive::kPrimInt:
768 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100769 int reg = FindFreeEntry(blocked_core_registers_, kNumberOfCoreRegisters);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100770 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100771 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
772 ArmManagedRegister current =
773 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
774 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100775 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100776 }
777 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100778 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100779 }
780
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000781 case Primitive::kPrimFloat: {
782 int reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfSRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100783 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100784 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100785
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000786 case Primitive::kPrimDouble: {
Nicolas Geoffray3c035032014-10-28 10:46:40 +0000787 int reg = FindTwoFreeConsecutiveAlignedEntries(blocked_fpu_registers_, kNumberOfSRegisters);
788 DCHECK_EQ(reg % 2, 0);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000789 return Location::FpuRegisterPairLocation(reg, reg + 1);
790 }
791
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100792 case Primitive::kPrimVoid:
793 LOG(FATAL) << "Unreachable type " << type;
794 }
795
Roland Levillain3b359c72015-11-17 19:35:12 +0000796 return Location::NoLocation();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100797}
798
Nicolas Geoffraya0bb2bd2015-01-26 12:49:35 +0000799void CodeGeneratorARM::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100800 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100801 blocked_register_pairs_[R1_R2] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100802
803 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100804 blocked_core_registers_[SP] = true;
805 blocked_core_registers_[LR] = true;
806 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100807
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100808 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100809 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100810
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100811 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100812 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100813
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000814 if (is_baseline) {
815 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
816 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
817 }
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000818
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000819 blocked_core_registers_[kCoreSavedRegisterForBaseline] = false;
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +0100820 }
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000821
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +0100822 if (is_baseline || GetGraph()->IsDebuggable()) {
823 // Stubs do not save callee-save floating point registers. If the graph
824 // is debuggable, we need to deal with these registers differently. For
825 // now, just block them.
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000826 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
827 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
828 }
829 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100830
831 UpdateBlockedPairRegisters();
832}
833
834void CodeGeneratorARM::UpdateBlockedPairRegisters() const {
835 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
836 ArmManagedRegister current =
837 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
838 if (blocked_core_registers_[current.AsRegisterPairLow()]
839 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
840 blocked_register_pairs_[i] = true;
841 }
842 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100843}
844
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100845InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
846 : HGraphVisitor(graph),
847 assembler_(codegen->GetAssembler()),
848 codegen_(codegen) {}
849
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000850void CodeGeneratorARM::ComputeSpillMask() {
851 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000852 // Save one extra register for baseline. Note that on thumb2, there is no easy
853 // instruction to restore just the PC, so this actually helps both baseline
854 // and non-baseline to save and restore at least two registers at entry and exit.
855 core_spill_mask_ |= (1 << kCoreSavedRegisterForBaseline);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000856 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
857 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
858 // We use vpush and vpop for saving and restoring floating point registers, which take
859 // a SRegister and the number of registers to save/restore after that SRegister. We
860 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
861 // but in the range.
862 if (fpu_spill_mask_ != 0) {
863 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
864 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
865 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
866 fpu_spill_mask_ |= (1 << i);
867 }
868 }
869}
870
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100871static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100872 return dwarf::Reg::ArmCore(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100873}
874
875static dwarf::Reg DWARFReg(SRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100876 return dwarf::Reg::ArmFp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100877}
878
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000879void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +0000880 bool skip_overflow_check =
881 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000882 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000883 __ Bind(&frame_entry_label_);
884
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000885 if (HasEmptyFrame()) {
886 return;
887 }
888
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100889 if (!skip_overflow_check) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000890 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
891 __ LoadFromOffset(kLoadWord, IP, IP, 0);
892 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100893 }
894
Andreas Gampe501fd632015-09-10 16:11:06 -0700895 __ PushList(core_spill_mask_);
896 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
897 __ cfi().RelOffsetForMany(DWARFReg(kMethodRegisterArgument), 0, core_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000898 if (fpu_spill_mask_ != 0) {
899 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
900 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100901 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
David Srbecky9d8606d2015-04-12 09:35:32 +0100902 __ cfi().RelOffsetForMany(DWARFReg(S0), 0, fpu_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000903 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100904 int adjust = GetFrameSize() - FrameEntrySpillSize();
905 __ AddConstant(SP, -adjust);
906 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100907 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, 0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000908}
909
910void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000911 if (HasEmptyFrame()) {
912 __ bx(LR);
913 return;
914 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100915 __ cfi().RememberState();
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100916 int adjust = GetFrameSize() - FrameEntrySpillSize();
917 __ AddConstant(SP, adjust);
918 __ cfi().AdjustCFAOffset(-adjust);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000919 if (fpu_spill_mask_ != 0) {
920 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
921 __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100922 __ cfi().AdjustCFAOffset(-kArmPointerSize * POPCOUNT(fpu_spill_mask_));
923 __ cfi().RestoreMany(DWARFReg(SRegister(0)), fpu_spill_mask_);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000924 }
Andreas Gampe501fd632015-09-10 16:11:06 -0700925 // Pop LR into PC to return.
926 DCHECK_NE(core_spill_mask_ & (1 << LR), 0U);
927 uint32_t pop_mask = (core_spill_mask_ & (~(1 << LR))) | 1 << PC;
928 __ PopList(pop_mask);
David Srbeckyc34dc932015-04-12 09:27:43 +0100929 __ cfi().RestoreState();
930 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000931}
932
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100933void CodeGeneratorARM::Bind(HBasicBlock* block) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -0700934 Label* label = GetLabelOf(block);
935 __ BindTrackedLabel(label);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000936}
937
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100938Location CodeGeneratorARM::GetStackLocation(HLoadLocal* load) const {
939 switch (load->GetType()) {
940 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100941 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100942 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100943
944 case Primitive::kPrimInt:
945 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100946 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100947 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100948
949 case Primitive::kPrimBoolean:
950 case Primitive::kPrimByte:
951 case Primitive::kPrimChar:
952 case Primitive::kPrimShort:
953 case Primitive::kPrimVoid:
954 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700955 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100956 }
957
958 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700959 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100960}
961
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100962Location InvokeDexCallingConventionVisitorARM::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100963 switch (type) {
964 case Primitive::kPrimBoolean:
965 case Primitive::kPrimByte:
966 case Primitive::kPrimChar:
967 case Primitive::kPrimShort:
968 case Primitive::kPrimInt:
969 case Primitive::kPrimNot: {
970 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000971 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100972 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100973 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100974 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000975 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100976 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100977 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100978
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000979 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100980 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000981 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100982 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000983 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100984 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000985 if (calling_convention.GetRegisterAt(index) == R1) {
986 // Skip R1, and use R2_R3 instead.
987 gp_index_++;
988 index++;
989 }
990 }
991 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
992 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +0000993 calling_convention.GetRegisterAt(index + 1));
Calin Juravle175dc732015-08-25 15:42:32 +0100994
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000995 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +0000996 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100997 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000998 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
999 }
1000 }
1001
1002 case Primitive::kPrimFloat: {
1003 uint32_t stack_index = stack_index_++;
1004 if (float_index_ % 2 == 0) {
1005 float_index_ = std::max(double_index_, float_index_);
1006 }
1007 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
1008 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
1009 } else {
1010 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
1011 }
1012 }
1013
1014 case Primitive::kPrimDouble: {
1015 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
1016 uint32_t stack_index = stack_index_;
1017 stack_index_ += 2;
1018 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
1019 uint32_t index = double_index_;
1020 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001021 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001022 calling_convention.GetFpuRegisterAt(index),
1023 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001024 DCHECK(ExpectedPairLayout(result));
1025 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001026 } else {
1027 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001028 }
1029 }
1030
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001031 case Primitive::kPrimVoid:
1032 LOG(FATAL) << "Unexpected parameter type " << type;
1033 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001034 }
Roland Levillain3b359c72015-11-17 19:35:12 +00001035 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001036}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001037
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001038Location InvokeDexCallingConventionVisitorARM::GetReturnLocation(Primitive::Type type) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001039 switch (type) {
1040 case Primitive::kPrimBoolean:
1041 case Primitive::kPrimByte:
1042 case Primitive::kPrimChar:
1043 case Primitive::kPrimShort:
1044 case Primitive::kPrimInt:
1045 case Primitive::kPrimNot: {
1046 return Location::RegisterLocation(R0);
1047 }
1048
1049 case Primitive::kPrimFloat: {
1050 return Location::FpuRegisterLocation(S0);
1051 }
1052
1053 case Primitive::kPrimLong: {
1054 return Location::RegisterPairLocation(R0, R1);
1055 }
1056
1057 case Primitive::kPrimDouble: {
1058 return Location::FpuRegisterPairLocation(S0, S1);
1059 }
1060
1061 case Primitive::kPrimVoid:
Roland Levillain3b359c72015-11-17 19:35:12 +00001062 return Location::NoLocation();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001063 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001064
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001065 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001066}
1067
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001068Location InvokeDexCallingConventionVisitorARM::GetMethodLocation() const {
1069 return Location::RegisterLocation(kMethodRegisterArgument);
1070}
1071
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001072void CodeGeneratorARM::Move32(Location destination, Location source) {
1073 if (source.Equals(destination)) {
1074 return;
1075 }
1076 if (destination.IsRegister()) {
1077 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001078 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001079 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001080 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001081 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001082 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001083 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001084 } else if (destination.IsFpuRegister()) {
1085 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001086 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001087 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001088 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001089 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001090 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001091 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001092 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001093 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001094 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001095 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001096 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001097 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001098 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001099 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001100 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
1101 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001102 }
1103 }
1104}
1105
1106void CodeGeneratorARM::Move64(Location destination, Location source) {
1107 if (source.Equals(destination)) {
1108 return;
1109 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001110 if (destination.IsRegisterPair()) {
1111 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001112 EmitParallelMoves(
1113 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1114 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001115 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001116 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001117 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1118 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001119 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001120 UNIMPLEMENTED(FATAL);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001121 } else if (source.IsFpuRegisterPair()) {
1122 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
1123 destination.AsRegisterPairHigh<Register>(),
1124 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001125 } else {
1126 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001127 DCHECK(ExpectedPairLayout(destination));
1128 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
1129 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001130 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001131 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001132 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001133 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1134 SP,
1135 source.GetStackIndex());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001136 } else if (source.IsRegisterPair()) {
1137 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1138 source.AsRegisterPairLow<Register>(),
1139 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001140 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001141 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001142 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001143 } else {
1144 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001145 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001146 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001147 if (source.AsRegisterPairLow<Register>() == R1) {
1148 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001149 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
1150 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001151 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001152 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001153 SP, destination.GetStackIndex());
1154 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001155 } else if (source.IsFpuRegisterPair()) {
1156 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
1157 SP,
1158 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001159 } else {
1160 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001161 EmitParallelMoves(
1162 Location::StackSlot(source.GetStackIndex()),
1163 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001164 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001165 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001166 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)),
1167 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001168 }
1169 }
1170}
1171
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001172void CodeGeneratorARM::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001173 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001174 if (instruction->IsCurrentMethod()) {
1175 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
1176 } else if (locations != nullptr && locations->Out().Equals(location)) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001177 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001178 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +00001179 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001180 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
1181 int32_t value = GetInt32ValueOf(const_to_move);
Calin Juravlea21f5982014-11-13 15:53:04 +00001182 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001183 __ LoadImmediate(location.AsRegister<Register>(), value);
Calin Juravlea21f5982014-11-13 15:53:04 +00001184 } else {
1185 DCHECK(location.IsStackSlot());
1186 __ LoadImmediate(IP, value);
1187 __ StoreToOffset(kStoreWord, IP, SP, location.GetStackIndex());
1188 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001189 } else {
Nicolas Geoffray3747b482015-01-19 17:17:16 +00001190 DCHECK(const_to_move->IsLongConstant()) << const_to_move->DebugName();
Calin Juravlea21f5982014-11-13 15:53:04 +00001191 int64_t value = const_to_move->AsLongConstant()->GetValue();
1192 if (location.IsRegisterPair()) {
1193 __ LoadImmediate(location.AsRegisterPairLow<Register>(), Low32Bits(value));
1194 __ LoadImmediate(location.AsRegisterPairHigh<Register>(), High32Bits(value));
1195 } else {
1196 DCHECK(location.IsDoubleStackSlot());
1197 __ LoadImmediate(IP, Low32Bits(value));
1198 __ StoreToOffset(kStoreWord, IP, SP, location.GetStackIndex());
1199 __ LoadImmediate(IP, High32Bits(value));
1200 __ StoreToOffset(kStoreWord, IP, SP, location.GetHighStackIndex(kArmWordSize));
1201 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001202 }
Roland Levillain476df552014-10-09 17:51:36 +01001203 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001204 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
1205 switch (instruction->GetType()) {
1206 case Primitive::kPrimBoolean:
1207 case Primitive::kPrimByte:
1208 case Primitive::kPrimChar:
1209 case Primitive::kPrimShort:
1210 case Primitive::kPrimInt:
1211 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001212 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001213 Move32(location, Location::StackSlot(stack_slot));
1214 break;
1215
1216 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001217 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001218 Move64(location, Location::DoubleStackSlot(stack_slot));
1219 break;
1220
1221 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001222 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001223 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00001224 } else if (instruction->IsTemporary()) {
1225 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001226 if (temp_location.IsStackSlot()) {
1227 Move32(location, temp_location);
1228 } else {
1229 DCHECK(temp_location.IsDoubleStackSlot());
1230 Move64(location, temp_location);
1231 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001232 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001233 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001234 switch (instruction->GetType()) {
1235 case Primitive::kPrimBoolean:
1236 case Primitive::kPrimByte:
1237 case Primitive::kPrimChar:
1238 case Primitive::kPrimShort:
1239 case Primitive::kPrimNot:
1240 case Primitive::kPrimInt:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001241 case Primitive::kPrimFloat:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001242 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001243 break;
1244
1245 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001246 case Primitive::kPrimDouble:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001247 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001248 break;
1249
1250 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001251 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001252 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001253 }
1254}
1255
Calin Juravle175dc732015-08-25 15:42:32 +01001256void CodeGeneratorARM::MoveConstant(Location location, int32_t value) {
1257 DCHECK(location.IsRegister());
1258 __ LoadImmediate(location.AsRegister<Register>(), value);
1259}
1260
Calin Juravlee460d1d2015-09-29 04:52:17 +01001261void CodeGeneratorARM::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
1262 if (Primitive::Is64BitType(dst_type)) {
1263 Move64(dst, src);
1264 } else {
1265 Move32(dst, src);
1266 }
1267}
1268
1269void CodeGeneratorARM::AddLocationAsTemp(Location location, LocationSummary* locations) {
1270 if (location.IsRegister()) {
1271 locations->AddTemp(location);
1272 } else if (location.IsRegisterPair()) {
1273 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1274 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1275 } else {
1276 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1277 }
1278}
1279
Calin Juravle175dc732015-08-25 15:42:32 +01001280void CodeGeneratorARM::InvokeRuntime(QuickEntrypointEnum entrypoint,
1281 HInstruction* instruction,
1282 uint32_t dex_pc,
1283 SlowPathCode* slow_path) {
1284 InvokeRuntime(GetThreadOffset<kArmWordSize>(entrypoint).Int32Value(),
1285 instruction,
1286 dex_pc,
1287 slow_path);
1288}
1289
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001290void CodeGeneratorARM::InvokeRuntime(int32_t entry_point_offset,
1291 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001292 uint32_t dex_pc,
1293 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001294 ValidateInvokeRuntime(instruction, slow_path);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001295 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
1296 __ blx(LR);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001297 RecordPcInfo(instruction, dex_pc, slow_path);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001298}
1299
David Brazdilfc6a86a2015-06-26 10:33:45 +00001300void InstructionCodeGeneratorARM::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001301 DCHECK(!successor->IsExitBlock());
1302
1303 HBasicBlock* block = got->GetBlock();
1304 HInstruction* previous = got->GetPrevious();
1305
1306 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001307 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001308 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1309 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1310 return;
1311 }
1312
1313 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1314 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1315 }
1316 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001317 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001318 }
1319}
1320
David Brazdilfc6a86a2015-06-26 10:33:45 +00001321void LocationsBuilderARM::VisitGoto(HGoto* got) {
1322 got->SetLocations(nullptr);
1323}
1324
1325void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
1326 HandleGoto(got, got->GetSuccessor());
1327}
1328
1329void LocationsBuilderARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1330 try_boundary->SetLocations(nullptr);
1331}
1332
1333void InstructionCodeGeneratorARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1334 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1335 if (!successor->IsExitBlock()) {
1336 HandleGoto(try_boundary, successor);
1337 }
1338}
1339
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001340void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001341 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001342}
1343
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001344void InstructionCodeGeneratorARM::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001345}
1346
Roland Levillain4fa13f62015-07-06 18:11:54 +01001347void InstructionCodeGeneratorARM::GenerateCompareWithImmediate(Register left, int32_t right) {
1348 ShifterOperand operand;
1349 if (GetAssembler()->ShifterOperandCanHold(R0, left, CMP, right, &operand)) {
1350 __ cmp(left, operand);
1351 } else {
1352 Register temp = IP;
1353 __ LoadImmediate(temp, right);
1354 __ cmp(left, ShifterOperand(temp));
1355 }
1356}
1357
1358void InstructionCodeGeneratorARM::GenerateFPJumps(HCondition* cond,
1359 Label* true_label,
1360 Label* false_label) {
1361 __ vmstat(); // transfer FP status register to ARM APSR.
Aart Bike9f37602015-10-09 11:15:55 -07001362 // TODO: merge into a single branch (except "equal or unordered" and "not equal")
Roland Levillain4fa13f62015-07-06 18:11:54 +01001363 if (cond->IsFPConditionTrueIfNaN()) {
1364 __ b(true_label, VS); // VS for unordered.
1365 } else if (cond->IsFPConditionFalseIfNaN()) {
1366 __ b(false_label, VS); // VS for unordered.
1367 }
Aart Bike9f37602015-10-09 11:15:55 -07001368 __ b(true_label, ARMCondition(cond->GetCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001369}
1370
1371void InstructionCodeGeneratorARM::GenerateLongComparesAndJumps(HCondition* cond,
1372 Label* true_label,
1373 Label* false_label) {
1374 LocationSummary* locations = cond->GetLocations();
1375 Location left = locations->InAt(0);
1376 Location right = locations->InAt(1);
1377 IfCondition if_cond = cond->GetCondition();
1378
1379 Register left_high = left.AsRegisterPairHigh<Register>();
1380 Register left_low = left.AsRegisterPairLow<Register>();
1381 IfCondition true_high_cond = if_cond;
1382 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001383 Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
Roland Levillain4fa13f62015-07-06 18:11:54 +01001384
1385 // Set the conditions for the test, remembering that == needs to be
1386 // decided using the low words.
Aart Bike9f37602015-10-09 11:15:55 -07001387 // TODO: consider avoiding jumps with temporary and CMP low+SBC high
Roland Levillain4fa13f62015-07-06 18:11:54 +01001388 switch (if_cond) {
1389 case kCondEQ:
1390 case kCondNE:
1391 // Nothing to do.
1392 break;
1393 case kCondLT:
1394 false_high_cond = kCondGT;
1395 break;
1396 case kCondLE:
1397 true_high_cond = kCondLT;
1398 break;
1399 case kCondGT:
1400 false_high_cond = kCondLT;
1401 break;
1402 case kCondGE:
1403 true_high_cond = kCondGT;
1404 break;
Aart Bike9f37602015-10-09 11:15:55 -07001405 case kCondB:
1406 false_high_cond = kCondA;
1407 break;
1408 case kCondBE:
1409 true_high_cond = kCondB;
1410 break;
1411 case kCondA:
1412 false_high_cond = kCondB;
1413 break;
1414 case kCondAE:
1415 true_high_cond = kCondA;
1416 break;
Roland Levillain4fa13f62015-07-06 18:11:54 +01001417 }
1418 if (right.IsConstant()) {
1419 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1420 int32_t val_low = Low32Bits(value);
1421 int32_t val_high = High32Bits(value);
1422
1423 GenerateCompareWithImmediate(left_high, val_high);
1424 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001425 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001426 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001427 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001428 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001429 __ b(true_label, ARMCondition(true_high_cond));
1430 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001431 }
1432 // Must be equal high, so compare the lows.
1433 GenerateCompareWithImmediate(left_low, val_low);
1434 } else {
1435 Register right_high = right.AsRegisterPairHigh<Register>();
1436 Register right_low = right.AsRegisterPairLow<Register>();
1437
1438 __ cmp(left_high, ShifterOperand(right_high));
1439 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001440 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001441 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001442 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001443 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001444 __ b(true_label, ARMCondition(true_high_cond));
1445 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001446 }
1447 // Must be equal high, so compare the lows.
1448 __ cmp(left_low, ShifterOperand(right_low));
1449 }
1450 // The last comparison might be unsigned.
Aart Bike9f37602015-10-09 11:15:55 -07001451 // TODO: optimize cases where this is always true/false
Roland Levillain4fa13f62015-07-06 18:11:54 +01001452 __ b(true_label, final_condition);
1453}
1454
David Brazdil0debae72015-11-12 18:37:00 +00001455void InstructionCodeGeneratorARM::GenerateCompareTestAndBranch(HCondition* condition,
1456 Label* true_target_in,
1457 Label* false_target_in) {
1458 // Generated branching requires both targets to be explicit. If either of the
1459 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1460 Label fallthrough_target;
1461 Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1462 Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1463
Roland Levillain4fa13f62015-07-06 18:11:54 +01001464 LocationSummary* locations = condition->GetLocations();
1465 Location left = locations->InAt(0);
1466 Location right = locations->InAt(1);
1467
Roland Levillain4fa13f62015-07-06 18:11:54 +01001468 Primitive::Type type = condition->InputAt(0)->GetType();
1469 switch (type) {
1470 case Primitive::kPrimLong:
1471 GenerateLongComparesAndJumps(condition, true_target, false_target);
1472 break;
1473 case Primitive::kPrimFloat:
1474 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
1475 GenerateFPJumps(condition, true_target, false_target);
1476 break;
1477 case Primitive::kPrimDouble:
1478 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
1479 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
1480 GenerateFPJumps(condition, true_target, false_target);
1481 break;
1482 default:
1483 LOG(FATAL) << "Unexpected compare type " << type;
1484 }
1485
David Brazdil0debae72015-11-12 18:37:00 +00001486 if (false_target != &fallthrough_target) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001487 __ b(false_target);
1488 }
David Brazdil0debae72015-11-12 18:37:00 +00001489
1490 if (fallthrough_target.IsLinked()) {
1491 __ Bind(&fallthrough_target);
1492 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001493}
1494
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001495void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001496 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001497 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00001498 Label* false_target) {
1499 HInstruction* cond = instruction->InputAt(condition_input_index);
1500
1501 if (true_target == nullptr && false_target == nullptr) {
1502 // Nothing to do. The code always falls through.
1503 return;
1504 } else if (cond->IsIntConstant()) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001505 // Constant condition, statically compared against 1.
David Brazdil0debae72015-11-12 18:37:00 +00001506 if (cond->AsIntConstant()->IsOne()) {
1507 if (true_target != nullptr) {
1508 __ b(true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001509 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001510 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001511 DCHECK(cond->AsIntConstant()->IsZero());
1512 if (false_target != nullptr) {
1513 __ b(false_target);
1514 }
1515 }
1516 return;
1517 }
1518
1519 // The following code generates these patterns:
1520 // (1) true_target == nullptr && false_target != nullptr
1521 // - opposite condition true => branch to false_target
1522 // (2) true_target != nullptr && false_target == nullptr
1523 // - condition true => branch to true_target
1524 // (3) true_target != nullptr && false_target != nullptr
1525 // - condition true => branch to true_target
1526 // - branch to false_target
1527 if (IsBooleanValueOrMaterializedCondition(cond)) {
1528 // Condition has been materialized, compare the output to 0.
1529 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
1530 DCHECK(cond_val.IsRegister());
1531 if (true_target == nullptr) {
1532 __ CompareAndBranchIfZero(cond_val.AsRegister<Register>(), false_target);
1533 } else {
1534 __ CompareAndBranchIfNonZero(cond_val.AsRegister<Register>(), true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001535 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001536 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001537 // Condition has not been materialized. Use its inputs as the comparison and
1538 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001539 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001540
1541 // If this is a long or FP comparison that has been folded into
1542 // the HCondition, generate the comparison directly.
1543 Primitive::Type type = condition->InputAt(0)->GetType();
1544 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1545 GenerateCompareTestAndBranch(condition, true_target, false_target);
1546 return;
1547 }
1548
1549 LocationSummary* locations = cond->GetLocations();
1550 DCHECK(locations->InAt(0).IsRegister());
1551 Register left = locations->InAt(0).AsRegister<Register>();
1552 Location right = locations->InAt(1);
1553 if (right.IsRegister()) {
1554 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001555 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001556 DCHECK(right.IsConstant());
1557 GenerateCompareWithImmediate(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
1558 }
1559 if (true_target == nullptr) {
1560 __ b(false_target, ARMCondition(condition->GetOppositeCondition()));
1561 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001562 __ b(true_target, ARMCondition(condition->GetCondition()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001563 }
Dave Allison20dfc792014-06-16 20:44:29 -07001564 }
David Brazdil0debae72015-11-12 18:37:00 +00001565
1566 // If neither branch falls through (case 3), the conditional branch to `true_target`
1567 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1568 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001569 __ b(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001570 }
1571}
1572
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001573void LocationsBuilderARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001574 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1575 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001576 locations->SetInAt(0, Location::RequiresRegister());
1577 }
1578}
1579
1580void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001581 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1582 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1583 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1584 nullptr : codegen_->GetLabelOf(true_successor);
1585 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1586 nullptr : codegen_->GetLabelOf(false_successor);
1587 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001588}
1589
1590void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
1591 LocationSummary* locations = new (GetGraph()->GetArena())
1592 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001593 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001594 locations->SetInAt(0, Location::RequiresRegister());
1595 }
1596}
1597
1598void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) {
David Brazdil0debae72015-11-12 18:37:00 +00001599 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DeoptimizationSlowPathARM(deoptimize);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001600 codegen_->AddSlowPath(slow_path);
David Brazdil0debae72015-11-12 18:37:00 +00001601 GenerateTestAndBranch(deoptimize,
1602 /* condition_input_index */ 0,
1603 slow_path->GetEntryLabel(),
1604 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001605}
Dave Allison20dfc792014-06-16 20:44:29 -07001606
Roland Levillain0d37cd02015-05-27 16:39:19 +01001607void LocationsBuilderARM::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001608 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001609 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001610 // Handle the long/FP comparisons made in instruction simplification.
1611 switch (cond->InputAt(0)->GetType()) {
1612 case Primitive::kPrimLong:
1613 locations->SetInAt(0, Location::RequiresRegister());
1614 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1615 if (cond->NeedsMaterialization()) {
1616 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1617 }
1618 break;
1619
1620 case Primitive::kPrimFloat:
1621 case Primitive::kPrimDouble:
1622 locations->SetInAt(0, Location::RequiresFpuRegister());
1623 locations->SetInAt(1, Location::RequiresFpuRegister());
1624 if (cond->NeedsMaterialization()) {
1625 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1626 }
1627 break;
1628
1629 default:
1630 locations->SetInAt(0, Location::RequiresRegister());
1631 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1632 if (cond->NeedsMaterialization()) {
1633 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1634 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001635 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001636}
1637
Roland Levillain0d37cd02015-05-27 16:39:19 +01001638void InstructionCodeGeneratorARM::VisitCondition(HCondition* cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001639 if (!cond->NeedsMaterialization()) {
1640 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001641 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001642
1643 LocationSummary* locations = cond->GetLocations();
1644 Location left = locations->InAt(0);
1645 Location right = locations->InAt(1);
1646 Register out = locations->Out().AsRegister<Register>();
1647 Label true_label, false_label;
1648
1649 switch (cond->InputAt(0)->GetType()) {
1650 default: {
1651 // Integer case.
1652 if (right.IsRegister()) {
1653 __ cmp(left.AsRegister<Register>(), ShifterOperand(right.AsRegister<Register>()));
1654 } else {
1655 DCHECK(right.IsConstant());
1656 GenerateCompareWithImmediate(left.AsRegister<Register>(),
1657 CodeGenerator::GetInt32ValueOf(right.GetConstant()));
1658 }
Aart Bike9f37602015-10-09 11:15:55 -07001659 __ it(ARMCondition(cond->GetCondition()), kItElse);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001660 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(1),
Aart Bike9f37602015-10-09 11:15:55 -07001661 ARMCondition(cond->GetCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001662 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(0),
Aart Bike9f37602015-10-09 11:15:55 -07001663 ARMCondition(cond->GetOppositeCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001664 return;
1665 }
1666 case Primitive::kPrimLong:
1667 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1668 break;
1669 case Primitive::kPrimFloat:
1670 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
1671 GenerateFPJumps(cond, &true_label, &false_label);
1672 break;
1673 case Primitive::kPrimDouble:
1674 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
1675 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
1676 GenerateFPJumps(cond, &true_label, &false_label);
1677 break;
1678 }
1679
1680 // Convert the jumps into the result.
1681 Label done_label;
1682
1683 // False case: result = 0.
1684 __ Bind(&false_label);
1685 __ LoadImmediate(out, 0);
1686 __ b(&done_label);
1687
1688 // True case: result = 1.
1689 __ Bind(&true_label);
1690 __ LoadImmediate(out, 1);
1691 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001692}
1693
1694void LocationsBuilderARM::VisitEqual(HEqual* comp) {
1695 VisitCondition(comp);
1696}
1697
1698void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
1699 VisitCondition(comp);
1700}
1701
1702void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
1703 VisitCondition(comp);
1704}
1705
1706void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
1707 VisitCondition(comp);
1708}
1709
1710void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
1711 VisitCondition(comp);
1712}
1713
1714void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
1715 VisitCondition(comp);
1716}
1717
1718void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1719 VisitCondition(comp);
1720}
1721
1722void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1723 VisitCondition(comp);
1724}
1725
1726void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
1727 VisitCondition(comp);
1728}
1729
1730void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
1731 VisitCondition(comp);
1732}
1733
1734void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1735 VisitCondition(comp);
1736}
1737
1738void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1739 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001740}
1741
Aart Bike9f37602015-10-09 11:15:55 -07001742void LocationsBuilderARM::VisitBelow(HBelow* comp) {
1743 VisitCondition(comp);
1744}
1745
1746void InstructionCodeGeneratorARM::VisitBelow(HBelow* comp) {
1747 VisitCondition(comp);
1748}
1749
1750void LocationsBuilderARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
1751 VisitCondition(comp);
1752}
1753
1754void InstructionCodeGeneratorARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
1755 VisitCondition(comp);
1756}
1757
1758void LocationsBuilderARM::VisitAbove(HAbove* comp) {
1759 VisitCondition(comp);
1760}
1761
1762void InstructionCodeGeneratorARM::VisitAbove(HAbove* comp) {
1763 VisitCondition(comp);
1764}
1765
1766void LocationsBuilderARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
1767 VisitCondition(comp);
1768}
1769
1770void InstructionCodeGeneratorARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
1771 VisitCondition(comp);
1772}
1773
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001774void LocationsBuilderARM::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001775 local->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001776}
1777
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001778void InstructionCodeGeneratorARM::VisitLocal(HLocal* local) {
1779 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001780}
1781
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001782void LocationsBuilderARM::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001783 load->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001784}
1785
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001786void InstructionCodeGeneratorARM::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001787 // Nothing to do, this is driven by the code generator.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001788}
1789
1790void LocationsBuilderARM::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001791 LocationSummary* locations =
1792 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001793 switch (store->InputAt(1)->GetType()) {
1794 case Primitive::kPrimBoolean:
1795 case Primitive::kPrimByte:
1796 case Primitive::kPrimChar:
1797 case Primitive::kPrimShort:
1798 case Primitive::kPrimInt:
1799 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001800 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001801 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1802 break;
1803
1804 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001805 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001806 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1807 break;
1808
1809 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001810 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001811 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001812}
1813
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001814void InstructionCodeGeneratorARM::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001815}
1816
1817void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001818 LocationSummary* locations =
1819 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001820 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001821}
1822
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001823void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001824 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001825}
1826
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001827void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
1828 LocationSummary* locations =
1829 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1830 locations->SetOut(Location::ConstantLocation(constant));
1831}
1832
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001833void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001834 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001835}
1836
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001837void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001838 LocationSummary* locations =
1839 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001840 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001841}
1842
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001843void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001844 // Will be generated at use site.
1845}
1846
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001847void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
1848 LocationSummary* locations =
1849 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1850 locations->SetOut(Location::ConstantLocation(constant));
1851}
1852
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001853void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001854 // Will be generated at use site.
1855}
1856
1857void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
1858 LocationSummary* locations =
1859 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1860 locations->SetOut(Location::ConstantLocation(constant));
1861}
1862
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001863void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001864 // Will be generated at use site.
1865}
1866
Calin Juravle27df7582015-04-17 19:12:31 +01001867void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1868 memory_barrier->SetLocations(nullptr);
1869}
1870
1871void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1872 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1873}
1874
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001875void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001876 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001877}
1878
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001879void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001880 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001881}
1882
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001883void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001884 LocationSummary* locations =
1885 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001886 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001887}
1888
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001889void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001890 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001891}
1892
Calin Juravle175dc732015-08-25 15:42:32 +01001893void LocationsBuilderARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1894 // The trampoline uses the same calling convention as dex calling conventions,
1895 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1896 // the method_idx.
1897 HandleInvoke(invoke);
1898}
1899
1900void InstructionCodeGeneratorARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1901 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1902}
1903
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001904void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001905 // When we do not run baseline, explicit clinit checks triggered by static
1906 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1907 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001908
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001909 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001910 codegen_->GetAssembler(),
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001911 codegen_->GetInstructionSetFeatures());
1912 if (intrinsic.TryDispatch(invoke)) {
1913 return;
1914 }
1915
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001916 HandleInvoke(invoke);
1917}
1918
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001919static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
1920 if (invoke->GetLocations()->Intrinsified()) {
1921 IntrinsicCodeGeneratorARM intrinsic(codegen);
1922 intrinsic.Dispatch(invoke);
1923 return true;
1924 }
1925 return false;
1926}
1927
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001928void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001929 // When we do not run baseline, explicit clinit checks triggered by static
1930 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1931 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001932
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001933 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1934 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001935 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001936
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001937 LocationSummary* locations = invoke->GetLocations();
1938 codegen_->GenerateStaticOrDirectCall(
1939 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001940 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001941}
1942
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001943void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001944 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001945 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001946}
1947
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001948void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001949 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001950 codegen_->GetAssembler(),
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001951 codegen_->GetInstructionSetFeatures());
1952 if (intrinsic.TryDispatch(invoke)) {
1953 return;
1954 }
1955
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001956 HandleInvoke(invoke);
1957}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001958
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001959void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001960 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1961 return;
1962 }
1963
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001964 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001965 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001966 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001967}
1968
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001969void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1970 HandleInvoke(invoke);
1971 // Add the hidden argument.
1972 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
1973}
1974
1975void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1976 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain3b359c72015-11-17 19:35:12 +00001977 LocationSummary* locations = invoke->GetLocations();
1978 Register temp = locations->GetTemp(0).AsRegister<Register>();
1979 Register hidden_reg = locations->GetTemp(1).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001980 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1981 invoke->GetImtIndex() % mirror::Class::kImtSize, kArmPointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001982 Location receiver = locations->InAt(0);
1983 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1984
Roland Levillain3b359c72015-11-17 19:35:12 +00001985 // Set the hidden argument. This is safe to do this here, as R12
1986 // won't be modified thereafter, before the `blx` (call) instruction.
1987 DCHECK_EQ(R12, hidden_reg);
1988 __ LoadImmediate(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001989
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001990 if (receiver.IsStackSlot()) {
1991 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
Roland Levillain3b359c72015-11-17 19:35:12 +00001992 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001993 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
1994 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00001995 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00001996 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001997 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001998 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00001999 // Instead of simply (possibly) unpoisoning `temp` here, we should
2000 // emit a read barrier for the previous class reference load.
2001 // However this is not required in practice, as this is an
2002 // intermediate/temporary reference and because the current
2003 // concurrent copying collector keeps the from-space memory
2004 // intact/accessible until the end of the marking phase (the
2005 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002006 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002007 // temp = temp->GetImtEntryAt(method_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00002008 uint32_t entry_point =
2009 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002010 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
2011 // LR = temp->GetEntryPoint();
2012 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
2013 // LR();
2014 __ blx(LR);
2015 DCHECK(!codegen_->IsLeafMethod());
2016 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2017}
2018
Roland Levillain88cb1752014-10-20 16:36:47 +01002019void LocationsBuilderARM::VisitNeg(HNeg* neg) {
2020 LocationSummary* locations =
2021 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2022 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002023 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01002024 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002025 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2026 break;
2027 }
2028 case Primitive::kPrimLong: {
2029 locations->SetInAt(0, Location::RequiresRegister());
2030 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01002031 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002032 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002033
Roland Levillain88cb1752014-10-20 16:36:47 +01002034 case Primitive::kPrimFloat:
2035 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002036 locations->SetInAt(0, Location::RequiresFpuRegister());
2037 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01002038 break;
2039
2040 default:
2041 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2042 }
2043}
2044
2045void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
2046 LocationSummary* locations = neg->GetLocations();
2047 Location out = locations->Out();
2048 Location in = locations->InAt(0);
2049 switch (neg->GetResultType()) {
2050 case Primitive::kPrimInt:
2051 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002052 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01002053 break;
2054
2055 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002056 DCHECK(in.IsRegisterPair());
2057 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
2058 __ rsbs(out.AsRegisterPairLow<Register>(),
2059 in.AsRegisterPairLow<Register>(),
2060 ShifterOperand(0));
2061 // We cannot emit an RSC (Reverse Subtract with Carry)
2062 // instruction here, as it does not exist in the Thumb-2
2063 // instruction set. We use the following approach
2064 // using SBC and SUB instead.
2065 //
2066 // out.hi = -C
2067 __ sbc(out.AsRegisterPairHigh<Register>(),
2068 out.AsRegisterPairHigh<Register>(),
2069 ShifterOperand(out.AsRegisterPairHigh<Register>()));
2070 // out.hi = out.hi - in.hi
2071 __ sub(out.AsRegisterPairHigh<Register>(),
2072 out.AsRegisterPairHigh<Register>(),
2073 ShifterOperand(in.AsRegisterPairHigh<Register>()));
2074 break;
2075
Roland Levillain88cb1752014-10-20 16:36:47 +01002076 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002077 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002078 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002079 break;
2080
Roland Levillain88cb1752014-10-20 16:36:47 +01002081 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002082 DCHECK(in.IsFpuRegisterPair());
2083 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2084 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01002085 break;
2086
2087 default:
2088 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2089 }
2090}
2091
Roland Levillaindff1f282014-11-05 14:15:05 +00002092void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002093 Primitive::Type result_type = conversion->GetResultType();
2094 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002095 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002096
Roland Levillain5b3ee562015-04-14 16:02:41 +01002097 // The float-to-long, double-to-long and long-to-float type conversions
2098 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002099 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01002100 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2101 && result_type == Primitive::kPrimLong)
2102 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Roland Levillain624279f2014-12-04 11:54:28 +00002103 ? LocationSummary::kCall
2104 : LocationSummary::kNoCall;
2105 LocationSummary* locations =
2106 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2107
David Brazdilb2bd1c52015-03-25 11:17:37 +00002108 // The Java language does not allow treating boolean as an integral type but
2109 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002110
Roland Levillaindff1f282014-11-05 14:15:05 +00002111 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002112 case Primitive::kPrimByte:
2113 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002114 case Primitive::kPrimBoolean:
2115 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002116 case Primitive::kPrimShort:
2117 case Primitive::kPrimInt:
2118 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002119 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002120 locations->SetInAt(0, Location::RequiresRegister());
2121 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2122 break;
2123
2124 default:
2125 LOG(FATAL) << "Unexpected type conversion from " << input_type
2126 << " to " << result_type;
2127 }
2128 break;
2129
Roland Levillain01a8d712014-11-14 16:27:39 +00002130 case Primitive::kPrimShort:
2131 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002132 case Primitive::kPrimBoolean:
2133 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002134 case Primitive::kPrimByte:
2135 case Primitive::kPrimInt:
2136 case Primitive::kPrimChar:
2137 // Processing a Dex `int-to-short' instruction.
2138 locations->SetInAt(0, Location::RequiresRegister());
2139 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2140 break;
2141
2142 default:
2143 LOG(FATAL) << "Unexpected type conversion from " << input_type
2144 << " to " << result_type;
2145 }
2146 break;
2147
Roland Levillain946e1432014-11-11 17:35:19 +00002148 case Primitive::kPrimInt:
2149 switch (input_type) {
2150 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002151 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002152 locations->SetInAt(0, Location::Any());
2153 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2154 break;
2155
2156 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002157 // Processing a Dex `float-to-int' instruction.
2158 locations->SetInAt(0, Location::RequiresFpuRegister());
2159 locations->SetOut(Location::RequiresRegister());
2160 locations->AddTemp(Location::RequiresFpuRegister());
2161 break;
2162
Roland Levillain946e1432014-11-11 17:35:19 +00002163 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002164 // Processing a Dex `double-to-int' instruction.
2165 locations->SetInAt(0, Location::RequiresFpuRegister());
2166 locations->SetOut(Location::RequiresRegister());
2167 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002168 break;
2169
2170 default:
2171 LOG(FATAL) << "Unexpected type conversion from " << input_type
2172 << " to " << result_type;
2173 }
2174 break;
2175
Roland Levillaindff1f282014-11-05 14:15:05 +00002176 case Primitive::kPrimLong:
2177 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002178 case Primitive::kPrimBoolean:
2179 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002180 case Primitive::kPrimByte:
2181 case Primitive::kPrimShort:
2182 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002183 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002184 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002185 locations->SetInAt(0, Location::RequiresRegister());
2186 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2187 break;
2188
Roland Levillain624279f2014-12-04 11:54:28 +00002189 case Primitive::kPrimFloat: {
2190 // Processing a Dex `float-to-long' instruction.
2191 InvokeRuntimeCallingConvention calling_convention;
2192 locations->SetInAt(0, Location::FpuRegisterLocation(
2193 calling_convention.GetFpuRegisterAt(0)));
2194 locations->SetOut(Location::RegisterPairLocation(R0, R1));
2195 break;
2196 }
2197
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002198 case Primitive::kPrimDouble: {
2199 // Processing a Dex `double-to-long' instruction.
2200 InvokeRuntimeCallingConvention calling_convention;
2201 locations->SetInAt(0, Location::FpuRegisterPairLocation(
2202 calling_convention.GetFpuRegisterAt(0),
2203 calling_convention.GetFpuRegisterAt(1)));
2204 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00002205 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002206 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002207
2208 default:
2209 LOG(FATAL) << "Unexpected type conversion from " << input_type
2210 << " to " << result_type;
2211 }
2212 break;
2213
Roland Levillain981e4542014-11-14 11:47:14 +00002214 case Primitive::kPrimChar:
2215 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002216 case Primitive::kPrimBoolean:
2217 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002218 case Primitive::kPrimByte:
2219 case Primitive::kPrimShort:
2220 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002221 // Processing a Dex `int-to-char' instruction.
2222 locations->SetInAt(0, Location::RequiresRegister());
2223 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2224 break;
2225
2226 default:
2227 LOG(FATAL) << "Unexpected type conversion from " << input_type
2228 << " to " << result_type;
2229 }
2230 break;
2231
Roland Levillaindff1f282014-11-05 14:15:05 +00002232 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002233 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002234 case Primitive::kPrimBoolean:
2235 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002236 case Primitive::kPrimByte:
2237 case Primitive::kPrimShort:
2238 case Primitive::kPrimInt:
2239 case Primitive::kPrimChar:
2240 // Processing a Dex `int-to-float' instruction.
2241 locations->SetInAt(0, Location::RequiresRegister());
2242 locations->SetOut(Location::RequiresFpuRegister());
2243 break;
2244
Roland Levillain5b3ee562015-04-14 16:02:41 +01002245 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00002246 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002247 InvokeRuntimeCallingConvention calling_convention;
2248 locations->SetInAt(0, Location::RegisterPairLocation(
2249 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2250 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00002251 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01002252 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002253
Roland Levillaincff13742014-11-17 14:32:17 +00002254 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002255 // Processing a Dex `double-to-float' instruction.
2256 locations->SetInAt(0, Location::RequiresFpuRegister());
2257 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002258 break;
2259
2260 default:
2261 LOG(FATAL) << "Unexpected type conversion from " << input_type
2262 << " to " << result_type;
2263 };
2264 break;
2265
Roland Levillaindff1f282014-11-05 14:15:05 +00002266 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002267 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002268 case Primitive::kPrimBoolean:
2269 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002270 case Primitive::kPrimByte:
2271 case Primitive::kPrimShort:
2272 case Primitive::kPrimInt:
2273 case Primitive::kPrimChar:
2274 // Processing a Dex `int-to-double' instruction.
2275 locations->SetInAt(0, Location::RequiresRegister());
2276 locations->SetOut(Location::RequiresFpuRegister());
2277 break;
2278
2279 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002280 // Processing a Dex `long-to-double' instruction.
2281 locations->SetInAt(0, Location::RequiresRegister());
2282 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01002283 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002284 locations->AddTemp(Location::RequiresFpuRegister());
2285 break;
2286
Roland Levillaincff13742014-11-17 14:32:17 +00002287 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002288 // Processing a Dex `float-to-double' instruction.
2289 locations->SetInAt(0, Location::RequiresFpuRegister());
2290 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002291 break;
2292
2293 default:
2294 LOG(FATAL) << "Unexpected type conversion from " << input_type
2295 << " to " << result_type;
2296 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002297 break;
2298
2299 default:
2300 LOG(FATAL) << "Unexpected type conversion from " << input_type
2301 << " to " << result_type;
2302 }
2303}
2304
2305void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
2306 LocationSummary* locations = conversion->GetLocations();
2307 Location out = locations->Out();
2308 Location in = locations->InAt(0);
2309 Primitive::Type result_type = conversion->GetResultType();
2310 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002311 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002312 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002313 case Primitive::kPrimByte:
2314 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002315 case Primitive::kPrimBoolean:
2316 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002317 case Primitive::kPrimShort:
2318 case Primitive::kPrimInt:
2319 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002320 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002321 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002322 break;
2323
2324 default:
2325 LOG(FATAL) << "Unexpected type conversion from " << input_type
2326 << " to " << result_type;
2327 }
2328 break;
2329
Roland Levillain01a8d712014-11-14 16:27:39 +00002330 case Primitive::kPrimShort:
2331 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002332 case Primitive::kPrimBoolean:
2333 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002334 case Primitive::kPrimByte:
2335 case Primitive::kPrimInt:
2336 case Primitive::kPrimChar:
2337 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002338 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00002339 break;
2340
2341 default:
2342 LOG(FATAL) << "Unexpected type conversion from " << input_type
2343 << " to " << result_type;
2344 }
2345 break;
2346
Roland Levillain946e1432014-11-11 17:35:19 +00002347 case Primitive::kPrimInt:
2348 switch (input_type) {
2349 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002350 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002351 DCHECK(out.IsRegister());
2352 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002353 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002354 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002355 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00002356 } else {
2357 DCHECK(in.IsConstant());
2358 DCHECK(in.GetConstant()->IsLongConstant());
2359 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002360 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00002361 }
2362 break;
2363
Roland Levillain3f8f9362014-12-02 17:45:01 +00002364 case Primitive::kPrimFloat: {
2365 // Processing a Dex `float-to-int' instruction.
2366 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
2367 __ vmovs(temp, in.AsFpuRegister<SRegister>());
2368 __ vcvtis(temp, temp);
2369 __ vmovrs(out.AsRegister<Register>(), temp);
2370 break;
2371 }
2372
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002373 case Primitive::kPrimDouble: {
2374 // Processing a Dex `double-to-int' instruction.
2375 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
2376 DRegister temp_d = FromLowSToD(temp_s);
2377 __ vmovd(temp_d, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
2378 __ vcvtid(temp_s, temp_d);
2379 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00002380 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002381 }
Roland Levillain946e1432014-11-11 17:35:19 +00002382
2383 default:
2384 LOG(FATAL) << "Unexpected type conversion from " << input_type
2385 << " to " << result_type;
2386 }
2387 break;
2388
Roland Levillaindff1f282014-11-05 14:15:05 +00002389 case Primitive::kPrimLong:
2390 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002391 case Primitive::kPrimBoolean:
2392 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002393 case Primitive::kPrimByte:
2394 case Primitive::kPrimShort:
2395 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002396 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002397 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002398 DCHECK(out.IsRegisterPair());
2399 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002400 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002401 // Sign extension.
2402 __ Asr(out.AsRegisterPairHigh<Register>(),
2403 out.AsRegisterPairLow<Register>(),
2404 31);
2405 break;
2406
2407 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002408 // Processing a Dex `float-to-long' instruction.
Roland Levillain624279f2014-12-04 11:54:28 +00002409 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2410 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002411 conversion->GetDexPc(),
2412 nullptr);
Roland Levillain624279f2014-12-04 11:54:28 +00002413 break;
2414
Roland Levillaindff1f282014-11-05 14:15:05 +00002415 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002416 // Processing a Dex `double-to-long' instruction.
2417 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2418 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002419 conversion->GetDexPc(),
2420 nullptr);
Roland Levillaindff1f282014-11-05 14:15:05 +00002421 break;
2422
2423 default:
2424 LOG(FATAL) << "Unexpected type conversion from " << input_type
2425 << " to " << result_type;
2426 }
2427 break;
2428
Roland Levillain981e4542014-11-14 11:47:14 +00002429 case Primitive::kPrimChar:
2430 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002431 case Primitive::kPrimBoolean:
2432 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002433 case Primitive::kPrimByte:
2434 case Primitive::kPrimShort:
2435 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002436 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002437 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00002438 break;
2439
2440 default:
2441 LOG(FATAL) << "Unexpected type conversion from " << input_type
2442 << " to " << result_type;
2443 }
2444 break;
2445
Roland Levillaindff1f282014-11-05 14:15:05 +00002446 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002447 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002448 case Primitive::kPrimBoolean:
2449 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002450 case Primitive::kPrimByte:
2451 case Primitive::kPrimShort:
2452 case Primitive::kPrimInt:
2453 case Primitive::kPrimChar: {
2454 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002455 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
2456 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002457 break;
2458 }
2459
Roland Levillain5b3ee562015-04-14 16:02:41 +01002460 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002461 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002462 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pL2f),
2463 conversion,
2464 conversion->GetDexPc(),
2465 nullptr);
Roland Levillain6d0e4832014-11-27 18:31:21 +00002466 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002467
Roland Levillaincff13742014-11-17 14:32:17 +00002468 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002469 // Processing a Dex `double-to-float' instruction.
2470 __ vcvtsd(out.AsFpuRegister<SRegister>(),
2471 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00002472 break;
2473
2474 default:
2475 LOG(FATAL) << "Unexpected type conversion from " << input_type
2476 << " to " << result_type;
2477 };
2478 break;
2479
Roland Levillaindff1f282014-11-05 14:15:05 +00002480 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002481 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002482 case Primitive::kPrimBoolean:
2483 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002484 case Primitive::kPrimByte:
2485 case Primitive::kPrimShort:
2486 case Primitive::kPrimInt:
2487 case Primitive::kPrimChar: {
2488 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002489 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002490 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2491 out.AsFpuRegisterPairLow<SRegister>());
2492 break;
2493 }
2494
Roland Levillain647b9ed2014-11-27 12:06:00 +00002495 case Primitive::kPrimLong: {
2496 // Processing a Dex `long-to-double' instruction.
2497 Register low = in.AsRegisterPairLow<Register>();
2498 Register high = in.AsRegisterPairHigh<Register>();
2499 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
2500 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002501 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00002502 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002503 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
2504 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002505
Roland Levillain682393c2015-04-14 15:57:52 +01002506 // temp_d = int-to-double(high)
2507 __ vmovsr(temp_s, high);
2508 __ vcvtdi(temp_d, temp_s);
2509 // constant_d = k2Pow32EncodingForDouble
2510 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
2511 // out_d = unsigned-to-double(low)
2512 __ vmovsr(out_s, low);
2513 __ vcvtdu(out_d, out_s);
2514 // out_d += temp_d * constant_d
2515 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002516 break;
2517 }
2518
Roland Levillaincff13742014-11-17 14:32:17 +00002519 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002520 // Processing a Dex `float-to-double' instruction.
2521 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2522 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002523 break;
2524
2525 default:
2526 LOG(FATAL) << "Unexpected type conversion from " << input_type
2527 << " to " << result_type;
2528 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002529 break;
2530
2531 default:
2532 LOG(FATAL) << "Unexpected type conversion from " << input_type
2533 << " to " << result_type;
2534 }
2535}
2536
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002537void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002538 LocationSummary* locations =
2539 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002540 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002541 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002542 locations->SetInAt(0, Location::RequiresRegister());
2543 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002544 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2545 break;
2546 }
2547
2548 case Primitive::kPrimLong: {
2549 locations->SetInAt(0, Location::RequiresRegister());
2550 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002551 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002552 break;
2553 }
2554
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002555 case Primitive::kPrimFloat:
2556 case Primitive::kPrimDouble: {
2557 locations->SetInAt(0, Location::RequiresFpuRegister());
2558 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002559 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002560 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002561 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002562
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002563 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002564 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002565 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002566}
2567
2568void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
2569 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002570 Location out = locations->Out();
2571 Location first = locations->InAt(0);
2572 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002573 switch (add->GetResultType()) {
2574 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002575 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002576 __ add(out.AsRegister<Register>(),
2577 first.AsRegister<Register>(),
2578 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002579 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002580 __ AddConstant(out.AsRegister<Register>(),
2581 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002582 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002583 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002584 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002585
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002586 case Primitive::kPrimLong: {
2587 DCHECK(second.IsRegisterPair());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002588 __ adds(out.AsRegisterPairLow<Register>(),
2589 first.AsRegisterPairLow<Register>(),
2590 ShifterOperand(second.AsRegisterPairLow<Register>()));
2591 __ adc(out.AsRegisterPairHigh<Register>(),
2592 first.AsRegisterPairHigh<Register>(),
2593 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002594 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002595 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002596
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002597 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00002598 __ vadds(out.AsFpuRegister<SRegister>(),
2599 first.AsFpuRegister<SRegister>(),
2600 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002601 break;
2602
2603 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002604 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2605 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2606 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002607 break;
2608
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002609 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002610 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002611 }
2612}
2613
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002614void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002615 LocationSummary* locations =
2616 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002617 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002618 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002619 locations->SetInAt(0, Location::RequiresRegister());
2620 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002621 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2622 break;
2623 }
2624
2625 case Primitive::kPrimLong: {
2626 locations->SetInAt(0, Location::RequiresRegister());
2627 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002628 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002629 break;
2630 }
Calin Juravle11351682014-10-23 15:38:15 +01002631 case Primitive::kPrimFloat:
2632 case Primitive::kPrimDouble: {
2633 locations->SetInAt(0, Location::RequiresFpuRegister());
2634 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002635 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002636 break;
Calin Juravle11351682014-10-23 15:38:15 +01002637 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002638 default:
Calin Juravle11351682014-10-23 15:38:15 +01002639 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002640 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002641}
2642
2643void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
2644 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002645 Location out = locations->Out();
2646 Location first = locations->InAt(0);
2647 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002648 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002649 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002650 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002651 __ sub(out.AsRegister<Register>(),
2652 first.AsRegister<Register>(),
2653 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002654 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002655 __ AddConstant(out.AsRegister<Register>(),
2656 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01002657 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002658 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002659 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002660 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002661
Calin Juravle11351682014-10-23 15:38:15 +01002662 case Primitive::kPrimLong: {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002663 DCHECK(second.IsRegisterPair());
Calin Juravle11351682014-10-23 15:38:15 +01002664 __ subs(out.AsRegisterPairLow<Register>(),
2665 first.AsRegisterPairLow<Register>(),
2666 ShifterOperand(second.AsRegisterPairLow<Register>()));
2667 __ sbc(out.AsRegisterPairHigh<Register>(),
2668 first.AsRegisterPairHigh<Register>(),
2669 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002670 break;
Calin Juravle11351682014-10-23 15:38:15 +01002671 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002672
Calin Juravle11351682014-10-23 15:38:15 +01002673 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002674 __ vsubs(out.AsFpuRegister<SRegister>(),
2675 first.AsFpuRegister<SRegister>(),
2676 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002677 break;
Calin Juravle11351682014-10-23 15:38:15 +01002678 }
2679
2680 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002681 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2682 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2683 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01002684 break;
2685 }
2686
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002687
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002688 default:
Calin Juravle11351682014-10-23 15:38:15 +01002689 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002690 }
2691}
2692
Calin Juravle34bacdf2014-10-07 20:23:36 +01002693void LocationsBuilderARM::VisitMul(HMul* mul) {
2694 LocationSummary* locations =
2695 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2696 switch (mul->GetResultType()) {
2697 case Primitive::kPrimInt:
2698 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002699 locations->SetInAt(0, Location::RequiresRegister());
2700 locations->SetInAt(1, Location::RequiresRegister());
2701 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002702 break;
2703 }
2704
Calin Juravleb5bfa962014-10-21 18:02:24 +01002705 case Primitive::kPrimFloat:
2706 case Primitive::kPrimDouble: {
2707 locations->SetInAt(0, Location::RequiresFpuRegister());
2708 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002709 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002710 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002711 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002712
2713 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002714 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002715 }
2716}
2717
2718void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
2719 LocationSummary* locations = mul->GetLocations();
2720 Location out = locations->Out();
2721 Location first = locations->InAt(0);
2722 Location second = locations->InAt(1);
2723 switch (mul->GetResultType()) {
2724 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00002725 __ mul(out.AsRegister<Register>(),
2726 first.AsRegister<Register>(),
2727 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002728 break;
2729 }
2730 case Primitive::kPrimLong: {
2731 Register out_hi = out.AsRegisterPairHigh<Register>();
2732 Register out_lo = out.AsRegisterPairLow<Register>();
2733 Register in1_hi = first.AsRegisterPairHigh<Register>();
2734 Register in1_lo = first.AsRegisterPairLow<Register>();
2735 Register in2_hi = second.AsRegisterPairHigh<Register>();
2736 Register in2_lo = second.AsRegisterPairLow<Register>();
2737
2738 // Extra checks to protect caused by the existence of R1_R2.
2739 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
2740 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
2741 DCHECK_NE(out_hi, in1_lo);
2742 DCHECK_NE(out_hi, in2_lo);
2743
2744 // input: in1 - 64 bits, in2 - 64 bits
2745 // output: out
2746 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2747 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2748 // parts: out.lo = (in1.lo * in2.lo)[31:0]
2749
2750 // IP <- in1.lo * in2.hi
2751 __ mul(IP, in1_lo, in2_hi);
2752 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2753 __ mla(out_hi, in1_hi, in2_lo, IP);
2754 // out.lo <- (in1.lo * in2.lo)[31:0];
2755 __ umull(out_lo, IP, in1_lo, in2_lo);
2756 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2757 __ add(out_hi, out_hi, ShifterOperand(IP));
2758 break;
2759 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002760
2761 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002762 __ vmuls(out.AsFpuRegister<SRegister>(),
2763 first.AsFpuRegister<SRegister>(),
2764 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002765 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002766 }
2767
2768 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002769 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2770 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2771 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01002772 break;
2773 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002774
2775 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002776 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002777 }
2778}
2779
Zheng Xuc6667102015-05-15 16:08:45 +08002780void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2781 DCHECK(instruction->IsDiv() || instruction->IsRem());
2782 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2783
2784 LocationSummary* locations = instruction->GetLocations();
2785 Location second = locations->InAt(1);
2786 DCHECK(second.IsConstant());
2787
2788 Register out = locations->Out().AsRegister<Register>();
2789 Register dividend = locations->InAt(0).AsRegister<Register>();
2790 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2791 DCHECK(imm == 1 || imm == -1);
2792
2793 if (instruction->IsRem()) {
2794 __ LoadImmediate(out, 0);
2795 } else {
2796 if (imm == 1) {
2797 __ Mov(out, dividend);
2798 } else {
2799 __ rsb(out, dividend, ShifterOperand(0));
2800 }
2801 }
2802}
2803
2804void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2805 DCHECK(instruction->IsDiv() || instruction->IsRem());
2806 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2807
2808 LocationSummary* locations = instruction->GetLocations();
2809 Location second = locations->InAt(1);
2810 DCHECK(second.IsConstant());
2811
2812 Register out = locations->Out().AsRegister<Register>();
2813 Register dividend = locations->InAt(0).AsRegister<Register>();
2814 Register temp = locations->GetTemp(0).AsRegister<Register>();
2815 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Vladimir Marko80afd022015-05-19 18:08:00 +01002816 uint32_t abs_imm = static_cast<uint32_t>(std::abs(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002817 DCHECK(IsPowerOfTwo(abs_imm));
2818 int ctz_imm = CTZ(abs_imm);
2819
2820 if (ctz_imm == 1) {
2821 __ Lsr(temp, dividend, 32 - ctz_imm);
2822 } else {
2823 __ Asr(temp, dividend, 31);
2824 __ Lsr(temp, temp, 32 - ctz_imm);
2825 }
2826 __ add(out, temp, ShifterOperand(dividend));
2827
2828 if (instruction->IsDiv()) {
2829 __ Asr(out, out, ctz_imm);
2830 if (imm < 0) {
2831 __ rsb(out, out, ShifterOperand(0));
2832 }
2833 } else {
2834 __ ubfx(out, out, 0, ctz_imm);
2835 __ sub(out, out, ShifterOperand(temp));
2836 }
2837}
2838
2839void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2840 DCHECK(instruction->IsDiv() || instruction->IsRem());
2841 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2842
2843 LocationSummary* locations = instruction->GetLocations();
2844 Location second = locations->InAt(1);
2845 DCHECK(second.IsConstant());
2846
2847 Register out = locations->Out().AsRegister<Register>();
2848 Register dividend = locations->InAt(0).AsRegister<Register>();
2849 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2850 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
2851 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2852
2853 int64_t magic;
2854 int shift;
2855 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2856
2857 __ LoadImmediate(temp1, magic);
2858 __ smull(temp2, temp1, dividend, temp1);
2859
2860 if (imm > 0 && magic < 0) {
2861 __ add(temp1, temp1, ShifterOperand(dividend));
2862 } else if (imm < 0 && magic > 0) {
2863 __ sub(temp1, temp1, ShifterOperand(dividend));
2864 }
2865
2866 if (shift != 0) {
2867 __ Asr(temp1, temp1, shift);
2868 }
2869
2870 if (instruction->IsDiv()) {
2871 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
2872 } else {
2873 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
2874 // TODO: Strength reduction for mls.
2875 __ LoadImmediate(temp2, imm);
2876 __ mls(out, temp1, temp2, dividend);
2877 }
2878}
2879
2880void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
2881 DCHECK(instruction->IsDiv() || instruction->IsRem());
2882 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2883
2884 LocationSummary* locations = instruction->GetLocations();
2885 Location second = locations->InAt(1);
2886 DCHECK(second.IsConstant());
2887
2888 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2889 if (imm == 0) {
2890 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2891 } else if (imm == 1 || imm == -1) {
2892 DivRemOneOrMinusOne(instruction);
2893 } else if (IsPowerOfTwo(std::abs(imm))) {
2894 DivRemByPowerOfTwo(instruction);
2895 } else {
2896 DCHECK(imm <= -2 || imm >= 2);
2897 GenerateDivRemWithAnyConstant(instruction);
2898 }
2899}
2900
Calin Juravle7c4954d2014-10-28 16:57:40 +00002901void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002902 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2903 if (div->GetResultType() == Primitive::kPrimLong) {
2904 // pLdiv runtime call.
2905 call_kind = LocationSummary::kCall;
Zheng Xuc6667102015-05-15 16:08:45 +08002906 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
2907 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002908 } else if (div->GetResultType() == Primitive::kPrimInt &&
2909 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
2910 // pIdivmod runtime call.
2911 call_kind = LocationSummary::kCall;
2912 }
2913
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002914 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2915
Calin Juravle7c4954d2014-10-28 16:57:40 +00002916 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002917 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002918 if (div->InputAt(1)->IsConstant()) {
2919 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00002920 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08002921 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2922 int32_t abs_imm = std::abs(div->InputAt(1)->AsIntConstant()->GetValue());
2923 if (abs_imm <= 1) {
2924 // No temp register required.
2925 } else {
2926 locations->AddTemp(Location::RequiresRegister());
2927 if (!IsPowerOfTwo(abs_imm)) {
2928 locations->AddTemp(Location::RequiresRegister());
2929 }
2930 }
2931 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002932 locations->SetInAt(0, Location::RequiresRegister());
2933 locations->SetInAt(1, Location::RequiresRegister());
2934 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2935 } else {
2936 InvokeRuntimeCallingConvention calling_convention;
2937 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2938 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2939 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
2940 // we only need the former.
2941 locations->SetOut(Location::RegisterLocation(R0));
2942 }
Calin Juravled0d48522014-11-04 16:40:20 +00002943 break;
2944 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002945 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002946 InvokeRuntimeCallingConvention calling_convention;
2947 locations->SetInAt(0, Location::RegisterPairLocation(
2948 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2949 locations->SetInAt(1, Location::RegisterPairLocation(
2950 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002951 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002952 break;
2953 }
2954 case Primitive::kPrimFloat:
2955 case Primitive::kPrimDouble: {
2956 locations->SetInAt(0, Location::RequiresFpuRegister());
2957 locations->SetInAt(1, Location::RequiresFpuRegister());
2958 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2959 break;
2960 }
2961
2962 default:
2963 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2964 }
2965}
2966
2967void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
2968 LocationSummary* locations = div->GetLocations();
2969 Location out = locations->Out();
2970 Location first = locations->InAt(0);
2971 Location second = locations->InAt(1);
2972
2973 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002974 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002975 if (second.IsConstant()) {
2976 GenerateDivRemConstantIntegral(div);
2977 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002978 __ sdiv(out.AsRegister<Register>(),
2979 first.AsRegister<Register>(),
2980 second.AsRegister<Register>());
2981 } else {
2982 InvokeRuntimeCallingConvention calling_convention;
2983 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
2984 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
2985 DCHECK_EQ(R0, out.AsRegister<Register>());
2986
2987 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), div, div->GetDexPc(), nullptr);
2988 }
Calin Juravled0d48522014-11-04 16:40:20 +00002989 break;
2990 }
2991
Calin Juravle7c4954d2014-10-28 16:57:40 +00002992 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002993 InvokeRuntimeCallingConvention calling_convention;
2994 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2995 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2996 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2997 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2998 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002999 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003000
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003001 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv), div, div->GetDexPc(), nullptr);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003002 break;
3003 }
3004
3005 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003006 __ vdivs(out.AsFpuRegister<SRegister>(),
3007 first.AsFpuRegister<SRegister>(),
3008 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003009 break;
3010 }
3011
3012 case Primitive::kPrimDouble: {
3013 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3014 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3015 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
3016 break;
3017 }
3018
3019 default:
3020 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3021 }
3022}
3023
Calin Juravlebacfec32014-11-14 15:54:36 +00003024void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003025 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003026
3027 // Most remainders are implemented in the runtime.
3028 LocationSummary::CallKind call_kind = LocationSummary::kCall;
Zheng Xuc6667102015-05-15 16:08:45 +08003029 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
3030 // sdiv will be replaced by other instruction sequence.
3031 call_kind = LocationSummary::kNoCall;
3032 } else if ((rem->GetResultType() == Primitive::kPrimInt)
3033 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003034 // Have hardware divide instruction for int, do it with three instructions.
3035 call_kind = LocationSummary::kNoCall;
3036 }
3037
Calin Juravlebacfec32014-11-14 15:54:36 +00003038 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3039
Calin Juravled2ec87d2014-12-08 14:24:46 +00003040 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003041 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003042 if (rem->InputAt(1)->IsConstant()) {
3043 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003044 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08003045 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3046 int32_t abs_imm = std::abs(rem->InputAt(1)->AsIntConstant()->GetValue());
3047 if (abs_imm <= 1) {
3048 // No temp register required.
3049 } else {
3050 locations->AddTemp(Location::RequiresRegister());
3051 if (!IsPowerOfTwo(abs_imm)) {
3052 locations->AddTemp(Location::RequiresRegister());
3053 }
3054 }
3055 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003056 locations->SetInAt(0, Location::RequiresRegister());
3057 locations->SetInAt(1, Location::RequiresRegister());
3058 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3059 locations->AddTemp(Location::RequiresRegister());
3060 } else {
3061 InvokeRuntimeCallingConvention calling_convention;
3062 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3063 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3064 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
3065 // we only need the latter.
3066 locations->SetOut(Location::RegisterLocation(R1));
3067 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003068 break;
3069 }
3070 case Primitive::kPrimLong: {
3071 InvokeRuntimeCallingConvention calling_convention;
3072 locations->SetInAt(0, Location::RegisterPairLocation(
3073 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3074 locations->SetInAt(1, Location::RegisterPairLocation(
3075 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3076 // The runtime helper puts the output in R2,R3.
3077 locations->SetOut(Location::RegisterPairLocation(R2, R3));
3078 break;
3079 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00003080 case Primitive::kPrimFloat: {
3081 InvokeRuntimeCallingConvention calling_convention;
3082 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3083 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3084 locations->SetOut(Location::FpuRegisterLocation(S0));
3085 break;
3086 }
3087
Calin Juravlebacfec32014-11-14 15:54:36 +00003088 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003089 InvokeRuntimeCallingConvention calling_convention;
3090 locations->SetInAt(0, Location::FpuRegisterPairLocation(
3091 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
3092 locations->SetInAt(1, Location::FpuRegisterPairLocation(
3093 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
3094 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00003095 break;
3096 }
3097
3098 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003099 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003100 }
3101}
3102
3103void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
3104 LocationSummary* locations = rem->GetLocations();
3105 Location out = locations->Out();
3106 Location first = locations->InAt(0);
3107 Location second = locations->InAt(1);
3108
Calin Juravled2ec87d2014-12-08 14:24:46 +00003109 Primitive::Type type = rem->GetResultType();
3110 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003111 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003112 if (second.IsConstant()) {
3113 GenerateDivRemConstantIntegral(rem);
3114 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003115 Register reg1 = first.AsRegister<Register>();
3116 Register reg2 = second.AsRegister<Register>();
3117 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003118
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003119 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003120 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003121 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003122 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003123 } else {
3124 InvokeRuntimeCallingConvention calling_convention;
3125 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3126 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3127 DCHECK_EQ(R1, out.AsRegister<Register>());
3128
3129 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), rem, rem->GetDexPc(), nullptr);
3130 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003131 break;
3132 }
3133
3134 case Primitive::kPrimLong: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003135 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod), rem, rem->GetDexPc(), nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00003136 break;
3137 }
3138
Calin Juravled2ec87d2014-12-08 14:24:46 +00003139 case Primitive::kPrimFloat: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003140 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf), rem, rem->GetDexPc(), nullptr);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003141 break;
3142 }
3143
Calin Juravlebacfec32014-11-14 15:54:36 +00003144 case Primitive::kPrimDouble: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003145 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod), rem, rem->GetDexPc(), nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00003146 break;
3147 }
3148
3149 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003150 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003151 }
3152}
3153
Calin Juravled0d48522014-11-04 16:40:20 +00003154void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003155 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3156 ? LocationSummary::kCallOnSlowPath
3157 : LocationSummary::kNoCall;
3158 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003159 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00003160 if (instruction->HasUses()) {
3161 locations->SetOut(Location::SameAsFirstInput());
3162 }
3163}
3164
3165void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003166 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003167 codegen_->AddSlowPath(slow_path);
3168
3169 LocationSummary* locations = instruction->GetLocations();
3170 Location value = locations->InAt(0);
3171
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003172 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003173 case Primitive::kPrimByte:
3174 case Primitive::kPrimChar:
3175 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003176 case Primitive::kPrimInt: {
3177 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003178 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003179 } else {
3180 DCHECK(value.IsConstant()) << value;
3181 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3182 __ b(slow_path->GetEntryLabel());
3183 }
3184 }
3185 break;
3186 }
3187 case Primitive::kPrimLong: {
3188 if (value.IsRegisterPair()) {
3189 __ orrs(IP,
3190 value.AsRegisterPairLow<Register>(),
3191 ShifterOperand(value.AsRegisterPairHigh<Register>()));
3192 __ b(slow_path->GetEntryLabel(), EQ);
3193 } else {
3194 DCHECK(value.IsConstant()) << value;
3195 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3196 __ b(slow_path->GetEntryLabel());
3197 }
3198 }
3199 break;
3200 default:
3201 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3202 }
3203 }
Calin Juravled0d48522014-11-04 16:40:20 +00003204}
3205
Calin Juravle9aec02f2014-11-18 23:06:35 +00003206void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
3207 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3208
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003209 LocationSummary* locations =
3210 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003211
3212 switch (op->GetResultType()) {
3213 case Primitive::kPrimInt: {
3214 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003215 if (op->InputAt(1)->IsConstant()) {
3216 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3217 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3218 } else {
3219 locations->SetInAt(1, Location::RequiresRegister());
3220 // Make the output overlap, as it will be used to hold the masked
3221 // second input.
3222 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3223 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003224 break;
3225 }
3226 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003227 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003228 if (op->InputAt(1)->IsConstant()) {
3229 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3230 // For simplicity, use kOutputOverlap even though we only require that low registers
3231 // don't clash with high registers which the register allocator currently guarantees.
3232 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3233 } else {
3234 locations->SetInAt(1, Location::RequiresRegister());
3235 locations->AddTemp(Location::RequiresRegister());
3236 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3237 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003238 break;
3239 }
3240 default:
3241 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3242 }
3243}
3244
3245void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
3246 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3247
3248 LocationSummary* locations = op->GetLocations();
3249 Location out = locations->Out();
3250 Location first = locations->InAt(0);
3251 Location second = locations->InAt(1);
3252
3253 Primitive::Type type = op->GetResultType();
3254 switch (type) {
3255 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003256 Register out_reg = out.AsRegister<Register>();
3257 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003258 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003259 Register second_reg = second.AsRegister<Register>();
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003260 // Arm doesn't mask the shift count so we need to do it ourselves.
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003261 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftValue));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003262 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003263 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003264 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003265 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003266 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003267 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003268 }
3269 } else {
3270 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
3271 uint32_t shift_value = static_cast<uint32_t>(cst & kMaxIntShiftValue);
3272 if (shift_value == 0) { // arm does not support shifting with 0 immediate.
3273 __ Mov(out_reg, first_reg);
3274 } else if (op->IsShl()) {
3275 __ Lsl(out_reg, first_reg, shift_value);
3276 } else if (op->IsShr()) {
3277 __ Asr(out_reg, first_reg, shift_value);
3278 } else {
3279 __ Lsr(out_reg, first_reg, shift_value);
3280 }
3281 }
3282 break;
3283 }
3284 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003285 Register o_h = out.AsRegisterPairHigh<Register>();
3286 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003287
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003288 Register high = first.AsRegisterPairHigh<Register>();
3289 Register low = first.AsRegisterPairLow<Register>();
3290
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003291 if (second.IsRegister()) {
3292 Register temp = locations->GetTemp(0).AsRegister<Register>();
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003293
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003294 Register second_reg = second.AsRegister<Register>();
3295
3296 if (op->IsShl()) {
3297 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftValue));
3298 // Shift the high part
3299 __ Lsl(o_h, high, o_l);
3300 // Shift the low part and `or` what overflew on the high part
3301 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
3302 __ Lsr(temp, low, temp);
3303 __ orr(o_h, o_h, ShifterOperand(temp));
3304 // If the shift is > 32 bits, override the high part
3305 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
3306 __ it(PL);
3307 __ Lsl(o_h, low, temp, PL);
3308 // Shift the low part
3309 __ Lsl(o_l, low, o_l);
3310 } else if (op->IsShr()) {
3311 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftValue));
3312 // Shift the low part
3313 __ Lsr(o_l, low, o_h);
3314 // Shift the high part and `or` what underflew on the low part
3315 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3316 __ Lsl(temp, high, temp);
3317 __ orr(o_l, o_l, ShifterOperand(temp));
3318 // If the shift is > 32 bits, override the low part
3319 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3320 __ it(PL);
3321 __ Asr(o_l, high, temp, PL);
3322 // Shift the high part
3323 __ Asr(o_h, high, o_h);
3324 } else {
3325 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftValue));
3326 // same as Shr except we use `Lsr`s and not `Asr`s
3327 __ Lsr(o_l, low, o_h);
3328 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3329 __ Lsl(temp, high, temp);
3330 __ orr(o_l, o_l, ShifterOperand(temp));
3331 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3332 __ it(PL);
3333 __ Lsr(o_l, high, temp, PL);
3334 __ Lsr(o_h, high, o_h);
3335 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003336 } else {
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003337 // Register allocator doesn't create partial overlap.
3338 DCHECK_NE(o_l, high);
3339 DCHECK_NE(o_h, low);
3340 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
3341 uint32_t shift_value = static_cast<uint32_t>(cst & kMaxLongShiftValue);
3342 if (shift_value > 32) {
3343 if (op->IsShl()) {
3344 __ Lsl(o_h, low, shift_value - 32);
3345 __ LoadImmediate(o_l, 0);
3346 } else if (op->IsShr()) {
3347 __ Asr(o_l, high, shift_value - 32);
3348 __ Asr(o_h, high, 31);
3349 } else {
3350 __ Lsr(o_l, high, shift_value - 32);
3351 __ LoadImmediate(o_h, 0);
3352 }
3353 } else if (shift_value == 32) {
3354 if (op->IsShl()) {
3355 __ mov(o_h, ShifterOperand(low));
3356 __ LoadImmediate(o_l, 0);
3357 } else if (op->IsShr()) {
3358 __ mov(o_l, ShifterOperand(high));
3359 __ Asr(o_h, high, 31);
3360 } else {
3361 __ mov(o_l, ShifterOperand(high));
3362 __ LoadImmediate(o_h, 0);
3363 }
3364 } else { // shift_value < 32
3365 if (op->IsShl()) {
3366 __ Lsl(o_h, high, shift_value);
3367 __ orr(o_h, o_h, ShifterOperand(low, LSR, 32 - shift_value));
3368 __ Lsl(o_l, low, shift_value);
3369 } else if (op->IsShr()) {
3370 __ Lsr(o_l, low, shift_value);
3371 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3372 __ Asr(o_h, high, shift_value);
3373 } else {
3374 __ Lsr(o_l, low, shift_value);
3375 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3376 __ Lsr(o_h, high, shift_value);
3377 }
3378 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003379 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003380 break;
3381 }
3382 default:
3383 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003384 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003385 }
3386}
3387
3388void LocationsBuilderARM::VisitShl(HShl* shl) {
3389 HandleShift(shl);
3390}
3391
3392void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
3393 HandleShift(shl);
3394}
3395
3396void LocationsBuilderARM::VisitShr(HShr* shr) {
3397 HandleShift(shr);
3398}
3399
3400void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
3401 HandleShift(shr);
3402}
3403
3404void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
3405 HandleShift(ushr);
3406}
3407
3408void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
3409 HandleShift(ushr);
3410}
3411
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003412void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003413 LocationSummary* locations =
3414 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003415 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003416 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003417 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003418 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003419}
3420
3421void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
3422 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003423 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003424 // Note: if heap poisoning is enabled, the entry point takes cares
3425 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003426 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003427 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003428 instruction->GetDexPc(),
3429 nullptr);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003430}
3431
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003432void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
3433 LocationSummary* locations =
3434 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3435 InvokeRuntimeCallingConvention calling_convention;
3436 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003437 locations->SetOut(Location::RegisterLocation(R0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003438 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003439 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003440}
3441
3442void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
3443 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003444 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003445 // Note: if heap poisoning is enabled, the entry point takes cares
3446 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003447 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003448 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003449 instruction->GetDexPc(),
3450 nullptr);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003451}
3452
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003453void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003454 LocationSummary* locations =
3455 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003456 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3457 if (location.IsStackSlot()) {
3458 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3459 } else if (location.IsDoubleStackSlot()) {
3460 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003461 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003462 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003463}
3464
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003465void InstructionCodeGeneratorARM::VisitParameterValue(
3466 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003467 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003468}
3469
3470void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
3471 LocationSummary* locations =
3472 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3473 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3474}
3475
3476void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3477 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003478}
3479
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003480void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003481 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003482 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003483 locations->SetInAt(0, Location::RequiresRegister());
3484 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003485}
3486
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003487void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
3488 LocationSummary* locations = not_->GetLocations();
3489 Location out = locations->Out();
3490 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003491 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003492 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003493 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003494 break;
3495
3496 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003497 __ mvn(out.AsRegisterPairLow<Register>(),
3498 ShifterOperand(in.AsRegisterPairLow<Register>()));
3499 __ mvn(out.AsRegisterPairHigh<Register>(),
3500 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003501 break;
3502
3503 default:
3504 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3505 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003506}
3507
David Brazdil66d126e2015-04-03 16:02:44 +01003508void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
3509 LocationSummary* locations =
3510 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3511 locations->SetInAt(0, Location::RequiresRegister());
3512 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3513}
3514
3515void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003516 LocationSummary* locations = bool_not->GetLocations();
3517 Location out = locations->Out();
3518 Location in = locations->InAt(0);
3519 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
3520}
3521
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003522void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003523 LocationSummary* locations =
3524 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003525 switch (compare->InputAt(0)->GetType()) {
3526 case Primitive::kPrimLong: {
3527 locations->SetInAt(0, Location::RequiresRegister());
3528 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003529 // Output overlaps because it is written before doing the low comparison.
3530 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00003531 break;
3532 }
3533 case Primitive::kPrimFloat:
3534 case Primitive::kPrimDouble: {
3535 locations->SetInAt(0, Location::RequiresFpuRegister());
3536 locations->SetInAt(1, Location::RequiresFpuRegister());
3537 locations->SetOut(Location::RequiresRegister());
3538 break;
3539 }
3540 default:
3541 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3542 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003543}
3544
3545void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003546 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003547 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003548 Location left = locations->InAt(0);
3549 Location right = locations->InAt(1);
3550
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003551 Label less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00003552 Primitive::Type type = compare->InputAt(0)->GetType();
3553 switch (type) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003554 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003555 __ cmp(left.AsRegisterPairHigh<Register>(),
3556 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003557 __ b(&less, LT);
3558 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003559 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00003560 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003561 __ cmp(left.AsRegisterPairLow<Register>(),
3562 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Calin Juravleddb7df22014-11-25 20:56:51 +00003563 break;
3564 }
3565 case Primitive::kPrimFloat:
3566 case Primitive::kPrimDouble: {
3567 __ LoadImmediate(out, 0);
3568 if (type == Primitive::kPrimFloat) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003569 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003570 } else {
3571 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
3572 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
3573 }
3574 __ vmstat(); // transfer FP status register to ARM APSR.
3575 __ b(compare->IsGtBias() ? &greater : &less, VS); // VS for unordered.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003576 break;
3577 }
3578 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003579 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003580 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003581 __ b(&done, EQ);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003582 __ b(&less, LO); // LO is for both: unsigned compare for longs and 'less than' for floats.
Calin Juravleddb7df22014-11-25 20:56:51 +00003583
3584 __ Bind(&greater);
3585 __ LoadImmediate(out, 1);
3586 __ b(&done);
3587
3588 __ Bind(&less);
3589 __ LoadImmediate(out, -1);
3590
3591 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003592}
3593
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003594void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003595 LocationSummary* locations =
3596 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003597 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3598 locations->SetInAt(i, Location::Any());
3599 }
3600 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003601}
3602
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003603void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003604 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003605}
3606
Calin Juravle52c48962014-12-16 17:02:57 +00003607void InstructionCodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
3608 // TODO (ported from quick): revisit Arm barrier kinds
Kenny Root1d8199d2015-06-02 11:01:10 -07003609 DmbOptions flavor = DmbOptions::ISH; // quiet c++ warnings
Calin Juravle52c48962014-12-16 17:02:57 +00003610 switch (kind) {
3611 case MemBarrierKind::kAnyStore:
3612 case MemBarrierKind::kLoadAny:
3613 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003614 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00003615 break;
3616 }
3617 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003618 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00003619 break;
3620 }
3621 default:
3622 LOG(FATAL) << "Unexpected memory barrier " << kind;
3623 }
Kenny Root1d8199d2015-06-02 11:01:10 -07003624 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00003625}
3626
3627void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
3628 uint32_t offset,
3629 Register out_lo,
3630 Register out_hi) {
3631 if (offset != 0) {
Roland Levillain3b359c72015-11-17 19:35:12 +00003632 // Ensure `out_lo` is different from `addr`, so that loading
3633 // `offset` into `out_lo` does not clutter `addr`.
3634 DCHECK_NE(out_lo, addr);
Calin Juravle52c48962014-12-16 17:02:57 +00003635 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003636 __ add(IP, addr, ShifterOperand(out_lo));
3637 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003638 }
3639 __ ldrexd(out_lo, out_hi, addr);
3640}
3641
3642void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
3643 uint32_t offset,
3644 Register value_lo,
3645 Register value_hi,
3646 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00003647 Register temp2,
3648 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003649 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00003650 if (offset != 0) {
3651 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003652 __ add(IP, addr, ShifterOperand(temp1));
3653 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003654 }
3655 __ Bind(&fail);
3656 // We need a load followed by store. (The address used in a STREX instruction must
3657 // be the same as the address in the most recently executed LDREX instruction.)
3658 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00003659 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003660 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003661 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00003662}
3663
3664void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3665 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3666
Nicolas Geoffray39468442014-09-02 15:17:15 +01003667 LocationSummary* locations =
3668 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003669 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003670
Calin Juravle52c48962014-12-16 17:02:57 +00003671 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003672 if (Primitive::IsFloatingPointType(field_type)) {
3673 locations->SetInAt(1, Location::RequiresFpuRegister());
3674 } else {
3675 locations->SetInAt(1, Location::RequiresRegister());
3676 }
3677
Calin Juravle52c48962014-12-16 17:02:57 +00003678 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00003679 bool generate_volatile = field_info.IsVolatile()
3680 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003681 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01003682 bool needs_write_barrier =
3683 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003684 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00003685 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01003686 if (needs_write_barrier) {
3687 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003688 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003689 } else if (generate_volatile) {
Calin Juravle52c48962014-12-16 17:02:57 +00003690 // Arm encoding have some additional constraints for ldrexd/strexd:
3691 // - registers need to be consecutive
3692 // - the first register should be even but not R14.
3693 // We don't test for Arm yet, and the assertion makes sure that we revisit this if we ever
3694 // enable Arm encoding.
3695 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3696
3697 locations->AddTemp(Location::RequiresRegister());
3698 locations->AddTemp(Location::RequiresRegister());
3699 if (field_type == Primitive::kPrimDouble) {
3700 // For doubles we need two more registers to copy the value.
3701 locations->AddTemp(Location::RegisterLocation(R2));
3702 locations->AddTemp(Location::RegisterLocation(R3));
3703 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003704 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003705}
3706
Calin Juravle52c48962014-12-16 17:02:57 +00003707void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003708 const FieldInfo& field_info,
3709 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003710 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3711
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003712 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003713 Register base = locations->InAt(0).AsRegister<Register>();
3714 Location value = locations->InAt(1);
3715
3716 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003717 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00003718 Primitive::Type field_type = field_info.GetFieldType();
3719 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01003720 bool needs_write_barrier =
3721 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003722
3723 if (is_volatile) {
3724 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3725 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003726
3727 switch (field_type) {
3728 case Primitive::kPrimBoolean:
3729 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003730 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003731 break;
3732 }
3733
3734 case Primitive::kPrimShort:
3735 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003736 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003737 break;
3738 }
3739
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003740 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003741 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01003742 if (kPoisonHeapReferences && needs_write_barrier) {
3743 // Note that in the case where `value` is a null reference,
3744 // we do not enter this block, as a null reference does not
3745 // need poisoning.
3746 DCHECK_EQ(field_type, Primitive::kPrimNot);
3747 Register temp = locations->GetTemp(0).AsRegister<Register>();
3748 __ Mov(temp, value.AsRegister<Register>());
3749 __ PoisonHeapReference(temp);
3750 __ StoreToOffset(kStoreWord, temp, base, offset);
3751 } else {
3752 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
3753 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003754 break;
3755 }
3756
3757 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00003758 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003759 GenerateWideAtomicStore(base, offset,
3760 value.AsRegisterPairLow<Register>(),
3761 value.AsRegisterPairHigh<Register>(),
3762 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003763 locations->GetTemp(1).AsRegister<Register>(),
3764 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003765 } else {
3766 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003767 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003768 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003769 break;
3770 }
3771
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003772 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003773 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003774 break;
3775 }
3776
3777 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003778 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00003779 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003780 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
3781 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
3782
3783 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
3784
3785 GenerateWideAtomicStore(base, offset,
3786 value_reg_lo,
3787 value_reg_hi,
3788 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003789 locations->GetTemp(3).AsRegister<Register>(),
3790 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003791 } else {
3792 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003793 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003794 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003795 break;
3796 }
3797
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003798 case Primitive::kPrimVoid:
3799 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003800 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003801 }
Calin Juravle52c48962014-12-16 17:02:57 +00003802
Calin Juravle77520bc2015-01-12 18:45:46 +00003803 // Longs and doubles are handled in the switch.
3804 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
3805 codegen_->MaybeRecordImplicitNullCheck(instruction);
3806 }
3807
3808 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3809 Register temp = locations->GetTemp(0).AsRegister<Register>();
3810 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003811 codegen_->MarkGCCard(
3812 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003813 }
3814
Calin Juravle52c48962014-12-16 17:02:57 +00003815 if (is_volatile) {
3816 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3817 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003818}
3819
Calin Juravle52c48962014-12-16 17:02:57 +00003820void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3821 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain3b359c72015-11-17 19:35:12 +00003822
3823 bool object_field_get_with_read_barrier =
3824 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003825 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00003826 new (GetGraph()->GetArena()) LocationSummary(instruction,
3827 object_field_get_with_read_barrier ?
3828 LocationSummary::kCallOnSlowPath :
3829 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003830 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003831
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003832 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00003833 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003834 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain3b359c72015-11-17 19:35:12 +00003835 // The output overlaps in case of volatile long: we don't want the
3836 // code generated by GenerateWideAtomicLoad to overwrite the
3837 // object's location. Likewise, in the case of an object field get
3838 // with read barriers enabled, we do not want the load to overwrite
3839 // the object's location, as we need it to emit the read barrier.
3840 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
3841 object_field_get_with_read_barrier;
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01003842
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003843 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3844 locations->SetOut(Location::RequiresFpuRegister());
3845 } else {
3846 locations->SetOut(Location::RequiresRegister(),
3847 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
3848 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003849 if (volatile_for_double) {
Calin Juravle52c48962014-12-16 17:02:57 +00003850 // Arm encoding have some additional constraints for ldrexd/strexd:
3851 // - registers need to be consecutive
3852 // - the first register should be even but not R14.
3853 // We don't test for Arm yet, and the assertion makes sure that we revisit this if we ever
3854 // enable Arm encoding.
3855 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3856 locations->AddTemp(Location::RequiresRegister());
3857 locations->AddTemp(Location::RequiresRegister());
3858 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003859}
3860
Vladimir Markod2b4ca22015-09-14 15:13:26 +01003861Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
3862 Opcode opcode) {
3863 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
3864 if (constant->IsConstant() &&
3865 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
3866 return Location::ConstantLocation(constant->AsConstant());
3867 }
3868 return Location::RequiresRegister();
3869}
3870
3871bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
3872 Opcode opcode) {
3873 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
3874 if (Primitive::Is64BitType(input_cst->GetType())) {
3875 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode) &&
3876 CanEncodeConstantAsImmediate(High32Bits(value), opcode);
3877 } else {
3878 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
3879 }
3880}
3881
3882bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value, Opcode opcode) {
3883 ShifterOperand so;
3884 ArmAssembler* assembler = codegen_->GetAssembler();
3885 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, &so)) {
3886 return true;
3887 }
3888 Opcode neg_opcode = kNoOperand;
3889 switch (opcode) {
3890 case AND:
3891 neg_opcode = BIC;
3892 break;
3893 case ORR:
3894 neg_opcode = ORN;
3895 break;
3896 default:
3897 return false;
3898 }
3899 return assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, neg_opcode, ~value, &so);
3900}
3901
Calin Juravle52c48962014-12-16 17:02:57 +00003902void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
3903 const FieldInfo& field_info) {
3904 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003905
Calin Juravle52c48962014-12-16 17:02:57 +00003906 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00003907 Location base_loc = locations->InAt(0);
3908 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00003909 Location out = locations->Out();
3910 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003911 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00003912 Primitive::Type field_type = field_info.GetFieldType();
3913 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3914
3915 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003916 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003917 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003918 break;
3919 }
3920
3921 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003922 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003923 break;
3924 }
3925
3926 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003927 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003928 break;
3929 }
3930
3931 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003932 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003933 break;
3934 }
3935
3936 case Primitive::kPrimInt:
3937 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003938 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003939 break;
3940 }
3941
3942 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00003943 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003944 GenerateWideAtomicLoad(base, offset,
3945 out.AsRegisterPairLow<Register>(),
3946 out.AsRegisterPairHigh<Register>());
3947 } else {
3948 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
3949 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003950 break;
3951 }
3952
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003953 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003954 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003955 break;
3956 }
3957
3958 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003959 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00003960 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003961 Register lo = locations->GetTemp(0).AsRegister<Register>();
3962 Register hi = locations->GetTemp(1).AsRegister<Register>();
3963 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00003964 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003965 __ vmovdrr(out_reg, lo, hi);
3966 } else {
3967 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003968 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003969 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003970 break;
3971 }
3972
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003973 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003974 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003975 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003976 }
Calin Juravle52c48962014-12-16 17:02:57 +00003977
Calin Juravle77520bc2015-01-12 18:45:46 +00003978 // Doubles are handled in the switch.
3979 if (field_type != Primitive::kPrimDouble) {
3980 codegen_->MaybeRecordImplicitNullCheck(instruction);
3981 }
3982
Calin Juravle52c48962014-12-16 17:02:57 +00003983 if (is_volatile) {
3984 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3985 }
Roland Levillain4d027112015-07-01 15:41:14 +01003986
3987 if (field_type == Primitive::kPrimNot) {
Roland Levillain3b359c72015-11-17 19:35:12 +00003988 codegen_->MaybeGenerateReadBarrier(instruction, out, out, base_loc, offset);
Roland Levillain4d027112015-07-01 15:41:14 +01003989 }
Calin Juravle52c48962014-12-16 17:02:57 +00003990}
3991
3992void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3993 HandleFieldSet(instruction, instruction->GetFieldInfo());
3994}
3995
3996void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003997 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003998}
3999
4000void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4001 HandleFieldGet(instruction, instruction->GetFieldInfo());
4002}
4003
4004void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4005 HandleFieldGet(instruction, instruction->GetFieldInfo());
4006}
4007
4008void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4009 HandleFieldGet(instruction, instruction->GetFieldInfo());
4010}
4011
4012void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4013 HandleFieldGet(instruction, instruction->GetFieldInfo());
4014}
4015
4016void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4017 HandleFieldSet(instruction, instruction->GetFieldInfo());
4018}
4019
4020void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004021 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004022}
4023
Calin Juravlee460d1d2015-09-29 04:52:17 +01004024void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
4025 HUnresolvedInstanceFieldGet* instruction) {
4026 FieldAccessCallingConventionARM calling_convention;
4027 codegen_->CreateUnresolvedFieldLocationSummary(
4028 instruction, instruction->GetFieldType(), calling_convention);
4029}
4030
4031void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
4032 HUnresolvedInstanceFieldGet* instruction) {
4033 FieldAccessCallingConventionARM calling_convention;
4034 codegen_->GenerateUnresolvedFieldAccess(instruction,
4035 instruction->GetFieldType(),
4036 instruction->GetFieldIndex(),
4037 instruction->GetDexPc(),
4038 calling_convention);
4039}
4040
4041void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
4042 HUnresolvedInstanceFieldSet* instruction) {
4043 FieldAccessCallingConventionARM calling_convention;
4044 codegen_->CreateUnresolvedFieldLocationSummary(
4045 instruction, instruction->GetFieldType(), calling_convention);
4046}
4047
4048void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
4049 HUnresolvedInstanceFieldSet* instruction) {
4050 FieldAccessCallingConventionARM calling_convention;
4051 codegen_->GenerateUnresolvedFieldAccess(instruction,
4052 instruction->GetFieldType(),
4053 instruction->GetFieldIndex(),
4054 instruction->GetDexPc(),
4055 calling_convention);
4056}
4057
4058void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
4059 HUnresolvedStaticFieldGet* instruction) {
4060 FieldAccessCallingConventionARM calling_convention;
4061 codegen_->CreateUnresolvedFieldLocationSummary(
4062 instruction, instruction->GetFieldType(), calling_convention);
4063}
4064
4065void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
4066 HUnresolvedStaticFieldGet* instruction) {
4067 FieldAccessCallingConventionARM calling_convention;
4068 codegen_->GenerateUnresolvedFieldAccess(instruction,
4069 instruction->GetFieldType(),
4070 instruction->GetFieldIndex(),
4071 instruction->GetDexPc(),
4072 calling_convention);
4073}
4074
4075void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
4076 HUnresolvedStaticFieldSet* instruction) {
4077 FieldAccessCallingConventionARM calling_convention;
4078 codegen_->CreateUnresolvedFieldLocationSummary(
4079 instruction, instruction->GetFieldType(), calling_convention);
4080}
4081
4082void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
4083 HUnresolvedStaticFieldSet* instruction) {
4084 FieldAccessCallingConventionARM calling_convention;
4085 codegen_->GenerateUnresolvedFieldAccess(instruction,
4086 instruction->GetFieldType(),
4087 instruction->GetFieldIndex(),
4088 instruction->GetDexPc(),
4089 calling_convention);
4090}
4091
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004092void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004093 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4094 ? LocationSummary::kCallOnSlowPath
4095 : LocationSummary::kNoCall;
4096 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravle77520bc2015-01-12 18:45:46 +00004097 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004098 if (instruction->HasUses()) {
4099 locations->SetOut(Location::SameAsFirstInput());
4100 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004101}
4102
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004103void InstructionCodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004104 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4105 return;
4106 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004107 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004108
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004109 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
4110 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4111}
4112
4113void InstructionCodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004114 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004115 codegen_->AddSlowPath(slow_path);
4116
4117 LocationSummary* locations = instruction->GetLocations();
4118 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004119
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004120 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004121}
4122
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004123void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004124 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004125 GenerateImplicitNullCheck(instruction);
4126 } else {
4127 GenerateExplicitNullCheck(instruction);
4128 }
4129}
4130
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004131void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004132 bool object_array_get_with_read_barrier =
4133 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004134 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004135 new (GetGraph()->GetArena()) LocationSummary(instruction,
4136 object_array_get_with_read_barrier ?
4137 LocationSummary::kCallOnSlowPath :
4138 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004139 locations->SetInAt(0, Location::RequiresRegister());
4140 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004141 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4142 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4143 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004144 // The output overlaps in the case of an object array get with
4145 // read barriers enabled: we do not want the move to overwrite the
4146 // array's location, as we need it to emit the read barrier.
4147 locations->SetOut(
4148 Location::RequiresRegister(),
4149 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004150 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004151}
4152
4153void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
4154 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004155 Location obj_loc = locations->InAt(0);
4156 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004157 Location index = locations->InAt(1);
Roland Levillain4d027112015-07-01 15:41:14 +01004158 Primitive::Type type = instruction->GetType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004159
Roland Levillain4d027112015-07-01 15:41:14 +01004160 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004161 case Primitive::kPrimBoolean: {
4162 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004163 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004164 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004165 size_t offset =
4166 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004167 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
4168 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004169 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004170 __ LoadFromOffset(kLoadUnsignedByte, out, IP, data_offset);
4171 }
4172 break;
4173 }
4174
4175 case Primitive::kPrimByte: {
4176 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004177 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004178 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004179 size_t offset =
4180 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004181 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
4182 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004183 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004184 __ LoadFromOffset(kLoadSignedByte, out, IP, data_offset);
4185 }
4186 break;
4187 }
4188
4189 case Primitive::kPrimShort: {
4190 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004191 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004192 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004193 size_t offset =
4194 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004195 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
4196 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004197 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004198 __ LoadFromOffset(kLoadSignedHalfword, out, IP, data_offset);
4199 }
4200 break;
4201 }
4202
4203 case Primitive::kPrimChar: {
4204 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004205 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004206 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004207 size_t offset =
4208 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004209 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
4210 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004211 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004212 __ LoadFromOffset(kLoadUnsignedHalfword, out, IP, data_offset);
4213 }
4214 break;
4215 }
4216
4217 case Primitive::kPrimInt:
4218 case Primitive::kPrimNot: {
Roland Levillain3b359c72015-11-17 19:35:12 +00004219 static_assert(
4220 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4221 "art::mirror::HeapReference<mirror::Object> and int32_t have different sizes.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004222 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004223 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004224 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004225 size_t offset =
4226 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004227 __ LoadFromOffset(kLoadWord, out, obj, offset);
4228 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004229 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004230 __ LoadFromOffset(kLoadWord, out, IP, data_offset);
4231 }
4232 break;
4233 }
4234
4235 case Primitive::kPrimLong: {
4236 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004237 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004238 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004239 size_t offset =
4240 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004241 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004242 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004243 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004244 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004245 }
4246 break;
4247 }
4248
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004249 case Primitive::kPrimFloat: {
4250 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4251 Location out = locations->Out();
4252 DCHECK(out.IsFpuRegister());
4253 if (index.IsConstant()) {
4254 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4255 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), obj, offset);
4256 } else {
4257 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4258 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), IP, data_offset);
4259 }
4260 break;
4261 }
4262
4263 case Primitive::kPrimDouble: {
4264 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4265 Location out = locations->Out();
4266 DCHECK(out.IsFpuRegisterPair());
4267 if (index.IsConstant()) {
4268 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
4269 __ LoadDFromOffset(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()), obj, offset);
4270 } else {
4271 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
4272 __ LoadDFromOffset(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
4273 }
4274 break;
4275 }
4276
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004277 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004278 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004279 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004280 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004281 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004282
4283 if (type == Primitive::kPrimNot) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004284 static_assert(
4285 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4286 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
4287 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4288 Location out = locations->Out();
4289 if (index.IsConstant()) {
4290 uint32_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4291 codegen_->MaybeGenerateReadBarrier(instruction, out, out, obj_loc, offset);
4292 } else {
4293 codegen_->MaybeGenerateReadBarrier(instruction, out, out, obj_loc, data_offset, index);
4294 }
Roland Levillain4d027112015-07-01 15:41:14 +01004295 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004296}
4297
4298void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004299 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004300
4301 bool needs_write_barrier =
4302 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain3b359c72015-11-17 19:35:12 +00004303 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
4304 bool object_array_set_with_read_barrier =
4305 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004306
Nicolas Geoffray39468442014-09-02 15:17:15 +01004307 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004308 instruction,
Roland Levillain3b359c72015-11-17 19:35:12 +00004309 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
4310 LocationSummary::kCallOnSlowPath :
4311 LocationSummary::kNoCall);
4312
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004313 locations->SetInAt(0, Location::RequiresRegister());
4314 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4315 if (Primitive::IsFloatingPointType(value_type)) {
4316 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004317 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004318 locations->SetInAt(2, Location::RequiresRegister());
4319 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004320 if (needs_write_barrier) {
4321 // Temporary registers for the write barrier.
4322 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Roland Levillain3b359c72015-11-17 19:35:12 +00004323 locations->AddTemp(Location::RequiresRegister()); // Possibly used for read barrier too.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004324 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004325}
4326
4327void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
4328 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004329 Location array_loc = locations->InAt(0);
4330 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004331 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004332 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain3b359c72015-11-17 19:35:12 +00004333 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004334 bool needs_write_barrier =
4335 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004336
4337 switch (value_type) {
4338 case Primitive::kPrimBoolean:
4339 case Primitive::kPrimByte: {
4340 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004341 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004342 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004343 size_t offset =
4344 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004345 __ StoreToOffset(kStoreByte, value, array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004346 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004347 __ add(IP, array, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004348 __ StoreToOffset(kStoreByte, value, IP, data_offset);
4349 }
4350 break;
4351 }
4352
4353 case Primitive::kPrimShort:
4354 case Primitive::kPrimChar: {
4355 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004356 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004357 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004358 size_t offset =
4359 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004360 __ StoreToOffset(kStoreHalfword, value, array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004361 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004362 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004363 __ StoreToOffset(kStoreHalfword, value, IP, data_offset);
4364 }
4365 break;
4366 }
4367
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004368 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004369 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00004370 Location value_loc = locations->InAt(2);
4371 Register value = value_loc.AsRegister<Register>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004372 Register source = value;
4373
4374 if (instruction->InputAt(2)->IsNullConstant()) {
4375 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004376 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004377 size_t offset =
4378 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004379 __ StoreToOffset(kStoreWord, source, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004380 } else {
4381 DCHECK(index.IsRegister()) << index;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004382 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillain4d027112015-07-01 15:41:14 +01004383 __ StoreToOffset(kStoreWord, source, IP, data_offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004384 }
Roland Levillain3b359c72015-11-17 19:35:12 +00004385 DCHECK(!needs_write_barrier);
4386 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004387 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004388 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004389
4390 DCHECK(needs_write_barrier);
4391 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
4392 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
4393 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4394 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4395 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4396 Label done;
4397 SlowPathCode* slow_path = nullptr;
4398
Roland Levillain3b359c72015-11-17 19:35:12 +00004399 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004400 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
4401 codegen_->AddSlowPath(slow_path);
4402 if (instruction->GetValueCanBeNull()) {
4403 Label non_zero;
4404 __ CompareAndBranchIfNonZero(value, &non_zero);
4405 if (index.IsConstant()) {
4406 size_t offset =
4407 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4408 __ StoreToOffset(kStoreWord, value, array, offset);
4409 } else {
4410 DCHECK(index.IsRegister()) << index;
4411 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4412 __ StoreToOffset(kStoreWord, value, IP, data_offset);
4413 }
4414 codegen_->MaybeRecordImplicitNullCheck(instruction);
4415 __ b(&done);
4416 __ Bind(&non_zero);
4417 }
4418
Roland Levillain3b359c72015-11-17 19:35:12 +00004419 if (kEmitCompilerReadBarrier) {
4420 // When read barriers are enabled, the type checking
4421 // instrumentation requires two read barriers:
4422 //
4423 // __ Mov(temp2, temp1);
4424 // // /* HeapReference<Class> */ temp1 = temp1->component_type_
4425 // __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
4426 // codegen_->GenerateReadBarrier(
4427 // instruction, temp1_loc, temp1_loc, temp2_loc, component_offset);
4428 //
4429 // // /* HeapReference<Class> */ temp2 = value->klass_
4430 // __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
4431 // codegen_->GenerateReadBarrier(
4432 // instruction, temp2_loc, temp2_loc, value_loc, class_offset, temp1_loc);
4433 //
4434 // __ cmp(temp1, ShifterOperand(temp2));
4435 //
4436 // However, the second read barrier may trash `temp`, as it
4437 // is a temporary register, and as such would not be saved
4438 // along with live registers before calling the runtime (nor
4439 // restored afterwards). So in this case, we bail out and
4440 // delegate the work to the array set slow path.
4441 //
4442 // TODO: Extend the register allocator to support a new
4443 // "(locally) live temp" location so as to avoid always
4444 // going into the slow path when read barriers are enabled.
4445 __ b(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004446 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004447 // /* HeapReference<Class> */ temp1 = array->klass_
4448 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
4449 codegen_->MaybeRecordImplicitNullCheck(instruction);
4450 __ MaybeUnpoisonHeapReference(temp1);
4451
4452 // /* HeapReference<Class> */ temp1 = temp1->component_type_
4453 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
4454 // /* HeapReference<Class> */ temp2 = value->klass_
4455 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
4456 // If heap poisoning is enabled, no need to unpoison `temp1`
4457 // nor `temp2`, as we are comparing two poisoned references.
4458 __ cmp(temp1, ShifterOperand(temp2));
4459
4460 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4461 Label do_put;
4462 __ b(&do_put, EQ);
4463 // If heap poisoning is enabled, the `temp1` reference has
4464 // not been unpoisoned yet; unpoison it now.
4465 __ MaybeUnpoisonHeapReference(temp1);
4466
4467 // /* HeapReference<Class> */ temp1 = temp1->super_class_
4468 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
4469 // If heap poisoning is enabled, no need to unpoison
4470 // `temp1`, as we are comparing against null below.
4471 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
4472 __ Bind(&do_put);
4473 } else {
4474 __ b(slow_path->GetEntryLabel(), NE);
4475 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004476 }
4477 }
4478
4479 if (kPoisonHeapReferences) {
4480 // Note that in the case where `value` is a null reference,
4481 // we do not enter this block, as a null reference does not
4482 // need poisoning.
4483 DCHECK_EQ(value_type, Primitive::kPrimNot);
4484 __ Mov(temp1, value);
4485 __ PoisonHeapReference(temp1);
4486 source = temp1;
4487 }
4488
4489 if (index.IsConstant()) {
4490 size_t offset =
4491 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4492 __ StoreToOffset(kStoreWord, source, array, offset);
4493 } else {
4494 DCHECK(index.IsRegister()) << index;
4495 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4496 __ StoreToOffset(kStoreWord, source, IP, data_offset);
4497 }
4498
Roland Levillain3b359c72015-11-17 19:35:12 +00004499 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004500 codegen_->MaybeRecordImplicitNullCheck(instruction);
4501 }
4502
4503 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
4504
4505 if (done.IsLinked()) {
4506 __ Bind(&done);
4507 }
4508
4509 if (slow_path != nullptr) {
4510 __ Bind(slow_path->GetExitLabel());
4511 }
4512
4513 break;
4514 }
4515
4516 case Primitive::kPrimInt: {
4517 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4518 Register value = locations->InAt(2).AsRegister<Register>();
4519 if (index.IsConstant()) {
4520 size_t offset =
4521 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4522 __ StoreToOffset(kStoreWord, value, array, offset);
4523 } else {
4524 DCHECK(index.IsRegister()) << index;
4525 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4526 __ StoreToOffset(kStoreWord, value, IP, data_offset);
4527 }
4528
4529 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004530 break;
4531 }
4532
4533 case Primitive::kPrimLong: {
4534 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004535 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004536 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004537 size_t offset =
4538 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004539 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004540 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004541 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004542 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004543 }
4544 break;
4545 }
4546
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004547 case Primitive::kPrimFloat: {
4548 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4549 Location value = locations->InAt(2);
4550 DCHECK(value.IsFpuRegister());
4551 if (index.IsConstant()) {
4552 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004553 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004554 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004555 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004556 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
4557 }
4558 break;
4559 }
4560
4561 case Primitive::kPrimDouble: {
4562 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4563 Location value = locations->InAt(2);
4564 DCHECK(value.IsFpuRegisterPair());
4565 if (index.IsConstant()) {
4566 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004567 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004568 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004569 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004570 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
4571 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004572
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004573 break;
4574 }
4575
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004576 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004577 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004578 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004579 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004580
4581 // Ints and objects are handled in the switch.
4582 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
4583 codegen_->MaybeRecordImplicitNullCheck(instruction);
4584 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004585}
4586
4587void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004588 LocationSummary* locations =
4589 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004590 locations->SetInAt(0, Location::RequiresRegister());
4591 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004592}
4593
4594void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
4595 LocationSummary* locations = instruction->GetLocations();
4596 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004597 Register obj = locations->InAt(0).AsRegister<Register>();
4598 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004599 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004600 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004601}
4602
4603void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004604 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4605 ? LocationSummary::kCallOnSlowPath
4606 : LocationSummary::kNoCall;
4607 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004608 locations->SetInAt(0, Location::RequiresRegister());
4609 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004610 if (instruction->HasUses()) {
4611 locations->SetOut(Location::SameAsFirstInput());
4612 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004613}
4614
4615void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
4616 LocationSummary* locations = instruction->GetLocations();
Andreas Gampe85b62f22015-09-09 13:15:38 -07004617 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004618 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004619 codegen_->AddSlowPath(slow_path);
4620
Roland Levillain271ab9c2014-11-27 15:23:57 +00004621 Register index = locations->InAt(0).AsRegister<Register>();
4622 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004623
4624 __ cmp(index, ShifterOperand(length));
Roland Levillain4fa13f62015-07-06 18:11:54 +01004625 __ b(slow_path->GetEntryLabel(), HS);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004626}
4627
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004628void CodeGeneratorARM::MarkGCCard(Register temp,
4629 Register card,
4630 Register object,
4631 Register value,
4632 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004633 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004634 if (can_be_null) {
4635 __ CompareAndBranchIfZero(value, &is_null);
4636 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004637 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmWordSize>().Int32Value());
4638 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
4639 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004640 if (can_be_null) {
4641 __ Bind(&is_null);
4642 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004643}
4644
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004645void LocationsBuilderARM::VisitTemporary(HTemporary* temp) {
4646 temp->SetLocations(nullptr);
4647}
4648
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004649void InstructionCodeGeneratorARM::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004650 // Nothing to do, this is driven by the code generator.
4651}
4652
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004653void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004654 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004655}
4656
4657void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004658 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4659}
4660
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004661void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
4662 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4663}
4664
4665void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004666 HBasicBlock* block = instruction->GetBlock();
4667 if (block->GetLoopInformation() != nullptr) {
4668 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4669 // The back edge will generate the suspend check.
4670 return;
4671 }
4672 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4673 // The goto will generate the suspend check.
4674 return;
4675 }
4676 GenerateSuspendCheck(instruction, nullptr);
4677}
4678
4679void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
4680 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004681 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004682 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
4683 if (slow_path == nullptr) {
4684 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
4685 instruction->SetSlowPath(slow_path);
4686 codegen_->AddSlowPath(slow_path);
4687 if (successor != nullptr) {
4688 DCHECK(successor->IsLoopHeader());
4689 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4690 }
4691 } else {
4692 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4693 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004694
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00004695 __ LoadFromOffset(
4696 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmWordSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004697 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004698 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004699 __ Bind(slow_path->GetReturnLabel());
4700 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004701 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004702 __ b(slow_path->GetEntryLabel());
4703 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004704}
4705
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004706ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
4707 return codegen_->GetAssembler();
4708}
4709
4710void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004711 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004712 Location source = move->GetSource();
4713 Location destination = move->GetDestination();
4714
4715 if (source.IsRegister()) {
4716 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004717 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004718 } else {
4719 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004720 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004721 SP, destination.GetStackIndex());
4722 }
4723 } else if (source.IsStackSlot()) {
4724 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004725 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004726 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004727 } else if (destination.IsFpuRegister()) {
4728 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004729 } else {
4730 DCHECK(destination.IsStackSlot());
4731 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
4732 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4733 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004734 } else if (source.IsFpuRegister()) {
4735 if (destination.IsFpuRegister()) {
4736 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004737 } else {
4738 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004739 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
4740 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004741 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004742 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004743 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
4744 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004745 } else if (destination.IsRegisterPair()) {
4746 DCHECK(ExpectedPairLayout(destination));
4747 __ LoadFromOffset(
4748 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
4749 } else {
4750 DCHECK(destination.IsFpuRegisterPair()) << destination;
4751 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
4752 SP,
4753 source.GetStackIndex());
4754 }
4755 } else if (source.IsRegisterPair()) {
4756 if (destination.IsRegisterPair()) {
4757 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
4758 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
4759 } else {
4760 DCHECK(destination.IsDoubleStackSlot()) << destination;
4761 DCHECK(ExpectedPairLayout(source));
4762 __ StoreToOffset(
4763 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
4764 }
4765 } else if (source.IsFpuRegisterPair()) {
4766 if (destination.IsFpuRegisterPair()) {
4767 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
4768 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
4769 } else {
4770 DCHECK(destination.IsDoubleStackSlot()) << destination;
4771 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
4772 SP,
4773 destination.GetStackIndex());
4774 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004775 } else {
4776 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004777 HConstant* constant = source.GetConstant();
4778 if (constant->IsIntConstant() || constant->IsNullConstant()) {
4779 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004780 if (destination.IsRegister()) {
4781 __ LoadImmediate(destination.AsRegister<Register>(), value);
4782 } else {
4783 DCHECK(destination.IsStackSlot());
4784 __ LoadImmediate(IP, value);
4785 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4786 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004787 } else if (constant->IsLongConstant()) {
4788 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004789 if (destination.IsRegisterPair()) {
4790 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
4791 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004792 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004793 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004794 __ LoadImmediate(IP, Low32Bits(value));
4795 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4796 __ LoadImmediate(IP, High32Bits(value));
4797 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
4798 }
4799 } else if (constant->IsDoubleConstant()) {
4800 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004801 if (destination.IsFpuRegisterPair()) {
4802 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004803 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004804 DCHECK(destination.IsDoubleStackSlot()) << destination;
4805 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004806 __ LoadImmediate(IP, Low32Bits(int_value));
4807 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4808 __ LoadImmediate(IP, High32Bits(int_value));
4809 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
4810 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004811 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004812 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004813 float value = constant->AsFloatConstant()->GetValue();
4814 if (destination.IsFpuRegister()) {
4815 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
4816 } else {
4817 DCHECK(destination.IsStackSlot());
4818 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
4819 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4820 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004821 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004822 }
4823}
4824
4825void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
4826 __ Mov(IP, reg);
4827 __ LoadFromOffset(kLoadWord, reg, SP, mem);
4828 __ StoreToOffset(kStoreWord, IP, SP, mem);
4829}
4830
4831void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
4832 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
4833 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
4834 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
4835 SP, mem1 + stack_offset);
4836 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
4837 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
4838 SP, mem2 + stack_offset);
4839 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
4840}
4841
4842void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004843 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004844 Location source = move->GetSource();
4845 Location destination = move->GetDestination();
4846
4847 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004848 DCHECK_NE(source.AsRegister<Register>(), IP);
4849 DCHECK_NE(destination.AsRegister<Register>(), IP);
4850 __ Mov(IP, source.AsRegister<Register>());
4851 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
4852 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004853 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004854 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004855 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004856 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004857 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4858 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004859 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00004860 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004861 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00004862 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004863 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004864 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004865 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004866 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004867 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
4868 destination.AsRegisterPairHigh<Register>(),
4869 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004870 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004871 Register low_reg = source.IsRegisterPair()
4872 ? source.AsRegisterPairLow<Register>()
4873 : destination.AsRegisterPairLow<Register>();
4874 int mem = source.IsRegisterPair()
4875 ? destination.GetStackIndex()
4876 : source.GetStackIndex();
4877 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004878 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004879 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004880 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004881 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004882 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
4883 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004884 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004885 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004886 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004887 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
4888 DRegister reg = source.IsFpuRegisterPair()
4889 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
4890 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
4891 int mem = source.IsFpuRegisterPair()
4892 ? destination.GetStackIndex()
4893 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004894 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004895 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004896 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004897 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
4898 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
4899 : destination.AsFpuRegister<SRegister>();
4900 int mem = source.IsFpuRegister()
4901 ? destination.GetStackIndex()
4902 : source.GetStackIndex();
4903
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00004904 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004905 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00004906 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00004907 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00004908 Exchange(source.GetStackIndex(), destination.GetStackIndex());
4909 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004910 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00004911 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004912 }
4913}
4914
4915void ParallelMoveResolverARM::SpillScratch(int reg) {
4916 __ Push(static_cast<Register>(reg));
4917}
4918
4919void ParallelMoveResolverARM::RestoreScratch(int reg) {
4920 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004921}
4922
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004923void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01004924 InvokeRuntimeCallingConvention calling_convention;
4925 CodeGenerator::CreateLoadClassLocationSummary(
4926 cls,
4927 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Roland Levillain3b359c72015-11-17 19:35:12 +00004928 Location::RegisterLocation(R0),
4929 /* code_generator_supports_read_barrier */ true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004930}
4931
4932void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004933 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01004934 if (cls->NeedsAccessCheck()) {
4935 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
4936 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
4937 cls,
4938 cls->GetDexPc(),
4939 nullptr);
Calin Juravle580b6092015-10-06 17:35:58 +01004940 return;
4941 }
4942
Roland Levillain3b359c72015-11-17 19:35:12 +00004943 Location out_loc = locations->Out();
4944 Register out = out_loc.AsRegister<Register>();
Calin Juravle580b6092015-10-06 17:35:58 +01004945 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00004946
Calin Juravle580b6092015-10-06 17:35:58 +01004947 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004948 DCHECK(!cls->CanCallRuntime());
4949 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillain3b359c72015-11-17 19:35:12 +00004950 uint32_t declaring_class_offset = ArtMethod::DeclaringClassOffset().Int32Value();
4951 if (kEmitCompilerReadBarrier) {
4952 // /* GcRoot<mirror::Class>* */ out = &(current_method->declaring_class_)
4953 __ AddConstant(out, current_method, declaring_class_offset);
4954 // /* mirror::Class* */ out = out->Read()
4955 codegen_->GenerateReadBarrierForRoot(cls, out_loc, out_loc);
4956 } else {
4957 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4958 __ LoadFromOffset(kLoadWord, out, current_method, declaring_class_offset);
4959 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004960 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004961 DCHECK(cls->CanCallRuntime());
Roland Levillain3b359c72015-11-17 19:35:12 +00004962 // /* GcRoot<mirror::Class>[] */ out =
4963 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004964 __ LoadFromOffset(kLoadWord,
4965 out,
4966 current_method,
Vladimir Marko05792b92015-08-03 11:56:49 +01004967 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
Roland Levillain3b359c72015-11-17 19:35:12 +00004968
4969 size_t cache_offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
4970 if (kEmitCompilerReadBarrier) {
4971 // /* GcRoot<mirror::Class>* */ out = &out[type_index]
4972 __ AddConstant(out, out, cache_offset);
4973 // /* mirror::Class* */ out = out->Read()
4974 codegen_->GenerateReadBarrierForRoot(cls, out_loc, out_loc);
4975 } else {
4976 // /* GcRoot<mirror::Class> */ out = out[type_index]
4977 __ LoadFromOffset(kLoadWord, out, out, cache_offset);
4978 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004979
Andreas Gampe85b62f22015-09-09 13:15:38 -07004980 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004981 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4982 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004983 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004984 if (cls->MustGenerateClinitCheck()) {
4985 GenerateClassInitializationCheck(slow_path, out);
4986 } else {
4987 __ Bind(slow_path->GetExitLabel());
4988 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004989 }
4990}
4991
4992void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
4993 LocationSummary* locations =
4994 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4995 locations->SetInAt(0, Location::RequiresRegister());
4996 if (check->HasUses()) {
4997 locations->SetOut(Location::SameAsFirstInput());
4998 }
4999}
5000
5001void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005002 // We assume the class is not null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005003 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005004 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005005 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005006 GenerateClassInitializationCheck(slow_path,
5007 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005008}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005009
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005010void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005011 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005012 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
5013 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
5014 __ b(slow_path->GetEntryLabel(), LT);
5015 // Even if the initialized flag is set, we may be in a situation where caches are not synced
5016 // properly. Therefore, we do a memory fence.
5017 __ dmb(ISH);
5018 __ Bind(slow_path->GetExitLabel());
5019}
5020
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005021void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
5022 LocationSummary* locations =
5023 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005024 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005025 locations->SetOut(Location::RequiresRegister());
5026}
5027
5028void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07005029 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005030 codegen_->AddSlowPath(slow_path);
5031
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005032 LocationSummary* locations = load->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005033 Location out_loc = locations->Out();
5034 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005035 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005036
5037 uint32_t declaring_class_offset = ArtMethod::DeclaringClassOffset().Int32Value();
5038 if (kEmitCompilerReadBarrier) {
5039 // /* GcRoot<mirror::Class>* */ out = &(current_method->declaring_class_)
5040 __ AddConstant(out, current_method, declaring_class_offset);
5041 // /* mirror::Class* */ out = out->Read()
5042 codegen_->GenerateReadBarrierForRoot(load, out_loc, out_loc);
5043 } else {
5044 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5045 __ LoadFromOffset(kLoadWord, out, current_method, declaring_class_offset);
5046 }
5047
5048 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
Mathieu Chartiereace4582014-11-24 18:29:54 -08005049 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
Roland Levillain3b359c72015-11-17 19:35:12 +00005050
5051 size_t cache_offset = CodeGenerator::GetCacheOffset(load->GetStringIndex());
5052 if (kEmitCompilerReadBarrier) {
5053 // /* GcRoot<mirror::String>* */ out = &out[string_index]
5054 __ AddConstant(out, out, cache_offset);
5055 // /* mirror::String* */ out = out->Read()
5056 codegen_->GenerateReadBarrierForRoot(load, out_loc, out_loc);
5057 } else {
5058 // /* GcRoot<mirror::String> */ out = out[string_index]
5059 __ LoadFromOffset(kLoadWord, out, out, cache_offset);
5060 }
5061
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005062 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005063 __ Bind(slow_path->GetExitLabel());
5064}
5065
David Brazdilcb1c0552015-08-04 16:22:25 +01005066static int32_t GetExceptionTlsOffset() {
5067 return Thread::ExceptionOffset<kArmWordSize>().Int32Value();
5068}
5069
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005070void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
5071 LocationSummary* locations =
5072 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5073 locations->SetOut(Location::RequiresRegister());
5074}
5075
5076void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005077 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01005078 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
5079}
5080
5081void LocationsBuilderARM::VisitClearException(HClearException* clear) {
5082 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5083}
5084
5085void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005086 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01005087 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005088}
5089
5090void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
5091 LocationSummary* locations =
5092 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5093 InvokeRuntimeCallingConvention calling_convention;
5094 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5095}
5096
5097void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
5098 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00005099 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005100}
5101
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005102void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005103 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain3b359c72015-11-17 19:35:12 +00005104 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5105 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005106 case TypeCheckKind::kExactCheck:
5107 case TypeCheckKind::kAbstractClassCheck:
5108 case TypeCheckKind::kClassHierarchyCheck:
5109 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005110 call_kind =
5111 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005112 break;
5113 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005114 case TypeCheckKind::kUnresolvedCheck:
5115 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005116 call_kind = LocationSummary::kCallOnSlowPath;
5117 break;
5118 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005119
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005120 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain3b359c72015-11-17 19:35:12 +00005121 locations->SetInAt(0, Location::RequiresRegister());
5122 locations->SetInAt(1, Location::RequiresRegister());
5123 // The "out" register is used as a temporary, so it overlaps with the inputs.
5124 // Note that TypeCheckSlowPathARM uses this register too.
5125 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5126 // When read barriers are enabled, we need a temporary register for
5127 // some cases.
5128 if (kEmitCompilerReadBarrier &&
5129 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5130 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5131 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
5132 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005133 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005134}
5135
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005136void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005137 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005138 Location obj_loc = locations->InAt(0);
5139 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005140 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005141 Location out_loc = locations->Out();
5142 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005143 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005144 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5145 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5146 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005147 Label done, zero;
Andreas Gampe85b62f22015-09-09 13:15:38 -07005148 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005149
5150 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005151 // avoid null check if we know obj is not null.
5152 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00005153 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005154 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005155
Roland Levillain3b359c72015-11-17 19:35:12 +00005156 // /* HeapReference<Class> */ out = obj->klass_
5157 __ LoadFromOffset(kLoadWord, out, obj, class_offset);
5158 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005159
5160 switch (instruction->GetTypeCheckKind()) {
5161 case TypeCheckKind::kExactCheck: {
5162 __ cmp(out, ShifterOperand(cls));
5163 // Classes must be equal for the instanceof to succeed.
5164 __ b(&zero, NE);
5165 __ LoadImmediate(out, 1);
5166 __ b(&done);
5167 break;
5168 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005169
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005170 case TypeCheckKind::kAbstractClassCheck: {
5171 // If the class is abstract, we eagerly fetch the super class of the
5172 // object to avoid doing a comparison we know will fail.
5173 Label loop;
5174 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005175 Location temp_loc = kEmitCompilerReadBarrier ? locations->GetTemp(0) : Location::NoLocation();
5176 if (kEmitCompilerReadBarrier) {
5177 // Save the value of `out` into `temp` before overwriting it
5178 // in the following move operation, as we will need it for the
5179 // read barrier below.
5180 Register temp = temp_loc.AsRegister<Register>();
5181 __ Mov(temp, out);
5182 }
5183 // /* HeapReference<Class> */ out = out->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005184 __ LoadFromOffset(kLoadWord, out, out, super_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00005185 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, temp_loc, super_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005186 // If `out` is null, we use it for the result, and jump to `done`.
5187 __ CompareAndBranchIfZero(out, &done);
5188 __ cmp(out, ShifterOperand(cls));
5189 __ b(&loop, NE);
5190 __ LoadImmediate(out, 1);
5191 if (zero.IsLinked()) {
5192 __ b(&done);
5193 }
5194 break;
5195 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005196
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005197 case TypeCheckKind::kClassHierarchyCheck: {
5198 // Walk over the class hierarchy to find a match.
5199 Label loop, success;
5200 __ Bind(&loop);
5201 __ cmp(out, ShifterOperand(cls));
5202 __ b(&success, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005203 Location temp_loc = kEmitCompilerReadBarrier ? locations->GetTemp(0) : Location::NoLocation();
5204 if (kEmitCompilerReadBarrier) {
5205 // Save the value of `out` into `temp` before overwriting it
5206 // in the following move operation, as we will need it for the
5207 // read barrier below.
5208 Register temp = temp_loc.AsRegister<Register>();
5209 __ Mov(temp, out);
5210 }
5211 // /* HeapReference<Class> */ out = out->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005212 __ LoadFromOffset(kLoadWord, out, out, super_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00005213 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, temp_loc, super_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005214 __ CompareAndBranchIfNonZero(out, &loop);
5215 // If `out` is null, we use it for the result, and jump to `done`.
5216 __ b(&done);
5217 __ Bind(&success);
5218 __ LoadImmediate(out, 1);
5219 if (zero.IsLinked()) {
5220 __ b(&done);
5221 }
5222 break;
5223 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005224
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005225 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005226 // Do an exact check.
5227 Label exact_check;
5228 __ cmp(out, ShifterOperand(cls));
5229 __ b(&exact_check, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005230 // Otherwise, we need to check that the object's class is a non-primitive array.
5231 Location temp_loc = kEmitCompilerReadBarrier ? locations->GetTemp(0) : Location::NoLocation();
5232 if (kEmitCompilerReadBarrier) {
5233 // Save the value of `out` into `temp` before overwriting it
5234 // in the following move operation, as we will need it for the
5235 // read barrier below.
5236 Register temp = temp_loc.AsRegister<Register>();
5237 __ Mov(temp, out);
5238 }
5239 // /* HeapReference<Class> */ out = out->component_type_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005240 __ LoadFromOffset(kLoadWord, out, out, component_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00005241 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, temp_loc, component_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005242 // If `out` is null, we use it for the result, and jump to `done`.
5243 __ CompareAndBranchIfZero(out, &done);
5244 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
5245 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
5246 __ CompareAndBranchIfNonZero(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005247 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005248 __ LoadImmediate(out, 1);
5249 __ b(&done);
5250 break;
5251 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005252
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005253 case TypeCheckKind::kArrayCheck: {
5254 __ cmp(out, ShifterOperand(cls));
5255 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain3b359c72015-11-17 19:35:12 +00005256 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5257 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005258 codegen_->AddSlowPath(slow_path);
5259 __ b(slow_path->GetEntryLabel(), NE);
5260 __ LoadImmediate(out, 1);
5261 if (zero.IsLinked()) {
5262 __ b(&done);
5263 }
5264 break;
5265 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005266
Calin Juravle98893e12015-10-02 21:05:03 +01005267 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005268 case TypeCheckKind::kInterfaceCheck: {
5269 // Note that we indeed only call on slow path, but we always go
5270 // into the slow path for the unresolved & interface check
5271 // cases.
5272 //
5273 // We cannot directly call the InstanceofNonTrivial runtime
5274 // entry point without resorting to a type checking slow path
5275 // here (i.e. by calling InvokeRuntime directly), as it would
5276 // require to assign fixed registers for the inputs of this
5277 // HInstanceOf instruction (following the runtime calling
5278 // convention), which might be cluttered by the potential first
5279 // read barrier emission at the beginning of this method.
5280 DCHECK(locations->OnlyCallsOnSlowPath());
5281 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5282 /* is_fatal */ false);
5283 codegen_->AddSlowPath(slow_path);
5284 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005285 if (zero.IsLinked()) {
5286 __ b(&done);
5287 }
5288 break;
5289 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005290 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005291
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005292 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005293 __ Bind(&zero);
5294 __ LoadImmediate(out, 0);
5295 }
5296
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005297 if (done.IsLinked()) {
5298 __ Bind(&done);
5299 }
5300
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005301 if (slow_path != nullptr) {
5302 __ Bind(slow_path->GetExitLabel());
5303 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005304}
5305
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005306void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005307 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5308 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
5309
Roland Levillain3b359c72015-11-17 19:35:12 +00005310 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5311 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005312 case TypeCheckKind::kExactCheck:
5313 case TypeCheckKind::kAbstractClassCheck:
5314 case TypeCheckKind::kClassHierarchyCheck:
5315 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005316 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5317 LocationSummary::kCallOnSlowPath :
5318 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005319 break;
5320 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005321 case TypeCheckKind::kUnresolvedCheck:
5322 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005323 call_kind = LocationSummary::kCallOnSlowPath;
5324 break;
5325 }
5326
Roland Levillain3b359c72015-11-17 19:35:12 +00005327 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5328 locations->SetInAt(0, Location::RequiresRegister());
5329 locations->SetInAt(1, Location::RequiresRegister());
5330 // Note that TypeCheckSlowPathARM uses this "temp" register too.
5331 locations->AddTemp(Location::RequiresRegister());
5332 // When read barriers are enabled, we need an additional temporary
5333 // register for some cases.
5334 if (kEmitCompilerReadBarrier &&
5335 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5336 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5337 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005338 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005339 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005340}
5341
5342void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
5343 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005344 Location obj_loc = locations->InAt(0);
5345 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005346 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005347 Location temp_loc = locations->GetTemp(0);
5348 Register temp = temp_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005349 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005350 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5351 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5352 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005353
Roland Levillain3b359c72015-11-17 19:35:12 +00005354 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5355 bool is_type_check_slow_path_fatal =
5356 (type_check_kind == TypeCheckKind::kExactCheck ||
5357 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5358 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5359 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5360 !instruction->CanThrowIntoCatchBlock();
5361 SlowPathCode* type_check_slow_path =
5362 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5363 is_type_check_slow_path_fatal);
5364 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005365
5366 Label done;
5367 // Avoid null check if we know obj is not null.
5368 if (instruction->MustDoNullCheck()) {
5369 __ CompareAndBranchIfZero(obj, &done);
5370 }
5371
Roland Levillain3b359c72015-11-17 19:35:12 +00005372 // /* HeapReference<Class> */ temp = obj->klass_
5373 __ LoadFromOffset(kLoadWord, temp, obj, class_offset);
5374 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005375
Roland Levillain3b359c72015-11-17 19:35:12 +00005376 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005377 case TypeCheckKind::kExactCheck:
5378 case TypeCheckKind::kArrayCheck: {
5379 __ cmp(temp, ShifterOperand(cls));
5380 // Jump to slow path for throwing the exception or doing a
5381 // more involved array check.
Roland Levillain3b359c72015-11-17 19:35:12 +00005382 __ b(type_check_slow_path->GetEntryLabel(), NE);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005383 break;
5384 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005385
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005386 case TypeCheckKind::kAbstractClassCheck: {
5387 // If the class is abstract, we eagerly fetch the super class of the
5388 // object to avoid doing a comparison we know will fail.
Roland Levillain3b359c72015-11-17 19:35:12 +00005389 Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005390 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005391 Location temp2_loc =
5392 kEmitCompilerReadBarrier ? locations->GetTemp(1) : Location::NoLocation();
5393 if (kEmitCompilerReadBarrier) {
5394 // Save the value of `temp` into `temp2` before overwriting it
5395 // in the following move operation, as we will need it for the
5396 // read barrier below.
5397 Register temp2 = temp2_loc.AsRegister<Register>();
5398 __ Mov(temp2, temp);
5399 }
5400 // /* HeapReference<Class> */ temp = temp->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005401 __ LoadFromOffset(kLoadWord, temp, temp, super_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00005402 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, temp2_loc, super_offset);
5403
5404 // If the class reference currently in `temp` is not null, jump
5405 // to the `compare_classes` label to compare it with the checked
5406 // class.
5407 __ CompareAndBranchIfNonZero(temp, &compare_classes);
5408 // Otherwise, jump to the slow path to throw the exception.
5409 //
5410 // But before, move back the object's class into `temp` before
5411 // going into the slow path, as it has been overwritten in the
5412 // meantime.
5413 // /* HeapReference<Class> */ temp = obj->klass_
5414 __ LoadFromOffset(kLoadWord, temp, obj, class_offset);
5415 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
5416 __ b(type_check_slow_path->GetEntryLabel());
5417
5418 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005419 __ cmp(temp, ShifterOperand(cls));
5420 __ b(&loop, NE);
5421 break;
5422 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005423
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005424 case TypeCheckKind::kClassHierarchyCheck: {
5425 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005426 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005427 __ Bind(&loop);
5428 __ cmp(temp, ShifterOperand(cls));
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005429 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005430
5431 Location temp2_loc =
5432 kEmitCompilerReadBarrier ? locations->GetTemp(1) : Location::NoLocation();
5433 if (kEmitCompilerReadBarrier) {
5434 // Save the value of `temp` into `temp2` before overwriting it
5435 // in the following move operation, as we will need it for the
5436 // read barrier below.
5437 Register temp2 = temp2_loc.AsRegister<Register>();
5438 __ Mov(temp2, temp);
5439 }
5440 // /* HeapReference<Class> */ temp = temp->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005441 __ LoadFromOffset(kLoadWord, temp, temp, super_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00005442 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, temp2_loc, super_offset);
5443
5444 // If the class reference currently in `temp` is not null, jump
5445 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005446 __ CompareAndBranchIfNonZero(temp, &loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005447 // Otherwise, jump to the slow path to throw the exception.
5448 //
5449 // But before, move back the object's class into `temp` before
5450 // going into the slow path, as it has been overwritten in the
5451 // meantime.
5452 // /* HeapReference<Class> */ temp = obj->klass_
5453 __ LoadFromOffset(kLoadWord, temp, obj, class_offset);
5454 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
5455 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005456 break;
5457 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005458
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005459 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005460 // Do an exact check.
Roland Levillain3b359c72015-11-17 19:35:12 +00005461 Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005462 __ cmp(temp, ShifterOperand(cls));
5463 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005464
5465 // Otherwise, we need to check that the object's class is a non-primitive array.
5466 Location temp2_loc =
5467 kEmitCompilerReadBarrier ? locations->GetTemp(1) : Location::NoLocation();
5468 if (kEmitCompilerReadBarrier) {
5469 // Save the value of `temp` into `temp2` before overwriting it
5470 // in the following move operation, as we will need it for the
5471 // read barrier below.
5472 Register temp2 = temp2_loc.AsRegister<Register>();
5473 __ Mov(temp2, temp);
5474 }
5475 // /* HeapReference<Class> */ temp = temp->component_type_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005476 __ LoadFromOffset(kLoadWord, temp, temp, component_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00005477 codegen_->MaybeGenerateReadBarrier(
5478 instruction, temp_loc, temp_loc, temp2_loc, component_offset);
5479
5480 // If the component type is not null (i.e. the object is indeed
5481 // an array), jump to label `check_non_primitive_component_type`
5482 // to further check that this component type is not a primitive
5483 // type.
5484 __ CompareAndBranchIfNonZero(temp, &check_non_primitive_component_type);
5485 // Otherwise, jump to the slow path to throw the exception.
5486 //
5487 // But before, move back the object's class into `temp` before
5488 // going into the slow path, as it has been overwritten in the
5489 // meantime.
5490 // /* HeapReference<Class> */ temp = obj->klass_
5491 __ LoadFromOffset(kLoadWord, temp, obj, class_offset);
5492 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
5493 __ b(type_check_slow_path->GetEntryLabel());
5494
5495 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005496 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00005497 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
5498 __ CompareAndBranchIfZero(temp, &done);
5499 // Same comment as above regarding `temp` and the slow path.
5500 // /* HeapReference<Class> */ temp = obj->klass_
5501 __ LoadFromOffset(kLoadWord, temp, obj, class_offset);
5502 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
5503 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005504 break;
5505 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005506
Calin Juravle98893e12015-10-02 21:05:03 +01005507 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005508 case TypeCheckKind::kInterfaceCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005509 // We always go into the type check slow path for the unresolved &
5510 // interface check cases.
5511 //
5512 // We cannot directly call the CheckCast runtime entry point
5513 // without resorting to a type checking slow path here (i.e. by
5514 // calling InvokeRuntime directly), as it would require to
5515 // assign fixed registers for the inputs of this HInstanceOf
5516 // instruction (following the runtime calling convention), which
5517 // might be cluttered by the potential first read barrier
5518 // emission at the beginning of this method.
5519 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005520 break;
5521 }
5522 __ Bind(&done);
5523
Roland Levillain3b359c72015-11-17 19:35:12 +00005524 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005525}
5526
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005527void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
5528 LocationSummary* locations =
5529 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5530 InvokeRuntimeCallingConvention calling_convention;
5531 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5532}
5533
5534void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
5535 codegen_->InvokeRuntime(instruction->IsEnter()
5536 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
5537 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00005538 instruction->GetDexPc(),
5539 nullptr);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005540}
5541
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005542void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
5543void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
5544void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005545
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005546void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005547 LocationSummary* locations =
5548 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5549 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
5550 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005551 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005552 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005553 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00005554 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005555}
5556
5557void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
5558 HandleBitwiseOperation(instruction);
5559}
5560
5561void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
5562 HandleBitwiseOperation(instruction);
5563}
5564
5565void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
5566 HandleBitwiseOperation(instruction);
5567}
5568
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005569void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
5570 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
5571 if (value == 0xffffffffu) {
5572 if (out != first) {
5573 __ mov(out, ShifterOperand(first));
5574 }
5575 return;
5576 }
5577 if (value == 0u) {
5578 __ mov(out, ShifterOperand(0));
5579 return;
5580 }
5581 ShifterOperand so;
5582 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
5583 __ and_(out, first, so);
5584 } else {
5585 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so));
5586 __ bic(out, first, ShifterOperand(~value));
5587 }
5588}
5589
5590void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
5591 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
5592 if (value == 0u) {
5593 if (out != first) {
5594 __ mov(out, ShifterOperand(first));
5595 }
5596 return;
5597 }
5598 if (value == 0xffffffffu) {
5599 __ mvn(out, ShifterOperand(0));
5600 return;
5601 }
5602 ShifterOperand so;
5603 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
5604 __ orr(out, first, so);
5605 } else {
5606 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
5607 __ orn(out, first, ShifterOperand(~value));
5608 }
5609}
5610
5611void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
5612 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
5613 if (value == 0u) {
5614 if (out != first) {
5615 __ mov(out, ShifterOperand(first));
5616 }
5617 return;
5618 }
5619 __ eor(out, first, ShifterOperand(value));
5620}
5621
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005622void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
5623 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005624 Location first = locations->InAt(0);
5625 Location second = locations->InAt(1);
5626 Location out = locations->Out();
5627
5628 if (second.IsConstant()) {
5629 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
5630 uint32_t value_low = Low32Bits(value);
5631 if (instruction->GetResultType() == Primitive::kPrimInt) {
5632 Register first_reg = first.AsRegister<Register>();
5633 Register out_reg = out.AsRegister<Register>();
5634 if (instruction->IsAnd()) {
5635 GenerateAndConst(out_reg, first_reg, value_low);
5636 } else if (instruction->IsOr()) {
5637 GenerateOrrConst(out_reg, first_reg, value_low);
5638 } else {
5639 DCHECK(instruction->IsXor());
5640 GenerateEorConst(out_reg, first_reg, value_low);
5641 }
5642 } else {
5643 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
5644 uint32_t value_high = High32Bits(value);
5645 Register first_low = first.AsRegisterPairLow<Register>();
5646 Register first_high = first.AsRegisterPairHigh<Register>();
5647 Register out_low = out.AsRegisterPairLow<Register>();
5648 Register out_high = out.AsRegisterPairHigh<Register>();
5649 if (instruction->IsAnd()) {
5650 GenerateAndConst(out_low, first_low, value_low);
5651 GenerateAndConst(out_high, first_high, value_high);
5652 } else if (instruction->IsOr()) {
5653 GenerateOrrConst(out_low, first_low, value_low);
5654 GenerateOrrConst(out_high, first_high, value_high);
5655 } else {
5656 DCHECK(instruction->IsXor());
5657 GenerateEorConst(out_low, first_low, value_low);
5658 GenerateEorConst(out_high, first_high, value_high);
5659 }
5660 }
5661 return;
5662 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005663
5664 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005665 Register first_reg = first.AsRegister<Register>();
5666 ShifterOperand second_reg(second.AsRegister<Register>());
5667 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005668 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005669 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005670 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005671 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005672 } else {
5673 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005674 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005675 }
5676 } else {
5677 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005678 Register first_low = first.AsRegisterPairLow<Register>();
5679 Register first_high = first.AsRegisterPairHigh<Register>();
5680 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
5681 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
5682 Register out_low = out.AsRegisterPairLow<Register>();
5683 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005684 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005685 __ and_(out_low, first_low, second_low);
5686 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005687 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005688 __ orr(out_low, first_low, second_low);
5689 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005690 } else {
5691 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005692 __ eor(out_low, first_low, second_low);
5693 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005694 }
5695 }
5696}
5697
Roland Levillain3b359c72015-11-17 19:35:12 +00005698void CodeGeneratorARM::GenerateReadBarrier(HInstruction* instruction,
5699 Location out,
5700 Location ref,
5701 Location obj,
5702 uint32_t offset,
5703 Location index) {
5704 DCHECK(kEmitCompilerReadBarrier);
5705
5706 // If heap poisoning is enabled, the unpoisoning of the loaded
5707 // reference will be carried out by the runtime within the slow
5708 // path.
5709 //
5710 // Note that `ref` currently does not get unpoisoned (when heap
5711 // poisoning is enabled), which is alright as the `ref` argument is
5712 // not used by the artReadBarrierSlow entry point.
5713 //
5714 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
5715 SlowPathCode* slow_path = new (GetGraph()->GetArena())
5716 ReadBarrierForHeapReferenceSlowPathARM(instruction, out, ref, obj, offset, index);
5717 AddSlowPath(slow_path);
5718
5719 // TODO: When read barrier has a fast path, add it here.
5720 /* Currently the read barrier call is inserted after the original load.
5721 * However, if we have a fast path, we need to perform the load of obj.LockWord *before* the
5722 * original load. This load-load ordering is required by the read barrier.
5723 * The fast path/slow path (for Baker's algorithm) should look like:
5724 *
5725 * bool isGray = obj.LockWord & kReadBarrierMask;
5726 * lfence; // load fence or artificial data dependence to prevent load-load reordering
5727 * ref = obj.field; // this is the original load
5728 * if (isGray) {
5729 * ref = Mark(ref); // ideally the slow path just does Mark(ref)
5730 * }
5731 */
5732
5733 __ b(slow_path->GetEntryLabel());
5734 __ Bind(slow_path->GetExitLabel());
5735}
5736
5737void CodeGeneratorARM::MaybeGenerateReadBarrier(HInstruction* instruction,
5738 Location out,
5739 Location ref,
5740 Location obj,
5741 uint32_t offset,
5742 Location index) {
5743 if (kEmitCompilerReadBarrier) {
5744 // If heap poisoning is enabled, unpoisoning will be taken care of
5745 // by the runtime within the slow path.
5746 GenerateReadBarrier(instruction, out, ref, obj, offset, index);
5747 } else if (kPoisonHeapReferences) {
5748 __ UnpoisonHeapReference(out.AsRegister<Register>());
5749 }
5750}
5751
5752void CodeGeneratorARM::GenerateReadBarrierForRoot(HInstruction* instruction,
5753 Location out,
5754 Location root) {
5755 DCHECK(kEmitCompilerReadBarrier);
5756
5757 // Note that GC roots are not affected by heap poisoning, so we do
5758 // not need to do anything special for this here.
5759 SlowPathCode* slow_path =
5760 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM(instruction, out, root);
5761 AddSlowPath(slow_path);
5762
5763 // TODO: Implement a fast path for ReadBarrierForRoot, performing
5764 // the following operation (for Baker's algorithm):
5765 //
5766 // if (thread.tls32_.is_gc_marking) {
5767 // root = Mark(root);
5768 // }
5769
5770 __ b(slow_path->GetEntryLabel());
5771 __ Bind(slow_path->GetExitLabel());
5772}
5773
Vladimir Markodc151b22015-10-15 18:02:30 +01005774HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch(
5775 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
5776 MethodReference target_method) {
5777 if (desired_dispatch_info.method_load_kind ==
5778 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative) {
5779 // TODO: Implement this type. For the moment, we fall back to kDexCacheViaMethod.
5780 return HInvokeStaticOrDirect::DispatchInfo {
5781 HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod,
5782 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
5783 0u,
5784 0u
5785 };
5786 }
5787 if (desired_dispatch_info.code_ptr_location ==
5788 HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative) {
5789 const DexFile& outer_dex_file = GetGraph()->GetDexFile();
5790 if (&outer_dex_file != target_method.dex_file) {
5791 // Calls across dex files are more likely to exceed the available BL range,
5792 // so use absolute patch with fixup if available and kCallArtMethod otherwise.
5793 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location =
5794 (desired_dispatch_info.method_load_kind ==
5795 HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup)
5796 ? HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup
5797 : HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
5798 return HInvokeStaticOrDirect::DispatchInfo {
5799 desired_dispatch_info.method_load_kind,
5800 code_ptr_location,
5801 desired_dispatch_info.method_load_data,
5802 0u
5803 };
5804 }
5805 }
5806 return desired_dispatch_info;
5807}
5808
Nicolas Geoffray38207af2015-06-01 15:46:22 +01005809void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00005810 // For better instruction scheduling we load the direct code pointer before the method pointer.
Vladimir Marko58155012015-08-19 12:49:41 +00005811 switch (invoke->GetCodePtrLocation()) {
Vladimir Marko58155012015-08-19 12:49:41 +00005812 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
5813 // LR = code address from literal pool with link-time patch.
5814 __ LoadLiteral(LR, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
Vladimir Marko58155012015-08-19 12:49:41 +00005815 break;
5816 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
5817 // LR = invoke->GetDirectCodePtr();
5818 __ LoadImmediate(LR, invoke->GetDirectCodePtr());
Vladimir Marko58155012015-08-19 12:49:41 +00005819 break;
5820 default:
5821 break;
5822 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08005823
Vladimir Marko58155012015-08-19 12:49:41 +00005824 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
5825 switch (invoke->GetMethodLoadKind()) {
5826 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
5827 // temp = thread->string_init_entrypoint
5828 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, invoke->GetStringInitOffset());
5829 break;
5830 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
5831 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
5832 break;
5833 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
5834 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
5835 break;
5836 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
5837 __ LoadLiteral(temp.AsRegister<Register>(),
5838 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
5839 break;
5840 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01005841 // TODO: Implement this type.
5842 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
5843 LOG(FATAL) << "Unsupported";
5844 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00005845 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
5846 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
5847 Register method_reg;
5848 Register reg = temp.AsRegister<Register>();
5849 if (current_method.IsRegister()) {
5850 method_reg = current_method.AsRegister<Register>();
5851 } else {
5852 DCHECK(invoke->GetLocations()->Intrinsified());
5853 DCHECK(!current_method.IsValid());
5854 method_reg = reg;
5855 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
5856 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005857 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
5858 __ LoadFromOffset(kLoadWord,
5859 reg,
5860 method_reg,
5861 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00005862 // temp = temp[index_in_cache]
5863 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
5864 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
5865 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01005866 }
Vladimir Marko58155012015-08-19 12:49:41 +00005867 }
5868
5869 switch (invoke->GetCodePtrLocation()) {
5870 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
5871 __ bl(GetFrameEntryLabel());
5872 break;
5873 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01005874 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07005875 __ BindTrackedLabel(&relative_call_patches_.back().label);
Vladimir Markodc151b22015-10-15 18:02:30 +01005876 // Arbitrarily branch to the BL itself, override at link time.
5877 __ bl(&relative_call_patches_.back().label);
5878 break;
Vladimir Marko58155012015-08-19 12:49:41 +00005879 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
5880 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
5881 // LR prepared above for better instruction scheduling.
Vladimir Marko58155012015-08-19 12:49:41 +00005882 // LR()
5883 __ blx(LR);
5884 break;
5885 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
5886 // LR = callee_method->entry_point_from_quick_compiled_code_
5887 __ LoadFromOffset(
5888 kLoadWord, LR, callee_method.AsRegister<Register>(),
5889 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize).Int32Value());
5890 // LR()
5891 __ blx(LR);
5892 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08005893 }
5894
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08005895 DCHECK(!IsLeafMethod());
5896}
5897
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005898void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
5899 Register temp = temp_location.AsRegister<Register>();
5900 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5901 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
5902 LocationSummary* locations = invoke->GetLocations();
5903 Location receiver = locations->InAt(0);
5904 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005905 DCHECK(receiver.IsRegister());
Roland Levillain3b359c72015-11-17 19:35:12 +00005906 // /* HeapReference<Class> */ temp = receiver->klass_
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005907 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
5908 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00005909 // Instead of simply (possibly) unpoisoning `temp` here, we should
5910 // emit a read barrier for the previous class reference load.
5911 // However this is not required in practice, as this is an
5912 // intermediate/temporary reference and because the current
5913 // concurrent copying collector keeps the from-space memory
5914 // intact/accessible until the end of the marking phase (the
5915 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005916 __ MaybeUnpoisonHeapReference(temp);
5917 // temp = temp->GetMethodAt(method_offset);
5918 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
5919 kArmWordSize).Int32Value();
5920 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
5921 // LR = temp->GetEntryPoint();
5922 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
5923 // LR();
5924 __ blx(LR);
5925}
5926
Vladimir Marko58155012015-08-19 12:49:41 +00005927void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
5928 DCHECK(linker_patches->empty());
5929 size_t size = method_patches_.size() + call_patches_.size() + relative_call_patches_.size();
5930 linker_patches->reserve(size);
5931 for (const auto& entry : method_patches_) {
5932 const MethodReference& target_method = entry.first;
5933 Literal* literal = entry.second;
5934 DCHECK(literal->GetLabel()->IsBound());
5935 uint32_t literal_offset = literal->GetLabel()->Position();
5936 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
5937 target_method.dex_file,
5938 target_method.dex_method_index));
5939 }
5940 for (const auto& entry : call_patches_) {
5941 const MethodReference& target_method = entry.first;
5942 Literal* literal = entry.second;
5943 DCHECK(literal->GetLabel()->IsBound());
5944 uint32_t literal_offset = literal->GetLabel()->Position();
5945 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
5946 target_method.dex_file,
5947 target_method.dex_method_index));
5948 }
5949 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
5950 uint32_t literal_offset = info.label.Position();
5951 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
5952 info.target_method.dex_file,
5953 info.target_method.dex_method_index));
5954 }
5955}
5956
5957Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
5958 MethodToLiteralMap* map) {
5959 // Look up the literal for target_method.
5960 auto lb = map->lower_bound(target_method);
5961 if (lb != map->end() && !map->key_comp()(target_method, lb->first)) {
5962 return lb->second;
5963 }
5964 // We don't have a literal for this method yet, insert a new one.
5965 Literal* literal = __ NewLiteral<uint32_t>(0u);
5966 map->PutBefore(lb, target_method, literal);
5967 return literal;
5968}
5969
5970Literal* CodeGeneratorARM::DeduplicateMethodAddressLiteral(MethodReference target_method) {
5971 return DeduplicateMethodLiteral(target_method, &method_patches_);
5972}
5973
5974Literal* CodeGeneratorARM::DeduplicateMethodCodeLiteral(MethodReference target_method) {
5975 return DeduplicateMethodLiteral(target_method, &call_patches_);
5976}
5977
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005978void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005979 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005980 LOG(FATAL) << "Unreachable";
5981}
5982
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005983void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005984 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005985 LOG(FATAL) << "Unreachable";
5986}
5987
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005988void LocationsBuilderARM::VisitFakeString(HFakeString* instruction) {
5989 DCHECK(codegen_->IsBaseline());
5990 LocationSummary* locations =
5991 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5992 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
5993}
5994
5995void InstructionCodeGeneratorARM::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
5996 DCHECK(codegen_->IsBaseline());
5997 // Will be generated at use site.
5998}
5999
Mark Mendellfe57faa2015-09-18 09:26:15 -04006000// Simple implementation of packed switch - generate cascaded compare/jumps.
6001void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6002 LocationSummary* locations =
6003 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6004 locations->SetInAt(0, Location::RequiresRegister());
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006005 if (switch_instr->GetNumEntries() >= kPackedSwitchJumpTableThreshold &&
6006 codegen_->GetAssembler()->IsThumb()) {
6007 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
6008 if (switch_instr->GetStartValue() != 0) {
6009 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
6010 }
6011 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04006012}
6013
6014void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6015 int32_t lower_bound = switch_instr->GetStartValue();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006016 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006017 LocationSummary* locations = switch_instr->GetLocations();
6018 Register value_reg = locations->InAt(0).AsRegister<Register>();
6019 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6020
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006021 if (num_entries < kPackedSwitchJumpTableThreshold || !codegen_->GetAssembler()->IsThumb()) {
6022 // Create a series of compare/jumps.
6023 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6024 for (uint32_t i = 0; i < num_entries; i++) {
6025 GenerateCompareWithImmediate(value_reg, lower_bound + i);
6026 __ b(codegen_->GetLabelOf(successors[i]), EQ);
6027 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04006028
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006029 // And the default for any other value.
6030 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6031 __ b(codegen_->GetLabelOf(default_block));
6032 }
6033 } else {
6034 // Create a table lookup.
6035 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
6036
6037 // Materialize a pointer to the switch table
6038 std::vector<Label*> labels(num_entries);
6039 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6040 for (uint32_t i = 0; i < num_entries; i++) {
6041 labels[i] = codegen_->GetLabelOf(successors[i]);
6042 }
6043 JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg);
6044
6045 // Remove the bias.
6046 Register key_reg;
6047 if (lower_bound != 0) {
6048 key_reg = locations->GetTemp(1).AsRegister<Register>();
6049 __ AddConstant(key_reg, value_reg, -lower_bound);
6050 } else {
6051 key_reg = value_reg;
6052 }
6053
6054 // Check whether the value is in the table, jump to default block if not.
6055 __ CmpConstant(key_reg, num_entries - 1);
6056 __ b(codegen_->GetLabelOf(default_block), Condition::HI);
6057
6058 // Load the displacement from the table.
6059 __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2));
6060
6061 // Dispatch is a direct add to the PC (for Thumb2).
6062 __ EmitJumpTableDispatch(table, temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006063 }
6064}
6065
Andreas Gampe85b62f22015-09-09 13:15:38 -07006066void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
6067 if (!trg.IsValid()) {
6068 DCHECK(type == Primitive::kPrimVoid);
6069 return;
6070 }
6071
6072 DCHECK_NE(type, Primitive::kPrimVoid);
6073
6074 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
6075 if (return_loc.Equals(trg)) {
6076 return;
6077 }
6078
6079 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
6080 // with the last branch.
6081 if (type == Primitive::kPrimLong) {
6082 HParallelMove parallel_move(GetGraph()->GetArena());
6083 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
6084 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
6085 GetMoveResolver()->EmitNativeCode(&parallel_move);
6086 } else if (type == Primitive::kPrimDouble) {
6087 HParallelMove parallel_move(GetGraph()->GetArena());
6088 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
6089 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
6090 GetMoveResolver()->EmitNativeCode(&parallel_move);
6091 } else {
6092 // Let the parallel move resolver take care of all of this.
6093 HParallelMove parallel_move(GetGraph()->GetArena());
6094 parallel_move.AddMove(return_loc, trg, type, nullptr);
6095 GetMoveResolver()->EmitNativeCode(&parallel_move);
6096 }
6097}
6098
Roland Levillain4d027112015-07-01 15:41:14 +01006099#undef __
6100#undef QUICK_ENTRY_POINT
6101
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006102} // namespace arm
6103} // namespace art