blob: 091b58a63ddc8925451dee4860d3979003cd6dd5 [file] [log] [blame]
Nicolas Geoffray76716a62014-05-23 10:14:19 +01001/*
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#ifndef ART_COMPILER_OPTIMIZING_LOCATIONS_H_
18#define ART_COMPILER_OPTIMIZING_LOCATIONS_H_
19
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010020#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080021#include "base/arena_object.h"
Nicolas Geoffray76716a62014-05-23 10:14:19 +010022#include "base/bit_field.h"
Vladimir Marko70e97462016-08-09 11:04:26 +010023#include "base/bit_utils.h"
Nicolas Geoffray39468442014-09-02 15:17:15 +010024#include "base/bit_vector.h"
Ian Rogers0279ebb2014-10-08 17:27:48 -070025#include "base/value_object.h"
Nicolas Geoffray76716a62014-05-23 10:14:19 +010026
27namespace art {
28
Nicolas Geoffray96f89a22014-07-11 10:57:49 +010029class HConstant;
Nicolas Geoffray76716a62014-05-23 10:14:19 +010030class HInstruction;
Nicolas Geoffray424f6762014-11-03 14:51:25 +000031class Location;
32
33std::ostream& operator<<(std::ostream& os, const Location& location);
Nicolas Geoffray76716a62014-05-23 10:14:19 +010034
35/**
36 * A Location is an abstraction over the potential location
37 * of an instruction. It could be in register or stack.
38 */
Vladimir Marko76c92ac2015-09-17 15:39:16 +010039class Location : public ValueObject {
Nicolas Geoffray76716a62014-05-23 10:14:19 +010040 public:
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +000041 enum OutputOverlap {
Roland Levillain3d312422016-06-23 13:53:42 +010042 // The liveness of the output overlaps the liveness of one or
43 // several input(s); the register allocator cannot reuse an
44 // input's location for the output's location.
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +000045 kOutputOverlap,
Roland Levillain3d312422016-06-23 13:53:42 +010046 // The liveness of the output does not overlap the liveness of any
47 // input; the register allocator is allowed to reuse an input's
48 // location for the output's location.
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +000049 kNoOutputOverlap
50 };
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +010051
Nicolas Geoffray76716a62014-05-23 10:14:19 +010052 enum Kind {
53 kInvalid = 0,
Nicolas Geoffray96f89a22014-07-11 10:57:49 +010054 kConstant = 1,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010055 kStackSlot = 2, // 32bit stack slot.
Nicolas Geoffray96f89a22014-07-11 10:57:49 +010056 kDoubleStackSlot = 3, // 64bit stack slot.
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010057
58 kRegister = 4, // Core register.
59
60 // We do not use the value 5 because it conflicts with kLocationConstantMask.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +010061 kDoNotUse5 = 5,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010062
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +000063 kFpuRegister = 6, // Float register.
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010064
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +000065 kRegisterPair = 7, // Long register.
66
67 kFpuRegisterPair = 8, // Double register.
68
69 // We do not use the value 9 because it conflicts with kLocationConstantMask.
70 kDoNotUse9 = 9,
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +010071
Nicolas Geoffray76716a62014-05-23 10:14:19 +010072 // Unallocated location represents a location that is not fixed and can be
73 // allocated by a register allocator. Each unallocated location has
74 // a policy that specifies what kind of location is suitable. Payload
75 // contains register allocation policy.
Mark Mendell3e6a3bf2015-01-19 14:09:22 -050076 kUnallocated = 10,
Nicolas Geoffray76716a62014-05-23 10:14:19 +010077 };
78
Vladimir Marko76c92ac2015-09-17 15:39:16 +010079 Location() : ValueObject(), value_(kInvalid) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010080 // Verify that non-constant location kinds do not interfere with kConstant.
Andreas Gampe785d2f22014-11-03 22:57:30 -080081 static_assert((kInvalid & kLocationConstantMask) != kConstant, "TagError");
82 static_assert((kUnallocated & kLocationConstantMask) != kConstant, "TagError");
83 static_assert((kStackSlot & kLocationConstantMask) != kConstant, "TagError");
84 static_assert((kDoubleStackSlot & kLocationConstantMask) != kConstant, "TagError");
85 static_assert((kRegister & kLocationConstantMask) != kConstant, "TagError");
Andreas Gampe785d2f22014-11-03 22:57:30 -080086 static_assert((kFpuRegister & kLocationConstantMask) != kConstant, "TagError");
87 static_assert((kRegisterPair & kLocationConstantMask) != kConstant, "TagError");
88 static_assert((kFpuRegisterPair & kLocationConstantMask) != kConstant, "TagError");
89 static_assert((kConstant & kLocationConstantMask) == kConstant, "TagError");
Nicolas Geoffray96f89a22014-07-11 10:57:49 +010090
Nicolas Geoffray76716a62014-05-23 10:14:19 +010091 DCHECK(!IsValid());
92 }
93
Andreas Gampe2e6f38a2016-11-03 14:06:20 -070094 Location(const Location& other) = default;
Nicolas Geoffray76716a62014-05-23 10:14:19 +010095
Andreas Gampe2e6f38a2016-11-03 14:06:20 -070096 Location& operator=(const Location& other) = default;
Nicolas Geoffray76716a62014-05-23 10:14:19 +010097
Nicolas Geoffray96f89a22014-07-11 10:57:49 +010098 bool IsConstant() const {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010099 return (value_ & kLocationConstantMask) == kConstant;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100100 }
101
102 static Location ConstantLocation(HConstant* constant) {
103 DCHECK(constant != nullptr);
Ian Rogers13735952014-10-08 12:43:28 -0700104 return Location(kConstant | reinterpret_cast<uintptr_t>(constant));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100105 }
106
107 HConstant* GetConstant() const {
108 DCHECK(IsConstant());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100109 return reinterpret_cast<HConstant*>(value_ & ~kLocationConstantMask);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100110 }
111
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100112 bool IsValid() const {
113 return value_ != kInvalid;
114 }
115
116 bool IsInvalid() const {
117 return !IsValid();
118 }
119
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100120 // Empty location. Used if there the location should be ignored.
121 static Location NoLocation() {
122 return Location();
123 }
124
125 // Register locations.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100126 static Location RegisterLocation(int reg) {
127 return Location(kRegister, reg);
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100128 }
129
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100130 static Location FpuRegisterLocation(int reg) {
131 return Location(kFpuRegister, reg);
132 }
133
134 static Location RegisterPairLocation(int low, int high) {
135 return Location(kRegisterPair, low << 16 | high);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100136 }
137
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000138 static Location FpuRegisterPairLocation(int low, int high) {
139 return Location(kFpuRegisterPair, low << 16 | high);
140 }
141
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100142 bool IsRegister() const {
143 return GetKind() == kRegister;
144 }
145
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100146 bool IsFpuRegister() const {
147 return GetKind() == kFpuRegister;
148 }
149
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100150 bool IsRegisterPair() const {
151 return GetKind() == kRegisterPair;
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100152 }
153
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000154 bool IsFpuRegisterPair() const {
155 return GetKind() == kFpuRegisterPair;
156 }
157
Nicolas Geoffrayda02afe2015-02-11 02:29:42 +0000158 bool IsRegisterKind() const {
159 return IsRegister() || IsFpuRegister() || IsRegisterPair() || IsFpuRegisterPair();
160 }
161
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100162 int reg() const {
163 DCHECK(IsRegister() || IsFpuRegister());
164 return GetPayload();
165 }
166
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000167 int low() const {
168 DCHECK(IsPair());
169 return GetPayload() >> 16;
170 }
171
172 int high() const {
173 DCHECK(IsPair());
174 return GetPayload() & 0xFFFF;
175 }
176
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100177 template <typename T>
Roland Levillain271ab9c2014-11-27 15:23:57 +0000178 T AsRegister() const {
179 DCHECK(IsRegister());
180 return static_cast<T>(reg());
181 }
182
183 template <typename T>
184 T AsFpuRegister() const {
185 DCHECK(IsFpuRegister());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100186 return static_cast<T>(reg());
187 }
188
189 template <typename T>
190 T AsRegisterPairLow() const {
191 DCHECK(IsRegisterPair());
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000192 return static_cast<T>(low());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100193 }
194
195 template <typename T>
196 T AsRegisterPairHigh() const {
197 DCHECK(IsRegisterPair());
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000198 return static_cast<T>(high());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100199 }
200
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000201 template <typename T>
202 T AsFpuRegisterPairLow() const {
203 DCHECK(IsFpuRegisterPair());
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000204 return static_cast<T>(low());
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000205 }
206
207 template <typename T>
208 T AsFpuRegisterPairHigh() const {
209 DCHECK(IsFpuRegisterPair());
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000210 return static_cast<T>(high());
211 }
212
213 bool IsPair() const {
214 return IsRegisterPair() || IsFpuRegisterPair();
215 }
216
217 Location ToLow() const {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000218 if (IsRegisterPair()) {
219 return Location::RegisterLocation(low());
220 } else if (IsFpuRegisterPair()) {
221 return Location::FpuRegisterLocation(low());
222 } else {
223 DCHECK(IsDoubleStackSlot());
224 return Location::StackSlot(GetStackIndex());
225 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000226 }
227
228 Location ToHigh() const {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000229 if (IsRegisterPair()) {
230 return Location::RegisterLocation(high());
231 } else if (IsFpuRegisterPair()) {
232 return Location::FpuRegisterLocation(high());
233 } else {
234 DCHECK(IsDoubleStackSlot());
235 return Location::StackSlot(GetHighStackIndex(4));
236 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000237 }
238
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100239 static uintptr_t EncodeStackIndex(intptr_t stack_index) {
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100240 DCHECK(-kStackIndexBias <= stack_index);
241 DCHECK(stack_index < kStackIndexBias);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100242 return static_cast<uintptr_t>(kStackIndexBias + stack_index);
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100243 }
244
245 static Location StackSlot(intptr_t stack_index) {
Ian Rogers13735952014-10-08 12:43:28 -0700246 uintptr_t payload = EncodeStackIndex(stack_index);
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100247 Location loc(kStackSlot, payload);
248 // Ensure that sign is preserved.
249 DCHECK_EQ(loc.GetStackIndex(), stack_index);
250 return loc;
251 }
252
253 bool IsStackSlot() const {
254 return GetKind() == kStackSlot;
255 }
256
257 static Location DoubleStackSlot(intptr_t stack_index) {
Ian Rogers13735952014-10-08 12:43:28 -0700258 uintptr_t payload = EncodeStackIndex(stack_index);
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100259 Location loc(kDoubleStackSlot, payload);
260 // Ensure that sign is preserved.
261 DCHECK_EQ(loc.GetStackIndex(), stack_index);
262 return loc;
263 }
264
265 bool IsDoubleStackSlot() const {
266 return GetKind() == kDoubleStackSlot;
267 }
268
269 intptr_t GetStackIndex() const {
270 DCHECK(IsStackSlot() || IsDoubleStackSlot());
271 // Decode stack index manually to preserve sign.
272 return GetPayload() - kStackIndexBias;
273 }
274
275 intptr_t GetHighStackIndex(uintptr_t word_size) const {
276 DCHECK(IsDoubleStackSlot());
277 // Decode stack index manually to preserve sign.
278 return GetPayload() - kStackIndexBias + word_size;
279 }
280
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100281 Kind GetKind() const {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100282 return IsConstant() ? kConstant : KindField::Decode(value_);
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100283 }
284
285 bool Equals(Location other) const {
286 return value_ == other.value_;
287 }
288
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +0000289 bool Contains(Location other) const {
290 if (Equals(other)) {
291 return true;
Zheng Xuad4450e2015-04-17 18:48:56 +0800292 } else if (IsPair() || IsDoubleStackSlot()) {
293 return ToLow().Equals(other) || ToHigh().Equals(other);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +0000294 }
295 return false;
296 }
297
Zheng Xuad4450e2015-04-17 18:48:56 +0800298 bool OverlapsWith(Location other) const {
299 // Only check the overlapping case that can happen with our register allocation algorithm.
300 bool overlap = Contains(other) || other.Contains(*this);
301 if (kIsDebugBuild && !overlap) {
302 // Note: These are also overlapping cases. But we are not able to handle them in
303 // ParallelMoveResolverWithSwap. Make sure that we do not meet such case with our compiler.
304 if ((IsPair() && other.IsPair()) || (IsDoubleStackSlot() && other.IsDoubleStackSlot())) {
305 DCHECK(!Contains(other.ToLow()));
306 DCHECK(!Contains(other.ToHigh()));
307 }
308 }
309 return overlap;
310 }
311
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100312 const char* DebugString() const {
313 switch (GetKind()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100314 case kInvalid: return "I";
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100315 case kRegister: return "R";
316 case kStackSlot: return "S";
317 case kDoubleStackSlot: return "DS";
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100318 case kUnallocated: return "U";
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100319 case kConstant: return "C";
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100320 case kFpuRegister: return "F";
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100321 case kRegisterPair: return "RP";
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000322 case kFpuRegisterPair: return "FP";
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100323 case kDoNotUse5: // fall-through
324 case kDoNotUse9:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100325 LOG(FATAL) << "Should not use this location kind";
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100326 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100327 UNREACHABLE();
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100328 }
329
330 // Unallocated locations.
331 enum Policy {
332 kAny,
333 kRequiresRegister,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100334 kRequiresFpuRegister,
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100335 kSameAsFirstInput,
336 };
337
338 bool IsUnallocated() const {
339 return GetKind() == kUnallocated;
340 }
341
342 static Location UnallocatedLocation(Policy policy) {
343 return Location(kUnallocated, PolicyField::Encode(policy));
344 }
345
346 // Any free register is suitable to replace this unallocated location.
347 static Location Any() {
348 return UnallocatedLocation(kAny);
349 }
350
351 static Location RequiresRegister() {
352 return UnallocatedLocation(kRequiresRegister);
353 }
354
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100355 static Location RequiresFpuRegister() {
356 return UnallocatedLocation(kRequiresFpuRegister);
357 }
358
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100359 static Location RegisterOrConstant(HInstruction* instruction);
Mark Mendellea5af682015-10-22 17:35:49 -0400360 static Location RegisterOrInt32Constant(HInstruction* instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100361 static Location ByteRegisterOrConstant(int reg, HInstruction* instruction);
Mark Mendellea5af682015-10-22 17:35:49 -0400362 static Location FpuRegisterOrConstant(HInstruction* instruction);
363 static Location FpuRegisterOrInt32Constant(HInstruction* instruction);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100364
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100365 // The location of the first input to the instruction will be
366 // used to replace this unallocated location.
367 static Location SameAsFirstInput() {
368 return UnallocatedLocation(kSameAsFirstInput);
369 }
370
371 Policy GetPolicy() const {
372 DCHECK(IsUnallocated());
373 return PolicyField::Decode(GetPayload());
374 }
375
Matthew Gharrityd9ffd0d2016-06-22 10:27:55 -0700376 bool RequiresRegisterKind() const {
377 return GetPolicy() == kRequiresRegister || GetPolicy() == kRequiresFpuRegister;
378 }
379
Ian Rogers13735952014-10-08 12:43:28 -0700380 uintptr_t GetEncoding() const {
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100381 return GetPayload();
382 }
383
384 private:
385 // Number of bits required to encode Kind value.
386 static constexpr uint32_t kBitsForKind = 4;
Ian Rogers13735952014-10-08 12:43:28 -0700387 static constexpr uint32_t kBitsForPayload = kBitsPerIntPtrT - kBitsForKind;
388 static constexpr uintptr_t kLocationConstantMask = 0x3;
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100389
Ian Rogers13735952014-10-08 12:43:28 -0700390 explicit Location(uintptr_t value) : value_(value) {}
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100391
Ian Rogers13735952014-10-08 12:43:28 -0700392 Location(Kind kind, uintptr_t payload)
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100393 : value_(KindField::Encode(kind) | PayloadField::Encode(payload)) {}
394
Ian Rogers13735952014-10-08 12:43:28 -0700395 uintptr_t GetPayload() const {
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100396 return PayloadField::Decode(value_);
397 }
398
399 typedef BitField<Kind, 0, kBitsForKind> KindField;
Ian Rogers13735952014-10-08 12:43:28 -0700400 typedef BitField<uintptr_t, kBitsForKind, kBitsForPayload> PayloadField;
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100401
402 // Layout for kUnallocated locations payload.
403 typedef BitField<Policy, 0, 3> PolicyField;
404
405 // Layout for stack slots.
406 static const intptr_t kStackIndexBias =
407 static_cast<intptr_t>(1) << (kBitsForPayload - 1);
408
409 // Location either contains kind and payload fields or a tagged handle for
410 // a constant locations. Values of enumeration Kind are selected in such a
411 // way that none of them can be interpreted as a kConstant tag.
Ian Rogers13735952014-10-08 12:43:28 -0700412 uintptr_t value_;
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100413};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700414std::ostream& operator<<(std::ostream& os, const Location::Kind& rhs);
415std::ostream& operator<<(std::ostream& os, const Location::Policy& rhs);
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100416
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100417class RegisterSet : public ValueObject {
418 public:
Vladimir Marko804b03f2016-09-14 16:26:36 +0100419 static RegisterSet Empty() { return RegisterSet(); }
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100420
421 void Add(Location loc) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100422 if (loc.IsRegister()) {
423 core_registers_ |= (1 << loc.reg());
424 } else {
425 DCHECK(loc.IsFpuRegister());
426 floating_point_registers_ |= (1 << loc.reg());
427 }
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100428 }
429
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100430 void Remove(Location loc) {
431 if (loc.IsRegister()) {
432 core_registers_ &= ~(1 << loc.reg());
433 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000434 DCHECK(loc.IsFpuRegister()) << loc;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100435 floating_point_registers_ &= ~(1 << loc.reg());
436 }
437 }
438
Nicolas Geoffray45b83af2015-07-06 15:12:53 +0000439 bool ContainsCoreRegister(uint32_t id) const {
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100440 return Contains(core_registers_, id);
441 }
442
Nicolas Geoffray45b83af2015-07-06 15:12:53 +0000443 bool ContainsFloatingPointRegister(uint32_t id) const {
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100444 return Contains(floating_point_registers_, id);
445 }
446
447 static bool Contains(uint32_t register_set, uint32_t reg) {
448 return (register_set & (1 << reg)) != 0;
449 }
450
Nicolas Geoffray87d03762014-11-19 15:17:56 +0000451 size_t GetNumberOfRegisters() const {
Vladimir Marko70e97462016-08-09 11:04:26 +0100452 return POPCOUNT(core_registers_) + POPCOUNT(floating_point_registers_);
Nicolas Geoffray87d03762014-11-19 15:17:56 +0000453 }
454
Nicolas Geoffray98893962015-01-21 12:32:32 +0000455 uint32_t GetCoreRegisters() const {
456 return core_registers_;
457 }
458
459 uint32_t GetFloatingPointRegisters() const {
460 return floating_point_registers_;
461 }
462
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100463 private:
Vladimir Marko804b03f2016-09-14 16:26:36 +0100464 RegisterSet() : core_registers_(0), floating_point_registers_(0) {}
465
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100466 uint32_t core_registers_;
467 uint32_t floating_point_registers_;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100468};
469
Andreas Gampe878d58c2015-01-15 23:24:00 -0800470static constexpr bool kIntrinsified = true;
471
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100472/**
473 * The code generator computes LocationSummary for each instruction so that
474 * the instruction itself knows what code to generate: where to find the inputs
475 * and where to place the result.
476 *
477 * The intent is to have the code for generating the instruction independent of
478 * register allocation. A register allocator just has to provide a LocationSummary.
479 */
Vladimir Marko5233f932015-09-29 19:01:15 +0100480class LocationSummary : public ArenaObject<kArenaAllocLocationSummary> {
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100481 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100482 enum CallKind {
483 kNoCall,
Serban Constantinescu806f0122016-03-09 11:10:16 +0000484 kCallOnMainAndSlowPath,
Nicolas Geoffray39468442014-09-02 15:17:15 +0100485 kCallOnSlowPath,
Serban Constantinescu54ff4822016-07-07 18:03:19 +0100486 kCallOnMainOnly
Nicolas Geoffray39468442014-09-02 15:17:15 +0100487 };
488
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700489 explicit LocationSummary(HInstruction* instruction,
490 CallKind call_kind = kNoCall,
491 bool intrinsified = false);
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100492
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100493 void SetInAt(uint32_t at, Location location) {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100494 inputs_[at] = location;
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100495 }
496
497 Location InAt(uint32_t at) const {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100498 return inputs_[at];
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100499 }
500
501 size_t GetInputCount() const {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100502 return inputs_.size();
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100503 }
504
Roland Levillain3d312422016-06-23 13:53:42 +0100505 // Set the output location. Argument `overlaps` tells whether the
506 // output overlaps any of the inputs (if so, it cannot share the
507 // same register as one of the inputs); it is set to
508 // `Location::kOutputOverlap` by default for safety.
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000509 void SetOut(Location location, Location::OutputOverlap overlaps = Location::kOutputOverlap) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000510 DCHECK(output_.IsInvalid());
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100511 output_overlaps_ = overlaps;
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000512 output_ = location;
513 }
514
515 void UpdateOut(Location location) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000516 // There are two reasons for updating an output:
517 // 1) Parameters, where we only know the exact stack slot after
518 // doing full register allocation.
519 // 2) Unallocated location.
520 DCHECK(output_.IsStackSlot() || output_.IsDoubleStackSlot() || output_.IsUnallocated());
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000521 output_ = location;
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100522 }
523
524 void AddTemp(Location location) {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100525 temps_.push_back(location);
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100526 }
527
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -0700528 void AddRegisterTemps(size_t count) {
529 for (size_t i = 0; i < count; ++i) {
530 AddTemp(Location::RequiresRegister());
531 }
532 }
533
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100534 Location GetTemp(uint32_t at) const {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100535 return temps_[at];
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100536 }
537
538 void SetTempAt(uint32_t at, Location location) {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100539 DCHECK(temps_[at].IsUnallocated() || temps_[at].IsInvalid());
540 temps_[at] = location;
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100541 }
542
543 size_t GetTempCount() const {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100544 return temps_.size();
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100545 }
546
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100547 bool HasTemps() const { return !temps_.empty(); }
Nicolas Geoffray94015b92015-06-04 18:21:04 +0100548
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100549 Location Out() const { return output_; }
550
Serban Constantinescu806f0122016-03-09 11:10:16 +0000551 bool CanCall() const {
552 return call_kind_ != kNoCall;
553 }
554
555 bool WillCall() const {
556 return call_kind_ == kCallOnMainOnly || call_kind_ == kCallOnMainAndSlowPath;
557 }
558
559 bool CallsOnSlowPath() const {
560 return call_kind_ == kCallOnSlowPath || call_kind_ == kCallOnMainAndSlowPath;
561 }
562
563 bool OnlyCallsOnSlowPath() const {
564 return call_kind_ == kCallOnSlowPath;
565 }
566
567 bool CallsOnMainAndSlowPath() const {
568 return call_kind_ == kCallOnMainAndSlowPath;
569 }
570
571 bool NeedsSafepoint() const {
572 return CanCall();
573 }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100574
Vladimir Marko70e97462016-08-09 11:04:26 +0100575 void SetCustomSlowPathCallerSaves(const RegisterSet& caller_saves) {
576 DCHECK(OnlyCallsOnSlowPath());
577 has_custom_slow_path_calling_convention_ = true;
578 custom_slow_path_caller_saves_ = caller_saves;
579 }
580
581 bool HasCustomSlowPathCallingConvention() const {
582 return has_custom_slow_path_calling_convention_;
583 }
584
585 const RegisterSet& GetCustomSlowPathCallerSaves() const {
586 DCHECK(HasCustomSlowPathCallingConvention());
587 return custom_slow_path_caller_saves_;
588 }
589
Nicolas Geoffray39468442014-09-02 15:17:15 +0100590 void SetStackBit(uint32_t index) {
591 stack_mask_->SetBit(index);
592 }
593
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100594 void ClearStackBit(uint32_t index) {
595 stack_mask_->ClearBit(index);
596 }
597
Nicolas Geoffray39468442014-09-02 15:17:15 +0100598 void SetRegisterBit(uint32_t reg_id) {
599 register_mask_ |= (1 << reg_id);
600 }
601
Nicolas Geoffray98893962015-01-21 12:32:32 +0000602 uint32_t GetRegisterMask() const {
603 return register_mask_;
604 }
605
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100606 bool RegisterContainsObject(uint32_t reg_id) {
607 return RegisterSet::Contains(register_mask_, reg_id);
608 }
609
610 void AddLiveRegister(Location location) {
611 live_registers_.Add(location);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100612 }
613
614 BitVector* GetStackMask() const {
615 return stack_mask_;
616 }
617
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100618 RegisterSet* GetLiveRegisters() {
619 return &live_registers_;
620 }
621
Nicolas Geoffray87d03762014-11-19 15:17:56 +0000622 size_t GetNumberOfLiveRegisters() const {
623 return live_registers_.GetNumberOfRegisters();
624 }
625
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000626 bool OutputUsesSameAs(uint32_t input_index) const {
627 return (input_index == 0)
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100628 && output_.IsUnallocated()
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000629 && (output_.GetPolicy() == Location::kSameAsFirstInput);
630 }
631
632 bool IsFixedInput(uint32_t input_index) const {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100633 Location input = inputs_[input_index];
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000634 return input.IsRegister()
Nicolas Geoffray98893962015-01-21 12:32:32 +0000635 || input.IsFpuRegister()
636 || input.IsPair()
637 || input.IsStackSlot()
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000638 || input.IsDoubleStackSlot();
Nicolas Geoffray76905622014-09-25 14:39:26 +0100639 }
640
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000641 bool OutputCanOverlapWithInputs() const {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000642 return output_overlaps_ == Location::kOutputOverlap;
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100643 }
644
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800645 bool Intrinsified() const {
646 return intrinsified_;
647 }
648
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100649 private:
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100650 ArenaVector<Location> inputs_;
651 ArenaVector<Location> temps_;
Vladimir Marko70e97462016-08-09 11:04:26 +0100652 const CallKind call_kind_;
653 // Whether these are locations for an intrinsified call.
654 const bool intrinsified_;
655 // Whether the slow path has default or custom calling convention.
656 bool has_custom_slow_path_calling_convention_;
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100657 // Whether the output overlaps with any of the inputs. If it overlaps, then it cannot
658 // share the same register as the inputs.
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000659 Location::OutputOverlap output_overlaps_;
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100660 Location output_;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100661
662 // Mask of objects that live in the stack.
663 BitVector* stack_mask_;
664
665 // Mask of objects that live in register.
666 uint32_t register_mask_;
667
668 // Registers that are in use at this position.
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100669 RegisterSet live_registers_;
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100670
Vladimir Marko70e97462016-08-09 11:04:26 +0100671 // Custom slow path caller saves. Valid only if indicated by slow_path_calling_convention_.
672 RegisterSet custom_slow_path_caller_saves_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800673
Matthew Gharrity2ac06bc2016-08-05 09:34:52 -0700674 friend class RegisterAllocatorTest;
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100675 DISALLOW_COPY_AND_ASSIGN(LocationSummary);
676};
677
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100678} // namespace art
679
680#endif // ART_COMPILER_OPTIMIZING_LOCATIONS_H_