blob: 68d605957fbe03fa7f465de27cbe09085a62a035 [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
20#include "base/bit_field.h"
Nicolas Geoffray39468442014-09-02 15:17:15 +010021#include "base/bit_vector.h"
Ian Rogers0279ebb2014-10-08 17:27:48 -070022#include "base/value_object.h"
23#include "utils/arena_object.h"
Nicolas Geoffray76716a62014-05-23 10:14:19 +010024#include "utils/growable_array.h"
Nicolas Geoffray76716a62014-05-23 10:14:19 +010025
26namespace art {
27
Nicolas Geoffray96f89a22014-07-11 10:57:49 +010028class HConstant;
Nicolas Geoffray76716a62014-05-23 10:14:19 +010029class HInstruction;
Nicolas Geoffray424f6762014-11-03 14:51:25 +000030class Location;
31
32std::ostream& operator<<(std::ostream& os, const Location& location);
Nicolas Geoffray76716a62014-05-23 10:14:19 +010033
34/**
35 * A Location is an abstraction over the potential location
36 * of an instruction. It could be in register or stack.
37 */
38class Location : public ValueObject {
39 public:
Nicolas Geoffrayc399fdc2015-01-21 12:42:57 +000040 static constexpr bool kNoOutputOverlap = false;
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +010041
Nicolas Geoffray76716a62014-05-23 10:14:19 +010042 enum Kind {
43 kInvalid = 0,
Nicolas Geoffray96f89a22014-07-11 10:57:49 +010044 kConstant = 1,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010045 kStackSlot = 2, // 32bit stack slot.
Nicolas Geoffray96f89a22014-07-11 10:57:49 +010046 kDoubleStackSlot = 3, // 64bit stack slot.
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010047
48 kRegister = 4, // Core register.
49
50 // We do not use the value 5 because it conflicts with kLocationConstantMask.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +010051 kDoNotUse5 = 5,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010052
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +000053 kFpuRegister = 6, // Float register.
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010054
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +000055 kRegisterPair = 7, // Long register.
56
57 kFpuRegisterPair = 8, // Double register.
58
59 // We do not use the value 9 because it conflicts with kLocationConstantMask.
60 kDoNotUse9 = 9,
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +010061
Nicolas Geoffray76716a62014-05-23 10:14:19 +010062 // On 32bits architectures, quick can pass a long where the
63 // low bits are in the last parameter register, and the high
64 // bits are in a stack slot. The kQuickParameter kind is for
65 // handling this special case.
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +000066 kQuickParameter = 10,
Nicolas Geoffray76716a62014-05-23 10:14:19 +010067
68 // Unallocated location represents a location that is not fixed and can be
69 // allocated by a register allocator. Each unallocated location has
70 // a policy that specifies what kind of location is suitable. Payload
71 // contains register allocation policy.
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +000072 kUnallocated = 11,
Nicolas Geoffray76716a62014-05-23 10:14:19 +010073 };
74
75 Location() : value_(kInvalid) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010076 // Verify that non-constant location kinds do not interfere with kConstant.
Andreas Gampe785d2f22014-11-03 22:57:30 -080077 static_assert((kInvalid & kLocationConstantMask) != kConstant, "TagError");
78 static_assert((kUnallocated & kLocationConstantMask) != kConstant, "TagError");
79 static_assert((kStackSlot & kLocationConstantMask) != kConstant, "TagError");
80 static_assert((kDoubleStackSlot & kLocationConstantMask) != kConstant, "TagError");
81 static_assert((kRegister & kLocationConstantMask) != kConstant, "TagError");
82 static_assert((kQuickParameter & kLocationConstantMask) != kConstant, "TagError");
83 static_assert((kFpuRegister & kLocationConstantMask) != kConstant, "TagError");
84 static_assert((kRegisterPair & kLocationConstantMask) != kConstant, "TagError");
85 static_assert((kFpuRegisterPair & kLocationConstantMask) != kConstant, "TagError");
86 static_assert((kConstant & kLocationConstantMask) == kConstant, "TagError");
Nicolas Geoffray96f89a22014-07-11 10:57:49 +010087
Nicolas Geoffray76716a62014-05-23 10:14:19 +010088 DCHECK(!IsValid());
89 }
90
91 Location(const Location& other) : ValueObject(), value_(other.value_) {}
92
93 Location& operator=(const Location& other) {
94 value_ = other.value_;
95 return *this;
96 }
97
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 Geoffray56b9ee62014-10-09 11:47:51 +0100158 int reg() const {
159 DCHECK(IsRegister() || IsFpuRegister());
160 return GetPayload();
161 }
162
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000163 int low() const {
164 DCHECK(IsPair());
165 return GetPayload() >> 16;
166 }
167
168 int high() const {
169 DCHECK(IsPair());
170 return GetPayload() & 0xFFFF;
171 }
172
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100173 template <typename T>
Roland Levillain271ab9c2014-11-27 15:23:57 +0000174 T AsRegister() const {
175 DCHECK(IsRegister());
176 return static_cast<T>(reg());
177 }
178
179 template <typename T>
180 T AsFpuRegister() const {
181 DCHECK(IsFpuRegister());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100182 return static_cast<T>(reg());
183 }
184
185 template <typename T>
186 T AsRegisterPairLow() const {
187 DCHECK(IsRegisterPair());
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000188 return static_cast<T>(low());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100189 }
190
191 template <typename T>
192 T AsRegisterPairHigh() const {
193 DCHECK(IsRegisterPair());
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000194 return static_cast<T>(high());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100195 }
196
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000197 template <typename T>
198 T AsFpuRegisterPairLow() const {
199 DCHECK(IsFpuRegisterPair());
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000200 return static_cast<T>(low());
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000201 }
202
203 template <typename T>
204 T AsFpuRegisterPairHigh() const {
205 DCHECK(IsFpuRegisterPair());
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000206 return static_cast<T>(high());
207 }
208
209 bool IsPair() const {
210 return IsRegisterPair() || IsFpuRegisterPair();
211 }
212
213 Location ToLow() const {
214 return IsRegisterPair()
215 ? Location::RegisterLocation(low())
216 : Location::FpuRegisterLocation(low());
217 }
218
219 Location ToHigh() const {
220 return IsRegisterPair()
221 ? Location::RegisterLocation(high())
222 : Location::FpuRegisterLocation(high());
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000223 }
224
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100225 static uintptr_t EncodeStackIndex(intptr_t stack_index) {
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100226 DCHECK(-kStackIndexBias <= stack_index);
227 DCHECK(stack_index < kStackIndexBias);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100228 return static_cast<uintptr_t>(kStackIndexBias + stack_index);
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100229 }
230
231 static Location StackSlot(intptr_t stack_index) {
Ian Rogers13735952014-10-08 12:43:28 -0700232 uintptr_t payload = EncodeStackIndex(stack_index);
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100233 Location loc(kStackSlot, payload);
234 // Ensure that sign is preserved.
235 DCHECK_EQ(loc.GetStackIndex(), stack_index);
236 return loc;
237 }
238
239 bool IsStackSlot() const {
240 return GetKind() == kStackSlot;
241 }
242
243 static Location DoubleStackSlot(intptr_t stack_index) {
Ian Rogers13735952014-10-08 12:43:28 -0700244 uintptr_t payload = EncodeStackIndex(stack_index);
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100245 Location loc(kDoubleStackSlot, payload);
246 // Ensure that sign is preserved.
247 DCHECK_EQ(loc.GetStackIndex(), stack_index);
248 return loc;
249 }
250
251 bool IsDoubleStackSlot() const {
252 return GetKind() == kDoubleStackSlot;
253 }
254
255 intptr_t GetStackIndex() const {
256 DCHECK(IsStackSlot() || IsDoubleStackSlot());
257 // Decode stack index manually to preserve sign.
258 return GetPayload() - kStackIndexBias;
259 }
260
261 intptr_t GetHighStackIndex(uintptr_t word_size) const {
262 DCHECK(IsDoubleStackSlot());
263 // Decode stack index manually to preserve sign.
264 return GetPayload() - kStackIndexBias + word_size;
265 }
266
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000267 static Location QuickParameter(uint16_t register_index, uint16_t stack_index) {
268 return Location(kQuickParameter, register_index << 16 | stack_index);
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100269 }
270
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000271 uint32_t GetQuickParameterRegisterIndex() const {
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100272 DCHECK(IsQuickParameter());
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000273 return GetPayload() >> 16;
274 }
275
276 uint32_t GetQuickParameterStackIndex() const {
277 DCHECK(IsQuickParameter());
278 return GetPayload() & 0xFFFF;
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100279 }
280
281 bool IsQuickParameter() const {
282 return GetKind() == kQuickParameter;
283 }
284
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100285 Kind GetKind() const {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100286 return IsConstant() ? kConstant : KindField::Decode(value_);
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100287 }
288
289 bool Equals(Location other) const {
290 return value_ == other.value_;
291 }
292
293 const char* DebugString() const {
294 switch (GetKind()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100295 case kInvalid: return "I";
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100296 case kRegister: return "R";
297 case kStackSlot: return "S";
298 case kDoubleStackSlot: return "DS";
299 case kQuickParameter: return "Q";
300 case kUnallocated: return "U";
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100301 case kConstant: return "C";
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100302 case kFpuRegister: return "F";
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100303 case kRegisterPair: return "RP";
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000304 case kFpuRegisterPair: return "FP";
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100305 case kDoNotUse5: // fall-through
306 case kDoNotUse9:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100307 LOG(FATAL) << "Should not use this location kind";
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100308 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100309 UNREACHABLE();
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100310 return "?";
311 }
312
313 // Unallocated locations.
314 enum Policy {
315 kAny,
316 kRequiresRegister,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100317 kRequiresFpuRegister,
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100318 kSameAsFirstInput,
319 };
320
321 bool IsUnallocated() const {
322 return GetKind() == kUnallocated;
323 }
324
325 static Location UnallocatedLocation(Policy policy) {
326 return Location(kUnallocated, PolicyField::Encode(policy));
327 }
328
329 // Any free register is suitable to replace this unallocated location.
330 static Location Any() {
331 return UnallocatedLocation(kAny);
332 }
333
334 static Location RequiresRegister() {
335 return UnallocatedLocation(kRequiresRegister);
336 }
337
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100338 static Location RequiresFpuRegister() {
339 return UnallocatedLocation(kRequiresFpuRegister);
340 }
341
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100342 static Location RegisterOrConstant(HInstruction* instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100343 static Location ByteRegisterOrConstant(int reg, HInstruction* instruction);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100344
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100345 // The location of the first input to the instruction will be
346 // used to replace this unallocated location.
347 static Location SameAsFirstInput() {
348 return UnallocatedLocation(kSameAsFirstInput);
349 }
350
351 Policy GetPolicy() const {
352 DCHECK(IsUnallocated());
353 return PolicyField::Decode(GetPayload());
354 }
355
Ian Rogers13735952014-10-08 12:43:28 -0700356 uintptr_t GetEncoding() const {
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100357 return GetPayload();
358 }
359
360 private:
361 // Number of bits required to encode Kind value.
362 static constexpr uint32_t kBitsForKind = 4;
Ian Rogers13735952014-10-08 12:43:28 -0700363 static constexpr uint32_t kBitsForPayload = kBitsPerIntPtrT - kBitsForKind;
364 static constexpr uintptr_t kLocationConstantMask = 0x3;
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100365
Ian Rogers13735952014-10-08 12:43:28 -0700366 explicit Location(uintptr_t value) : value_(value) {}
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100367
Ian Rogers13735952014-10-08 12:43:28 -0700368 Location(Kind kind, uintptr_t payload)
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100369 : value_(KindField::Encode(kind) | PayloadField::Encode(payload)) {}
370
Ian Rogers13735952014-10-08 12:43:28 -0700371 uintptr_t GetPayload() const {
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100372 return PayloadField::Decode(value_);
373 }
374
375 typedef BitField<Kind, 0, kBitsForKind> KindField;
Ian Rogers13735952014-10-08 12:43:28 -0700376 typedef BitField<uintptr_t, kBitsForKind, kBitsForPayload> PayloadField;
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100377
378 // Layout for kUnallocated locations payload.
379 typedef BitField<Policy, 0, 3> PolicyField;
380
381 // Layout for stack slots.
382 static const intptr_t kStackIndexBias =
383 static_cast<intptr_t>(1) << (kBitsForPayload - 1);
384
385 // Location either contains kind and payload fields or a tagged handle for
386 // a constant locations. Values of enumeration Kind are selected in such a
387 // way that none of them can be interpreted as a kConstant tag.
Ian Rogers13735952014-10-08 12:43:28 -0700388 uintptr_t value_;
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100389};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700390std::ostream& operator<<(std::ostream& os, const Location::Kind& rhs);
391std::ostream& operator<<(std::ostream& os, const Location::Policy& rhs);
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100392
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100393class RegisterSet : public ValueObject {
394 public:
395 RegisterSet() : core_registers_(0), floating_point_registers_(0) {}
396
397 void Add(Location loc) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100398 if (loc.IsRegister()) {
399 core_registers_ |= (1 << loc.reg());
400 } else {
401 DCHECK(loc.IsFpuRegister());
402 floating_point_registers_ |= (1 << loc.reg());
403 }
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100404 }
405
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100406 void Remove(Location loc) {
407 if (loc.IsRegister()) {
408 core_registers_ &= ~(1 << loc.reg());
409 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000410 DCHECK(loc.IsFpuRegister()) << loc;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100411 floating_point_registers_ &= ~(1 << loc.reg());
412 }
413 }
414
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100415 bool ContainsCoreRegister(uint32_t id) {
416 return Contains(core_registers_, id);
417 }
418
419 bool ContainsFloatingPointRegister(uint32_t id) {
420 return Contains(floating_point_registers_, id);
421 }
422
423 static bool Contains(uint32_t register_set, uint32_t reg) {
424 return (register_set & (1 << reg)) != 0;
425 }
426
Nicolas Geoffray87d03762014-11-19 15:17:56 +0000427 size_t GetNumberOfRegisters() const {
428 return __builtin_popcount(core_registers_) + __builtin_popcount(floating_point_registers_);
429 }
430
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100431 private:
432 uint32_t core_registers_;
433 uint32_t floating_point_registers_;
434
435 DISALLOW_COPY_AND_ASSIGN(RegisterSet);
436};
437
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100438/**
439 * The code generator computes LocationSummary for each instruction so that
440 * the instruction itself knows what code to generate: where to find the inputs
441 * and where to place the result.
442 *
443 * The intent is to have the code for generating the instruction independent of
444 * register allocation. A register allocator just has to provide a LocationSummary.
445 */
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700446class LocationSummary : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100447 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100448 enum CallKind {
449 kNoCall,
450 kCallOnSlowPath,
451 kCall
452 };
453
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800454 LocationSummary(HInstruction* instruction,
455 CallKind call_kind = kNoCall,
456 bool intrinsified = false);
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100457
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100458 void SetInAt(uint32_t at, Location location) {
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000459 DCHECK(inputs_.Get(at).IsUnallocated() || inputs_.Get(at).IsInvalid());
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100460 inputs_.Put(at, location);
461 }
462
463 Location InAt(uint32_t at) const {
464 return inputs_.Get(at);
465 }
466
467 size_t GetInputCount() const {
468 return inputs_.Size();
469 }
470
Nicolas Geoffrayc399fdc2015-01-21 12:42:57 +0000471 void SetOut(Location location, bool overlaps = true) {
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000472 DCHECK(output_.IsUnallocated() || output_.IsInvalid());
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100473 output_overlaps_ = overlaps;
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000474 output_ = location;
475 }
476
477 void UpdateOut(Location location) {
478 // The only reason for updating an output is for parameters where
479 // we only know the exact stack slot after doing full register
480 // allocation.
481 DCHECK(output_.IsStackSlot() || output_.IsDoubleStackSlot());
482 output_ = location;
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100483 }
484
485 void AddTemp(Location location) {
486 temps_.Add(location);
487 }
488
489 Location GetTemp(uint32_t at) const {
490 return temps_.Get(at);
491 }
492
493 void SetTempAt(uint32_t at, Location location) {
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000494 DCHECK(temps_.Get(at).IsUnallocated() || temps_.Get(at).IsInvalid());
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100495 temps_.Put(at, location);
496 }
497
498 size_t GetTempCount() const {
499 return temps_.Size();
500 }
501
Nicolas Geoffray39468442014-09-02 15:17:15 +0100502 void SetEnvironmentAt(uint32_t at, Location location) {
503 environment_.Put(at, location);
504 }
505
506 Location GetEnvironmentAt(uint32_t at) const {
507 return environment_.Get(at);
508 }
509
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100510 Location Out() const { return output_; }
511
Nicolas Geoffray39468442014-09-02 15:17:15 +0100512 bool CanCall() const { return call_kind_ != kNoCall; }
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100513 bool WillCall() const { return call_kind_ == kCall; }
514 bool OnlyCallsOnSlowPath() const { return call_kind_ == kCallOnSlowPath; }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100515 bool NeedsSafepoint() const { return CanCall(); }
516
517 void SetStackBit(uint32_t index) {
518 stack_mask_->SetBit(index);
519 }
520
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100521 void ClearStackBit(uint32_t index) {
522 stack_mask_->ClearBit(index);
523 }
524
Nicolas Geoffray39468442014-09-02 15:17:15 +0100525 void SetRegisterBit(uint32_t reg_id) {
526 register_mask_ |= (1 << reg_id);
527 }
528
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100529 bool RegisterContainsObject(uint32_t reg_id) {
530 return RegisterSet::Contains(register_mask_, reg_id);
531 }
532
533 void AddLiveRegister(Location location) {
534 live_registers_.Add(location);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100535 }
536
537 BitVector* GetStackMask() const {
538 return stack_mask_;
539 }
540
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100541 RegisterSet* GetLiveRegisters() {
542 return &live_registers_;
543 }
544
Nicolas Geoffray87d03762014-11-19 15:17:56 +0000545 size_t GetNumberOfLiveRegisters() const {
546 return live_registers_.GetNumberOfRegisters();
547 }
548
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100549 bool InputOverlapsWithOutputOrTemp(uint32_t input_index, bool is_environment) const {
Nicolas Geoffray76905622014-09-25 14:39:26 +0100550 if (is_environment) return true;
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100551 if ((input_index == 0)
552 && output_.IsUnallocated()
553 && (output_.GetPolicy() == Location::kSameAsFirstInput)) {
Nicolas Geoffray76905622014-09-25 14:39:26 +0100554 return false;
555 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000556 Location input = inputs_.Get(input_index);
557 if (input.IsRegister() || input.IsFpuRegister() || input.IsPair()) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +0100558 return false;
559 }
Nicolas Geoffray76905622014-09-25 14:39:26 +0100560 return true;
561 }
562
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100563 bool OutputOverlapsWithInputs() const {
Nicolas Geoffrayc399fdc2015-01-21 12:42:57 +0000564 return output_overlaps_;
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100565 }
566
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800567 bool Intrinsified() const {
568 return intrinsified_;
569 }
570
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100571 private:
572 GrowableArray<Location> inputs_;
573 GrowableArray<Location> temps_;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100574 GrowableArray<Location> environment_;
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100575 // Whether the output overlaps with any of the inputs. If it overlaps, then it cannot
576 // share the same register as the inputs.
Nicolas Geoffrayc399fdc2015-01-21 12:42:57 +0000577 bool output_overlaps_;
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100578 Location output_;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100579 const CallKind call_kind_;
580
581 // Mask of objects that live in the stack.
582 BitVector* stack_mask_;
583
584 // Mask of objects that live in register.
585 uint32_t register_mask_;
586
587 // Registers that are in use at this position.
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100588 RegisterSet live_registers_;
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100589
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800590 // Whether these are locations for an intrinsified call.
591 const bool intrinsified_;
592
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000593 ART_FRIEND_TEST(RegisterAllocatorTest, ExpectedInRegisterHint);
594 ART_FRIEND_TEST(RegisterAllocatorTest, SameAsFirstInputHint);
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100595 DISALLOW_COPY_AND_ASSIGN(LocationSummary);
596};
597
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100598} // namespace art
599
600#endif // ART_COMPILER_OPTIMIZING_LOCATIONS_H_