blob: 9ce8d351a98e1c4498e60ac2540977a6e33ea22e [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 Geoffray6c2dff82015-01-21 14:56:54 +000040 enum OutputOverlap {
41 kOutputOverlap,
42 kNoOutputOverlap
43 };
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +010044
Nicolas Geoffray76716a62014-05-23 10:14:19 +010045 enum Kind {
46 kInvalid = 0,
Nicolas Geoffray96f89a22014-07-11 10:57:49 +010047 kConstant = 1,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010048 kStackSlot = 2, // 32bit stack slot.
Nicolas Geoffray96f89a22014-07-11 10:57:49 +010049 kDoubleStackSlot = 3, // 64bit stack slot.
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010050
51 kRegister = 4, // Core register.
52
53 // We do not use the value 5 because it conflicts with kLocationConstantMask.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +010054 kDoNotUse5 = 5,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010055
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +000056 kFpuRegister = 6, // Float register.
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010057
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +000058 kRegisterPair = 7, // Long register.
59
60 kFpuRegisterPair = 8, // Double register.
61
62 // We do not use the value 9 because it conflicts with kLocationConstantMask.
63 kDoNotUse9 = 9,
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +010064
Nicolas Geoffray76716a62014-05-23 10:14:19 +010065 // Unallocated location represents a location that is not fixed and can be
66 // allocated by a register allocator. Each unallocated location has
67 // a policy that specifies what kind of location is suitable. Payload
68 // contains register allocation policy.
Mark Mendell3e6a3bf2015-01-19 14:09:22 -050069 kUnallocated = 10,
Nicolas Geoffray76716a62014-05-23 10:14:19 +010070 };
71
72 Location() : value_(kInvalid) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010073 // Verify that non-constant location kinds do not interfere with kConstant.
Andreas Gampe785d2f22014-11-03 22:57:30 -080074 static_assert((kInvalid & kLocationConstantMask) != kConstant, "TagError");
75 static_assert((kUnallocated & kLocationConstantMask) != kConstant, "TagError");
76 static_assert((kStackSlot & kLocationConstantMask) != kConstant, "TagError");
77 static_assert((kDoubleStackSlot & kLocationConstantMask) != kConstant, "TagError");
78 static_assert((kRegister & kLocationConstantMask) != kConstant, "TagError");
Andreas Gampe785d2f22014-11-03 22:57:30 -080079 static_assert((kFpuRegister & kLocationConstantMask) != kConstant, "TagError");
80 static_assert((kRegisterPair & kLocationConstantMask) != kConstant, "TagError");
81 static_assert((kFpuRegisterPair & kLocationConstantMask) != kConstant, "TagError");
82 static_assert((kConstant & kLocationConstantMask) == kConstant, "TagError");
Nicolas Geoffray96f89a22014-07-11 10:57:49 +010083
Nicolas Geoffray76716a62014-05-23 10:14:19 +010084 DCHECK(!IsValid());
85 }
86
87 Location(const Location& other) : ValueObject(), value_(other.value_) {}
88
89 Location& operator=(const Location& other) {
90 value_ = other.value_;
91 return *this;
92 }
93
Nicolas Geoffray96f89a22014-07-11 10:57:49 +010094 bool IsConstant() const {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010095 return (value_ & kLocationConstantMask) == kConstant;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +010096 }
97
98 static Location ConstantLocation(HConstant* constant) {
99 DCHECK(constant != nullptr);
Ian Rogers13735952014-10-08 12:43:28 -0700100 return Location(kConstant | reinterpret_cast<uintptr_t>(constant));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100101 }
102
103 HConstant* GetConstant() const {
104 DCHECK(IsConstant());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100105 return reinterpret_cast<HConstant*>(value_ & ~kLocationConstantMask);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100106 }
107
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100108 bool IsValid() const {
109 return value_ != kInvalid;
110 }
111
112 bool IsInvalid() const {
113 return !IsValid();
114 }
115
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100116 // Empty location. Used if there the location should be ignored.
117 static Location NoLocation() {
118 return Location();
119 }
120
121 // Register locations.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100122 static Location RegisterLocation(int reg) {
123 return Location(kRegister, reg);
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100124 }
125
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100126 static Location FpuRegisterLocation(int reg) {
127 return Location(kFpuRegister, reg);
128 }
129
130 static Location RegisterPairLocation(int low, int high) {
131 return Location(kRegisterPair, low << 16 | high);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100132 }
133
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000134 static Location FpuRegisterPairLocation(int low, int high) {
135 return Location(kFpuRegisterPair, low << 16 | high);
136 }
137
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100138 bool IsRegister() const {
139 return GetKind() == kRegister;
140 }
141
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100142 bool IsFpuRegister() const {
143 return GetKind() == kFpuRegister;
144 }
145
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100146 bool IsRegisterPair() const {
147 return GetKind() == kRegisterPair;
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100148 }
149
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000150 bool IsFpuRegisterPair() const {
151 return GetKind() == kFpuRegisterPair;
152 }
153
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100154 int reg() const {
155 DCHECK(IsRegister() || IsFpuRegister());
156 return GetPayload();
157 }
158
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000159 int low() const {
160 DCHECK(IsPair());
161 return GetPayload() >> 16;
162 }
163
164 int high() const {
165 DCHECK(IsPair());
166 return GetPayload() & 0xFFFF;
167 }
168
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100169 template <typename T>
Roland Levillain271ab9c2014-11-27 15:23:57 +0000170 T AsRegister() const {
171 DCHECK(IsRegister());
172 return static_cast<T>(reg());
173 }
174
175 template <typename T>
176 T AsFpuRegister() const {
177 DCHECK(IsFpuRegister());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100178 return static_cast<T>(reg());
179 }
180
181 template <typename T>
182 T AsRegisterPairLow() const {
183 DCHECK(IsRegisterPair());
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000184 return static_cast<T>(low());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100185 }
186
187 template <typename T>
188 T AsRegisterPairHigh() const {
189 DCHECK(IsRegisterPair());
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000190 return static_cast<T>(high());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100191 }
192
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000193 template <typename T>
194 T AsFpuRegisterPairLow() const {
195 DCHECK(IsFpuRegisterPair());
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000196 return static_cast<T>(low());
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000197 }
198
199 template <typename T>
200 T AsFpuRegisterPairHigh() const {
201 DCHECK(IsFpuRegisterPair());
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000202 return static_cast<T>(high());
203 }
204
205 bool IsPair() const {
206 return IsRegisterPair() || IsFpuRegisterPair();
207 }
208
209 Location ToLow() const {
210 return IsRegisterPair()
211 ? Location::RegisterLocation(low())
212 : Location::FpuRegisterLocation(low());
213 }
214
215 Location ToHigh() const {
216 return IsRegisterPair()
217 ? Location::RegisterLocation(high())
218 : Location::FpuRegisterLocation(high());
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000219 }
220
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100221 static uintptr_t EncodeStackIndex(intptr_t stack_index) {
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100222 DCHECK(-kStackIndexBias <= stack_index);
223 DCHECK(stack_index < kStackIndexBias);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100224 return static_cast<uintptr_t>(kStackIndexBias + stack_index);
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100225 }
226
227 static Location StackSlot(intptr_t stack_index) {
Ian Rogers13735952014-10-08 12:43:28 -0700228 uintptr_t payload = EncodeStackIndex(stack_index);
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100229 Location loc(kStackSlot, payload);
230 // Ensure that sign is preserved.
231 DCHECK_EQ(loc.GetStackIndex(), stack_index);
232 return loc;
233 }
234
235 bool IsStackSlot() const {
236 return GetKind() == kStackSlot;
237 }
238
239 static Location DoubleStackSlot(intptr_t stack_index) {
Ian Rogers13735952014-10-08 12:43:28 -0700240 uintptr_t payload = EncodeStackIndex(stack_index);
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100241 Location loc(kDoubleStackSlot, payload);
242 // Ensure that sign is preserved.
243 DCHECK_EQ(loc.GetStackIndex(), stack_index);
244 return loc;
245 }
246
247 bool IsDoubleStackSlot() const {
248 return GetKind() == kDoubleStackSlot;
249 }
250
251 intptr_t GetStackIndex() const {
252 DCHECK(IsStackSlot() || IsDoubleStackSlot());
253 // Decode stack index manually to preserve sign.
254 return GetPayload() - kStackIndexBias;
255 }
256
257 intptr_t GetHighStackIndex(uintptr_t word_size) const {
258 DCHECK(IsDoubleStackSlot());
259 // Decode stack index manually to preserve sign.
260 return GetPayload() - kStackIndexBias + word_size;
261 }
262
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100263 Kind GetKind() const {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100264 return IsConstant() ? kConstant : KindField::Decode(value_);
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100265 }
266
267 bool Equals(Location other) const {
268 return value_ == other.value_;
269 }
270
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +0000271 bool Contains(Location other) const {
272 if (Equals(other)) {
273 return true;
274 } else if (IsFpuRegisterPair() && other.IsFpuRegister()) {
275 return other.reg() == low() || other.reg() == high();
276 } else if (IsRegisterPair() && other.IsRegister()) {
277 return other.reg() == low() || other.reg() == high();
278 } else if (IsDoubleStackSlot() && other.IsStackSlot()) {
279 return (GetStackIndex() == other.GetStackIndex())
280 || (GetStackIndex() + 4 == other.GetStackIndex());
281 }
282 return false;
283 }
284
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100285 const char* DebugString() const {
286 switch (GetKind()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100287 case kInvalid: return "I";
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100288 case kRegister: return "R";
289 case kStackSlot: return "S";
290 case kDoubleStackSlot: return "DS";
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100291 case kUnallocated: return "U";
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100292 case kConstant: return "C";
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100293 case kFpuRegister: return "F";
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100294 case kRegisterPair: return "RP";
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000295 case kFpuRegisterPair: return "FP";
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100296 case kDoNotUse5: // fall-through
297 case kDoNotUse9:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100298 LOG(FATAL) << "Should not use this location kind";
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100299 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100300 UNREACHABLE();
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100301 return "?";
302 }
303
304 // Unallocated locations.
305 enum Policy {
306 kAny,
307 kRequiresRegister,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100308 kRequiresFpuRegister,
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100309 kSameAsFirstInput,
310 };
311
312 bool IsUnallocated() const {
313 return GetKind() == kUnallocated;
314 }
315
316 static Location UnallocatedLocation(Policy policy) {
317 return Location(kUnallocated, PolicyField::Encode(policy));
318 }
319
320 // Any free register is suitable to replace this unallocated location.
321 static Location Any() {
322 return UnallocatedLocation(kAny);
323 }
324
325 static Location RequiresRegister() {
326 return UnallocatedLocation(kRequiresRegister);
327 }
328
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100329 static Location RequiresFpuRegister() {
330 return UnallocatedLocation(kRequiresFpuRegister);
331 }
332
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100333 static Location RegisterOrConstant(HInstruction* instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100334 static Location ByteRegisterOrConstant(int reg, HInstruction* instruction);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100335
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100336 // The location of the first input to the instruction will be
337 // used to replace this unallocated location.
338 static Location SameAsFirstInput() {
339 return UnallocatedLocation(kSameAsFirstInput);
340 }
341
342 Policy GetPolicy() const {
343 DCHECK(IsUnallocated());
344 return PolicyField::Decode(GetPayload());
345 }
346
Ian Rogers13735952014-10-08 12:43:28 -0700347 uintptr_t GetEncoding() const {
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100348 return GetPayload();
349 }
350
351 private:
352 // Number of bits required to encode Kind value.
353 static constexpr uint32_t kBitsForKind = 4;
Ian Rogers13735952014-10-08 12:43:28 -0700354 static constexpr uint32_t kBitsForPayload = kBitsPerIntPtrT - kBitsForKind;
355 static constexpr uintptr_t kLocationConstantMask = 0x3;
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100356
Ian Rogers13735952014-10-08 12:43:28 -0700357 explicit Location(uintptr_t value) : value_(value) {}
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100358
Ian Rogers13735952014-10-08 12:43:28 -0700359 Location(Kind kind, uintptr_t payload)
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100360 : value_(KindField::Encode(kind) | PayloadField::Encode(payload)) {}
361
Ian Rogers13735952014-10-08 12:43:28 -0700362 uintptr_t GetPayload() const {
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100363 return PayloadField::Decode(value_);
364 }
365
366 typedef BitField<Kind, 0, kBitsForKind> KindField;
Ian Rogers13735952014-10-08 12:43:28 -0700367 typedef BitField<uintptr_t, kBitsForKind, kBitsForPayload> PayloadField;
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100368
369 // Layout for kUnallocated locations payload.
370 typedef BitField<Policy, 0, 3> PolicyField;
371
372 // Layout for stack slots.
373 static const intptr_t kStackIndexBias =
374 static_cast<intptr_t>(1) << (kBitsForPayload - 1);
375
376 // Location either contains kind and payload fields or a tagged handle for
377 // a constant locations. Values of enumeration Kind are selected in such a
378 // way that none of them can be interpreted as a kConstant tag.
Ian Rogers13735952014-10-08 12:43:28 -0700379 uintptr_t value_;
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100380};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700381std::ostream& operator<<(std::ostream& os, const Location::Kind& rhs);
382std::ostream& operator<<(std::ostream& os, const Location::Policy& rhs);
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100383
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100384class RegisterSet : public ValueObject {
385 public:
386 RegisterSet() : core_registers_(0), floating_point_registers_(0) {}
387
388 void Add(Location loc) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100389 if (loc.IsRegister()) {
390 core_registers_ |= (1 << loc.reg());
391 } else {
392 DCHECK(loc.IsFpuRegister());
393 floating_point_registers_ |= (1 << loc.reg());
394 }
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100395 }
396
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100397 void Remove(Location loc) {
398 if (loc.IsRegister()) {
399 core_registers_ &= ~(1 << loc.reg());
400 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000401 DCHECK(loc.IsFpuRegister()) << loc;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100402 floating_point_registers_ &= ~(1 << loc.reg());
403 }
404 }
405
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100406 bool ContainsCoreRegister(uint32_t id) {
407 return Contains(core_registers_, id);
408 }
409
410 bool ContainsFloatingPointRegister(uint32_t id) {
411 return Contains(floating_point_registers_, id);
412 }
413
414 static bool Contains(uint32_t register_set, uint32_t reg) {
415 return (register_set & (1 << reg)) != 0;
416 }
417
Nicolas Geoffray87d03762014-11-19 15:17:56 +0000418 size_t GetNumberOfRegisters() const {
419 return __builtin_popcount(core_registers_) + __builtin_popcount(floating_point_registers_);
420 }
421
Nicolas Geoffray98893962015-01-21 12:32:32 +0000422 uint32_t GetCoreRegisters() const {
423 return core_registers_;
424 }
425
426 uint32_t GetFloatingPointRegisters() const {
427 return floating_point_registers_;
428 }
429
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100430 private:
431 uint32_t core_registers_;
432 uint32_t floating_point_registers_;
433
434 DISALLOW_COPY_AND_ASSIGN(RegisterSet);
435};
436
Andreas Gampe878d58c2015-01-15 23:24:00 -0800437static constexpr bool kIntrinsified = true;
438
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100439/**
440 * The code generator computes LocationSummary for each instruction so that
441 * the instruction itself knows what code to generate: where to find the inputs
442 * and where to place the result.
443 *
444 * The intent is to have the code for generating the instruction independent of
445 * register allocation. A register allocator just has to provide a LocationSummary.
446 */
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700447class LocationSummary : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100448 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100449 enum CallKind {
450 kNoCall,
451 kCallOnSlowPath,
452 kCall
453 };
454
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800455 LocationSummary(HInstruction* instruction,
456 CallKind call_kind = kNoCall,
457 bool intrinsified = false);
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100458
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100459 void SetInAt(uint32_t at, Location location) {
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000460 DCHECK(inputs_.Get(at).IsUnallocated() || inputs_.Get(at).IsInvalid());
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100461 inputs_.Put(at, location);
462 }
463
464 Location InAt(uint32_t at) const {
465 return inputs_.Get(at);
466 }
467
468 size_t GetInputCount() const {
469 return inputs_.Size();
470 }
471
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000472 void SetOut(Location location, Location::OutputOverlap overlaps = Location::kOutputOverlap) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000473 DCHECK(output_.IsInvalid());
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100474 output_overlaps_ = overlaps;
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000475 output_ = location;
476 }
477
478 void UpdateOut(Location location) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000479 // There are two reasons for updating an output:
480 // 1) Parameters, where we only know the exact stack slot after
481 // doing full register allocation.
482 // 2) Unallocated location.
483 DCHECK(output_.IsStackSlot() || output_.IsDoubleStackSlot() || output_.IsUnallocated());
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000484 output_ = location;
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100485 }
486
487 void AddTemp(Location location) {
488 temps_.Add(location);
489 }
490
491 Location GetTemp(uint32_t at) const {
492 return temps_.Get(at);
493 }
494
495 void SetTempAt(uint32_t at, Location location) {
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000496 DCHECK(temps_.Get(at).IsUnallocated() || temps_.Get(at).IsInvalid());
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100497 temps_.Put(at, location);
498 }
499
500 size_t GetTempCount() const {
501 return temps_.Size();
502 }
503
Nicolas Geoffray39468442014-09-02 15:17:15 +0100504 void SetEnvironmentAt(uint32_t at, Location location) {
505 environment_.Put(at, location);
506 }
507
508 Location GetEnvironmentAt(uint32_t at) const {
509 return environment_.Get(at);
510 }
511
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100512 Location Out() const { return output_; }
513
Nicolas Geoffray39468442014-09-02 15:17:15 +0100514 bool CanCall() const { return call_kind_ != kNoCall; }
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100515 bool WillCall() const { return call_kind_ == kCall; }
516 bool OnlyCallsOnSlowPath() const { return call_kind_ == kCallOnSlowPath; }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100517 bool NeedsSafepoint() const { return CanCall(); }
518
519 void SetStackBit(uint32_t index) {
520 stack_mask_->SetBit(index);
521 }
522
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100523 void ClearStackBit(uint32_t index) {
524 stack_mask_->ClearBit(index);
525 }
526
Nicolas Geoffray39468442014-09-02 15:17:15 +0100527 void SetRegisterBit(uint32_t reg_id) {
528 register_mask_ |= (1 << reg_id);
529 }
530
Nicolas Geoffray98893962015-01-21 12:32:32 +0000531 uint32_t GetRegisterMask() const {
532 return register_mask_;
533 }
534
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100535 bool RegisterContainsObject(uint32_t reg_id) {
536 return RegisterSet::Contains(register_mask_, reg_id);
537 }
538
539 void AddLiveRegister(Location location) {
540 live_registers_.Add(location);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100541 }
542
543 BitVector* GetStackMask() const {
544 return stack_mask_;
545 }
546
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100547 RegisterSet* GetLiveRegisters() {
548 return &live_registers_;
549 }
550
Nicolas Geoffray87d03762014-11-19 15:17:56 +0000551 size_t GetNumberOfLiveRegisters() const {
552 return live_registers_.GetNumberOfRegisters();
553 }
554
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000555 bool OutputUsesSameAs(uint32_t input_index) const {
556 return (input_index == 0)
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100557 && output_.IsUnallocated()
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000558 && (output_.GetPolicy() == Location::kSameAsFirstInput);
559 }
560
561 bool IsFixedInput(uint32_t input_index) const {
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000562 Location input = inputs_.Get(input_index);
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000563 return input.IsRegister()
Nicolas Geoffray98893962015-01-21 12:32:32 +0000564 || input.IsFpuRegister()
565 || input.IsPair()
566 || input.IsStackSlot()
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000567 || input.IsDoubleStackSlot();
Nicolas Geoffray76905622014-09-25 14:39:26 +0100568 }
569
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000570 bool OutputCanOverlapWithInputs() const {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000571 return output_overlaps_ == Location::kOutputOverlap;
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100572 }
573
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800574 bool Intrinsified() const {
575 return intrinsified_;
576 }
577
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100578 private:
579 GrowableArray<Location> inputs_;
580 GrowableArray<Location> temps_;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100581 GrowableArray<Location> environment_;
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100582 // Whether the output overlaps with any of the inputs. If it overlaps, then it cannot
583 // share the same register as the inputs.
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000584 Location::OutputOverlap output_overlaps_;
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100585 Location output_;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100586 const CallKind call_kind_;
587
588 // Mask of objects that live in the stack.
589 BitVector* stack_mask_;
590
591 // Mask of objects that live in register.
592 uint32_t register_mask_;
593
594 // Registers that are in use at this position.
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100595 RegisterSet live_registers_;
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100596
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800597 // Whether these are locations for an intrinsified call.
598 const bool intrinsified_;
599
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000600 ART_FRIEND_TEST(RegisterAllocatorTest, ExpectedInRegisterHint);
601 ART_FRIEND_TEST(RegisterAllocatorTest, SameAsFirstInputHint);
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100602 DISALLOW_COPY_AND_ASSIGN(LocationSummary);
603};
604
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100605} // namespace art
606
607#endif // ART_COMPILER_OPTIMIZING_LOCATIONS_H_