blob: cf6f7e3338566ad7d65943a9eb894a1336ce0002 [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 }
Vladimir Markof9d741e2015-11-20 15:08:11 +00003364 } else if (shift_value == 1) {
3365 if (op->IsShl()) {
3366 __ Lsls(o_l, low, 1);
3367 __ adc(o_h, high, ShifterOperand(high));
3368 } else if (op->IsShr()) {
3369 __ Asrs(o_h, high, 1);
3370 __ Rrx(o_l, low);
3371 } else {
3372 __ Lsrs(o_h, high, 1);
3373 __ Rrx(o_l, low);
3374 }
3375 } else {
3376 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003377 if (op->IsShl()) {
3378 __ Lsl(o_h, high, shift_value);
3379 __ orr(o_h, o_h, ShifterOperand(low, LSR, 32 - shift_value));
3380 __ Lsl(o_l, low, shift_value);
3381 } else if (op->IsShr()) {
3382 __ Lsr(o_l, low, shift_value);
3383 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3384 __ Asr(o_h, high, shift_value);
3385 } else {
3386 __ Lsr(o_l, low, shift_value);
3387 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3388 __ Lsr(o_h, high, shift_value);
3389 }
3390 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003391 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003392 break;
3393 }
3394 default:
3395 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003396 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003397 }
3398}
3399
3400void LocationsBuilderARM::VisitShl(HShl* shl) {
3401 HandleShift(shl);
3402}
3403
3404void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
3405 HandleShift(shl);
3406}
3407
3408void LocationsBuilderARM::VisitShr(HShr* shr) {
3409 HandleShift(shr);
3410}
3411
3412void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
3413 HandleShift(shr);
3414}
3415
3416void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
3417 HandleShift(ushr);
3418}
3419
3420void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
3421 HandleShift(ushr);
3422}
3423
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003424void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003425 LocationSummary* locations =
3426 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003427 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003428 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3429 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003430 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003431}
3432
3433void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003434 // Note: if heap poisoning is enabled, the entry point takes cares
3435 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003436 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003437 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003438 instruction->GetDexPc(),
3439 nullptr);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003440}
3441
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003442void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
3443 LocationSummary* locations =
3444 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3445 InvokeRuntimeCallingConvention calling_convention;
3446 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003447 locations->SetOut(Location::RegisterLocation(R0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003448 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003449 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003450}
3451
3452void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
3453 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003454 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003455 // Note: if heap poisoning is enabled, the entry point takes cares
3456 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003457 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003458 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003459 instruction->GetDexPc(),
3460 nullptr);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003461}
3462
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003463void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003464 LocationSummary* locations =
3465 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003466 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3467 if (location.IsStackSlot()) {
3468 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3469 } else if (location.IsDoubleStackSlot()) {
3470 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003471 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003472 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003473}
3474
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003475void InstructionCodeGeneratorARM::VisitParameterValue(
3476 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003477 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003478}
3479
3480void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
3481 LocationSummary* locations =
3482 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3483 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3484}
3485
3486void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3487 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003488}
3489
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003490void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003491 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003492 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003493 locations->SetInAt(0, Location::RequiresRegister());
3494 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003495}
3496
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003497void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
3498 LocationSummary* locations = not_->GetLocations();
3499 Location out = locations->Out();
3500 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003501 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003502 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003503 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003504 break;
3505
3506 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003507 __ mvn(out.AsRegisterPairLow<Register>(),
3508 ShifterOperand(in.AsRegisterPairLow<Register>()));
3509 __ mvn(out.AsRegisterPairHigh<Register>(),
3510 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003511 break;
3512
3513 default:
3514 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3515 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003516}
3517
David Brazdil66d126e2015-04-03 16:02:44 +01003518void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
3519 LocationSummary* locations =
3520 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3521 locations->SetInAt(0, Location::RequiresRegister());
3522 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3523}
3524
3525void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003526 LocationSummary* locations = bool_not->GetLocations();
3527 Location out = locations->Out();
3528 Location in = locations->InAt(0);
3529 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
3530}
3531
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003532void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003533 LocationSummary* locations =
3534 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003535 switch (compare->InputAt(0)->GetType()) {
3536 case Primitive::kPrimLong: {
3537 locations->SetInAt(0, Location::RequiresRegister());
3538 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003539 // Output overlaps because it is written before doing the low comparison.
3540 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00003541 break;
3542 }
3543 case Primitive::kPrimFloat:
3544 case Primitive::kPrimDouble: {
3545 locations->SetInAt(0, Location::RequiresFpuRegister());
3546 locations->SetInAt(1, Location::RequiresFpuRegister());
3547 locations->SetOut(Location::RequiresRegister());
3548 break;
3549 }
3550 default:
3551 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3552 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003553}
3554
3555void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003556 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003557 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003558 Location left = locations->InAt(0);
3559 Location right = locations->InAt(1);
3560
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003561 Label less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00003562 Primitive::Type type = compare->InputAt(0)->GetType();
3563 switch (type) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003564 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003565 __ cmp(left.AsRegisterPairHigh<Register>(),
3566 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003567 __ b(&less, LT);
3568 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003569 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00003570 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003571 __ cmp(left.AsRegisterPairLow<Register>(),
3572 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Calin Juravleddb7df22014-11-25 20:56:51 +00003573 break;
3574 }
3575 case Primitive::kPrimFloat:
3576 case Primitive::kPrimDouble: {
3577 __ LoadImmediate(out, 0);
3578 if (type == Primitive::kPrimFloat) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003579 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003580 } else {
3581 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
3582 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
3583 }
3584 __ vmstat(); // transfer FP status register to ARM APSR.
3585 __ b(compare->IsGtBias() ? &greater : &less, VS); // VS for unordered.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003586 break;
3587 }
3588 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003589 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003590 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003591 __ b(&done, EQ);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003592 __ b(&less, LO); // LO is for both: unsigned compare for longs and 'less than' for floats.
Calin Juravleddb7df22014-11-25 20:56:51 +00003593
3594 __ Bind(&greater);
3595 __ LoadImmediate(out, 1);
3596 __ b(&done);
3597
3598 __ Bind(&less);
3599 __ LoadImmediate(out, -1);
3600
3601 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003602}
3603
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003604void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003605 LocationSummary* locations =
3606 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003607 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3608 locations->SetInAt(i, Location::Any());
3609 }
3610 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003611}
3612
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003613void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003614 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003615}
3616
Calin Juravle52c48962014-12-16 17:02:57 +00003617void InstructionCodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
3618 // TODO (ported from quick): revisit Arm barrier kinds
Kenny Root1d8199d2015-06-02 11:01:10 -07003619 DmbOptions flavor = DmbOptions::ISH; // quiet c++ warnings
Calin Juravle52c48962014-12-16 17:02:57 +00003620 switch (kind) {
3621 case MemBarrierKind::kAnyStore:
3622 case MemBarrierKind::kLoadAny:
3623 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003624 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00003625 break;
3626 }
3627 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003628 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00003629 break;
3630 }
3631 default:
3632 LOG(FATAL) << "Unexpected memory barrier " << kind;
3633 }
Kenny Root1d8199d2015-06-02 11:01:10 -07003634 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00003635}
3636
3637void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
3638 uint32_t offset,
3639 Register out_lo,
3640 Register out_hi) {
3641 if (offset != 0) {
Roland Levillain3b359c72015-11-17 19:35:12 +00003642 // Ensure `out_lo` is different from `addr`, so that loading
3643 // `offset` into `out_lo` does not clutter `addr`.
3644 DCHECK_NE(out_lo, addr);
Calin Juravle52c48962014-12-16 17:02:57 +00003645 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003646 __ add(IP, addr, ShifterOperand(out_lo));
3647 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003648 }
3649 __ ldrexd(out_lo, out_hi, addr);
3650}
3651
3652void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
3653 uint32_t offset,
3654 Register value_lo,
3655 Register value_hi,
3656 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00003657 Register temp2,
3658 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003659 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00003660 if (offset != 0) {
3661 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003662 __ add(IP, addr, ShifterOperand(temp1));
3663 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003664 }
3665 __ Bind(&fail);
3666 // We need a load followed by store. (The address used in a STREX instruction must
3667 // be the same as the address in the most recently executed LDREX instruction.)
3668 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00003669 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003670 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003671 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00003672}
3673
3674void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3675 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3676
Nicolas Geoffray39468442014-09-02 15:17:15 +01003677 LocationSummary* locations =
3678 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003679 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003680
Calin Juravle52c48962014-12-16 17:02:57 +00003681 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003682 if (Primitive::IsFloatingPointType(field_type)) {
3683 locations->SetInAt(1, Location::RequiresFpuRegister());
3684 } else {
3685 locations->SetInAt(1, Location::RequiresRegister());
3686 }
3687
Calin Juravle52c48962014-12-16 17:02:57 +00003688 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00003689 bool generate_volatile = field_info.IsVolatile()
3690 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003691 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01003692 bool needs_write_barrier =
3693 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003694 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00003695 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01003696 if (needs_write_barrier) {
3697 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003698 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003699 } else if (generate_volatile) {
Calin Juravle52c48962014-12-16 17:02:57 +00003700 // Arm encoding have some additional constraints for ldrexd/strexd:
3701 // - registers need to be consecutive
3702 // - the first register should be even but not R14.
3703 // We don't test for Arm yet, and the assertion makes sure that we revisit this if we ever
3704 // enable Arm encoding.
3705 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3706
3707 locations->AddTemp(Location::RequiresRegister());
3708 locations->AddTemp(Location::RequiresRegister());
3709 if (field_type == Primitive::kPrimDouble) {
3710 // For doubles we need two more registers to copy the value.
3711 locations->AddTemp(Location::RegisterLocation(R2));
3712 locations->AddTemp(Location::RegisterLocation(R3));
3713 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003714 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003715}
3716
Calin Juravle52c48962014-12-16 17:02:57 +00003717void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003718 const FieldInfo& field_info,
3719 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003720 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3721
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003722 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003723 Register base = locations->InAt(0).AsRegister<Register>();
3724 Location value = locations->InAt(1);
3725
3726 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003727 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00003728 Primitive::Type field_type = field_info.GetFieldType();
3729 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01003730 bool needs_write_barrier =
3731 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003732
3733 if (is_volatile) {
3734 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3735 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003736
3737 switch (field_type) {
3738 case Primitive::kPrimBoolean:
3739 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003740 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003741 break;
3742 }
3743
3744 case Primitive::kPrimShort:
3745 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003746 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003747 break;
3748 }
3749
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003750 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003751 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01003752 if (kPoisonHeapReferences && needs_write_barrier) {
3753 // Note that in the case where `value` is a null reference,
3754 // we do not enter this block, as a null reference does not
3755 // need poisoning.
3756 DCHECK_EQ(field_type, Primitive::kPrimNot);
3757 Register temp = locations->GetTemp(0).AsRegister<Register>();
3758 __ Mov(temp, value.AsRegister<Register>());
3759 __ PoisonHeapReference(temp);
3760 __ StoreToOffset(kStoreWord, temp, base, offset);
3761 } else {
3762 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
3763 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003764 break;
3765 }
3766
3767 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00003768 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003769 GenerateWideAtomicStore(base, offset,
3770 value.AsRegisterPairLow<Register>(),
3771 value.AsRegisterPairHigh<Register>(),
3772 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003773 locations->GetTemp(1).AsRegister<Register>(),
3774 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003775 } else {
3776 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003777 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003778 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003779 break;
3780 }
3781
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003782 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003783 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003784 break;
3785 }
3786
3787 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003788 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00003789 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003790 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
3791 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
3792
3793 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
3794
3795 GenerateWideAtomicStore(base, offset,
3796 value_reg_lo,
3797 value_reg_hi,
3798 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003799 locations->GetTemp(3).AsRegister<Register>(),
3800 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003801 } else {
3802 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003803 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003804 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003805 break;
3806 }
3807
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003808 case Primitive::kPrimVoid:
3809 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003810 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003811 }
Calin Juravle52c48962014-12-16 17:02:57 +00003812
Calin Juravle77520bc2015-01-12 18:45:46 +00003813 // Longs and doubles are handled in the switch.
3814 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
3815 codegen_->MaybeRecordImplicitNullCheck(instruction);
3816 }
3817
3818 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3819 Register temp = locations->GetTemp(0).AsRegister<Register>();
3820 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003821 codegen_->MarkGCCard(
3822 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003823 }
3824
Calin Juravle52c48962014-12-16 17:02:57 +00003825 if (is_volatile) {
3826 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3827 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003828}
3829
Calin Juravle52c48962014-12-16 17:02:57 +00003830void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3831 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain3b359c72015-11-17 19:35:12 +00003832
3833 bool object_field_get_with_read_barrier =
3834 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003835 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00003836 new (GetGraph()->GetArena()) LocationSummary(instruction,
3837 object_field_get_with_read_barrier ?
3838 LocationSummary::kCallOnSlowPath :
3839 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003840 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003841
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003842 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00003843 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003844 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain3b359c72015-11-17 19:35:12 +00003845 // The output overlaps in case of volatile long: we don't want the
3846 // code generated by GenerateWideAtomicLoad to overwrite the
3847 // object's location. Likewise, in the case of an object field get
3848 // with read barriers enabled, we do not want the load to overwrite
3849 // the object's location, as we need it to emit the read barrier.
3850 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
3851 object_field_get_with_read_barrier;
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01003852
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003853 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3854 locations->SetOut(Location::RequiresFpuRegister());
3855 } else {
3856 locations->SetOut(Location::RequiresRegister(),
3857 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
3858 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003859 if (volatile_for_double) {
Calin Juravle52c48962014-12-16 17:02:57 +00003860 // Arm encoding have some additional constraints for ldrexd/strexd:
3861 // - registers need to be consecutive
3862 // - the first register should be even but not R14.
3863 // We don't test for Arm yet, and the assertion makes sure that we revisit this if we ever
3864 // enable Arm encoding.
3865 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3866 locations->AddTemp(Location::RequiresRegister());
3867 locations->AddTemp(Location::RequiresRegister());
3868 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003869}
3870
Vladimir Markod2b4ca22015-09-14 15:13:26 +01003871Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
3872 Opcode opcode) {
3873 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
3874 if (constant->IsConstant() &&
3875 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
3876 return Location::ConstantLocation(constant->AsConstant());
3877 }
3878 return Location::RequiresRegister();
3879}
3880
3881bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
3882 Opcode opcode) {
3883 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
3884 if (Primitive::Is64BitType(input_cst->GetType())) {
3885 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode) &&
3886 CanEncodeConstantAsImmediate(High32Bits(value), opcode);
3887 } else {
3888 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
3889 }
3890}
3891
3892bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value, Opcode opcode) {
3893 ShifterOperand so;
3894 ArmAssembler* assembler = codegen_->GetAssembler();
3895 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, &so)) {
3896 return true;
3897 }
3898 Opcode neg_opcode = kNoOperand;
3899 switch (opcode) {
3900 case AND:
3901 neg_opcode = BIC;
3902 break;
3903 case ORR:
3904 neg_opcode = ORN;
3905 break;
3906 default:
3907 return false;
3908 }
3909 return assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, neg_opcode, ~value, &so);
3910}
3911
Calin Juravle52c48962014-12-16 17:02:57 +00003912void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
3913 const FieldInfo& field_info) {
3914 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003915
Calin Juravle52c48962014-12-16 17:02:57 +00003916 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00003917 Location base_loc = locations->InAt(0);
3918 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00003919 Location out = locations->Out();
3920 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003921 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00003922 Primitive::Type field_type = field_info.GetFieldType();
3923 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3924
3925 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003926 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003927 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003928 break;
3929 }
3930
3931 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003932 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003933 break;
3934 }
3935
3936 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003937 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003938 break;
3939 }
3940
3941 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003942 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003943 break;
3944 }
3945
3946 case Primitive::kPrimInt:
3947 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003948 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003949 break;
3950 }
3951
3952 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00003953 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003954 GenerateWideAtomicLoad(base, offset,
3955 out.AsRegisterPairLow<Register>(),
3956 out.AsRegisterPairHigh<Register>());
3957 } else {
3958 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
3959 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003960 break;
3961 }
3962
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003963 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003964 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003965 break;
3966 }
3967
3968 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003969 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00003970 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003971 Register lo = locations->GetTemp(0).AsRegister<Register>();
3972 Register hi = locations->GetTemp(1).AsRegister<Register>();
3973 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00003974 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003975 __ vmovdrr(out_reg, lo, hi);
3976 } else {
3977 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003978 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003979 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003980 break;
3981 }
3982
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003983 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003984 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003985 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003986 }
Calin Juravle52c48962014-12-16 17:02:57 +00003987
Calin Juravle77520bc2015-01-12 18:45:46 +00003988 // Doubles are handled in the switch.
3989 if (field_type != Primitive::kPrimDouble) {
3990 codegen_->MaybeRecordImplicitNullCheck(instruction);
3991 }
3992
Calin Juravle52c48962014-12-16 17:02:57 +00003993 if (is_volatile) {
3994 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3995 }
Roland Levillain4d027112015-07-01 15:41:14 +01003996
3997 if (field_type == Primitive::kPrimNot) {
Roland Levillain3b359c72015-11-17 19:35:12 +00003998 codegen_->MaybeGenerateReadBarrier(instruction, out, out, base_loc, offset);
Roland Levillain4d027112015-07-01 15:41:14 +01003999 }
Calin Juravle52c48962014-12-16 17:02:57 +00004000}
4001
4002void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4003 HandleFieldSet(instruction, instruction->GetFieldInfo());
4004}
4005
4006void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004007 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004008}
4009
4010void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4011 HandleFieldGet(instruction, instruction->GetFieldInfo());
4012}
4013
4014void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4015 HandleFieldGet(instruction, instruction->GetFieldInfo());
4016}
4017
4018void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4019 HandleFieldGet(instruction, instruction->GetFieldInfo());
4020}
4021
4022void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4023 HandleFieldGet(instruction, instruction->GetFieldInfo());
4024}
4025
4026void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4027 HandleFieldSet(instruction, instruction->GetFieldInfo());
4028}
4029
4030void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004031 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004032}
4033
Calin Juravlee460d1d2015-09-29 04:52:17 +01004034void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
4035 HUnresolvedInstanceFieldGet* instruction) {
4036 FieldAccessCallingConventionARM calling_convention;
4037 codegen_->CreateUnresolvedFieldLocationSummary(
4038 instruction, instruction->GetFieldType(), calling_convention);
4039}
4040
4041void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
4042 HUnresolvedInstanceFieldGet* instruction) {
4043 FieldAccessCallingConventionARM calling_convention;
4044 codegen_->GenerateUnresolvedFieldAccess(instruction,
4045 instruction->GetFieldType(),
4046 instruction->GetFieldIndex(),
4047 instruction->GetDexPc(),
4048 calling_convention);
4049}
4050
4051void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
4052 HUnresolvedInstanceFieldSet* instruction) {
4053 FieldAccessCallingConventionARM calling_convention;
4054 codegen_->CreateUnresolvedFieldLocationSummary(
4055 instruction, instruction->GetFieldType(), calling_convention);
4056}
4057
4058void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
4059 HUnresolvedInstanceFieldSet* instruction) {
4060 FieldAccessCallingConventionARM calling_convention;
4061 codegen_->GenerateUnresolvedFieldAccess(instruction,
4062 instruction->GetFieldType(),
4063 instruction->GetFieldIndex(),
4064 instruction->GetDexPc(),
4065 calling_convention);
4066}
4067
4068void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
4069 HUnresolvedStaticFieldGet* instruction) {
4070 FieldAccessCallingConventionARM calling_convention;
4071 codegen_->CreateUnresolvedFieldLocationSummary(
4072 instruction, instruction->GetFieldType(), calling_convention);
4073}
4074
4075void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
4076 HUnresolvedStaticFieldGet* instruction) {
4077 FieldAccessCallingConventionARM calling_convention;
4078 codegen_->GenerateUnresolvedFieldAccess(instruction,
4079 instruction->GetFieldType(),
4080 instruction->GetFieldIndex(),
4081 instruction->GetDexPc(),
4082 calling_convention);
4083}
4084
4085void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
4086 HUnresolvedStaticFieldSet* instruction) {
4087 FieldAccessCallingConventionARM calling_convention;
4088 codegen_->CreateUnresolvedFieldLocationSummary(
4089 instruction, instruction->GetFieldType(), calling_convention);
4090}
4091
4092void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
4093 HUnresolvedStaticFieldSet* instruction) {
4094 FieldAccessCallingConventionARM calling_convention;
4095 codegen_->GenerateUnresolvedFieldAccess(instruction,
4096 instruction->GetFieldType(),
4097 instruction->GetFieldIndex(),
4098 instruction->GetDexPc(),
4099 calling_convention);
4100}
4101
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004102void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004103 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4104 ? LocationSummary::kCallOnSlowPath
4105 : LocationSummary::kNoCall;
4106 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravle77520bc2015-01-12 18:45:46 +00004107 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004108 if (instruction->HasUses()) {
4109 locations->SetOut(Location::SameAsFirstInput());
4110 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004111}
4112
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004113void InstructionCodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004114 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4115 return;
4116 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004117 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004118
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004119 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
4120 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4121}
4122
4123void InstructionCodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004124 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004125 codegen_->AddSlowPath(slow_path);
4126
4127 LocationSummary* locations = instruction->GetLocations();
4128 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004129
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004130 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004131}
4132
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004133void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004134 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004135 GenerateImplicitNullCheck(instruction);
4136 } else {
4137 GenerateExplicitNullCheck(instruction);
4138 }
4139}
4140
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004141void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004142 bool object_array_get_with_read_barrier =
4143 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004144 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004145 new (GetGraph()->GetArena()) LocationSummary(instruction,
4146 object_array_get_with_read_barrier ?
4147 LocationSummary::kCallOnSlowPath :
4148 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004149 locations->SetInAt(0, Location::RequiresRegister());
4150 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004151 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4152 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4153 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004154 // The output overlaps in the case of an object array get with
4155 // read barriers enabled: we do not want the move to overwrite the
4156 // array's location, as we need it to emit the read barrier.
4157 locations->SetOut(
4158 Location::RequiresRegister(),
4159 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004160 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004161}
4162
4163void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
4164 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004165 Location obj_loc = locations->InAt(0);
4166 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004167 Location index = locations->InAt(1);
Roland Levillain4d027112015-07-01 15:41:14 +01004168 Primitive::Type type = instruction->GetType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004169
Roland Levillain4d027112015-07-01 15:41:14 +01004170 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004171 case Primitive::kPrimBoolean: {
4172 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004173 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004174 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004175 size_t offset =
4176 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004177 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
4178 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004179 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004180 __ LoadFromOffset(kLoadUnsignedByte, out, IP, data_offset);
4181 }
4182 break;
4183 }
4184
4185 case Primitive::kPrimByte: {
4186 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004187 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004188 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004189 size_t offset =
4190 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004191 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
4192 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004193 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004194 __ LoadFromOffset(kLoadSignedByte, out, IP, data_offset);
4195 }
4196 break;
4197 }
4198
4199 case Primitive::kPrimShort: {
4200 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004201 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004202 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004203 size_t offset =
4204 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004205 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
4206 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004207 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004208 __ LoadFromOffset(kLoadSignedHalfword, out, IP, data_offset);
4209 }
4210 break;
4211 }
4212
4213 case Primitive::kPrimChar: {
4214 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004215 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004216 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004217 size_t offset =
4218 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004219 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
4220 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004221 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004222 __ LoadFromOffset(kLoadUnsignedHalfword, out, IP, data_offset);
4223 }
4224 break;
4225 }
4226
4227 case Primitive::kPrimInt:
4228 case Primitive::kPrimNot: {
Roland Levillain3b359c72015-11-17 19:35:12 +00004229 static_assert(
4230 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4231 "art::mirror::HeapReference<mirror::Object> and int32_t have different sizes.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004232 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004233 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004234 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004235 size_t offset =
4236 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004237 __ LoadFromOffset(kLoadWord, out, obj, offset);
4238 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004239 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004240 __ LoadFromOffset(kLoadWord, out, IP, data_offset);
4241 }
4242 break;
4243 }
4244
4245 case Primitive::kPrimLong: {
4246 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004247 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004248 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004249 size_t offset =
4250 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004251 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004252 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004253 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004254 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004255 }
4256 break;
4257 }
4258
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004259 case Primitive::kPrimFloat: {
4260 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4261 Location out = locations->Out();
4262 DCHECK(out.IsFpuRegister());
4263 if (index.IsConstant()) {
4264 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4265 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), obj, offset);
4266 } else {
4267 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4268 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), IP, data_offset);
4269 }
4270 break;
4271 }
4272
4273 case Primitive::kPrimDouble: {
4274 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4275 Location out = locations->Out();
4276 DCHECK(out.IsFpuRegisterPair());
4277 if (index.IsConstant()) {
4278 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
4279 __ LoadDFromOffset(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()), obj, offset);
4280 } else {
4281 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
4282 __ LoadDFromOffset(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
4283 }
4284 break;
4285 }
4286
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004287 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004288 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004289 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004290 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004291 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004292
4293 if (type == Primitive::kPrimNot) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004294 static_assert(
4295 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4296 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
4297 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4298 Location out = locations->Out();
4299 if (index.IsConstant()) {
4300 uint32_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4301 codegen_->MaybeGenerateReadBarrier(instruction, out, out, obj_loc, offset);
4302 } else {
4303 codegen_->MaybeGenerateReadBarrier(instruction, out, out, obj_loc, data_offset, index);
4304 }
Roland Levillain4d027112015-07-01 15:41:14 +01004305 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004306}
4307
4308void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004309 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004310
4311 bool needs_write_barrier =
4312 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain3b359c72015-11-17 19:35:12 +00004313 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
4314 bool object_array_set_with_read_barrier =
4315 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004316
Nicolas Geoffray39468442014-09-02 15:17:15 +01004317 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004318 instruction,
Roland Levillain3b359c72015-11-17 19:35:12 +00004319 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
4320 LocationSummary::kCallOnSlowPath :
4321 LocationSummary::kNoCall);
4322
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004323 locations->SetInAt(0, Location::RequiresRegister());
4324 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4325 if (Primitive::IsFloatingPointType(value_type)) {
4326 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004327 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004328 locations->SetInAt(2, Location::RequiresRegister());
4329 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004330 if (needs_write_barrier) {
4331 // Temporary registers for the write barrier.
4332 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Roland Levillain3b359c72015-11-17 19:35:12 +00004333 locations->AddTemp(Location::RequiresRegister()); // Possibly used for read barrier too.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004334 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004335}
4336
4337void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
4338 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004339 Location array_loc = locations->InAt(0);
4340 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004341 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004342 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain3b359c72015-11-17 19:35:12 +00004343 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004344 bool needs_write_barrier =
4345 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004346
4347 switch (value_type) {
4348 case Primitive::kPrimBoolean:
4349 case Primitive::kPrimByte: {
4350 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004351 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004352 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004353 size_t offset =
4354 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004355 __ StoreToOffset(kStoreByte, value, array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004356 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004357 __ add(IP, array, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004358 __ StoreToOffset(kStoreByte, value, IP, data_offset);
4359 }
4360 break;
4361 }
4362
4363 case Primitive::kPrimShort:
4364 case Primitive::kPrimChar: {
4365 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004366 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004367 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004368 size_t offset =
4369 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004370 __ StoreToOffset(kStoreHalfword, value, array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004371 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004372 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004373 __ StoreToOffset(kStoreHalfword, value, IP, data_offset);
4374 }
4375 break;
4376 }
4377
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004378 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004379 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00004380 Location value_loc = locations->InAt(2);
4381 Register value = value_loc.AsRegister<Register>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004382 Register source = value;
4383
4384 if (instruction->InputAt(2)->IsNullConstant()) {
4385 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004386 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004387 size_t offset =
4388 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004389 __ StoreToOffset(kStoreWord, source, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004390 } else {
4391 DCHECK(index.IsRegister()) << index;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004392 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillain4d027112015-07-01 15:41:14 +01004393 __ StoreToOffset(kStoreWord, source, IP, data_offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004394 }
Roland Levillain3b359c72015-11-17 19:35:12 +00004395 DCHECK(!needs_write_barrier);
4396 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004397 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004398 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004399
4400 DCHECK(needs_write_barrier);
4401 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
4402 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
4403 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4404 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4405 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4406 Label done;
4407 SlowPathCode* slow_path = nullptr;
4408
Roland Levillain3b359c72015-11-17 19:35:12 +00004409 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004410 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
4411 codegen_->AddSlowPath(slow_path);
4412 if (instruction->GetValueCanBeNull()) {
4413 Label non_zero;
4414 __ CompareAndBranchIfNonZero(value, &non_zero);
4415 if (index.IsConstant()) {
4416 size_t offset =
4417 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4418 __ StoreToOffset(kStoreWord, value, array, offset);
4419 } else {
4420 DCHECK(index.IsRegister()) << index;
4421 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4422 __ StoreToOffset(kStoreWord, value, IP, data_offset);
4423 }
4424 codegen_->MaybeRecordImplicitNullCheck(instruction);
4425 __ b(&done);
4426 __ Bind(&non_zero);
4427 }
4428
Roland Levillain3b359c72015-11-17 19:35:12 +00004429 if (kEmitCompilerReadBarrier) {
4430 // When read barriers are enabled, the type checking
4431 // instrumentation requires two read barriers:
4432 //
4433 // __ Mov(temp2, temp1);
4434 // // /* HeapReference<Class> */ temp1 = temp1->component_type_
4435 // __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
4436 // codegen_->GenerateReadBarrier(
4437 // instruction, temp1_loc, temp1_loc, temp2_loc, component_offset);
4438 //
4439 // // /* HeapReference<Class> */ temp2 = value->klass_
4440 // __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
4441 // codegen_->GenerateReadBarrier(
4442 // instruction, temp2_loc, temp2_loc, value_loc, class_offset, temp1_loc);
4443 //
4444 // __ cmp(temp1, ShifterOperand(temp2));
4445 //
4446 // However, the second read barrier may trash `temp`, as it
4447 // is a temporary register, and as such would not be saved
4448 // along with live registers before calling the runtime (nor
4449 // restored afterwards). So in this case, we bail out and
4450 // delegate the work to the array set slow path.
4451 //
4452 // TODO: Extend the register allocator to support a new
4453 // "(locally) live temp" location so as to avoid always
4454 // going into the slow path when read barriers are enabled.
4455 __ b(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004456 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004457 // /* HeapReference<Class> */ temp1 = array->klass_
4458 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
4459 codegen_->MaybeRecordImplicitNullCheck(instruction);
4460 __ MaybeUnpoisonHeapReference(temp1);
4461
4462 // /* HeapReference<Class> */ temp1 = temp1->component_type_
4463 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
4464 // /* HeapReference<Class> */ temp2 = value->klass_
4465 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
4466 // If heap poisoning is enabled, no need to unpoison `temp1`
4467 // nor `temp2`, as we are comparing two poisoned references.
4468 __ cmp(temp1, ShifterOperand(temp2));
4469
4470 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4471 Label do_put;
4472 __ b(&do_put, EQ);
4473 // If heap poisoning is enabled, the `temp1` reference has
4474 // not been unpoisoned yet; unpoison it now.
4475 __ MaybeUnpoisonHeapReference(temp1);
4476
4477 // /* HeapReference<Class> */ temp1 = temp1->super_class_
4478 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
4479 // If heap poisoning is enabled, no need to unpoison
4480 // `temp1`, as we are comparing against null below.
4481 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
4482 __ Bind(&do_put);
4483 } else {
4484 __ b(slow_path->GetEntryLabel(), NE);
4485 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004486 }
4487 }
4488
4489 if (kPoisonHeapReferences) {
4490 // Note that in the case where `value` is a null reference,
4491 // we do not enter this block, as a null reference does not
4492 // need poisoning.
4493 DCHECK_EQ(value_type, Primitive::kPrimNot);
4494 __ Mov(temp1, value);
4495 __ PoisonHeapReference(temp1);
4496 source = temp1;
4497 }
4498
4499 if (index.IsConstant()) {
4500 size_t offset =
4501 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4502 __ StoreToOffset(kStoreWord, source, array, offset);
4503 } else {
4504 DCHECK(index.IsRegister()) << index;
4505 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4506 __ StoreToOffset(kStoreWord, source, IP, data_offset);
4507 }
4508
Roland Levillain3b359c72015-11-17 19:35:12 +00004509 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004510 codegen_->MaybeRecordImplicitNullCheck(instruction);
4511 }
4512
4513 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
4514
4515 if (done.IsLinked()) {
4516 __ Bind(&done);
4517 }
4518
4519 if (slow_path != nullptr) {
4520 __ Bind(slow_path->GetExitLabel());
4521 }
4522
4523 break;
4524 }
4525
4526 case Primitive::kPrimInt: {
4527 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4528 Register value = locations->InAt(2).AsRegister<Register>();
4529 if (index.IsConstant()) {
4530 size_t offset =
4531 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4532 __ StoreToOffset(kStoreWord, value, array, offset);
4533 } else {
4534 DCHECK(index.IsRegister()) << index;
4535 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4536 __ StoreToOffset(kStoreWord, value, IP, data_offset);
4537 }
4538
4539 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004540 break;
4541 }
4542
4543 case Primitive::kPrimLong: {
4544 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004545 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004546 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004547 size_t offset =
4548 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004549 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004550 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004551 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004552 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004553 }
4554 break;
4555 }
4556
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004557 case Primitive::kPrimFloat: {
4558 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4559 Location value = locations->InAt(2);
4560 DCHECK(value.IsFpuRegister());
4561 if (index.IsConstant()) {
4562 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004563 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004564 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004565 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004566 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
4567 }
4568 break;
4569 }
4570
4571 case Primitive::kPrimDouble: {
4572 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4573 Location value = locations->InAt(2);
4574 DCHECK(value.IsFpuRegisterPair());
4575 if (index.IsConstant()) {
4576 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004577 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004578 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004579 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004580 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
4581 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004582
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004583 break;
4584 }
4585
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004586 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004587 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004588 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004589 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004590
4591 // Ints and objects are handled in the switch.
4592 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
4593 codegen_->MaybeRecordImplicitNullCheck(instruction);
4594 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004595}
4596
4597void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004598 LocationSummary* locations =
4599 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004600 locations->SetInAt(0, Location::RequiresRegister());
4601 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004602}
4603
4604void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
4605 LocationSummary* locations = instruction->GetLocations();
4606 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004607 Register obj = locations->InAt(0).AsRegister<Register>();
4608 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004609 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004610 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004611}
4612
4613void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004614 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4615 ? LocationSummary::kCallOnSlowPath
4616 : LocationSummary::kNoCall;
4617 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004618 locations->SetInAt(0, Location::RequiresRegister());
4619 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004620 if (instruction->HasUses()) {
4621 locations->SetOut(Location::SameAsFirstInput());
4622 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004623}
4624
4625void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
4626 LocationSummary* locations = instruction->GetLocations();
Andreas Gampe85b62f22015-09-09 13:15:38 -07004627 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004628 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004629 codegen_->AddSlowPath(slow_path);
4630
Roland Levillain271ab9c2014-11-27 15:23:57 +00004631 Register index = locations->InAt(0).AsRegister<Register>();
4632 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004633
4634 __ cmp(index, ShifterOperand(length));
Roland Levillain4fa13f62015-07-06 18:11:54 +01004635 __ b(slow_path->GetEntryLabel(), HS);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004636}
4637
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004638void CodeGeneratorARM::MarkGCCard(Register temp,
4639 Register card,
4640 Register object,
4641 Register value,
4642 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004643 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004644 if (can_be_null) {
4645 __ CompareAndBranchIfZero(value, &is_null);
4646 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004647 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmWordSize>().Int32Value());
4648 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
4649 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004650 if (can_be_null) {
4651 __ Bind(&is_null);
4652 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004653}
4654
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004655void LocationsBuilderARM::VisitTemporary(HTemporary* temp) {
4656 temp->SetLocations(nullptr);
4657}
4658
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004659void InstructionCodeGeneratorARM::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004660 // Nothing to do, this is driven by the code generator.
4661}
4662
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004663void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004664 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004665}
4666
4667void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004668 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4669}
4670
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004671void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
4672 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4673}
4674
4675void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004676 HBasicBlock* block = instruction->GetBlock();
4677 if (block->GetLoopInformation() != nullptr) {
4678 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4679 // The back edge will generate the suspend check.
4680 return;
4681 }
4682 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4683 // The goto will generate the suspend check.
4684 return;
4685 }
4686 GenerateSuspendCheck(instruction, nullptr);
4687}
4688
4689void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
4690 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004691 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004692 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
4693 if (slow_path == nullptr) {
4694 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
4695 instruction->SetSlowPath(slow_path);
4696 codegen_->AddSlowPath(slow_path);
4697 if (successor != nullptr) {
4698 DCHECK(successor->IsLoopHeader());
4699 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4700 }
4701 } else {
4702 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4703 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004704
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00004705 __ LoadFromOffset(
4706 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmWordSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004707 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004708 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004709 __ Bind(slow_path->GetReturnLabel());
4710 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004711 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004712 __ b(slow_path->GetEntryLabel());
4713 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004714}
4715
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004716ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
4717 return codegen_->GetAssembler();
4718}
4719
4720void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004721 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004722 Location source = move->GetSource();
4723 Location destination = move->GetDestination();
4724
4725 if (source.IsRegister()) {
4726 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004727 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004728 } else {
4729 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004730 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004731 SP, destination.GetStackIndex());
4732 }
4733 } else if (source.IsStackSlot()) {
4734 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004735 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004736 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004737 } else if (destination.IsFpuRegister()) {
4738 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004739 } else {
4740 DCHECK(destination.IsStackSlot());
4741 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
4742 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4743 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004744 } else if (source.IsFpuRegister()) {
4745 if (destination.IsFpuRegister()) {
4746 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004747 } else {
4748 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004749 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
4750 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004751 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004752 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004753 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
4754 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004755 } else if (destination.IsRegisterPair()) {
4756 DCHECK(ExpectedPairLayout(destination));
4757 __ LoadFromOffset(
4758 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
4759 } else {
4760 DCHECK(destination.IsFpuRegisterPair()) << destination;
4761 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
4762 SP,
4763 source.GetStackIndex());
4764 }
4765 } else if (source.IsRegisterPair()) {
4766 if (destination.IsRegisterPair()) {
4767 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
4768 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
4769 } else {
4770 DCHECK(destination.IsDoubleStackSlot()) << destination;
4771 DCHECK(ExpectedPairLayout(source));
4772 __ StoreToOffset(
4773 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
4774 }
4775 } else if (source.IsFpuRegisterPair()) {
4776 if (destination.IsFpuRegisterPair()) {
4777 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
4778 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
4779 } else {
4780 DCHECK(destination.IsDoubleStackSlot()) << destination;
4781 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
4782 SP,
4783 destination.GetStackIndex());
4784 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004785 } else {
4786 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004787 HConstant* constant = source.GetConstant();
4788 if (constant->IsIntConstant() || constant->IsNullConstant()) {
4789 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004790 if (destination.IsRegister()) {
4791 __ LoadImmediate(destination.AsRegister<Register>(), value);
4792 } else {
4793 DCHECK(destination.IsStackSlot());
4794 __ LoadImmediate(IP, value);
4795 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4796 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004797 } else if (constant->IsLongConstant()) {
4798 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004799 if (destination.IsRegisterPair()) {
4800 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
4801 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004802 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004803 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004804 __ LoadImmediate(IP, Low32Bits(value));
4805 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4806 __ LoadImmediate(IP, High32Bits(value));
4807 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
4808 }
4809 } else if (constant->IsDoubleConstant()) {
4810 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004811 if (destination.IsFpuRegisterPair()) {
4812 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004813 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004814 DCHECK(destination.IsDoubleStackSlot()) << destination;
4815 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004816 __ LoadImmediate(IP, Low32Bits(int_value));
4817 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4818 __ LoadImmediate(IP, High32Bits(int_value));
4819 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
4820 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004821 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004822 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004823 float value = constant->AsFloatConstant()->GetValue();
4824 if (destination.IsFpuRegister()) {
4825 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
4826 } else {
4827 DCHECK(destination.IsStackSlot());
4828 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
4829 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4830 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004831 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004832 }
4833}
4834
4835void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
4836 __ Mov(IP, reg);
4837 __ LoadFromOffset(kLoadWord, reg, SP, mem);
4838 __ StoreToOffset(kStoreWord, IP, SP, mem);
4839}
4840
4841void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
4842 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
4843 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
4844 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
4845 SP, mem1 + stack_offset);
4846 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
4847 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
4848 SP, mem2 + stack_offset);
4849 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
4850}
4851
4852void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004853 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004854 Location source = move->GetSource();
4855 Location destination = move->GetDestination();
4856
4857 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004858 DCHECK_NE(source.AsRegister<Register>(), IP);
4859 DCHECK_NE(destination.AsRegister<Register>(), IP);
4860 __ Mov(IP, source.AsRegister<Register>());
4861 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
4862 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004863 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004864 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004865 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004866 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004867 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4868 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004869 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00004870 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004871 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00004872 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004873 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004874 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004875 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004876 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004877 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
4878 destination.AsRegisterPairHigh<Register>(),
4879 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004880 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004881 Register low_reg = source.IsRegisterPair()
4882 ? source.AsRegisterPairLow<Register>()
4883 : destination.AsRegisterPairLow<Register>();
4884 int mem = source.IsRegisterPair()
4885 ? destination.GetStackIndex()
4886 : source.GetStackIndex();
4887 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004888 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004889 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004890 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004891 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004892 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
4893 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004894 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004895 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004896 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004897 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
4898 DRegister reg = source.IsFpuRegisterPair()
4899 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
4900 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
4901 int mem = source.IsFpuRegisterPair()
4902 ? destination.GetStackIndex()
4903 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004904 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004905 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004906 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004907 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
4908 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
4909 : destination.AsFpuRegister<SRegister>();
4910 int mem = source.IsFpuRegister()
4911 ? destination.GetStackIndex()
4912 : source.GetStackIndex();
4913
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00004914 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004915 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00004916 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00004917 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00004918 Exchange(source.GetStackIndex(), destination.GetStackIndex());
4919 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004920 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00004921 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004922 }
4923}
4924
4925void ParallelMoveResolverARM::SpillScratch(int reg) {
4926 __ Push(static_cast<Register>(reg));
4927}
4928
4929void ParallelMoveResolverARM::RestoreScratch(int reg) {
4930 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004931}
4932
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004933void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01004934 InvokeRuntimeCallingConvention calling_convention;
4935 CodeGenerator::CreateLoadClassLocationSummary(
4936 cls,
4937 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Roland Levillain3b359c72015-11-17 19:35:12 +00004938 Location::RegisterLocation(R0),
4939 /* code_generator_supports_read_barrier */ true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004940}
4941
4942void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004943 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01004944 if (cls->NeedsAccessCheck()) {
4945 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
4946 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
4947 cls,
4948 cls->GetDexPc(),
4949 nullptr);
Calin Juravle580b6092015-10-06 17:35:58 +01004950 return;
4951 }
4952
Roland Levillain3b359c72015-11-17 19:35:12 +00004953 Location out_loc = locations->Out();
4954 Register out = out_loc.AsRegister<Register>();
Calin Juravle580b6092015-10-06 17:35:58 +01004955 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00004956
Calin Juravle580b6092015-10-06 17:35:58 +01004957 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004958 DCHECK(!cls->CanCallRuntime());
4959 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillain3b359c72015-11-17 19:35:12 +00004960 uint32_t declaring_class_offset = ArtMethod::DeclaringClassOffset().Int32Value();
4961 if (kEmitCompilerReadBarrier) {
4962 // /* GcRoot<mirror::Class>* */ out = &(current_method->declaring_class_)
4963 __ AddConstant(out, current_method, declaring_class_offset);
4964 // /* mirror::Class* */ out = out->Read()
4965 codegen_->GenerateReadBarrierForRoot(cls, out_loc, out_loc);
4966 } else {
4967 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4968 __ LoadFromOffset(kLoadWord, out, current_method, declaring_class_offset);
4969 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004970 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004971 DCHECK(cls->CanCallRuntime());
Roland Levillain3b359c72015-11-17 19:35:12 +00004972 // /* GcRoot<mirror::Class>[] */ out =
4973 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004974 __ LoadFromOffset(kLoadWord,
4975 out,
4976 current_method,
Vladimir Marko05792b92015-08-03 11:56:49 +01004977 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
Roland Levillain3b359c72015-11-17 19:35:12 +00004978
4979 size_t cache_offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
4980 if (kEmitCompilerReadBarrier) {
4981 // /* GcRoot<mirror::Class>* */ out = &out[type_index]
4982 __ AddConstant(out, out, cache_offset);
4983 // /* mirror::Class* */ out = out->Read()
4984 codegen_->GenerateReadBarrierForRoot(cls, out_loc, out_loc);
4985 } else {
4986 // /* GcRoot<mirror::Class> */ out = out[type_index]
4987 __ LoadFromOffset(kLoadWord, out, out, cache_offset);
4988 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004989
Andreas Gampe85b62f22015-09-09 13:15:38 -07004990 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004991 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4992 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004993 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004994 if (cls->MustGenerateClinitCheck()) {
4995 GenerateClassInitializationCheck(slow_path, out);
4996 } else {
4997 __ Bind(slow_path->GetExitLabel());
4998 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004999 }
5000}
5001
5002void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
5003 LocationSummary* locations =
5004 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5005 locations->SetInAt(0, Location::RequiresRegister());
5006 if (check->HasUses()) {
5007 locations->SetOut(Location::SameAsFirstInput());
5008 }
5009}
5010
5011void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005012 // We assume the class is not null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005013 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005014 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005015 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005016 GenerateClassInitializationCheck(slow_path,
5017 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005018}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005019
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005020void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005021 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005022 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
5023 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
5024 __ b(slow_path->GetEntryLabel(), LT);
5025 // Even if the initialized flag is set, we may be in a situation where caches are not synced
5026 // properly. Therefore, we do a memory fence.
5027 __ dmb(ISH);
5028 __ Bind(slow_path->GetExitLabel());
5029}
5030
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005031void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
5032 LocationSummary* locations =
5033 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005034 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005035 locations->SetOut(Location::RequiresRegister());
5036}
5037
5038void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07005039 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005040 codegen_->AddSlowPath(slow_path);
5041
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005042 LocationSummary* locations = load->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005043 Location out_loc = locations->Out();
5044 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005045 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005046
5047 uint32_t declaring_class_offset = ArtMethod::DeclaringClassOffset().Int32Value();
5048 if (kEmitCompilerReadBarrier) {
5049 // /* GcRoot<mirror::Class>* */ out = &(current_method->declaring_class_)
5050 __ AddConstant(out, current_method, declaring_class_offset);
5051 // /* mirror::Class* */ out = out->Read()
5052 codegen_->GenerateReadBarrierForRoot(load, out_loc, out_loc);
5053 } else {
5054 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5055 __ LoadFromOffset(kLoadWord, out, current_method, declaring_class_offset);
5056 }
5057
5058 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
Mathieu Chartiereace4582014-11-24 18:29:54 -08005059 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
Roland Levillain3b359c72015-11-17 19:35:12 +00005060
5061 size_t cache_offset = CodeGenerator::GetCacheOffset(load->GetStringIndex());
5062 if (kEmitCompilerReadBarrier) {
5063 // /* GcRoot<mirror::String>* */ out = &out[string_index]
5064 __ AddConstant(out, out, cache_offset);
5065 // /* mirror::String* */ out = out->Read()
5066 codegen_->GenerateReadBarrierForRoot(load, out_loc, out_loc);
5067 } else {
5068 // /* GcRoot<mirror::String> */ out = out[string_index]
5069 __ LoadFromOffset(kLoadWord, out, out, cache_offset);
5070 }
5071
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005072 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005073 __ Bind(slow_path->GetExitLabel());
5074}
5075
David Brazdilcb1c0552015-08-04 16:22:25 +01005076static int32_t GetExceptionTlsOffset() {
5077 return Thread::ExceptionOffset<kArmWordSize>().Int32Value();
5078}
5079
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005080void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
5081 LocationSummary* locations =
5082 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5083 locations->SetOut(Location::RequiresRegister());
5084}
5085
5086void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005087 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01005088 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
5089}
5090
5091void LocationsBuilderARM::VisitClearException(HClearException* clear) {
5092 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5093}
5094
5095void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005096 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01005097 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005098}
5099
5100void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
5101 LocationSummary* locations =
5102 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5103 InvokeRuntimeCallingConvention calling_convention;
5104 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5105}
5106
5107void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
5108 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00005109 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005110}
5111
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005112void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005113 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain3b359c72015-11-17 19:35:12 +00005114 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5115 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005116 case TypeCheckKind::kExactCheck:
5117 case TypeCheckKind::kAbstractClassCheck:
5118 case TypeCheckKind::kClassHierarchyCheck:
5119 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005120 call_kind =
5121 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005122 break;
5123 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005124 case TypeCheckKind::kUnresolvedCheck:
5125 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005126 call_kind = LocationSummary::kCallOnSlowPath;
5127 break;
5128 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005129
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005130 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain3b359c72015-11-17 19:35:12 +00005131 locations->SetInAt(0, Location::RequiresRegister());
5132 locations->SetInAt(1, Location::RequiresRegister());
5133 // The "out" register is used as a temporary, so it overlaps with the inputs.
5134 // Note that TypeCheckSlowPathARM uses this register too.
5135 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5136 // When read barriers are enabled, we need a temporary register for
5137 // some cases.
5138 if (kEmitCompilerReadBarrier &&
5139 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5140 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5141 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
5142 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005143 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005144}
5145
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005146void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005147 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005148 Location obj_loc = locations->InAt(0);
5149 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005150 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005151 Location out_loc = locations->Out();
5152 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005153 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005154 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5155 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5156 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005157 Label done, zero;
Andreas Gampe85b62f22015-09-09 13:15:38 -07005158 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005159
5160 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005161 // avoid null check if we know obj is not null.
5162 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00005163 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005164 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005165
Roland Levillain3b359c72015-11-17 19:35:12 +00005166 // /* HeapReference<Class> */ out = obj->klass_
5167 __ LoadFromOffset(kLoadWord, out, obj, class_offset);
5168 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005169
5170 switch (instruction->GetTypeCheckKind()) {
5171 case TypeCheckKind::kExactCheck: {
5172 __ cmp(out, ShifterOperand(cls));
5173 // Classes must be equal for the instanceof to succeed.
5174 __ b(&zero, NE);
5175 __ LoadImmediate(out, 1);
5176 __ b(&done);
5177 break;
5178 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005179
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005180 case TypeCheckKind::kAbstractClassCheck: {
5181 // If the class is abstract, we eagerly fetch the super class of the
5182 // object to avoid doing a comparison we know will fail.
5183 Label loop;
5184 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005185 Location temp_loc = kEmitCompilerReadBarrier ? locations->GetTemp(0) : Location::NoLocation();
5186 if (kEmitCompilerReadBarrier) {
5187 // Save the value of `out` into `temp` before overwriting it
5188 // in the following move operation, as we will need it for the
5189 // read barrier below.
5190 Register temp = temp_loc.AsRegister<Register>();
5191 __ Mov(temp, out);
5192 }
5193 // /* HeapReference<Class> */ out = out->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005194 __ LoadFromOffset(kLoadWord, out, out, super_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00005195 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, temp_loc, super_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005196 // If `out` is null, we use it for the result, and jump to `done`.
5197 __ CompareAndBranchIfZero(out, &done);
5198 __ cmp(out, ShifterOperand(cls));
5199 __ b(&loop, NE);
5200 __ LoadImmediate(out, 1);
5201 if (zero.IsLinked()) {
5202 __ b(&done);
5203 }
5204 break;
5205 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005206
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005207 case TypeCheckKind::kClassHierarchyCheck: {
5208 // Walk over the class hierarchy to find a match.
5209 Label loop, success;
5210 __ Bind(&loop);
5211 __ cmp(out, ShifterOperand(cls));
5212 __ b(&success, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005213 Location temp_loc = kEmitCompilerReadBarrier ? locations->GetTemp(0) : Location::NoLocation();
5214 if (kEmitCompilerReadBarrier) {
5215 // Save the value of `out` into `temp` before overwriting it
5216 // in the following move operation, as we will need it for the
5217 // read barrier below.
5218 Register temp = temp_loc.AsRegister<Register>();
5219 __ Mov(temp, out);
5220 }
5221 // /* HeapReference<Class> */ out = out->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005222 __ LoadFromOffset(kLoadWord, out, out, super_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00005223 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, temp_loc, super_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005224 __ CompareAndBranchIfNonZero(out, &loop);
5225 // If `out` is null, we use it for the result, and jump to `done`.
5226 __ b(&done);
5227 __ Bind(&success);
5228 __ LoadImmediate(out, 1);
5229 if (zero.IsLinked()) {
5230 __ b(&done);
5231 }
5232 break;
5233 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005234
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005235 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005236 // Do an exact check.
5237 Label exact_check;
5238 __ cmp(out, ShifterOperand(cls));
5239 __ b(&exact_check, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005240 // Otherwise, we need to check that the object's class is a non-primitive array.
5241 Location temp_loc = kEmitCompilerReadBarrier ? locations->GetTemp(0) : Location::NoLocation();
5242 if (kEmitCompilerReadBarrier) {
5243 // Save the value of `out` into `temp` before overwriting it
5244 // in the following move operation, as we will need it for the
5245 // read barrier below.
5246 Register temp = temp_loc.AsRegister<Register>();
5247 __ Mov(temp, out);
5248 }
5249 // /* HeapReference<Class> */ out = out->component_type_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005250 __ LoadFromOffset(kLoadWord, out, out, component_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00005251 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, temp_loc, component_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005252 // If `out` is null, we use it for the result, and jump to `done`.
5253 __ CompareAndBranchIfZero(out, &done);
5254 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
5255 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
5256 __ CompareAndBranchIfNonZero(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005257 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005258 __ LoadImmediate(out, 1);
5259 __ b(&done);
5260 break;
5261 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005262
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005263 case TypeCheckKind::kArrayCheck: {
5264 __ cmp(out, ShifterOperand(cls));
5265 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain3b359c72015-11-17 19:35:12 +00005266 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5267 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005268 codegen_->AddSlowPath(slow_path);
5269 __ b(slow_path->GetEntryLabel(), NE);
5270 __ LoadImmediate(out, 1);
5271 if (zero.IsLinked()) {
5272 __ b(&done);
5273 }
5274 break;
5275 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005276
Calin Juravle98893e12015-10-02 21:05:03 +01005277 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005278 case TypeCheckKind::kInterfaceCheck: {
5279 // Note that we indeed only call on slow path, but we always go
5280 // into the slow path for the unresolved & interface check
5281 // cases.
5282 //
5283 // We cannot directly call the InstanceofNonTrivial runtime
5284 // entry point without resorting to a type checking slow path
5285 // here (i.e. by calling InvokeRuntime directly), as it would
5286 // require to assign fixed registers for the inputs of this
5287 // HInstanceOf instruction (following the runtime calling
5288 // convention), which might be cluttered by the potential first
5289 // read barrier emission at the beginning of this method.
5290 DCHECK(locations->OnlyCallsOnSlowPath());
5291 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5292 /* is_fatal */ false);
5293 codegen_->AddSlowPath(slow_path);
5294 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005295 if (zero.IsLinked()) {
5296 __ b(&done);
5297 }
5298 break;
5299 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005300 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005301
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005302 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005303 __ Bind(&zero);
5304 __ LoadImmediate(out, 0);
5305 }
5306
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005307 if (done.IsLinked()) {
5308 __ Bind(&done);
5309 }
5310
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005311 if (slow_path != nullptr) {
5312 __ Bind(slow_path->GetExitLabel());
5313 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005314}
5315
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005316void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005317 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5318 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
5319
Roland Levillain3b359c72015-11-17 19:35:12 +00005320 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5321 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005322 case TypeCheckKind::kExactCheck:
5323 case TypeCheckKind::kAbstractClassCheck:
5324 case TypeCheckKind::kClassHierarchyCheck:
5325 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005326 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5327 LocationSummary::kCallOnSlowPath :
5328 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005329 break;
5330 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005331 case TypeCheckKind::kUnresolvedCheck:
5332 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005333 call_kind = LocationSummary::kCallOnSlowPath;
5334 break;
5335 }
5336
Roland Levillain3b359c72015-11-17 19:35:12 +00005337 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5338 locations->SetInAt(0, Location::RequiresRegister());
5339 locations->SetInAt(1, Location::RequiresRegister());
5340 // Note that TypeCheckSlowPathARM uses this "temp" register too.
5341 locations->AddTemp(Location::RequiresRegister());
5342 // When read barriers are enabled, we need an additional temporary
5343 // register for some cases.
5344 if (kEmitCompilerReadBarrier &&
5345 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5346 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5347 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005348 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005349 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005350}
5351
5352void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
5353 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005354 Location obj_loc = locations->InAt(0);
5355 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005356 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005357 Location temp_loc = locations->GetTemp(0);
5358 Register temp = temp_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005359 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005360 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5361 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5362 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005363
Roland Levillain3b359c72015-11-17 19:35:12 +00005364 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5365 bool is_type_check_slow_path_fatal =
5366 (type_check_kind == TypeCheckKind::kExactCheck ||
5367 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5368 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5369 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5370 !instruction->CanThrowIntoCatchBlock();
5371 SlowPathCode* type_check_slow_path =
5372 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5373 is_type_check_slow_path_fatal);
5374 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005375
5376 Label done;
5377 // Avoid null check if we know obj is not null.
5378 if (instruction->MustDoNullCheck()) {
5379 __ CompareAndBranchIfZero(obj, &done);
5380 }
5381
Roland Levillain3b359c72015-11-17 19:35:12 +00005382 // /* HeapReference<Class> */ temp = obj->klass_
5383 __ LoadFromOffset(kLoadWord, temp, obj, class_offset);
5384 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005385
Roland Levillain3b359c72015-11-17 19:35:12 +00005386 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005387 case TypeCheckKind::kExactCheck:
5388 case TypeCheckKind::kArrayCheck: {
5389 __ cmp(temp, ShifterOperand(cls));
5390 // Jump to slow path for throwing the exception or doing a
5391 // more involved array check.
Roland Levillain3b359c72015-11-17 19:35:12 +00005392 __ b(type_check_slow_path->GetEntryLabel(), NE);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005393 break;
5394 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005395
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005396 case TypeCheckKind::kAbstractClassCheck: {
5397 // If the class is abstract, we eagerly fetch the super class of the
5398 // object to avoid doing a comparison we know will fail.
Roland Levillain3b359c72015-11-17 19:35:12 +00005399 Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005400 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005401 Location temp2_loc =
5402 kEmitCompilerReadBarrier ? locations->GetTemp(1) : Location::NoLocation();
5403 if (kEmitCompilerReadBarrier) {
5404 // Save the value of `temp` into `temp2` before overwriting it
5405 // in the following move operation, as we will need it for the
5406 // read barrier below.
5407 Register temp2 = temp2_loc.AsRegister<Register>();
5408 __ Mov(temp2, temp);
5409 }
5410 // /* HeapReference<Class> */ temp = temp->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005411 __ LoadFromOffset(kLoadWord, temp, temp, super_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00005412 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, temp2_loc, super_offset);
5413
5414 // If the class reference currently in `temp` is not null, jump
5415 // to the `compare_classes` label to compare it with the checked
5416 // class.
5417 __ CompareAndBranchIfNonZero(temp, &compare_classes);
5418 // Otherwise, jump to the slow path to throw the exception.
5419 //
5420 // But before, move back the object's class into `temp` before
5421 // going into the slow path, as it has been overwritten in the
5422 // meantime.
5423 // /* HeapReference<Class> */ temp = obj->klass_
5424 __ LoadFromOffset(kLoadWord, temp, obj, class_offset);
5425 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
5426 __ b(type_check_slow_path->GetEntryLabel());
5427
5428 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005429 __ cmp(temp, ShifterOperand(cls));
5430 __ b(&loop, NE);
5431 break;
5432 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005433
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005434 case TypeCheckKind::kClassHierarchyCheck: {
5435 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005436 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005437 __ Bind(&loop);
5438 __ cmp(temp, ShifterOperand(cls));
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005439 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005440
5441 Location temp2_loc =
5442 kEmitCompilerReadBarrier ? locations->GetTemp(1) : Location::NoLocation();
5443 if (kEmitCompilerReadBarrier) {
5444 // Save the value of `temp` into `temp2` before overwriting it
5445 // in the following move operation, as we will need it for the
5446 // read barrier below.
5447 Register temp2 = temp2_loc.AsRegister<Register>();
5448 __ Mov(temp2, temp);
5449 }
5450 // /* HeapReference<Class> */ temp = temp->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005451 __ LoadFromOffset(kLoadWord, temp, temp, super_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00005452 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, temp2_loc, super_offset);
5453
5454 // If the class reference currently in `temp` is not null, jump
5455 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005456 __ CompareAndBranchIfNonZero(temp, &loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005457 // Otherwise, jump to the slow path to throw the exception.
5458 //
5459 // But before, move back the object's class into `temp` before
5460 // going into the slow path, as it has been overwritten in the
5461 // meantime.
5462 // /* HeapReference<Class> */ temp = obj->klass_
5463 __ LoadFromOffset(kLoadWord, temp, obj, class_offset);
5464 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
5465 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005466 break;
5467 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005468
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005469 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005470 // Do an exact check.
Roland Levillain3b359c72015-11-17 19:35:12 +00005471 Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005472 __ cmp(temp, ShifterOperand(cls));
5473 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005474
5475 // Otherwise, we need to check that the object's class is a non-primitive array.
5476 Location temp2_loc =
5477 kEmitCompilerReadBarrier ? locations->GetTemp(1) : Location::NoLocation();
5478 if (kEmitCompilerReadBarrier) {
5479 // Save the value of `temp` into `temp2` before overwriting it
5480 // in the following move operation, as we will need it for the
5481 // read barrier below.
5482 Register temp2 = temp2_loc.AsRegister<Register>();
5483 __ Mov(temp2, temp);
5484 }
5485 // /* HeapReference<Class> */ temp = temp->component_type_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005486 __ LoadFromOffset(kLoadWord, temp, temp, component_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00005487 codegen_->MaybeGenerateReadBarrier(
5488 instruction, temp_loc, temp_loc, temp2_loc, component_offset);
5489
5490 // If the component type is not null (i.e. the object is indeed
5491 // an array), jump to label `check_non_primitive_component_type`
5492 // to further check that this component type is not a primitive
5493 // type.
5494 __ CompareAndBranchIfNonZero(temp, &check_non_primitive_component_type);
5495 // Otherwise, jump to the slow path to throw the exception.
5496 //
5497 // But before, move back the object's class into `temp` before
5498 // going into the slow path, as it has been overwritten in the
5499 // meantime.
5500 // /* HeapReference<Class> */ temp = obj->klass_
5501 __ LoadFromOffset(kLoadWord, temp, obj, class_offset);
5502 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
5503 __ b(type_check_slow_path->GetEntryLabel());
5504
5505 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005506 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00005507 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
5508 __ CompareAndBranchIfZero(temp, &done);
5509 // Same comment as above regarding `temp` and the slow path.
5510 // /* HeapReference<Class> */ temp = obj->klass_
5511 __ LoadFromOffset(kLoadWord, temp, obj, class_offset);
5512 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
5513 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005514 break;
5515 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005516
Calin Juravle98893e12015-10-02 21:05:03 +01005517 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005518 case TypeCheckKind::kInterfaceCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005519 // We always go into the type check slow path for the unresolved &
5520 // interface check cases.
5521 //
5522 // We cannot directly call the CheckCast runtime entry point
5523 // without resorting to a type checking slow path here (i.e. by
5524 // calling InvokeRuntime directly), as it would require to
5525 // assign fixed registers for the inputs of this HInstanceOf
5526 // instruction (following the runtime calling convention), which
5527 // might be cluttered by the potential first read barrier
5528 // emission at the beginning of this method.
5529 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005530 break;
5531 }
5532 __ Bind(&done);
5533
Roland Levillain3b359c72015-11-17 19:35:12 +00005534 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005535}
5536
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005537void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
5538 LocationSummary* locations =
5539 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5540 InvokeRuntimeCallingConvention calling_convention;
5541 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5542}
5543
5544void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
5545 codegen_->InvokeRuntime(instruction->IsEnter()
5546 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
5547 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00005548 instruction->GetDexPc(),
5549 nullptr);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005550}
5551
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005552void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
5553void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
5554void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005555
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005556void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005557 LocationSummary* locations =
5558 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5559 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
5560 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005561 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005562 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005563 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00005564 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005565}
5566
5567void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
5568 HandleBitwiseOperation(instruction);
5569}
5570
5571void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
5572 HandleBitwiseOperation(instruction);
5573}
5574
5575void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
5576 HandleBitwiseOperation(instruction);
5577}
5578
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005579void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
5580 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
5581 if (value == 0xffffffffu) {
5582 if (out != first) {
5583 __ mov(out, ShifterOperand(first));
5584 }
5585 return;
5586 }
5587 if (value == 0u) {
5588 __ mov(out, ShifterOperand(0));
5589 return;
5590 }
5591 ShifterOperand so;
5592 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
5593 __ and_(out, first, so);
5594 } else {
5595 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so));
5596 __ bic(out, first, ShifterOperand(~value));
5597 }
5598}
5599
5600void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
5601 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
5602 if (value == 0u) {
5603 if (out != first) {
5604 __ mov(out, ShifterOperand(first));
5605 }
5606 return;
5607 }
5608 if (value == 0xffffffffu) {
5609 __ mvn(out, ShifterOperand(0));
5610 return;
5611 }
5612 ShifterOperand so;
5613 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
5614 __ orr(out, first, so);
5615 } else {
5616 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
5617 __ orn(out, first, ShifterOperand(~value));
5618 }
5619}
5620
5621void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
5622 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
5623 if (value == 0u) {
5624 if (out != first) {
5625 __ mov(out, ShifterOperand(first));
5626 }
5627 return;
5628 }
5629 __ eor(out, first, ShifterOperand(value));
5630}
5631
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005632void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
5633 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005634 Location first = locations->InAt(0);
5635 Location second = locations->InAt(1);
5636 Location out = locations->Out();
5637
5638 if (second.IsConstant()) {
5639 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
5640 uint32_t value_low = Low32Bits(value);
5641 if (instruction->GetResultType() == Primitive::kPrimInt) {
5642 Register first_reg = first.AsRegister<Register>();
5643 Register out_reg = out.AsRegister<Register>();
5644 if (instruction->IsAnd()) {
5645 GenerateAndConst(out_reg, first_reg, value_low);
5646 } else if (instruction->IsOr()) {
5647 GenerateOrrConst(out_reg, first_reg, value_low);
5648 } else {
5649 DCHECK(instruction->IsXor());
5650 GenerateEorConst(out_reg, first_reg, value_low);
5651 }
5652 } else {
5653 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
5654 uint32_t value_high = High32Bits(value);
5655 Register first_low = first.AsRegisterPairLow<Register>();
5656 Register first_high = first.AsRegisterPairHigh<Register>();
5657 Register out_low = out.AsRegisterPairLow<Register>();
5658 Register out_high = out.AsRegisterPairHigh<Register>();
5659 if (instruction->IsAnd()) {
5660 GenerateAndConst(out_low, first_low, value_low);
5661 GenerateAndConst(out_high, first_high, value_high);
5662 } else if (instruction->IsOr()) {
5663 GenerateOrrConst(out_low, first_low, value_low);
5664 GenerateOrrConst(out_high, first_high, value_high);
5665 } else {
5666 DCHECK(instruction->IsXor());
5667 GenerateEorConst(out_low, first_low, value_low);
5668 GenerateEorConst(out_high, first_high, value_high);
5669 }
5670 }
5671 return;
5672 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005673
5674 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005675 Register first_reg = first.AsRegister<Register>();
5676 ShifterOperand second_reg(second.AsRegister<Register>());
5677 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005678 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005679 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005680 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005681 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005682 } else {
5683 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005684 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005685 }
5686 } else {
5687 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005688 Register first_low = first.AsRegisterPairLow<Register>();
5689 Register first_high = first.AsRegisterPairHigh<Register>();
5690 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
5691 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
5692 Register out_low = out.AsRegisterPairLow<Register>();
5693 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005694 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005695 __ and_(out_low, first_low, second_low);
5696 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005697 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005698 __ orr(out_low, first_low, second_low);
5699 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005700 } else {
5701 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005702 __ eor(out_low, first_low, second_low);
5703 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005704 }
5705 }
5706}
5707
Roland Levillain3b359c72015-11-17 19:35:12 +00005708void CodeGeneratorARM::GenerateReadBarrier(HInstruction* instruction,
5709 Location out,
5710 Location ref,
5711 Location obj,
5712 uint32_t offset,
5713 Location index) {
5714 DCHECK(kEmitCompilerReadBarrier);
5715
5716 // If heap poisoning is enabled, the unpoisoning of the loaded
5717 // reference will be carried out by the runtime within the slow
5718 // path.
5719 //
5720 // Note that `ref` currently does not get unpoisoned (when heap
5721 // poisoning is enabled), which is alright as the `ref` argument is
5722 // not used by the artReadBarrierSlow entry point.
5723 //
5724 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
5725 SlowPathCode* slow_path = new (GetGraph()->GetArena())
5726 ReadBarrierForHeapReferenceSlowPathARM(instruction, out, ref, obj, offset, index);
5727 AddSlowPath(slow_path);
5728
5729 // TODO: When read barrier has a fast path, add it here.
5730 /* Currently the read barrier call is inserted after the original load.
5731 * However, if we have a fast path, we need to perform the load of obj.LockWord *before* the
5732 * original load. This load-load ordering is required by the read barrier.
5733 * The fast path/slow path (for Baker's algorithm) should look like:
5734 *
5735 * bool isGray = obj.LockWord & kReadBarrierMask;
5736 * lfence; // load fence or artificial data dependence to prevent load-load reordering
5737 * ref = obj.field; // this is the original load
5738 * if (isGray) {
5739 * ref = Mark(ref); // ideally the slow path just does Mark(ref)
5740 * }
5741 */
5742
5743 __ b(slow_path->GetEntryLabel());
5744 __ Bind(slow_path->GetExitLabel());
5745}
5746
5747void CodeGeneratorARM::MaybeGenerateReadBarrier(HInstruction* instruction,
5748 Location out,
5749 Location ref,
5750 Location obj,
5751 uint32_t offset,
5752 Location index) {
5753 if (kEmitCompilerReadBarrier) {
5754 // If heap poisoning is enabled, unpoisoning will be taken care of
5755 // by the runtime within the slow path.
5756 GenerateReadBarrier(instruction, out, ref, obj, offset, index);
5757 } else if (kPoisonHeapReferences) {
5758 __ UnpoisonHeapReference(out.AsRegister<Register>());
5759 }
5760}
5761
5762void CodeGeneratorARM::GenerateReadBarrierForRoot(HInstruction* instruction,
5763 Location out,
5764 Location root) {
5765 DCHECK(kEmitCompilerReadBarrier);
5766
5767 // Note that GC roots are not affected by heap poisoning, so we do
5768 // not need to do anything special for this here.
5769 SlowPathCode* slow_path =
5770 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM(instruction, out, root);
5771 AddSlowPath(slow_path);
5772
5773 // TODO: Implement a fast path for ReadBarrierForRoot, performing
5774 // the following operation (for Baker's algorithm):
5775 //
5776 // if (thread.tls32_.is_gc_marking) {
5777 // root = Mark(root);
5778 // }
5779
5780 __ b(slow_path->GetEntryLabel());
5781 __ Bind(slow_path->GetExitLabel());
5782}
5783
Vladimir Markodc151b22015-10-15 18:02:30 +01005784HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch(
5785 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
5786 MethodReference target_method) {
5787 if (desired_dispatch_info.method_load_kind ==
5788 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative) {
5789 // TODO: Implement this type. For the moment, we fall back to kDexCacheViaMethod.
5790 return HInvokeStaticOrDirect::DispatchInfo {
5791 HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod,
5792 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
5793 0u,
5794 0u
5795 };
5796 }
5797 if (desired_dispatch_info.code_ptr_location ==
5798 HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative) {
5799 const DexFile& outer_dex_file = GetGraph()->GetDexFile();
5800 if (&outer_dex_file != target_method.dex_file) {
5801 // Calls across dex files are more likely to exceed the available BL range,
5802 // so use absolute patch with fixup if available and kCallArtMethod otherwise.
5803 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location =
5804 (desired_dispatch_info.method_load_kind ==
5805 HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup)
5806 ? HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup
5807 : HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
5808 return HInvokeStaticOrDirect::DispatchInfo {
5809 desired_dispatch_info.method_load_kind,
5810 code_ptr_location,
5811 desired_dispatch_info.method_load_data,
5812 0u
5813 };
5814 }
5815 }
5816 return desired_dispatch_info;
5817}
5818
Nicolas Geoffray38207af2015-06-01 15:46:22 +01005819void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00005820 // For better instruction scheduling we load the direct code pointer before the method pointer.
Vladimir Marko58155012015-08-19 12:49:41 +00005821 switch (invoke->GetCodePtrLocation()) {
Vladimir Marko58155012015-08-19 12:49:41 +00005822 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
5823 // LR = code address from literal pool with link-time patch.
5824 __ LoadLiteral(LR, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
Vladimir Marko58155012015-08-19 12:49:41 +00005825 break;
5826 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
5827 // LR = invoke->GetDirectCodePtr();
5828 __ LoadImmediate(LR, invoke->GetDirectCodePtr());
Vladimir Marko58155012015-08-19 12:49:41 +00005829 break;
5830 default:
5831 break;
5832 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08005833
Vladimir Marko58155012015-08-19 12:49:41 +00005834 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
5835 switch (invoke->GetMethodLoadKind()) {
5836 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
5837 // temp = thread->string_init_entrypoint
5838 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, invoke->GetStringInitOffset());
5839 break;
5840 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00005841 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00005842 break;
5843 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
5844 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
5845 break;
5846 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
5847 __ LoadLiteral(temp.AsRegister<Register>(),
5848 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
5849 break;
5850 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01005851 // TODO: Implement this type.
5852 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
5853 LOG(FATAL) << "Unsupported";
5854 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00005855 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00005856 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00005857 Register method_reg;
5858 Register reg = temp.AsRegister<Register>();
5859 if (current_method.IsRegister()) {
5860 method_reg = current_method.AsRegister<Register>();
5861 } else {
5862 DCHECK(invoke->GetLocations()->Intrinsified());
5863 DCHECK(!current_method.IsValid());
5864 method_reg = reg;
5865 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
5866 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005867 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
5868 __ LoadFromOffset(kLoadWord,
5869 reg,
5870 method_reg,
5871 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00005872 // temp = temp[index_in_cache]
5873 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
5874 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
5875 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01005876 }
Vladimir Marko58155012015-08-19 12:49:41 +00005877 }
5878
5879 switch (invoke->GetCodePtrLocation()) {
5880 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
5881 __ bl(GetFrameEntryLabel());
5882 break;
5883 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01005884 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07005885 __ BindTrackedLabel(&relative_call_patches_.back().label);
Vladimir Markodc151b22015-10-15 18:02:30 +01005886 // Arbitrarily branch to the BL itself, override at link time.
5887 __ bl(&relative_call_patches_.back().label);
5888 break;
Vladimir Marko58155012015-08-19 12:49:41 +00005889 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
5890 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
5891 // LR prepared above for better instruction scheduling.
Vladimir Marko58155012015-08-19 12:49:41 +00005892 // LR()
5893 __ blx(LR);
5894 break;
5895 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
5896 // LR = callee_method->entry_point_from_quick_compiled_code_
5897 __ LoadFromOffset(
5898 kLoadWord, LR, callee_method.AsRegister<Register>(),
5899 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize).Int32Value());
5900 // LR()
5901 __ blx(LR);
5902 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08005903 }
5904
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08005905 DCHECK(!IsLeafMethod());
5906}
5907
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005908void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
5909 Register temp = temp_location.AsRegister<Register>();
5910 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5911 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
5912 LocationSummary* locations = invoke->GetLocations();
5913 Location receiver = locations->InAt(0);
5914 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005915 DCHECK(receiver.IsRegister());
Roland Levillain3b359c72015-11-17 19:35:12 +00005916 // /* HeapReference<Class> */ temp = receiver->klass_
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005917 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
5918 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00005919 // Instead of simply (possibly) unpoisoning `temp` here, we should
5920 // emit a read barrier for the previous class reference load.
5921 // However this is not required in practice, as this is an
5922 // intermediate/temporary reference and because the current
5923 // concurrent copying collector keeps the from-space memory
5924 // intact/accessible until the end of the marking phase (the
5925 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005926 __ MaybeUnpoisonHeapReference(temp);
5927 // temp = temp->GetMethodAt(method_offset);
5928 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
5929 kArmWordSize).Int32Value();
5930 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
5931 // LR = temp->GetEntryPoint();
5932 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
5933 // LR();
5934 __ blx(LR);
5935}
5936
Vladimir Marko58155012015-08-19 12:49:41 +00005937void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
5938 DCHECK(linker_patches->empty());
5939 size_t size = method_patches_.size() + call_patches_.size() + relative_call_patches_.size();
5940 linker_patches->reserve(size);
5941 for (const auto& entry : method_patches_) {
5942 const MethodReference& target_method = entry.first;
5943 Literal* literal = entry.second;
5944 DCHECK(literal->GetLabel()->IsBound());
5945 uint32_t literal_offset = literal->GetLabel()->Position();
5946 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
5947 target_method.dex_file,
5948 target_method.dex_method_index));
5949 }
5950 for (const auto& entry : call_patches_) {
5951 const MethodReference& target_method = entry.first;
5952 Literal* literal = entry.second;
5953 DCHECK(literal->GetLabel()->IsBound());
5954 uint32_t literal_offset = literal->GetLabel()->Position();
5955 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
5956 target_method.dex_file,
5957 target_method.dex_method_index));
5958 }
5959 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
5960 uint32_t literal_offset = info.label.Position();
5961 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
5962 info.target_method.dex_file,
5963 info.target_method.dex_method_index));
5964 }
5965}
5966
5967Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
5968 MethodToLiteralMap* map) {
5969 // Look up the literal for target_method.
5970 auto lb = map->lower_bound(target_method);
5971 if (lb != map->end() && !map->key_comp()(target_method, lb->first)) {
5972 return lb->second;
5973 }
5974 // We don't have a literal for this method yet, insert a new one.
5975 Literal* literal = __ NewLiteral<uint32_t>(0u);
5976 map->PutBefore(lb, target_method, literal);
5977 return literal;
5978}
5979
5980Literal* CodeGeneratorARM::DeduplicateMethodAddressLiteral(MethodReference target_method) {
5981 return DeduplicateMethodLiteral(target_method, &method_patches_);
5982}
5983
5984Literal* CodeGeneratorARM::DeduplicateMethodCodeLiteral(MethodReference target_method) {
5985 return DeduplicateMethodLiteral(target_method, &call_patches_);
5986}
5987
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005988void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005989 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005990 LOG(FATAL) << "Unreachable";
5991}
5992
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005993void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005994 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005995 LOG(FATAL) << "Unreachable";
5996}
5997
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005998void LocationsBuilderARM::VisitFakeString(HFakeString* instruction) {
5999 DCHECK(codegen_->IsBaseline());
6000 LocationSummary* locations =
6001 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6002 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
6003}
6004
6005void InstructionCodeGeneratorARM::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
6006 DCHECK(codegen_->IsBaseline());
6007 // Will be generated at use site.
6008}
6009
Mark Mendellfe57faa2015-09-18 09:26:15 -04006010// Simple implementation of packed switch - generate cascaded compare/jumps.
6011void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6012 LocationSummary* locations =
6013 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6014 locations->SetInAt(0, Location::RequiresRegister());
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006015 if (switch_instr->GetNumEntries() >= kPackedSwitchJumpTableThreshold &&
6016 codegen_->GetAssembler()->IsThumb()) {
6017 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
6018 if (switch_instr->GetStartValue() != 0) {
6019 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
6020 }
6021 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04006022}
6023
6024void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6025 int32_t lower_bound = switch_instr->GetStartValue();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006026 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006027 LocationSummary* locations = switch_instr->GetLocations();
6028 Register value_reg = locations->InAt(0).AsRegister<Register>();
6029 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6030
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006031 if (num_entries < kPackedSwitchJumpTableThreshold || !codegen_->GetAssembler()->IsThumb()) {
6032 // Create a series of compare/jumps.
6033 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6034 for (uint32_t i = 0; i < num_entries; i++) {
6035 GenerateCompareWithImmediate(value_reg, lower_bound + i);
6036 __ b(codegen_->GetLabelOf(successors[i]), EQ);
6037 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04006038
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006039 // And the default for any other value.
6040 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6041 __ b(codegen_->GetLabelOf(default_block));
6042 }
6043 } else {
6044 // Create a table lookup.
6045 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
6046
6047 // Materialize a pointer to the switch table
6048 std::vector<Label*> labels(num_entries);
6049 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6050 for (uint32_t i = 0; i < num_entries; i++) {
6051 labels[i] = codegen_->GetLabelOf(successors[i]);
6052 }
6053 JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg);
6054
6055 // Remove the bias.
6056 Register key_reg;
6057 if (lower_bound != 0) {
6058 key_reg = locations->GetTemp(1).AsRegister<Register>();
6059 __ AddConstant(key_reg, value_reg, -lower_bound);
6060 } else {
6061 key_reg = value_reg;
6062 }
6063
6064 // Check whether the value is in the table, jump to default block if not.
6065 __ CmpConstant(key_reg, num_entries - 1);
6066 __ b(codegen_->GetLabelOf(default_block), Condition::HI);
6067
6068 // Load the displacement from the table.
6069 __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2));
6070
6071 // Dispatch is a direct add to the PC (for Thumb2).
6072 __ EmitJumpTableDispatch(table, temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006073 }
6074}
6075
Andreas Gampe85b62f22015-09-09 13:15:38 -07006076void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
6077 if (!trg.IsValid()) {
6078 DCHECK(type == Primitive::kPrimVoid);
6079 return;
6080 }
6081
6082 DCHECK_NE(type, Primitive::kPrimVoid);
6083
6084 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
6085 if (return_loc.Equals(trg)) {
6086 return;
6087 }
6088
6089 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
6090 // with the last branch.
6091 if (type == Primitive::kPrimLong) {
6092 HParallelMove parallel_move(GetGraph()->GetArena());
6093 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
6094 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
6095 GetMoveResolver()->EmitNativeCode(&parallel_move);
6096 } else if (type == Primitive::kPrimDouble) {
6097 HParallelMove parallel_move(GetGraph()->GetArena());
6098 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
6099 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
6100 GetMoveResolver()->EmitNativeCode(&parallel_move);
6101 } else {
6102 // Let the parallel move resolver take care of all of this.
6103 HParallelMove parallel_move(GetGraph()->GetArena());
6104 parallel_move.AddMove(return_loc, trg, type, nullptr);
6105 GetMoveResolver()->EmitNativeCode(&parallel_move);
6106 }
6107}
6108
Roland Levillain4d027112015-07-01 15:41:14 +01006109#undef __
6110#undef QUICK_ENTRY_POINT
6111
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006112} // namespace arm
6113} // namespace art