blob: 566c0daf1ecb65182abd9366f7ead815c880fa0b [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
Mathieu Chartierb666f482015-02-18 14:33:14 -080020#include "base/arena_object.h"
Nicolas Geoffray76716a62014-05-23 10:14:19 +010021#include "base/bit_field.h"
Nicolas Geoffray39468442014-09-02 15:17:15 +010022#include "base/bit_vector.h"
Ian Rogers0279ebb2014-10-08 17:27:48 -070023#include "base/value_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 Geoffrayda02afe2015-02-11 02:29:42 +0000154 bool IsRegisterKind() const {
155 return IsRegister() || IsFpuRegister() || IsRegisterPair() || IsFpuRegisterPair();
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 {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000214 if (IsRegisterPair()) {
215 return Location::RegisterLocation(low());
216 } else if (IsFpuRegisterPair()) {
217 return Location::FpuRegisterLocation(low());
218 } else {
219 DCHECK(IsDoubleStackSlot());
220 return Location::StackSlot(GetStackIndex());
221 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000222 }
223
224 Location ToHigh() const {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000225 if (IsRegisterPair()) {
226 return Location::RegisterLocation(high());
227 } else if (IsFpuRegisterPair()) {
228 return Location::FpuRegisterLocation(high());
229 } else {
230 DCHECK(IsDoubleStackSlot());
231 return Location::StackSlot(GetHighStackIndex(4));
232 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000233 }
234
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100235 static uintptr_t EncodeStackIndex(intptr_t stack_index) {
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100236 DCHECK(-kStackIndexBias <= stack_index);
237 DCHECK(stack_index < kStackIndexBias);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100238 return static_cast<uintptr_t>(kStackIndexBias + stack_index);
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100239 }
240
241 static Location StackSlot(intptr_t stack_index) {
Ian Rogers13735952014-10-08 12:43:28 -0700242 uintptr_t payload = EncodeStackIndex(stack_index);
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100243 Location loc(kStackSlot, payload);
244 // Ensure that sign is preserved.
245 DCHECK_EQ(loc.GetStackIndex(), stack_index);
246 return loc;
247 }
248
249 bool IsStackSlot() const {
250 return GetKind() == kStackSlot;
251 }
252
253 static Location DoubleStackSlot(intptr_t stack_index) {
Ian Rogers13735952014-10-08 12:43:28 -0700254 uintptr_t payload = EncodeStackIndex(stack_index);
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100255 Location loc(kDoubleStackSlot, payload);
256 // Ensure that sign is preserved.
257 DCHECK_EQ(loc.GetStackIndex(), stack_index);
258 return loc;
259 }
260
261 bool IsDoubleStackSlot() const {
262 return GetKind() == kDoubleStackSlot;
263 }
264
265 intptr_t GetStackIndex() const {
266 DCHECK(IsStackSlot() || IsDoubleStackSlot());
267 // Decode stack index manually to preserve sign.
268 return GetPayload() - kStackIndexBias;
269 }
270
271 intptr_t GetHighStackIndex(uintptr_t word_size) const {
272 DCHECK(IsDoubleStackSlot());
273 // Decode stack index manually to preserve sign.
274 return GetPayload() - kStackIndexBias + word_size;
275 }
276
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100277 Kind GetKind() const {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100278 return IsConstant() ? kConstant : KindField::Decode(value_);
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100279 }
280
281 bool Equals(Location other) const {
282 return value_ == other.value_;
283 }
284
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +0000285 bool Contains(Location other) const {
286 if (Equals(other)) {
287 return true;
288 } else if (IsFpuRegisterPair() && other.IsFpuRegister()) {
289 return other.reg() == low() || other.reg() == high();
290 } else if (IsRegisterPair() && other.IsRegister()) {
291 return other.reg() == low() || other.reg() == high();
292 } else if (IsDoubleStackSlot() && other.IsStackSlot()) {
293 return (GetStackIndex() == other.GetStackIndex())
294 || (GetStackIndex() + 4 == other.GetStackIndex());
295 }
296 return false;
297 }
298
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100299 const char* DebugString() const {
300 switch (GetKind()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100301 case kInvalid: return "I";
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100302 case kRegister: return "R";
303 case kStackSlot: return "S";
304 case kDoubleStackSlot: return "DS";
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100305 case kUnallocated: return "U";
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100306 case kConstant: return "C";
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100307 case kFpuRegister: return "F";
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100308 case kRegisterPair: return "RP";
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000309 case kFpuRegisterPair: return "FP";
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100310 case kDoNotUse5: // fall-through
311 case kDoNotUse9:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100312 LOG(FATAL) << "Should not use this location kind";
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100313 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100314 UNREACHABLE();
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100315 return "?";
316 }
317
318 // Unallocated locations.
319 enum Policy {
320 kAny,
321 kRequiresRegister,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100322 kRequiresFpuRegister,
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100323 kSameAsFirstInput,
324 };
325
326 bool IsUnallocated() const {
327 return GetKind() == kUnallocated;
328 }
329
330 static Location UnallocatedLocation(Policy policy) {
331 return Location(kUnallocated, PolicyField::Encode(policy));
332 }
333
334 // Any free register is suitable to replace this unallocated location.
335 static Location Any() {
336 return UnallocatedLocation(kAny);
337 }
338
339 static Location RequiresRegister() {
340 return UnallocatedLocation(kRequiresRegister);
341 }
342
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100343 static Location RequiresFpuRegister() {
344 return UnallocatedLocation(kRequiresFpuRegister);
345 }
346
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100347 static Location RegisterOrConstant(HInstruction* instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100348 static Location ByteRegisterOrConstant(int reg, HInstruction* instruction);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100349
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100350 // The location of the first input to the instruction will be
351 // used to replace this unallocated location.
352 static Location SameAsFirstInput() {
353 return UnallocatedLocation(kSameAsFirstInput);
354 }
355
356 Policy GetPolicy() const {
357 DCHECK(IsUnallocated());
358 return PolicyField::Decode(GetPayload());
359 }
360
Ian Rogers13735952014-10-08 12:43:28 -0700361 uintptr_t GetEncoding() const {
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100362 return GetPayload();
363 }
364
365 private:
366 // Number of bits required to encode Kind value.
367 static constexpr uint32_t kBitsForKind = 4;
Ian Rogers13735952014-10-08 12:43:28 -0700368 static constexpr uint32_t kBitsForPayload = kBitsPerIntPtrT - kBitsForKind;
369 static constexpr uintptr_t kLocationConstantMask = 0x3;
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100370
Ian Rogers13735952014-10-08 12:43:28 -0700371 explicit Location(uintptr_t value) : value_(value) {}
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100372
Ian Rogers13735952014-10-08 12:43:28 -0700373 Location(Kind kind, uintptr_t payload)
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100374 : value_(KindField::Encode(kind) | PayloadField::Encode(payload)) {}
375
Ian Rogers13735952014-10-08 12:43:28 -0700376 uintptr_t GetPayload() const {
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100377 return PayloadField::Decode(value_);
378 }
379
380 typedef BitField<Kind, 0, kBitsForKind> KindField;
Ian Rogers13735952014-10-08 12:43:28 -0700381 typedef BitField<uintptr_t, kBitsForKind, kBitsForPayload> PayloadField;
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100382
383 // Layout for kUnallocated locations payload.
384 typedef BitField<Policy, 0, 3> PolicyField;
385
386 // Layout for stack slots.
387 static const intptr_t kStackIndexBias =
388 static_cast<intptr_t>(1) << (kBitsForPayload - 1);
389
390 // Location either contains kind and payload fields or a tagged handle for
391 // a constant locations. Values of enumeration Kind are selected in such a
392 // way that none of them can be interpreted as a kConstant tag.
Ian Rogers13735952014-10-08 12:43:28 -0700393 uintptr_t value_;
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100394};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700395std::ostream& operator<<(std::ostream& os, const Location::Kind& rhs);
396std::ostream& operator<<(std::ostream& os, const Location::Policy& rhs);
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100397
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100398class RegisterSet : public ValueObject {
399 public:
400 RegisterSet() : core_registers_(0), floating_point_registers_(0) {}
401
402 void Add(Location loc) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100403 if (loc.IsRegister()) {
404 core_registers_ |= (1 << loc.reg());
405 } else {
406 DCHECK(loc.IsFpuRegister());
407 floating_point_registers_ |= (1 << loc.reg());
408 }
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100409 }
410
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100411 void Remove(Location loc) {
412 if (loc.IsRegister()) {
413 core_registers_ &= ~(1 << loc.reg());
414 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000415 DCHECK(loc.IsFpuRegister()) << loc;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100416 floating_point_registers_ &= ~(1 << loc.reg());
417 }
418 }
419
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100420 bool ContainsCoreRegister(uint32_t id) {
421 return Contains(core_registers_, id);
422 }
423
424 bool ContainsFloatingPointRegister(uint32_t id) {
425 return Contains(floating_point_registers_, id);
426 }
427
428 static bool Contains(uint32_t register_set, uint32_t reg) {
429 return (register_set & (1 << reg)) != 0;
430 }
431
Nicolas Geoffray87d03762014-11-19 15:17:56 +0000432 size_t GetNumberOfRegisters() const {
433 return __builtin_popcount(core_registers_) + __builtin_popcount(floating_point_registers_);
434 }
435
Nicolas Geoffray98893962015-01-21 12:32:32 +0000436 uint32_t GetCoreRegisters() const {
437 return core_registers_;
438 }
439
440 uint32_t GetFloatingPointRegisters() const {
441 return floating_point_registers_;
442 }
443
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100444 private:
445 uint32_t core_registers_;
446 uint32_t floating_point_registers_;
447
448 DISALLOW_COPY_AND_ASSIGN(RegisterSet);
449};
450
Andreas Gampe878d58c2015-01-15 23:24:00 -0800451static constexpr bool kIntrinsified = true;
452
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100453/**
454 * The code generator computes LocationSummary for each instruction so that
455 * the instruction itself knows what code to generate: where to find the inputs
456 * and where to place the result.
457 *
458 * The intent is to have the code for generating the instruction independent of
459 * register allocation. A register allocator just has to provide a LocationSummary.
460 */
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700461class LocationSummary : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100462 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100463 enum CallKind {
464 kNoCall,
465 kCallOnSlowPath,
466 kCall
467 };
468
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800469 LocationSummary(HInstruction* instruction,
470 CallKind call_kind = kNoCall,
471 bool intrinsified = false);
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100472
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100473 void SetInAt(uint32_t at, Location location) {
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000474 DCHECK(inputs_.Get(at).IsUnallocated() || inputs_.Get(at).IsInvalid());
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100475 inputs_.Put(at, location);
476 }
477
478 Location InAt(uint32_t at) const {
479 return inputs_.Get(at);
480 }
481
482 size_t GetInputCount() const {
483 return inputs_.Size();
484 }
485
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000486 void SetOut(Location location, Location::OutputOverlap overlaps = Location::kOutputOverlap) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000487 DCHECK(output_.IsInvalid());
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100488 output_overlaps_ = overlaps;
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000489 output_ = location;
490 }
491
492 void UpdateOut(Location location) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000493 // There are two reasons for updating an output:
494 // 1) Parameters, where we only know the exact stack slot after
495 // doing full register allocation.
496 // 2) Unallocated location.
497 DCHECK(output_.IsStackSlot() || output_.IsDoubleStackSlot() || output_.IsUnallocated());
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000498 output_ = location;
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100499 }
500
501 void AddTemp(Location location) {
502 temps_.Add(location);
503 }
504
505 Location GetTemp(uint32_t at) const {
506 return temps_.Get(at);
507 }
508
509 void SetTempAt(uint32_t at, Location location) {
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000510 DCHECK(temps_.Get(at).IsUnallocated() || temps_.Get(at).IsInvalid());
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100511 temps_.Put(at, location);
512 }
513
514 size_t GetTempCount() const {
515 return temps_.Size();
516 }
517
Nicolas Geoffray39468442014-09-02 15:17:15 +0100518 void SetEnvironmentAt(uint32_t at, Location location) {
519 environment_.Put(at, location);
520 }
521
522 Location GetEnvironmentAt(uint32_t at) const {
523 return environment_.Get(at);
524 }
525
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100526 Location Out() const { return output_; }
527
Nicolas Geoffray39468442014-09-02 15:17:15 +0100528 bool CanCall() const { return call_kind_ != kNoCall; }
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100529 bool WillCall() const { return call_kind_ == kCall; }
530 bool OnlyCallsOnSlowPath() const { return call_kind_ == kCallOnSlowPath; }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100531 bool NeedsSafepoint() const { return CanCall(); }
532
533 void SetStackBit(uint32_t index) {
534 stack_mask_->SetBit(index);
535 }
536
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100537 void ClearStackBit(uint32_t index) {
538 stack_mask_->ClearBit(index);
539 }
540
Nicolas Geoffray39468442014-09-02 15:17:15 +0100541 void SetRegisterBit(uint32_t reg_id) {
542 register_mask_ |= (1 << reg_id);
543 }
544
Nicolas Geoffray98893962015-01-21 12:32:32 +0000545 uint32_t GetRegisterMask() const {
546 return register_mask_;
547 }
548
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100549 bool RegisterContainsObject(uint32_t reg_id) {
550 return RegisterSet::Contains(register_mask_, reg_id);
551 }
552
553 void AddLiveRegister(Location location) {
554 live_registers_.Add(location);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100555 }
556
557 BitVector* GetStackMask() const {
558 return stack_mask_;
559 }
560
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100561 RegisterSet* GetLiveRegisters() {
562 return &live_registers_;
563 }
564
Nicolas Geoffray87d03762014-11-19 15:17:56 +0000565 size_t GetNumberOfLiveRegisters() const {
566 return live_registers_.GetNumberOfRegisters();
567 }
568
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000569 bool OutputUsesSameAs(uint32_t input_index) const {
570 return (input_index == 0)
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100571 && output_.IsUnallocated()
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000572 && (output_.GetPolicy() == Location::kSameAsFirstInput);
573 }
574
575 bool IsFixedInput(uint32_t input_index) const {
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000576 Location input = inputs_.Get(input_index);
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000577 return input.IsRegister()
Nicolas Geoffray98893962015-01-21 12:32:32 +0000578 || input.IsFpuRegister()
579 || input.IsPair()
580 || input.IsStackSlot()
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000581 || input.IsDoubleStackSlot();
Nicolas Geoffray76905622014-09-25 14:39:26 +0100582 }
583
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000584 bool OutputCanOverlapWithInputs() const {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000585 return output_overlaps_ == Location::kOutputOverlap;
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100586 }
587
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800588 bool Intrinsified() const {
589 return intrinsified_;
590 }
591
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100592 private:
593 GrowableArray<Location> inputs_;
594 GrowableArray<Location> temps_;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100595 GrowableArray<Location> environment_;
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100596 // Whether the output overlaps with any of the inputs. If it overlaps, then it cannot
597 // share the same register as the inputs.
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000598 Location::OutputOverlap output_overlaps_;
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100599 Location output_;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100600 const CallKind call_kind_;
601
602 // Mask of objects that live in the stack.
603 BitVector* stack_mask_;
604
605 // Mask of objects that live in register.
606 uint32_t register_mask_;
607
608 // Registers that are in use at this position.
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100609 RegisterSet live_registers_;
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100610
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800611 // Whether these are locations for an intrinsified call.
612 const bool intrinsified_;
613
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000614 ART_FRIEND_TEST(RegisterAllocatorTest, ExpectedInRegisterHint);
615 ART_FRIEND_TEST(RegisterAllocatorTest, SameAsFirstInputHint);
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100616 DISALLOW_COPY_AND_ASSIGN(LocationSummary);
617};
618
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100619} // namespace art
620
621#endif // ART_COMPILER_OPTIMIZING_LOCATIONS_H_