blob: 1c3bd6c38ac8568e91954a0449321c82b938b24d [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 Geoffray729645a2015-11-19 13:29:02 +00003416 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3417 locations->SetInAt(1, 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) {
Roland Levillain4d027112015-07-01 15:41:14 +01003422 // Note: if heap poisoning is enabled, the entry point takes cares
3423 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003424 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003425 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003426 instruction->GetDexPc(),
3427 nullptr);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003428}
3429
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003430void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
3431 LocationSummary* locations =
3432 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3433 InvokeRuntimeCallingConvention calling_convention;
3434 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003435 locations->SetOut(Location::RegisterLocation(R0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003436 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003437 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003438}
3439
3440void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
3441 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003442 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003443 // Note: if heap poisoning is enabled, the entry point takes cares
3444 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003445 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003446 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003447 instruction->GetDexPc(),
3448 nullptr);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003449}
3450
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003451void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003452 LocationSummary* locations =
3453 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003454 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3455 if (location.IsStackSlot()) {
3456 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3457 } else if (location.IsDoubleStackSlot()) {
3458 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003459 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003460 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003461}
3462
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003463void InstructionCodeGeneratorARM::VisitParameterValue(
3464 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003465 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003466}
3467
3468void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
3469 LocationSummary* locations =
3470 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3471 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3472}
3473
3474void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3475 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003476}
3477
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003478void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003479 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003480 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003481 locations->SetInAt(0, Location::RequiresRegister());
3482 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003483}
3484
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003485void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
3486 LocationSummary* locations = not_->GetLocations();
3487 Location out = locations->Out();
3488 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003489 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003490 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003491 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003492 break;
3493
3494 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003495 __ mvn(out.AsRegisterPairLow<Register>(),
3496 ShifterOperand(in.AsRegisterPairLow<Register>()));
3497 __ mvn(out.AsRegisterPairHigh<Register>(),
3498 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003499 break;
3500
3501 default:
3502 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3503 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003504}
3505
David Brazdil66d126e2015-04-03 16:02:44 +01003506void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
3507 LocationSummary* locations =
3508 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3509 locations->SetInAt(0, Location::RequiresRegister());
3510 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3511}
3512
3513void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003514 LocationSummary* locations = bool_not->GetLocations();
3515 Location out = locations->Out();
3516 Location in = locations->InAt(0);
3517 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
3518}
3519
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003520void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003521 LocationSummary* locations =
3522 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003523 switch (compare->InputAt(0)->GetType()) {
3524 case Primitive::kPrimLong: {
3525 locations->SetInAt(0, Location::RequiresRegister());
3526 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003527 // Output overlaps because it is written before doing the low comparison.
3528 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00003529 break;
3530 }
3531 case Primitive::kPrimFloat:
3532 case Primitive::kPrimDouble: {
3533 locations->SetInAt(0, Location::RequiresFpuRegister());
3534 locations->SetInAt(1, Location::RequiresFpuRegister());
3535 locations->SetOut(Location::RequiresRegister());
3536 break;
3537 }
3538 default:
3539 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3540 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003541}
3542
3543void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003544 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003545 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003546 Location left = locations->InAt(0);
3547 Location right = locations->InAt(1);
3548
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003549 Label less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00003550 Primitive::Type type = compare->InputAt(0)->GetType();
3551 switch (type) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003552 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003553 __ cmp(left.AsRegisterPairHigh<Register>(),
3554 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003555 __ b(&less, LT);
3556 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003557 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00003558 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003559 __ cmp(left.AsRegisterPairLow<Register>(),
3560 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Calin Juravleddb7df22014-11-25 20:56:51 +00003561 break;
3562 }
3563 case Primitive::kPrimFloat:
3564 case Primitive::kPrimDouble: {
3565 __ LoadImmediate(out, 0);
3566 if (type == Primitive::kPrimFloat) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003567 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003568 } else {
3569 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
3570 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
3571 }
3572 __ vmstat(); // transfer FP status register to ARM APSR.
3573 __ b(compare->IsGtBias() ? &greater : &less, VS); // VS for unordered.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003574 break;
3575 }
3576 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003577 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003578 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003579 __ b(&done, EQ);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003580 __ b(&less, LO); // LO is for both: unsigned compare for longs and 'less than' for floats.
Calin Juravleddb7df22014-11-25 20:56:51 +00003581
3582 __ Bind(&greater);
3583 __ LoadImmediate(out, 1);
3584 __ b(&done);
3585
3586 __ Bind(&less);
3587 __ LoadImmediate(out, -1);
3588
3589 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003590}
3591
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003592void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003593 LocationSummary* locations =
3594 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003595 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3596 locations->SetInAt(i, Location::Any());
3597 }
3598 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003599}
3600
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003601void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003602 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003603}
3604
Calin Juravle52c48962014-12-16 17:02:57 +00003605void InstructionCodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
3606 // TODO (ported from quick): revisit Arm barrier kinds
Kenny Root1d8199d2015-06-02 11:01:10 -07003607 DmbOptions flavor = DmbOptions::ISH; // quiet c++ warnings
Calin Juravle52c48962014-12-16 17:02:57 +00003608 switch (kind) {
3609 case MemBarrierKind::kAnyStore:
3610 case MemBarrierKind::kLoadAny:
3611 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003612 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00003613 break;
3614 }
3615 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003616 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00003617 break;
3618 }
3619 default:
3620 LOG(FATAL) << "Unexpected memory barrier " << kind;
3621 }
Kenny Root1d8199d2015-06-02 11:01:10 -07003622 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00003623}
3624
3625void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
3626 uint32_t offset,
3627 Register out_lo,
3628 Register out_hi) {
3629 if (offset != 0) {
Roland Levillain3b359c72015-11-17 19:35:12 +00003630 // Ensure `out_lo` is different from `addr`, so that loading
3631 // `offset` into `out_lo` does not clutter `addr`.
3632 DCHECK_NE(out_lo, addr);
Calin Juravle52c48962014-12-16 17:02:57 +00003633 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003634 __ add(IP, addr, ShifterOperand(out_lo));
3635 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003636 }
3637 __ ldrexd(out_lo, out_hi, addr);
3638}
3639
3640void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
3641 uint32_t offset,
3642 Register value_lo,
3643 Register value_hi,
3644 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00003645 Register temp2,
3646 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003647 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00003648 if (offset != 0) {
3649 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003650 __ add(IP, addr, ShifterOperand(temp1));
3651 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003652 }
3653 __ Bind(&fail);
3654 // We need a load followed by store. (The address used in a STREX instruction must
3655 // be the same as the address in the most recently executed LDREX instruction.)
3656 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00003657 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003658 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003659 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00003660}
3661
3662void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3663 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3664
Nicolas Geoffray39468442014-09-02 15:17:15 +01003665 LocationSummary* locations =
3666 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003667 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003668
Calin Juravle52c48962014-12-16 17:02:57 +00003669 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003670 if (Primitive::IsFloatingPointType(field_type)) {
3671 locations->SetInAt(1, Location::RequiresFpuRegister());
3672 } else {
3673 locations->SetInAt(1, Location::RequiresRegister());
3674 }
3675
Calin Juravle52c48962014-12-16 17:02:57 +00003676 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00003677 bool generate_volatile = field_info.IsVolatile()
3678 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003679 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01003680 bool needs_write_barrier =
3681 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003682 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00003683 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01003684 if (needs_write_barrier) {
3685 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003686 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003687 } else if (generate_volatile) {
Calin Juravle52c48962014-12-16 17:02:57 +00003688 // Arm encoding have some additional constraints for ldrexd/strexd:
3689 // - registers need to be consecutive
3690 // - the first register should be even but not R14.
3691 // We don't test for Arm yet, and the assertion makes sure that we revisit this if we ever
3692 // enable Arm encoding.
3693 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3694
3695 locations->AddTemp(Location::RequiresRegister());
3696 locations->AddTemp(Location::RequiresRegister());
3697 if (field_type == Primitive::kPrimDouble) {
3698 // For doubles we need two more registers to copy the value.
3699 locations->AddTemp(Location::RegisterLocation(R2));
3700 locations->AddTemp(Location::RegisterLocation(R3));
3701 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003702 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003703}
3704
Calin Juravle52c48962014-12-16 17:02:57 +00003705void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003706 const FieldInfo& field_info,
3707 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003708 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3709
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003710 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003711 Register base = locations->InAt(0).AsRegister<Register>();
3712 Location value = locations->InAt(1);
3713
3714 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003715 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00003716 Primitive::Type field_type = field_info.GetFieldType();
3717 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01003718 bool needs_write_barrier =
3719 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003720
3721 if (is_volatile) {
3722 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3723 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003724
3725 switch (field_type) {
3726 case Primitive::kPrimBoolean:
3727 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003728 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003729 break;
3730 }
3731
3732 case Primitive::kPrimShort:
3733 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003734 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003735 break;
3736 }
3737
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003738 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003739 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01003740 if (kPoisonHeapReferences && needs_write_barrier) {
3741 // Note that in the case where `value` is a null reference,
3742 // we do not enter this block, as a null reference does not
3743 // need poisoning.
3744 DCHECK_EQ(field_type, Primitive::kPrimNot);
3745 Register temp = locations->GetTemp(0).AsRegister<Register>();
3746 __ Mov(temp, value.AsRegister<Register>());
3747 __ PoisonHeapReference(temp);
3748 __ StoreToOffset(kStoreWord, temp, base, offset);
3749 } else {
3750 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
3751 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003752 break;
3753 }
3754
3755 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00003756 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003757 GenerateWideAtomicStore(base, offset,
3758 value.AsRegisterPairLow<Register>(),
3759 value.AsRegisterPairHigh<Register>(),
3760 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003761 locations->GetTemp(1).AsRegister<Register>(),
3762 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003763 } else {
3764 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003765 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003766 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003767 break;
3768 }
3769
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003770 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003771 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003772 break;
3773 }
3774
3775 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003776 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00003777 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003778 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
3779 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
3780
3781 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
3782
3783 GenerateWideAtomicStore(base, offset,
3784 value_reg_lo,
3785 value_reg_hi,
3786 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003787 locations->GetTemp(3).AsRegister<Register>(),
3788 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003789 } else {
3790 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003791 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003792 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003793 break;
3794 }
3795
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003796 case Primitive::kPrimVoid:
3797 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003798 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003799 }
Calin Juravle52c48962014-12-16 17:02:57 +00003800
Calin Juravle77520bc2015-01-12 18:45:46 +00003801 // Longs and doubles are handled in the switch.
3802 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
3803 codegen_->MaybeRecordImplicitNullCheck(instruction);
3804 }
3805
3806 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3807 Register temp = locations->GetTemp(0).AsRegister<Register>();
3808 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003809 codegen_->MarkGCCard(
3810 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003811 }
3812
Calin Juravle52c48962014-12-16 17:02:57 +00003813 if (is_volatile) {
3814 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3815 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003816}
3817
Calin Juravle52c48962014-12-16 17:02:57 +00003818void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3819 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain3b359c72015-11-17 19:35:12 +00003820
3821 bool object_field_get_with_read_barrier =
3822 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003823 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00003824 new (GetGraph()->GetArena()) LocationSummary(instruction,
3825 object_field_get_with_read_barrier ?
3826 LocationSummary::kCallOnSlowPath :
3827 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003828 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003829
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003830 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00003831 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003832 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain3b359c72015-11-17 19:35:12 +00003833 // The output overlaps in case of volatile long: we don't want the
3834 // code generated by GenerateWideAtomicLoad to overwrite the
3835 // object's location. Likewise, in the case of an object field get
3836 // with read barriers enabled, we do not want the load to overwrite
3837 // the object's location, as we need it to emit the read barrier.
3838 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
3839 object_field_get_with_read_barrier;
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01003840
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003841 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3842 locations->SetOut(Location::RequiresFpuRegister());
3843 } else {
3844 locations->SetOut(Location::RequiresRegister(),
3845 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
3846 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003847 if (volatile_for_double) {
Calin Juravle52c48962014-12-16 17:02:57 +00003848 // Arm encoding have some additional constraints for ldrexd/strexd:
3849 // - registers need to be consecutive
3850 // - the first register should be even but not R14.
3851 // We don't test for Arm yet, and the assertion makes sure that we revisit this if we ever
3852 // enable Arm encoding.
3853 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3854 locations->AddTemp(Location::RequiresRegister());
3855 locations->AddTemp(Location::RequiresRegister());
3856 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003857}
3858
Vladimir Markod2b4ca22015-09-14 15:13:26 +01003859Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
3860 Opcode opcode) {
3861 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
3862 if (constant->IsConstant() &&
3863 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
3864 return Location::ConstantLocation(constant->AsConstant());
3865 }
3866 return Location::RequiresRegister();
3867}
3868
3869bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
3870 Opcode opcode) {
3871 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
3872 if (Primitive::Is64BitType(input_cst->GetType())) {
3873 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode) &&
3874 CanEncodeConstantAsImmediate(High32Bits(value), opcode);
3875 } else {
3876 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
3877 }
3878}
3879
3880bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value, Opcode opcode) {
3881 ShifterOperand so;
3882 ArmAssembler* assembler = codegen_->GetAssembler();
3883 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, &so)) {
3884 return true;
3885 }
3886 Opcode neg_opcode = kNoOperand;
3887 switch (opcode) {
3888 case AND:
3889 neg_opcode = BIC;
3890 break;
3891 case ORR:
3892 neg_opcode = ORN;
3893 break;
3894 default:
3895 return false;
3896 }
3897 return assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, neg_opcode, ~value, &so);
3898}
3899
Calin Juravle52c48962014-12-16 17:02:57 +00003900void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
3901 const FieldInfo& field_info) {
3902 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003903
Calin Juravle52c48962014-12-16 17:02:57 +00003904 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00003905 Location base_loc = locations->InAt(0);
3906 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00003907 Location out = locations->Out();
3908 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003909 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00003910 Primitive::Type field_type = field_info.GetFieldType();
3911 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3912
3913 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003914 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003915 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003916 break;
3917 }
3918
3919 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003920 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003921 break;
3922 }
3923
3924 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003925 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003926 break;
3927 }
3928
3929 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003930 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003931 break;
3932 }
3933
3934 case Primitive::kPrimInt:
3935 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003936 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003937 break;
3938 }
3939
3940 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00003941 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003942 GenerateWideAtomicLoad(base, offset,
3943 out.AsRegisterPairLow<Register>(),
3944 out.AsRegisterPairHigh<Register>());
3945 } else {
3946 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
3947 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003948 break;
3949 }
3950
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003951 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003952 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003953 break;
3954 }
3955
3956 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003957 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00003958 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003959 Register lo = locations->GetTemp(0).AsRegister<Register>();
3960 Register hi = locations->GetTemp(1).AsRegister<Register>();
3961 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00003962 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003963 __ vmovdrr(out_reg, lo, hi);
3964 } else {
3965 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003966 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003967 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003968 break;
3969 }
3970
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003971 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003972 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003973 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003974 }
Calin Juravle52c48962014-12-16 17:02:57 +00003975
Calin Juravle77520bc2015-01-12 18:45:46 +00003976 // Doubles are handled in the switch.
3977 if (field_type != Primitive::kPrimDouble) {
3978 codegen_->MaybeRecordImplicitNullCheck(instruction);
3979 }
3980
Calin Juravle52c48962014-12-16 17:02:57 +00003981 if (is_volatile) {
3982 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3983 }
Roland Levillain4d027112015-07-01 15:41:14 +01003984
3985 if (field_type == Primitive::kPrimNot) {
Roland Levillain3b359c72015-11-17 19:35:12 +00003986 codegen_->MaybeGenerateReadBarrier(instruction, out, out, base_loc, offset);
Roland Levillain4d027112015-07-01 15:41:14 +01003987 }
Calin Juravle52c48962014-12-16 17:02:57 +00003988}
3989
3990void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3991 HandleFieldSet(instruction, instruction->GetFieldInfo());
3992}
3993
3994void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003995 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003996}
3997
3998void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3999 HandleFieldGet(instruction, instruction->GetFieldInfo());
4000}
4001
4002void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4003 HandleFieldGet(instruction, instruction->GetFieldInfo());
4004}
4005
4006void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4007 HandleFieldGet(instruction, instruction->GetFieldInfo());
4008}
4009
4010void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4011 HandleFieldGet(instruction, instruction->GetFieldInfo());
4012}
4013
4014void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4015 HandleFieldSet(instruction, instruction->GetFieldInfo());
4016}
4017
4018void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004019 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004020}
4021
Calin Juravlee460d1d2015-09-29 04:52:17 +01004022void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
4023 HUnresolvedInstanceFieldGet* instruction) {
4024 FieldAccessCallingConventionARM calling_convention;
4025 codegen_->CreateUnresolvedFieldLocationSummary(
4026 instruction, instruction->GetFieldType(), calling_convention);
4027}
4028
4029void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
4030 HUnresolvedInstanceFieldGet* instruction) {
4031 FieldAccessCallingConventionARM calling_convention;
4032 codegen_->GenerateUnresolvedFieldAccess(instruction,
4033 instruction->GetFieldType(),
4034 instruction->GetFieldIndex(),
4035 instruction->GetDexPc(),
4036 calling_convention);
4037}
4038
4039void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
4040 HUnresolvedInstanceFieldSet* instruction) {
4041 FieldAccessCallingConventionARM calling_convention;
4042 codegen_->CreateUnresolvedFieldLocationSummary(
4043 instruction, instruction->GetFieldType(), calling_convention);
4044}
4045
4046void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
4047 HUnresolvedInstanceFieldSet* instruction) {
4048 FieldAccessCallingConventionARM calling_convention;
4049 codegen_->GenerateUnresolvedFieldAccess(instruction,
4050 instruction->GetFieldType(),
4051 instruction->GetFieldIndex(),
4052 instruction->GetDexPc(),
4053 calling_convention);
4054}
4055
4056void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
4057 HUnresolvedStaticFieldGet* instruction) {
4058 FieldAccessCallingConventionARM calling_convention;
4059 codegen_->CreateUnresolvedFieldLocationSummary(
4060 instruction, instruction->GetFieldType(), calling_convention);
4061}
4062
4063void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
4064 HUnresolvedStaticFieldGet* instruction) {
4065 FieldAccessCallingConventionARM calling_convention;
4066 codegen_->GenerateUnresolvedFieldAccess(instruction,
4067 instruction->GetFieldType(),
4068 instruction->GetFieldIndex(),
4069 instruction->GetDexPc(),
4070 calling_convention);
4071}
4072
4073void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
4074 HUnresolvedStaticFieldSet* instruction) {
4075 FieldAccessCallingConventionARM calling_convention;
4076 codegen_->CreateUnresolvedFieldLocationSummary(
4077 instruction, instruction->GetFieldType(), calling_convention);
4078}
4079
4080void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
4081 HUnresolvedStaticFieldSet* instruction) {
4082 FieldAccessCallingConventionARM calling_convention;
4083 codegen_->GenerateUnresolvedFieldAccess(instruction,
4084 instruction->GetFieldType(),
4085 instruction->GetFieldIndex(),
4086 instruction->GetDexPc(),
4087 calling_convention);
4088}
4089
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004090void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004091 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4092 ? LocationSummary::kCallOnSlowPath
4093 : LocationSummary::kNoCall;
4094 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravle77520bc2015-01-12 18:45:46 +00004095 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004096 if (instruction->HasUses()) {
4097 locations->SetOut(Location::SameAsFirstInput());
4098 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004099}
4100
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004101void InstructionCodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004102 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4103 return;
4104 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004105 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004106
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004107 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
4108 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4109}
4110
4111void InstructionCodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004112 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004113 codegen_->AddSlowPath(slow_path);
4114
4115 LocationSummary* locations = instruction->GetLocations();
4116 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004117
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004118 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004119}
4120
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004121void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004122 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004123 GenerateImplicitNullCheck(instruction);
4124 } else {
4125 GenerateExplicitNullCheck(instruction);
4126 }
4127}
4128
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004129void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004130 bool object_array_get_with_read_barrier =
4131 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004132 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004133 new (GetGraph()->GetArena()) LocationSummary(instruction,
4134 object_array_get_with_read_barrier ?
4135 LocationSummary::kCallOnSlowPath :
4136 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004137 locations->SetInAt(0, Location::RequiresRegister());
4138 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004139 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4140 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4141 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004142 // The output overlaps in the case of an object array get with
4143 // read barriers enabled: we do not want the move to overwrite the
4144 // array's location, as we need it to emit the read barrier.
4145 locations->SetOut(
4146 Location::RequiresRegister(),
4147 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004148 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004149}
4150
4151void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
4152 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004153 Location obj_loc = locations->InAt(0);
4154 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004155 Location index = locations->InAt(1);
Roland Levillain4d027112015-07-01 15:41:14 +01004156 Primitive::Type type = instruction->GetType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004157
Roland Levillain4d027112015-07-01 15:41:14 +01004158 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004159 case Primitive::kPrimBoolean: {
4160 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004161 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004162 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004163 size_t offset =
4164 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004165 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
4166 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004167 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004168 __ LoadFromOffset(kLoadUnsignedByte, out, IP, data_offset);
4169 }
4170 break;
4171 }
4172
4173 case Primitive::kPrimByte: {
4174 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004175 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004176 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004177 size_t offset =
4178 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004179 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
4180 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004181 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004182 __ LoadFromOffset(kLoadSignedByte, out, IP, data_offset);
4183 }
4184 break;
4185 }
4186
4187 case Primitive::kPrimShort: {
4188 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004189 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004190 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004191 size_t offset =
4192 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004193 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
4194 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004195 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004196 __ LoadFromOffset(kLoadSignedHalfword, out, IP, data_offset);
4197 }
4198 break;
4199 }
4200
4201 case Primitive::kPrimChar: {
4202 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004203 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004204 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004205 size_t offset =
4206 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004207 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
4208 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004209 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004210 __ LoadFromOffset(kLoadUnsignedHalfword, out, IP, data_offset);
4211 }
4212 break;
4213 }
4214
4215 case Primitive::kPrimInt:
4216 case Primitive::kPrimNot: {
Roland Levillain3b359c72015-11-17 19:35:12 +00004217 static_assert(
4218 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4219 "art::mirror::HeapReference<mirror::Object> and int32_t have different sizes.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004220 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004221 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004222 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004223 size_t offset =
4224 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004225 __ LoadFromOffset(kLoadWord, out, obj, offset);
4226 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004227 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004228 __ LoadFromOffset(kLoadWord, out, IP, data_offset);
4229 }
4230 break;
4231 }
4232
4233 case Primitive::kPrimLong: {
4234 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004235 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004236 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004237 size_t offset =
4238 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004239 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004240 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004241 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004242 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004243 }
4244 break;
4245 }
4246
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004247 case Primitive::kPrimFloat: {
4248 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4249 Location out = locations->Out();
4250 DCHECK(out.IsFpuRegister());
4251 if (index.IsConstant()) {
4252 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4253 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), obj, offset);
4254 } else {
4255 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4256 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), IP, data_offset);
4257 }
4258 break;
4259 }
4260
4261 case Primitive::kPrimDouble: {
4262 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4263 Location out = locations->Out();
4264 DCHECK(out.IsFpuRegisterPair());
4265 if (index.IsConstant()) {
4266 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
4267 __ LoadDFromOffset(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()), obj, offset);
4268 } else {
4269 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
4270 __ LoadDFromOffset(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
4271 }
4272 break;
4273 }
4274
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004275 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004276 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004277 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004278 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004279 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004280
4281 if (type == Primitive::kPrimNot) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004282 static_assert(
4283 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4284 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
4285 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4286 Location out = locations->Out();
4287 if (index.IsConstant()) {
4288 uint32_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4289 codegen_->MaybeGenerateReadBarrier(instruction, out, out, obj_loc, offset);
4290 } else {
4291 codegen_->MaybeGenerateReadBarrier(instruction, out, out, obj_loc, data_offset, index);
4292 }
Roland Levillain4d027112015-07-01 15:41:14 +01004293 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004294}
4295
4296void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004297 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004298
4299 bool needs_write_barrier =
4300 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain3b359c72015-11-17 19:35:12 +00004301 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
4302 bool object_array_set_with_read_barrier =
4303 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004304
Nicolas Geoffray39468442014-09-02 15:17:15 +01004305 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004306 instruction,
Roland Levillain3b359c72015-11-17 19:35:12 +00004307 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
4308 LocationSummary::kCallOnSlowPath :
4309 LocationSummary::kNoCall);
4310
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004311 locations->SetInAt(0, Location::RequiresRegister());
4312 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4313 if (Primitive::IsFloatingPointType(value_type)) {
4314 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004315 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004316 locations->SetInAt(2, Location::RequiresRegister());
4317 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004318 if (needs_write_barrier) {
4319 // Temporary registers for the write barrier.
4320 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Roland Levillain3b359c72015-11-17 19:35:12 +00004321 locations->AddTemp(Location::RequiresRegister()); // Possibly used for read barrier too.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004322 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004323}
4324
4325void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
4326 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004327 Location array_loc = locations->InAt(0);
4328 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004329 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004330 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain3b359c72015-11-17 19:35:12 +00004331 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004332 bool needs_write_barrier =
4333 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004334
4335 switch (value_type) {
4336 case Primitive::kPrimBoolean:
4337 case Primitive::kPrimByte: {
4338 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004339 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004340 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004341 size_t offset =
4342 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004343 __ StoreToOffset(kStoreByte, value, array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004344 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004345 __ add(IP, array, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004346 __ StoreToOffset(kStoreByte, value, IP, data_offset);
4347 }
4348 break;
4349 }
4350
4351 case Primitive::kPrimShort:
4352 case Primitive::kPrimChar: {
4353 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004354 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004355 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004356 size_t offset =
4357 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004358 __ StoreToOffset(kStoreHalfword, value, array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004359 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004360 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004361 __ StoreToOffset(kStoreHalfword, value, IP, data_offset);
4362 }
4363 break;
4364 }
4365
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004366 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004367 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00004368 Location value_loc = locations->InAt(2);
4369 Register value = value_loc.AsRegister<Register>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004370 Register source = value;
4371
4372 if (instruction->InputAt(2)->IsNullConstant()) {
4373 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004374 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004375 size_t offset =
4376 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004377 __ StoreToOffset(kStoreWord, source, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004378 } else {
4379 DCHECK(index.IsRegister()) << index;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004380 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillain4d027112015-07-01 15:41:14 +01004381 __ StoreToOffset(kStoreWord, source, IP, data_offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004382 }
Roland Levillain3b359c72015-11-17 19:35:12 +00004383 DCHECK(!needs_write_barrier);
4384 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004385 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004386 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004387
4388 DCHECK(needs_write_barrier);
4389 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
4390 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
4391 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4392 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4393 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4394 Label done;
4395 SlowPathCode* slow_path = nullptr;
4396
Roland Levillain3b359c72015-11-17 19:35:12 +00004397 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004398 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
4399 codegen_->AddSlowPath(slow_path);
4400 if (instruction->GetValueCanBeNull()) {
4401 Label non_zero;
4402 __ CompareAndBranchIfNonZero(value, &non_zero);
4403 if (index.IsConstant()) {
4404 size_t offset =
4405 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4406 __ StoreToOffset(kStoreWord, value, array, offset);
4407 } else {
4408 DCHECK(index.IsRegister()) << index;
4409 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4410 __ StoreToOffset(kStoreWord, value, IP, data_offset);
4411 }
4412 codegen_->MaybeRecordImplicitNullCheck(instruction);
4413 __ b(&done);
4414 __ Bind(&non_zero);
4415 }
4416
Roland Levillain3b359c72015-11-17 19:35:12 +00004417 if (kEmitCompilerReadBarrier) {
4418 // When read barriers are enabled, the type checking
4419 // instrumentation requires two read barriers:
4420 //
4421 // __ Mov(temp2, temp1);
4422 // // /* HeapReference<Class> */ temp1 = temp1->component_type_
4423 // __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
4424 // codegen_->GenerateReadBarrier(
4425 // instruction, temp1_loc, temp1_loc, temp2_loc, component_offset);
4426 //
4427 // // /* HeapReference<Class> */ temp2 = value->klass_
4428 // __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
4429 // codegen_->GenerateReadBarrier(
4430 // instruction, temp2_loc, temp2_loc, value_loc, class_offset, temp1_loc);
4431 //
4432 // __ cmp(temp1, ShifterOperand(temp2));
4433 //
4434 // However, the second read barrier may trash `temp`, as it
4435 // is a temporary register, and as such would not be saved
4436 // along with live registers before calling the runtime (nor
4437 // restored afterwards). So in this case, we bail out and
4438 // delegate the work to the array set slow path.
4439 //
4440 // TODO: Extend the register allocator to support a new
4441 // "(locally) live temp" location so as to avoid always
4442 // going into the slow path when read barriers are enabled.
4443 __ b(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004444 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004445 // /* HeapReference<Class> */ temp1 = array->klass_
4446 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
4447 codegen_->MaybeRecordImplicitNullCheck(instruction);
4448 __ MaybeUnpoisonHeapReference(temp1);
4449
4450 // /* HeapReference<Class> */ temp1 = temp1->component_type_
4451 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
4452 // /* HeapReference<Class> */ temp2 = value->klass_
4453 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
4454 // If heap poisoning is enabled, no need to unpoison `temp1`
4455 // nor `temp2`, as we are comparing two poisoned references.
4456 __ cmp(temp1, ShifterOperand(temp2));
4457
4458 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4459 Label do_put;
4460 __ b(&do_put, EQ);
4461 // If heap poisoning is enabled, the `temp1` reference has
4462 // not been unpoisoned yet; unpoison it now.
4463 __ MaybeUnpoisonHeapReference(temp1);
4464
4465 // /* HeapReference<Class> */ temp1 = temp1->super_class_
4466 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
4467 // If heap poisoning is enabled, no need to unpoison
4468 // `temp1`, as we are comparing against null below.
4469 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
4470 __ Bind(&do_put);
4471 } else {
4472 __ b(slow_path->GetEntryLabel(), NE);
4473 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004474 }
4475 }
4476
4477 if (kPoisonHeapReferences) {
4478 // Note that in the case where `value` is a null reference,
4479 // we do not enter this block, as a null reference does not
4480 // need poisoning.
4481 DCHECK_EQ(value_type, Primitive::kPrimNot);
4482 __ Mov(temp1, value);
4483 __ PoisonHeapReference(temp1);
4484 source = temp1;
4485 }
4486
4487 if (index.IsConstant()) {
4488 size_t offset =
4489 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4490 __ StoreToOffset(kStoreWord, source, array, offset);
4491 } else {
4492 DCHECK(index.IsRegister()) << index;
4493 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4494 __ StoreToOffset(kStoreWord, source, IP, data_offset);
4495 }
4496
Roland Levillain3b359c72015-11-17 19:35:12 +00004497 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004498 codegen_->MaybeRecordImplicitNullCheck(instruction);
4499 }
4500
4501 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
4502
4503 if (done.IsLinked()) {
4504 __ Bind(&done);
4505 }
4506
4507 if (slow_path != nullptr) {
4508 __ Bind(slow_path->GetExitLabel());
4509 }
4510
4511 break;
4512 }
4513
4514 case Primitive::kPrimInt: {
4515 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4516 Register value = locations->InAt(2).AsRegister<Register>();
4517 if (index.IsConstant()) {
4518 size_t offset =
4519 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4520 __ StoreToOffset(kStoreWord, value, array, offset);
4521 } else {
4522 DCHECK(index.IsRegister()) << index;
4523 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4524 __ StoreToOffset(kStoreWord, value, IP, data_offset);
4525 }
4526
4527 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004528 break;
4529 }
4530
4531 case Primitive::kPrimLong: {
4532 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004533 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004534 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004535 size_t offset =
4536 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004537 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004538 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004539 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004540 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004541 }
4542 break;
4543 }
4544
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004545 case Primitive::kPrimFloat: {
4546 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4547 Location value = locations->InAt(2);
4548 DCHECK(value.IsFpuRegister());
4549 if (index.IsConstant()) {
4550 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004551 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004552 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004553 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004554 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
4555 }
4556 break;
4557 }
4558
4559 case Primitive::kPrimDouble: {
4560 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4561 Location value = locations->InAt(2);
4562 DCHECK(value.IsFpuRegisterPair());
4563 if (index.IsConstant()) {
4564 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004565 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004566 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004567 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004568 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
4569 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004570
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004571 break;
4572 }
4573
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004574 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004575 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004576 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004577 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004578
4579 // Ints and objects are handled in the switch.
4580 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
4581 codegen_->MaybeRecordImplicitNullCheck(instruction);
4582 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004583}
4584
4585void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004586 LocationSummary* locations =
4587 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004588 locations->SetInAt(0, Location::RequiresRegister());
4589 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004590}
4591
4592void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
4593 LocationSummary* locations = instruction->GetLocations();
4594 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004595 Register obj = locations->InAt(0).AsRegister<Register>();
4596 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004597 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004598 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004599}
4600
4601void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004602 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4603 ? LocationSummary::kCallOnSlowPath
4604 : LocationSummary::kNoCall;
4605 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004606 locations->SetInAt(0, Location::RequiresRegister());
4607 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004608 if (instruction->HasUses()) {
4609 locations->SetOut(Location::SameAsFirstInput());
4610 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004611}
4612
4613void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
4614 LocationSummary* locations = instruction->GetLocations();
Andreas Gampe85b62f22015-09-09 13:15:38 -07004615 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004616 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004617 codegen_->AddSlowPath(slow_path);
4618
Roland Levillain271ab9c2014-11-27 15:23:57 +00004619 Register index = locations->InAt(0).AsRegister<Register>();
4620 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004621
4622 __ cmp(index, ShifterOperand(length));
Roland Levillain4fa13f62015-07-06 18:11:54 +01004623 __ b(slow_path->GetEntryLabel(), HS);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004624}
4625
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004626void CodeGeneratorARM::MarkGCCard(Register temp,
4627 Register card,
4628 Register object,
4629 Register value,
4630 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004631 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004632 if (can_be_null) {
4633 __ CompareAndBranchIfZero(value, &is_null);
4634 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004635 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmWordSize>().Int32Value());
4636 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
4637 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004638 if (can_be_null) {
4639 __ Bind(&is_null);
4640 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004641}
4642
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004643void LocationsBuilderARM::VisitTemporary(HTemporary* temp) {
4644 temp->SetLocations(nullptr);
4645}
4646
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004647void InstructionCodeGeneratorARM::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004648 // Nothing to do, this is driven by the code generator.
4649}
4650
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004651void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004652 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004653}
4654
4655void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004656 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4657}
4658
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004659void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
4660 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4661}
4662
4663void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004664 HBasicBlock* block = instruction->GetBlock();
4665 if (block->GetLoopInformation() != nullptr) {
4666 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4667 // The back edge will generate the suspend check.
4668 return;
4669 }
4670 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4671 // The goto will generate the suspend check.
4672 return;
4673 }
4674 GenerateSuspendCheck(instruction, nullptr);
4675}
4676
4677void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
4678 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004679 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004680 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
4681 if (slow_path == nullptr) {
4682 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
4683 instruction->SetSlowPath(slow_path);
4684 codegen_->AddSlowPath(slow_path);
4685 if (successor != nullptr) {
4686 DCHECK(successor->IsLoopHeader());
4687 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4688 }
4689 } else {
4690 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4691 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004692
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00004693 __ LoadFromOffset(
4694 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmWordSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004695 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004696 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004697 __ Bind(slow_path->GetReturnLabel());
4698 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004699 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004700 __ b(slow_path->GetEntryLabel());
4701 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004702}
4703
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004704ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
4705 return codegen_->GetAssembler();
4706}
4707
4708void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004709 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004710 Location source = move->GetSource();
4711 Location destination = move->GetDestination();
4712
4713 if (source.IsRegister()) {
4714 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004715 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004716 } else {
4717 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004718 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004719 SP, destination.GetStackIndex());
4720 }
4721 } else if (source.IsStackSlot()) {
4722 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004723 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004724 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004725 } else if (destination.IsFpuRegister()) {
4726 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004727 } else {
4728 DCHECK(destination.IsStackSlot());
4729 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
4730 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4731 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004732 } else if (source.IsFpuRegister()) {
4733 if (destination.IsFpuRegister()) {
4734 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004735 } else {
4736 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004737 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
4738 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004739 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004740 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004741 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
4742 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004743 } else if (destination.IsRegisterPair()) {
4744 DCHECK(ExpectedPairLayout(destination));
4745 __ LoadFromOffset(
4746 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
4747 } else {
4748 DCHECK(destination.IsFpuRegisterPair()) << destination;
4749 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
4750 SP,
4751 source.GetStackIndex());
4752 }
4753 } else if (source.IsRegisterPair()) {
4754 if (destination.IsRegisterPair()) {
4755 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
4756 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
4757 } else {
4758 DCHECK(destination.IsDoubleStackSlot()) << destination;
4759 DCHECK(ExpectedPairLayout(source));
4760 __ StoreToOffset(
4761 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
4762 }
4763 } else if (source.IsFpuRegisterPair()) {
4764 if (destination.IsFpuRegisterPair()) {
4765 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
4766 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
4767 } else {
4768 DCHECK(destination.IsDoubleStackSlot()) << destination;
4769 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
4770 SP,
4771 destination.GetStackIndex());
4772 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004773 } else {
4774 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004775 HConstant* constant = source.GetConstant();
4776 if (constant->IsIntConstant() || constant->IsNullConstant()) {
4777 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004778 if (destination.IsRegister()) {
4779 __ LoadImmediate(destination.AsRegister<Register>(), value);
4780 } else {
4781 DCHECK(destination.IsStackSlot());
4782 __ LoadImmediate(IP, value);
4783 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4784 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004785 } else if (constant->IsLongConstant()) {
4786 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004787 if (destination.IsRegisterPair()) {
4788 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
4789 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004790 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004791 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004792 __ LoadImmediate(IP, Low32Bits(value));
4793 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4794 __ LoadImmediate(IP, High32Bits(value));
4795 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
4796 }
4797 } else if (constant->IsDoubleConstant()) {
4798 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004799 if (destination.IsFpuRegisterPair()) {
4800 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004801 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004802 DCHECK(destination.IsDoubleStackSlot()) << destination;
4803 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004804 __ LoadImmediate(IP, Low32Bits(int_value));
4805 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4806 __ LoadImmediate(IP, High32Bits(int_value));
4807 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
4808 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004809 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004810 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004811 float value = constant->AsFloatConstant()->GetValue();
4812 if (destination.IsFpuRegister()) {
4813 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
4814 } else {
4815 DCHECK(destination.IsStackSlot());
4816 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
4817 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4818 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004819 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004820 }
4821}
4822
4823void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
4824 __ Mov(IP, reg);
4825 __ LoadFromOffset(kLoadWord, reg, SP, mem);
4826 __ StoreToOffset(kStoreWord, IP, SP, mem);
4827}
4828
4829void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
4830 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
4831 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
4832 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
4833 SP, mem1 + stack_offset);
4834 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
4835 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
4836 SP, mem2 + stack_offset);
4837 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
4838}
4839
4840void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004841 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004842 Location source = move->GetSource();
4843 Location destination = move->GetDestination();
4844
4845 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004846 DCHECK_NE(source.AsRegister<Register>(), IP);
4847 DCHECK_NE(destination.AsRegister<Register>(), IP);
4848 __ Mov(IP, source.AsRegister<Register>());
4849 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
4850 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004851 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004852 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004853 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004854 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004855 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4856 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004857 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00004858 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004859 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00004860 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004861 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004862 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004863 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004864 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004865 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
4866 destination.AsRegisterPairHigh<Register>(),
4867 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004868 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004869 Register low_reg = source.IsRegisterPair()
4870 ? source.AsRegisterPairLow<Register>()
4871 : destination.AsRegisterPairLow<Register>();
4872 int mem = source.IsRegisterPair()
4873 ? destination.GetStackIndex()
4874 : source.GetStackIndex();
4875 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004876 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004877 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004878 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004879 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004880 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
4881 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004882 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004883 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004884 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004885 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
4886 DRegister reg = source.IsFpuRegisterPair()
4887 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
4888 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
4889 int mem = source.IsFpuRegisterPair()
4890 ? destination.GetStackIndex()
4891 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004892 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004893 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004894 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004895 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
4896 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
4897 : destination.AsFpuRegister<SRegister>();
4898 int mem = source.IsFpuRegister()
4899 ? destination.GetStackIndex()
4900 : source.GetStackIndex();
4901
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00004902 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004903 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00004904 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00004905 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00004906 Exchange(source.GetStackIndex(), destination.GetStackIndex());
4907 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004908 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00004909 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004910 }
4911}
4912
4913void ParallelMoveResolverARM::SpillScratch(int reg) {
4914 __ Push(static_cast<Register>(reg));
4915}
4916
4917void ParallelMoveResolverARM::RestoreScratch(int reg) {
4918 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004919}
4920
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004921void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01004922 InvokeRuntimeCallingConvention calling_convention;
4923 CodeGenerator::CreateLoadClassLocationSummary(
4924 cls,
4925 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Roland Levillain3b359c72015-11-17 19:35:12 +00004926 Location::RegisterLocation(R0),
4927 /* code_generator_supports_read_barrier */ true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004928}
4929
4930void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004931 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01004932 if (cls->NeedsAccessCheck()) {
4933 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
4934 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
4935 cls,
4936 cls->GetDexPc(),
4937 nullptr);
Calin Juravle580b6092015-10-06 17:35:58 +01004938 return;
4939 }
4940
Roland Levillain3b359c72015-11-17 19:35:12 +00004941 Location out_loc = locations->Out();
4942 Register out = out_loc.AsRegister<Register>();
Calin Juravle580b6092015-10-06 17:35:58 +01004943 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00004944
Calin Juravle580b6092015-10-06 17:35:58 +01004945 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004946 DCHECK(!cls->CanCallRuntime());
4947 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillain3b359c72015-11-17 19:35:12 +00004948 uint32_t declaring_class_offset = ArtMethod::DeclaringClassOffset().Int32Value();
4949 if (kEmitCompilerReadBarrier) {
4950 // /* GcRoot<mirror::Class>* */ out = &(current_method->declaring_class_)
4951 __ AddConstant(out, current_method, declaring_class_offset);
4952 // /* mirror::Class* */ out = out->Read()
4953 codegen_->GenerateReadBarrierForRoot(cls, out_loc, out_loc);
4954 } else {
4955 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4956 __ LoadFromOffset(kLoadWord, out, current_method, declaring_class_offset);
4957 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004958 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004959 DCHECK(cls->CanCallRuntime());
Roland Levillain3b359c72015-11-17 19:35:12 +00004960 // /* GcRoot<mirror::Class>[] */ out =
4961 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004962 __ LoadFromOffset(kLoadWord,
4963 out,
4964 current_method,
Vladimir Marko05792b92015-08-03 11:56:49 +01004965 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
Roland Levillain3b359c72015-11-17 19:35:12 +00004966
4967 size_t cache_offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
4968 if (kEmitCompilerReadBarrier) {
4969 // /* GcRoot<mirror::Class>* */ out = &out[type_index]
4970 __ AddConstant(out, out, cache_offset);
4971 // /* mirror::Class* */ out = out->Read()
4972 codegen_->GenerateReadBarrierForRoot(cls, out_loc, out_loc);
4973 } else {
4974 // /* GcRoot<mirror::Class> */ out = out[type_index]
4975 __ LoadFromOffset(kLoadWord, out, out, cache_offset);
4976 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004977
Andreas Gampe85b62f22015-09-09 13:15:38 -07004978 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004979 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4980 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004981 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004982 if (cls->MustGenerateClinitCheck()) {
4983 GenerateClassInitializationCheck(slow_path, out);
4984 } else {
4985 __ Bind(slow_path->GetExitLabel());
4986 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004987 }
4988}
4989
4990void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
4991 LocationSummary* locations =
4992 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4993 locations->SetInAt(0, Location::RequiresRegister());
4994 if (check->HasUses()) {
4995 locations->SetOut(Location::SameAsFirstInput());
4996 }
4997}
4998
4999void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005000 // We assume the class is not null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005001 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005002 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005003 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005004 GenerateClassInitializationCheck(slow_path,
5005 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005006}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005007
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005008void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005009 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005010 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
5011 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
5012 __ b(slow_path->GetEntryLabel(), LT);
5013 // Even if the initialized flag is set, we may be in a situation where caches are not synced
5014 // properly. Therefore, we do a memory fence.
5015 __ dmb(ISH);
5016 __ Bind(slow_path->GetExitLabel());
5017}
5018
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005019void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
5020 LocationSummary* locations =
5021 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005022 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005023 locations->SetOut(Location::RequiresRegister());
5024}
5025
5026void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07005027 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005028 codegen_->AddSlowPath(slow_path);
5029
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005030 LocationSummary* locations = load->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005031 Location out_loc = locations->Out();
5032 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005033 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005034
5035 uint32_t declaring_class_offset = ArtMethod::DeclaringClassOffset().Int32Value();
5036 if (kEmitCompilerReadBarrier) {
5037 // /* GcRoot<mirror::Class>* */ out = &(current_method->declaring_class_)
5038 __ AddConstant(out, current_method, declaring_class_offset);
5039 // /* mirror::Class* */ out = out->Read()
5040 codegen_->GenerateReadBarrierForRoot(load, out_loc, out_loc);
5041 } else {
5042 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5043 __ LoadFromOffset(kLoadWord, out, current_method, declaring_class_offset);
5044 }
5045
5046 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
Mathieu Chartiereace4582014-11-24 18:29:54 -08005047 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
Roland Levillain3b359c72015-11-17 19:35:12 +00005048
5049 size_t cache_offset = CodeGenerator::GetCacheOffset(load->GetStringIndex());
5050 if (kEmitCompilerReadBarrier) {
5051 // /* GcRoot<mirror::String>* */ out = &out[string_index]
5052 __ AddConstant(out, out, cache_offset);
5053 // /* mirror::String* */ out = out->Read()
5054 codegen_->GenerateReadBarrierForRoot(load, out_loc, out_loc);
5055 } else {
5056 // /* GcRoot<mirror::String> */ out = out[string_index]
5057 __ LoadFromOffset(kLoadWord, out, out, cache_offset);
5058 }
5059
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005060 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005061 __ Bind(slow_path->GetExitLabel());
5062}
5063
David Brazdilcb1c0552015-08-04 16:22:25 +01005064static int32_t GetExceptionTlsOffset() {
5065 return Thread::ExceptionOffset<kArmWordSize>().Int32Value();
5066}
5067
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005068void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
5069 LocationSummary* locations =
5070 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5071 locations->SetOut(Location::RequiresRegister());
5072}
5073
5074void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005075 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01005076 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
5077}
5078
5079void LocationsBuilderARM::VisitClearException(HClearException* clear) {
5080 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5081}
5082
5083void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005084 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01005085 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005086}
5087
5088void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
5089 LocationSummary* locations =
5090 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5091 InvokeRuntimeCallingConvention calling_convention;
5092 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5093}
5094
5095void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
5096 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00005097 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005098}
5099
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005100void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005101 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain3b359c72015-11-17 19:35:12 +00005102 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5103 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005104 case TypeCheckKind::kExactCheck:
5105 case TypeCheckKind::kAbstractClassCheck:
5106 case TypeCheckKind::kClassHierarchyCheck:
5107 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005108 call_kind =
5109 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005110 break;
5111 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005112 case TypeCheckKind::kUnresolvedCheck:
5113 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005114 call_kind = LocationSummary::kCallOnSlowPath;
5115 break;
5116 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005117
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005118 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain3b359c72015-11-17 19:35:12 +00005119 locations->SetInAt(0, Location::RequiresRegister());
5120 locations->SetInAt(1, Location::RequiresRegister());
5121 // The "out" register is used as a temporary, so it overlaps with the inputs.
5122 // Note that TypeCheckSlowPathARM uses this register too.
5123 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5124 // When read barriers are enabled, we need a temporary register for
5125 // some cases.
5126 if (kEmitCompilerReadBarrier &&
5127 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5128 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5129 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
5130 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005131 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005132}
5133
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005134void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005135 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005136 Location obj_loc = locations->InAt(0);
5137 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005138 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005139 Location out_loc = locations->Out();
5140 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005141 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005142 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5143 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5144 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005145 Label done, zero;
Andreas Gampe85b62f22015-09-09 13:15:38 -07005146 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005147
5148 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005149 // avoid null check if we know obj is not null.
5150 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00005151 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005152 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005153
Roland Levillain3b359c72015-11-17 19:35:12 +00005154 // /* HeapReference<Class> */ out = obj->klass_
5155 __ LoadFromOffset(kLoadWord, out, obj, class_offset);
5156 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005157
5158 switch (instruction->GetTypeCheckKind()) {
5159 case TypeCheckKind::kExactCheck: {
5160 __ cmp(out, ShifterOperand(cls));
5161 // Classes must be equal for the instanceof to succeed.
5162 __ b(&zero, NE);
5163 __ LoadImmediate(out, 1);
5164 __ b(&done);
5165 break;
5166 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005167
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005168 case TypeCheckKind::kAbstractClassCheck: {
5169 // If the class is abstract, we eagerly fetch the super class of the
5170 // object to avoid doing a comparison we know will fail.
5171 Label loop;
5172 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005173 Location temp_loc = kEmitCompilerReadBarrier ? locations->GetTemp(0) : Location::NoLocation();
5174 if (kEmitCompilerReadBarrier) {
5175 // Save the value of `out` into `temp` before overwriting it
5176 // in the following move operation, as we will need it for the
5177 // read barrier below.
5178 Register temp = temp_loc.AsRegister<Register>();
5179 __ Mov(temp, out);
5180 }
5181 // /* HeapReference<Class> */ out = out->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005182 __ LoadFromOffset(kLoadWord, out, out, super_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00005183 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, temp_loc, super_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005184 // If `out` is null, we use it for the result, and jump to `done`.
5185 __ CompareAndBranchIfZero(out, &done);
5186 __ cmp(out, ShifterOperand(cls));
5187 __ b(&loop, NE);
5188 __ LoadImmediate(out, 1);
5189 if (zero.IsLinked()) {
5190 __ b(&done);
5191 }
5192 break;
5193 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005194
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005195 case TypeCheckKind::kClassHierarchyCheck: {
5196 // Walk over the class hierarchy to find a match.
5197 Label loop, success;
5198 __ Bind(&loop);
5199 __ cmp(out, ShifterOperand(cls));
5200 __ b(&success, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005201 Location temp_loc = kEmitCompilerReadBarrier ? locations->GetTemp(0) : Location::NoLocation();
5202 if (kEmitCompilerReadBarrier) {
5203 // Save the value of `out` into `temp` before overwriting it
5204 // in the following move operation, as we will need it for the
5205 // read barrier below.
5206 Register temp = temp_loc.AsRegister<Register>();
5207 __ Mov(temp, out);
5208 }
5209 // /* HeapReference<Class> */ out = out->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005210 __ LoadFromOffset(kLoadWord, out, out, super_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00005211 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, temp_loc, super_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005212 __ CompareAndBranchIfNonZero(out, &loop);
5213 // If `out` is null, we use it for the result, and jump to `done`.
5214 __ b(&done);
5215 __ Bind(&success);
5216 __ LoadImmediate(out, 1);
5217 if (zero.IsLinked()) {
5218 __ b(&done);
5219 }
5220 break;
5221 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005222
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005223 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005224 // Do an exact check.
5225 Label exact_check;
5226 __ cmp(out, ShifterOperand(cls));
5227 __ b(&exact_check, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005228 // Otherwise, we need to check that the object's class is a non-primitive array.
5229 Location temp_loc = kEmitCompilerReadBarrier ? locations->GetTemp(0) : Location::NoLocation();
5230 if (kEmitCompilerReadBarrier) {
5231 // Save the value of `out` into `temp` before overwriting it
5232 // in the following move operation, as we will need it for the
5233 // read barrier below.
5234 Register temp = temp_loc.AsRegister<Register>();
5235 __ Mov(temp, out);
5236 }
5237 // /* HeapReference<Class> */ out = out->component_type_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005238 __ LoadFromOffset(kLoadWord, out, out, component_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00005239 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, temp_loc, component_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005240 // If `out` is null, we use it for the result, and jump to `done`.
5241 __ CompareAndBranchIfZero(out, &done);
5242 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
5243 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
5244 __ CompareAndBranchIfNonZero(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005245 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005246 __ LoadImmediate(out, 1);
5247 __ b(&done);
5248 break;
5249 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005250
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005251 case TypeCheckKind::kArrayCheck: {
5252 __ cmp(out, ShifterOperand(cls));
5253 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain3b359c72015-11-17 19:35:12 +00005254 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5255 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005256 codegen_->AddSlowPath(slow_path);
5257 __ b(slow_path->GetEntryLabel(), NE);
5258 __ LoadImmediate(out, 1);
5259 if (zero.IsLinked()) {
5260 __ b(&done);
5261 }
5262 break;
5263 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005264
Calin Juravle98893e12015-10-02 21:05:03 +01005265 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005266 case TypeCheckKind::kInterfaceCheck: {
5267 // Note that we indeed only call on slow path, but we always go
5268 // into the slow path for the unresolved & interface check
5269 // cases.
5270 //
5271 // We cannot directly call the InstanceofNonTrivial runtime
5272 // entry point without resorting to a type checking slow path
5273 // here (i.e. by calling InvokeRuntime directly), as it would
5274 // require to assign fixed registers for the inputs of this
5275 // HInstanceOf instruction (following the runtime calling
5276 // convention), which might be cluttered by the potential first
5277 // read barrier emission at the beginning of this method.
5278 DCHECK(locations->OnlyCallsOnSlowPath());
5279 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5280 /* is_fatal */ false);
5281 codegen_->AddSlowPath(slow_path);
5282 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005283 if (zero.IsLinked()) {
5284 __ b(&done);
5285 }
5286 break;
5287 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005288 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005289
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005290 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005291 __ Bind(&zero);
5292 __ LoadImmediate(out, 0);
5293 }
5294
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005295 if (done.IsLinked()) {
5296 __ Bind(&done);
5297 }
5298
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005299 if (slow_path != nullptr) {
5300 __ Bind(slow_path->GetExitLabel());
5301 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005302}
5303
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005304void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005305 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5306 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
5307
Roland Levillain3b359c72015-11-17 19:35:12 +00005308 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5309 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005310 case TypeCheckKind::kExactCheck:
5311 case TypeCheckKind::kAbstractClassCheck:
5312 case TypeCheckKind::kClassHierarchyCheck:
5313 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005314 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5315 LocationSummary::kCallOnSlowPath :
5316 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005317 break;
5318 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005319 case TypeCheckKind::kUnresolvedCheck:
5320 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005321 call_kind = LocationSummary::kCallOnSlowPath;
5322 break;
5323 }
5324
Roland Levillain3b359c72015-11-17 19:35:12 +00005325 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5326 locations->SetInAt(0, Location::RequiresRegister());
5327 locations->SetInAt(1, Location::RequiresRegister());
5328 // Note that TypeCheckSlowPathARM uses this "temp" register too.
5329 locations->AddTemp(Location::RequiresRegister());
5330 // When read barriers are enabled, we need an additional temporary
5331 // register for some cases.
5332 if (kEmitCompilerReadBarrier &&
5333 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5334 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5335 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005336 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005337 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005338}
5339
5340void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
5341 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005342 Location obj_loc = locations->InAt(0);
5343 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005344 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005345 Location temp_loc = locations->GetTemp(0);
5346 Register temp = temp_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005347 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005348 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5349 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5350 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005351
Roland Levillain3b359c72015-11-17 19:35:12 +00005352 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5353 bool is_type_check_slow_path_fatal =
5354 (type_check_kind == TypeCheckKind::kExactCheck ||
5355 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5356 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5357 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5358 !instruction->CanThrowIntoCatchBlock();
5359 SlowPathCode* type_check_slow_path =
5360 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5361 is_type_check_slow_path_fatal);
5362 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005363
5364 Label done;
5365 // Avoid null check if we know obj is not null.
5366 if (instruction->MustDoNullCheck()) {
5367 __ CompareAndBranchIfZero(obj, &done);
5368 }
5369
Roland Levillain3b359c72015-11-17 19:35:12 +00005370 // /* HeapReference<Class> */ temp = obj->klass_
5371 __ LoadFromOffset(kLoadWord, temp, obj, class_offset);
5372 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005373
Roland Levillain3b359c72015-11-17 19:35:12 +00005374 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005375 case TypeCheckKind::kExactCheck:
5376 case TypeCheckKind::kArrayCheck: {
5377 __ cmp(temp, ShifterOperand(cls));
5378 // Jump to slow path for throwing the exception or doing a
5379 // more involved array check.
Roland Levillain3b359c72015-11-17 19:35:12 +00005380 __ b(type_check_slow_path->GetEntryLabel(), NE);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005381 break;
5382 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005383
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005384 case TypeCheckKind::kAbstractClassCheck: {
5385 // If the class is abstract, we eagerly fetch the super class of the
5386 // object to avoid doing a comparison we know will fail.
Roland Levillain3b359c72015-11-17 19:35:12 +00005387 Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005388 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005389 Location temp2_loc =
5390 kEmitCompilerReadBarrier ? locations->GetTemp(1) : Location::NoLocation();
5391 if (kEmitCompilerReadBarrier) {
5392 // Save the value of `temp` into `temp2` before overwriting it
5393 // in the following move operation, as we will need it for the
5394 // read barrier below.
5395 Register temp2 = temp2_loc.AsRegister<Register>();
5396 __ Mov(temp2, temp);
5397 }
5398 // /* HeapReference<Class> */ temp = temp->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005399 __ LoadFromOffset(kLoadWord, temp, temp, super_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00005400 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, temp2_loc, super_offset);
5401
5402 // If the class reference currently in `temp` is not null, jump
5403 // to the `compare_classes` label to compare it with the checked
5404 // class.
5405 __ CompareAndBranchIfNonZero(temp, &compare_classes);
5406 // Otherwise, jump to the slow path to throw the exception.
5407 //
5408 // But before, move back the object's class into `temp` before
5409 // going into the slow path, as it has been overwritten in the
5410 // meantime.
5411 // /* HeapReference<Class> */ temp = obj->klass_
5412 __ LoadFromOffset(kLoadWord, temp, obj, class_offset);
5413 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
5414 __ b(type_check_slow_path->GetEntryLabel());
5415
5416 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005417 __ cmp(temp, ShifterOperand(cls));
5418 __ b(&loop, NE);
5419 break;
5420 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005421
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005422 case TypeCheckKind::kClassHierarchyCheck: {
5423 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005424 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005425 __ Bind(&loop);
5426 __ cmp(temp, ShifterOperand(cls));
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005427 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005428
5429 Location temp2_loc =
5430 kEmitCompilerReadBarrier ? locations->GetTemp(1) : Location::NoLocation();
5431 if (kEmitCompilerReadBarrier) {
5432 // Save the value of `temp` into `temp2` before overwriting it
5433 // in the following move operation, as we will need it for the
5434 // read barrier below.
5435 Register temp2 = temp2_loc.AsRegister<Register>();
5436 __ Mov(temp2, temp);
5437 }
5438 // /* HeapReference<Class> */ temp = temp->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005439 __ LoadFromOffset(kLoadWord, temp, temp, super_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00005440 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, temp2_loc, super_offset);
5441
5442 // If the class reference currently in `temp` is not null, jump
5443 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005444 __ CompareAndBranchIfNonZero(temp, &loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005445 // Otherwise, jump to the slow path to throw the exception.
5446 //
5447 // But before, move back the object's class into `temp` before
5448 // going into the slow path, as it has been overwritten in the
5449 // meantime.
5450 // /* HeapReference<Class> */ temp = obj->klass_
5451 __ LoadFromOffset(kLoadWord, temp, obj, class_offset);
5452 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
5453 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005454 break;
5455 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005456
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005457 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005458 // Do an exact check.
Roland Levillain3b359c72015-11-17 19:35:12 +00005459 Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005460 __ cmp(temp, ShifterOperand(cls));
5461 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005462
5463 // Otherwise, we need to check that the object's class is a non-primitive array.
5464 Location temp2_loc =
5465 kEmitCompilerReadBarrier ? locations->GetTemp(1) : Location::NoLocation();
5466 if (kEmitCompilerReadBarrier) {
5467 // Save the value of `temp` into `temp2` before overwriting it
5468 // in the following move operation, as we will need it for the
5469 // read barrier below.
5470 Register temp2 = temp2_loc.AsRegister<Register>();
5471 __ Mov(temp2, temp);
5472 }
5473 // /* HeapReference<Class> */ temp = temp->component_type_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005474 __ LoadFromOffset(kLoadWord, temp, temp, component_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00005475 codegen_->MaybeGenerateReadBarrier(
5476 instruction, temp_loc, temp_loc, temp2_loc, component_offset);
5477
5478 // If the component type is not null (i.e. the object is indeed
5479 // an array), jump to label `check_non_primitive_component_type`
5480 // to further check that this component type is not a primitive
5481 // type.
5482 __ CompareAndBranchIfNonZero(temp, &check_non_primitive_component_type);
5483 // Otherwise, jump to the slow path to throw the exception.
5484 //
5485 // But before, move back the object's class into `temp` before
5486 // going into the slow path, as it has been overwritten in the
5487 // meantime.
5488 // /* HeapReference<Class> */ temp = obj->klass_
5489 __ LoadFromOffset(kLoadWord, temp, obj, class_offset);
5490 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
5491 __ b(type_check_slow_path->GetEntryLabel());
5492
5493 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005494 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00005495 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
5496 __ CompareAndBranchIfZero(temp, &done);
5497 // Same comment as above regarding `temp` and the slow path.
5498 // /* HeapReference<Class> */ temp = obj->klass_
5499 __ LoadFromOffset(kLoadWord, temp, obj, class_offset);
5500 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
5501 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005502 break;
5503 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005504
Calin Juravle98893e12015-10-02 21:05:03 +01005505 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005506 case TypeCheckKind::kInterfaceCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005507 // We always go into the type check slow path for the unresolved &
5508 // interface check cases.
5509 //
5510 // We cannot directly call the CheckCast runtime entry point
5511 // without resorting to a type checking slow path here (i.e. by
5512 // calling InvokeRuntime directly), as it would require to
5513 // assign fixed registers for the inputs of this HInstanceOf
5514 // instruction (following the runtime calling convention), which
5515 // might be cluttered by the potential first read barrier
5516 // emission at the beginning of this method.
5517 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005518 break;
5519 }
5520 __ Bind(&done);
5521
Roland Levillain3b359c72015-11-17 19:35:12 +00005522 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005523}
5524
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005525void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
5526 LocationSummary* locations =
5527 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5528 InvokeRuntimeCallingConvention calling_convention;
5529 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5530}
5531
5532void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
5533 codegen_->InvokeRuntime(instruction->IsEnter()
5534 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
5535 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00005536 instruction->GetDexPc(),
5537 nullptr);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005538}
5539
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005540void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
5541void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
5542void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005543
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005544void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005545 LocationSummary* locations =
5546 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5547 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
5548 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005549 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005550 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005551 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00005552 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005553}
5554
5555void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
5556 HandleBitwiseOperation(instruction);
5557}
5558
5559void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
5560 HandleBitwiseOperation(instruction);
5561}
5562
5563void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
5564 HandleBitwiseOperation(instruction);
5565}
5566
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005567void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
5568 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
5569 if (value == 0xffffffffu) {
5570 if (out != first) {
5571 __ mov(out, ShifterOperand(first));
5572 }
5573 return;
5574 }
5575 if (value == 0u) {
5576 __ mov(out, ShifterOperand(0));
5577 return;
5578 }
5579 ShifterOperand so;
5580 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
5581 __ and_(out, first, so);
5582 } else {
5583 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so));
5584 __ bic(out, first, ShifterOperand(~value));
5585 }
5586}
5587
5588void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
5589 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
5590 if (value == 0u) {
5591 if (out != first) {
5592 __ mov(out, ShifterOperand(first));
5593 }
5594 return;
5595 }
5596 if (value == 0xffffffffu) {
5597 __ mvn(out, ShifterOperand(0));
5598 return;
5599 }
5600 ShifterOperand so;
5601 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
5602 __ orr(out, first, so);
5603 } else {
5604 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
5605 __ orn(out, first, ShifterOperand(~value));
5606 }
5607}
5608
5609void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
5610 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
5611 if (value == 0u) {
5612 if (out != first) {
5613 __ mov(out, ShifterOperand(first));
5614 }
5615 return;
5616 }
5617 __ eor(out, first, ShifterOperand(value));
5618}
5619
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005620void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
5621 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005622 Location first = locations->InAt(0);
5623 Location second = locations->InAt(1);
5624 Location out = locations->Out();
5625
5626 if (second.IsConstant()) {
5627 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
5628 uint32_t value_low = Low32Bits(value);
5629 if (instruction->GetResultType() == Primitive::kPrimInt) {
5630 Register first_reg = first.AsRegister<Register>();
5631 Register out_reg = out.AsRegister<Register>();
5632 if (instruction->IsAnd()) {
5633 GenerateAndConst(out_reg, first_reg, value_low);
5634 } else if (instruction->IsOr()) {
5635 GenerateOrrConst(out_reg, first_reg, value_low);
5636 } else {
5637 DCHECK(instruction->IsXor());
5638 GenerateEorConst(out_reg, first_reg, value_low);
5639 }
5640 } else {
5641 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
5642 uint32_t value_high = High32Bits(value);
5643 Register first_low = first.AsRegisterPairLow<Register>();
5644 Register first_high = first.AsRegisterPairHigh<Register>();
5645 Register out_low = out.AsRegisterPairLow<Register>();
5646 Register out_high = out.AsRegisterPairHigh<Register>();
5647 if (instruction->IsAnd()) {
5648 GenerateAndConst(out_low, first_low, value_low);
5649 GenerateAndConst(out_high, first_high, value_high);
5650 } else if (instruction->IsOr()) {
5651 GenerateOrrConst(out_low, first_low, value_low);
5652 GenerateOrrConst(out_high, first_high, value_high);
5653 } else {
5654 DCHECK(instruction->IsXor());
5655 GenerateEorConst(out_low, first_low, value_low);
5656 GenerateEorConst(out_high, first_high, value_high);
5657 }
5658 }
5659 return;
5660 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005661
5662 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005663 Register first_reg = first.AsRegister<Register>();
5664 ShifterOperand second_reg(second.AsRegister<Register>());
5665 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005666 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005667 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005668 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005669 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005670 } else {
5671 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005672 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005673 }
5674 } else {
5675 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005676 Register first_low = first.AsRegisterPairLow<Register>();
5677 Register first_high = first.AsRegisterPairHigh<Register>();
5678 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
5679 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
5680 Register out_low = out.AsRegisterPairLow<Register>();
5681 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005682 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005683 __ and_(out_low, first_low, second_low);
5684 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005685 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005686 __ orr(out_low, first_low, second_low);
5687 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005688 } else {
5689 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005690 __ eor(out_low, first_low, second_low);
5691 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005692 }
5693 }
5694}
5695
Roland Levillain3b359c72015-11-17 19:35:12 +00005696void CodeGeneratorARM::GenerateReadBarrier(HInstruction* instruction,
5697 Location out,
5698 Location ref,
5699 Location obj,
5700 uint32_t offset,
5701 Location index) {
5702 DCHECK(kEmitCompilerReadBarrier);
5703
5704 // If heap poisoning is enabled, the unpoisoning of the loaded
5705 // reference will be carried out by the runtime within the slow
5706 // path.
5707 //
5708 // Note that `ref` currently does not get unpoisoned (when heap
5709 // poisoning is enabled), which is alright as the `ref` argument is
5710 // not used by the artReadBarrierSlow entry point.
5711 //
5712 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
5713 SlowPathCode* slow_path = new (GetGraph()->GetArena())
5714 ReadBarrierForHeapReferenceSlowPathARM(instruction, out, ref, obj, offset, index);
5715 AddSlowPath(slow_path);
5716
5717 // TODO: When read barrier has a fast path, add it here.
5718 /* Currently the read barrier call is inserted after the original load.
5719 * However, if we have a fast path, we need to perform the load of obj.LockWord *before* the
5720 * original load. This load-load ordering is required by the read barrier.
5721 * The fast path/slow path (for Baker's algorithm) should look like:
5722 *
5723 * bool isGray = obj.LockWord & kReadBarrierMask;
5724 * lfence; // load fence or artificial data dependence to prevent load-load reordering
5725 * ref = obj.field; // this is the original load
5726 * if (isGray) {
5727 * ref = Mark(ref); // ideally the slow path just does Mark(ref)
5728 * }
5729 */
5730
5731 __ b(slow_path->GetEntryLabel());
5732 __ Bind(slow_path->GetExitLabel());
5733}
5734
5735void CodeGeneratorARM::MaybeGenerateReadBarrier(HInstruction* instruction,
5736 Location out,
5737 Location ref,
5738 Location obj,
5739 uint32_t offset,
5740 Location index) {
5741 if (kEmitCompilerReadBarrier) {
5742 // If heap poisoning is enabled, unpoisoning will be taken care of
5743 // by the runtime within the slow path.
5744 GenerateReadBarrier(instruction, out, ref, obj, offset, index);
5745 } else if (kPoisonHeapReferences) {
5746 __ UnpoisonHeapReference(out.AsRegister<Register>());
5747 }
5748}
5749
5750void CodeGeneratorARM::GenerateReadBarrierForRoot(HInstruction* instruction,
5751 Location out,
5752 Location root) {
5753 DCHECK(kEmitCompilerReadBarrier);
5754
5755 // Note that GC roots are not affected by heap poisoning, so we do
5756 // not need to do anything special for this here.
5757 SlowPathCode* slow_path =
5758 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM(instruction, out, root);
5759 AddSlowPath(slow_path);
5760
5761 // TODO: Implement a fast path for ReadBarrierForRoot, performing
5762 // the following operation (for Baker's algorithm):
5763 //
5764 // if (thread.tls32_.is_gc_marking) {
5765 // root = Mark(root);
5766 // }
5767
5768 __ b(slow_path->GetEntryLabel());
5769 __ Bind(slow_path->GetExitLabel());
5770}
5771
Vladimir Markodc151b22015-10-15 18:02:30 +01005772HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch(
5773 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
5774 MethodReference target_method) {
5775 if (desired_dispatch_info.method_load_kind ==
5776 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative) {
5777 // TODO: Implement this type. For the moment, we fall back to kDexCacheViaMethod.
5778 return HInvokeStaticOrDirect::DispatchInfo {
5779 HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod,
5780 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
5781 0u,
5782 0u
5783 };
5784 }
5785 if (desired_dispatch_info.code_ptr_location ==
5786 HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative) {
5787 const DexFile& outer_dex_file = GetGraph()->GetDexFile();
5788 if (&outer_dex_file != target_method.dex_file) {
5789 // Calls across dex files are more likely to exceed the available BL range,
5790 // so use absolute patch with fixup if available and kCallArtMethod otherwise.
5791 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location =
5792 (desired_dispatch_info.method_load_kind ==
5793 HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup)
5794 ? HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup
5795 : HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
5796 return HInvokeStaticOrDirect::DispatchInfo {
5797 desired_dispatch_info.method_load_kind,
5798 code_ptr_location,
5799 desired_dispatch_info.method_load_data,
5800 0u
5801 };
5802 }
5803 }
5804 return desired_dispatch_info;
5805}
5806
Nicolas Geoffray38207af2015-06-01 15:46:22 +01005807void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00005808 // For better instruction scheduling we load the direct code pointer before the method pointer.
Vladimir Marko58155012015-08-19 12:49:41 +00005809 switch (invoke->GetCodePtrLocation()) {
Vladimir Marko58155012015-08-19 12:49:41 +00005810 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
5811 // LR = code address from literal pool with link-time patch.
5812 __ LoadLiteral(LR, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
Vladimir Marko58155012015-08-19 12:49:41 +00005813 break;
5814 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
5815 // LR = invoke->GetDirectCodePtr();
5816 __ LoadImmediate(LR, invoke->GetDirectCodePtr());
Vladimir Marko58155012015-08-19 12:49:41 +00005817 break;
5818 default:
5819 break;
5820 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08005821
Vladimir Marko58155012015-08-19 12:49:41 +00005822 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
5823 switch (invoke->GetMethodLoadKind()) {
5824 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
5825 // temp = thread->string_init_entrypoint
5826 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, invoke->GetStringInitOffset());
5827 break;
5828 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00005829 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00005830 break;
5831 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
5832 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
5833 break;
5834 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
5835 __ LoadLiteral(temp.AsRegister<Register>(),
5836 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
5837 break;
5838 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01005839 // TODO: Implement this type.
5840 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
5841 LOG(FATAL) << "Unsupported";
5842 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00005843 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00005844 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00005845 Register method_reg;
5846 Register reg = temp.AsRegister<Register>();
5847 if (current_method.IsRegister()) {
5848 method_reg = current_method.AsRegister<Register>();
5849 } else {
5850 DCHECK(invoke->GetLocations()->Intrinsified());
5851 DCHECK(!current_method.IsValid());
5852 method_reg = reg;
5853 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
5854 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005855 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
5856 __ LoadFromOffset(kLoadWord,
5857 reg,
5858 method_reg,
5859 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00005860 // temp = temp[index_in_cache]
5861 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
5862 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
5863 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01005864 }
Vladimir Marko58155012015-08-19 12:49:41 +00005865 }
5866
5867 switch (invoke->GetCodePtrLocation()) {
5868 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
5869 __ bl(GetFrameEntryLabel());
5870 break;
5871 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01005872 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07005873 __ BindTrackedLabel(&relative_call_patches_.back().label);
Vladimir Markodc151b22015-10-15 18:02:30 +01005874 // Arbitrarily branch to the BL itself, override at link time.
5875 __ bl(&relative_call_patches_.back().label);
5876 break;
Vladimir Marko58155012015-08-19 12:49:41 +00005877 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
5878 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
5879 // LR prepared above for better instruction scheduling.
Vladimir Marko58155012015-08-19 12:49:41 +00005880 // LR()
5881 __ blx(LR);
5882 break;
5883 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
5884 // LR = callee_method->entry_point_from_quick_compiled_code_
5885 __ LoadFromOffset(
5886 kLoadWord, LR, callee_method.AsRegister<Register>(),
5887 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize).Int32Value());
5888 // LR()
5889 __ blx(LR);
5890 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08005891 }
5892
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08005893 DCHECK(!IsLeafMethod());
5894}
5895
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005896void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
5897 Register temp = temp_location.AsRegister<Register>();
5898 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5899 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
5900 LocationSummary* locations = invoke->GetLocations();
5901 Location receiver = locations->InAt(0);
5902 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005903 DCHECK(receiver.IsRegister());
Roland Levillain3b359c72015-11-17 19:35:12 +00005904 // /* HeapReference<Class> */ temp = receiver->klass_
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005905 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
5906 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00005907 // Instead of simply (possibly) unpoisoning `temp` here, we should
5908 // emit a read barrier for the previous class reference load.
5909 // However this is not required in practice, as this is an
5910 // intermediate/temporary reference and because the current
5911 // concurrent copying collector keeps the from-space memory
5912 // intact/accessible until the end of the marking phase (the
5913 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005914 __ MaybeUnpoisonHeapReference(temp);
5915 // temp = temp->GetMethodAt(method_offset);
5916 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
5917 kArmWordSize).Int32Value();
5918 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
5919 // LR = temp->GetEntryPoint();
5920 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
5921 // LR();
5922 __ blx(LR);
5923}
5924
Vladimir Marko58155012015-08-19 12:49:41 +00005925void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
5926 DCHECK(linker_patches->empty());
5927 size_t size = method_patches_.size() + call_patches_.size() + relative_call_patches_.size();
5928 linker_patches->reserve(size);
5929 for (const auto& entry : method_patches_) {
5930 const MethodReference& target_method = entry.first;
5931 Literal* literal = entry.second;
5932 DCHECK(literal->GetLabel()->IsBound());
5933 uint32_t literal_offset = literal->GetLabel()->Position();
5934 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
5935 target_method.dex_file,
5936 target_method.dex_method_index));
5937 }
5938 for (const auto& entry : call_patches_) {
5939 const MethodReference& target_method = entry.first;
5940 Literal* literal = entry.second;
5941 DCHECK(literal->GetLabel()->IsBound());
5942 uint32_t literal_offset = literal->GetLabel()->Position();
5943 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
5944 target_method.dex_file,
5945 target_method.dex_method_index));
5946 }
5947 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
5948 uint32_t literal_offset = info.label.Position();
5949 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
5950 info.target_method.dex_file,
5951 info.target_method.dex_method_index));
5952 }
5953}
5954
5955Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
5956 MethodToLiteralMap* map) {
5957 // Look up the literal for target_method.
5958 auto lb = map->lower_bound(target_method);
5959 if (lb != map->end() && !map->key_comp()(target_method, lb->first)) {
5960 return lb->second;
5961 }
5962 // We don't have a literal for this method yet, insert a new one.
5963 Literal* literal = __ NewLiteral<uint32_t>(0u);
5964 map->PutBefore(lb, target_method, literal);
5965 return literal;
5966}
5967
5968Literal* CodeGeneratorARM::DeduplicateMethodAddressLiteral(MethodReference target_method) {
5969 return DeduplicateMethodLiteral(target_method, &method_patches_);
5970}
5971
5972Literal* CodeGeneratorARM::DeduplicateMethodCodeLiteral(MethodReference target_method) {
5973 return DeduplicateMethodLiteral(target_method, &call_patches_);
5974}
5975
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005976void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005977 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005978 LOG(FATAL) << "Unreachable";
5979}
5980
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005981void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005982 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005983 LOG(FATAL) << "Unreachable";
5984}
5985
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005986void LocationsBuilderARM::VisitFakeString(HFakeString* instruction) {
5987 DCHECK(codegen_->IsBaseline());
5988 LocationSummary* locations =
5989 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5990 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
5991}
5992
5993void InstructionCodeGeneratorARM::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
5994 DCHECK(codegen_->IsBaseline());
5995 // Will be generated at use site.
5996}
5997
Mark Mendellfe57faa2015-09-18 09:26:15 -04005998// Simple implementation of packed switch - generate cascaded compare/jumps.
5999void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6000 LocationSummary* locations =
6001 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6002 locations->SetInAt(0, Location::RequiresRegister());
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006003 if (switch_instr->GetNumEntries() >= kPackedSwitchJumpTableThreshold &&
6004 codegen_->GetAssembler()->IsThumb()) {
6005 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
6006 if (switch_instr->GetStartValue() != 0) {
6007 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
6008 }
6009 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04006010}
6011
6012void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6013 int32_t lower_bound = switch_instr->GetStartValue();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006014 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006015 LocationSummary* locations = switch_instr->GetLocations();
6016 Register value_reg = locations->InAt(0).AsRegister<Register>();
6017 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6018
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006019 if (num_entries < kPackedSwitchJumpTableThreshold || !codegen_->GetAssembler()->IsThumb()) {
6020 // Create a series of compare/jumps.
6021 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6022 for (uint32_t i = 0; i < num_entries; i++) {
6023 GenerateCompareWithImmediate(value_reg, lower_bound + i);
6024 __ b(codegen_->GetLabelOf(successors[i]), EQ);
6025 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04006026
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006027 // And the default for any other value.
6028 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6029 __ b(codegen_->GetLabelOf(default_block));
6030 }
6031 } else {
6032 // Create a table lookup.
6033 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
6034
6035 // Materialize a pointer to the switch table
6036 std::vector<Label*> labels(num_entries);
6037 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6038 for (uint32_t i = 0; i < num_entries; i++) {
6039 labels[i] = codegen_->GetLabelOf(successors[i]);
6040 }
6041 JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg);
6042
6043 // Remove the bias.
6044 Register key_reg;
6045 if (lower_bound != 0) {
6046 key_reg = locations->GetTemp(1).AsRegister<Register>();
6047 __ AddConstant(key_reg, value_reg, -lower_bound);
6048 } else {
6049 key_reg = value_reg;
6050 }
6051
6052 // Check whether the value is in the table, jump to default block if not.
6053 __ CmpConstant(key_reg, num_entries - 1);
6054 __ b(codegen_->GetLabelOf(default_block), Condition::HI);
6055
6056 // Load the displacement from the table.
6057 __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2));
6058
6059 // Dispatch is a direct add to the PC (for Thumb2).
6060 __ EmitJumpTableDispatch(table, temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006061 }
6062}
6063
Andreas Gampe85b62f22015-09-09 13:15:38 -07006064void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
6065 if (!trg.IsValid()) {
6066 DCHECK(type == Primitive::kPrimVoid);
6067 return;
6068 }
6069
6070 DCHECK_NE(type, Primitive::kPrimVoid);
6071
6072 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
6073 if (return_loc.Equals(trg)) {
6074 return;
6075 }
6076
6077 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
6078 // with the last branch.
6079 if (type == Primitive::kPrimLong) {
6080 HParallelMove parallel_move(GetGraph()->GetArena());
6081 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
6082 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
6083 GetMoveResolver()->EmitNativeCode(&parallel_move);
6084 } else if (type == Primitive::kPrimDouble) {
6085 HParallelMove parallel_move(GetGraph()->GetArena());
6086 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
6087 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
6088 GetMoveResolver()->EmitNativeCode(&parallel_move);
6089 } else {
6090 // Let the parallel move resolver take care of all of this.
6091 HParallelMove parallel_move(GetGraph()->GetArena());
6092 parallel_move.AddMove(return_loc, trg, type, nullptr);
6093 GetMoveResolver()->EmitNativeCode(&parallel_move);
6094 }
6095}
6096
Roland Levillain4d027112015-07-01 15:41:14 +01006097#undef __
6098#undef QUICK_ENTRY_POINT
6099
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006100} // namespace arm
6101} // namespace art