blob: 198cc15cce108daeae81663489bc99fcce4328fc [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 {
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 Geoffray76716a62014-05-23 10:14:19 +0100267 Kind GetKind() const {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100268 return IsConstant() ? kConstant : KindField::Decode(value_);
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100269 }
270
271 bool Equals(Location other) const {
272 return value_ == other.value_;
273 }
274
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +0000275 bool Contains(Location other) const {
276 if (Equals(other)) {
277 return true;
278 } else if (IsFpuRegisterPair() && other.IsFpuRegister()) {
279 return other.reg() == low() || other.reg() == high();
280 } else if (IsRegisterPair() && other.IsRegister()) {
281 return other.reg() == low() || other.reg() == high();
282 } else if (IsDoubleStackSlot() && other.IsStackSlot()) {
283 return (GetStackIndex() == other.GetStackIndex())
284 || (GetStackIndex() + 4 == other.GetStackIndex());
285 }
286 return false;
287 }
288
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100289 const char* DebugString() const {
290 switch (GetKind()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100291 case kInvalid: return "I";
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100292 case kRegister: return "R";
293 case kStackSlot: return "S";
294 case kDoubleStackSlot: return "DS";
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100295 case kUnallocated: return "U";
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100296 case kConstant: return "C";
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100297 case kFpuRegister: return "F";
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100298 case kRegisterPair: return "RP";
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000299 case kFpuRegisterPair: return "FP";
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100300 case kDoNotUse5: // fall-through
301 case kDoNotUse9:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100302 LOG(FATAL) << "Should not use this location kind";
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100303 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100304 UNREACHABLE();
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100305 return "?";
306 }
307
308 // Unallocated locations.
309 enum Policy {
310 kAny,
311 kRequiresRegister,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100312 kRequiresFpuRegister,
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100313 kSameAsFirstInput,
314 };
315
316 bool IsUnallocated() const {
317 return GetKind() == kUnallocated;
318 }
319
320 static Location UnallocatedLocation(Policy policy) {
321 return Location(kUnallocated, PolicyField::Encode(policy));
322 }
323
324 // Any free register is suitable to replace this unallocated location.
325 static Location Any() {
326 return UnallocatedLocation(kAny);
327 }
328
329 static Location RequiresRegister() {
330 return UnallocatedLocation(kRequiresRegister);
331 }
332
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100333 static Location RequiresFpuRegister() {
334 return UnallocatedLocation(kRequiresFpuRegister);
335 }
336
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100337 static Location RegisterOrConstant(HInstruction* instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100338 static Location ByteRegisterOrConstant(int reg, HInstruction* instruction);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100339
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100340 // The location of the first input to the instruction will be
341 // used to replace this unallocated location.
342 static Location SameAsFirstInput() {
343 return UnallocatedLocation(kSameAsFirstInput);
344 }
345
346 Policy GetPolicy() const {
347 DCHECK(IsUnallocated());
348 return PolicyField::Decode(GetPayload());
349 }
350
Ian Rogers13735952014-10-08 12:43:28 -0700351 uintptr_t GetEncoding() const {
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100352 return GetPayload();
353 }
354
355 private:
356 // Number of bits required to encode Kind value.
357 static constexpr uint32_t kBitsForKind = 4;
Ian Rogers13735952014-10-08 12:43:28 -0700358 static constexpr uint32_t kBitsForPayload = kBitsPerIntPtrT - kBitsForKind;
359 static constexpr uintptr_t kLocationConstantMask = 0x3;
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100360
Ian Rogers13735952014-10-08 12:43:28 -0700361 explicit Location(uintptr_t value) : value_(value) {}
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100362
Ian Rogers13735952014-10-08 12:43:28 -0700363 Location(Kind kind, uintptr_t payload)
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100364 : value_(KindField::Encode(kind) | PayloadField::Encode(payload)) {}
365
Ian Rogers13735952014-10-08 12:43:28 -0700366 uintptr_t GetPayload() const {
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100367 return PayloadField::Decode(value_);
368 }
369
370 typedef BitField<Kind, 0, kBitsForKind> KindField;
Ian Rogers13735952014-10-08 12:43:28 -0700371 typedef BitField<uintptr_t, kBitsForKind, kBitsForPayload> PayloadField;
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100372
373 // Layout for kUnallocated locations payload.
374 typedef BitField<Policy, 0, 3> PolicyField;
375
376 // Layout for stack slots.
377 static const intptr_t kStackIndexBias =
378 static_cast<intptr_t>(1) << (kBitsForPayload - 1);
379
380 // Location either contains kind and payload fields or a tagged handle for
381 // a constant locations. Values of enumeration Kind are selected in such a
382 // way that none of them can be interpreted as a kConstant tag.
Ian Rogers13735952014-10-08 12:43:28 -0700383 uintptr_t value_;
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100384};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700385std::ostream& operator<<(std::ostream& os, const Location::Kind& rhs);
386std::ostream& operator<<(std::ostream& os, const Location::Policy& rhs);
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100387
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100388class RegisterSet : public ValueObject {
389 public:
390 RegisterSet() : core_registers_(0), floating_point_registers_(0) {}
391
392 void Add(Location loc) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100393 if (loc.IsRegister()) {
394 core_registers_ |= (1 << loc.reg());
395 } else {
396 DCHECK(loc.IsFpuRegister());
397 floating_point_registers_ |= (1 << loc.reg());
398 }
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100399 }
400
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100401 void Remove(Location loc) {
402 if (loc.IsRegister()) {
403 core_registers_ &= ~(1 << loc.reg());
404 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000405 DCHECK(loc.IsFpuRegister()) << loc;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100406 floating_point_registers_ &= ~(1 << loc.reg());
407 }
408 }
409
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100410 bool ContainsCoreRegister(uint32_t id) {
411 return Contains(core_registers_, id);
412 }
413
414 bool ContainsFloatingPointRegister(uint32_t id) {
415 return Contains(floating_point_registers_, id);
416 }
417
418 static bool Contains(uint32_t register_set, uint32_t reg) {
419 return (register_set & (1 << reg)) != 0;
420 }
421
Nicolas Geoffray87d03762014-11-19 15:17:56 +0000422 size_t GetNumberOfRegisters() const {
423 return __builtin_popcount(core_registers_) + __builtin_popcount(floating_point_registers_);
424 }
425
Nicolas Geoffray98893962015-01-21 12:32:32 +0000426 uint32_t GetCoreRegisters() const {
427 return core_registers_;
428 }
429
430 uint32_t GetFloatingPointRegisters() const {
431 return floating_point_registers_;
432 }
433
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100434 private:
435 uint32_t core_registers_;
436 uint32_t floating_point_registers_;
437
438 DISALLOW_COPY_AND_ASSIGN(RegisterSet);
439};
440
Andreas Gampe878d58c2015-01-15 23:24:00 -0800441static constexpr bool kIntrinsified = true;
442
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100443/**
444 * The code generator computes LocationSummary for each instruction so that
445 * the instruction itself knows what code to generate: where to find the inputs
446 * and where to place the result.
447 *
448 * The intent is to have the code for generating the instruction independent of
449 * register allocation. A register allocator just has to provide a LocationSummary.
450 */
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700451class LocationSummary : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100452 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100453 enum CallKind {
454 kNoCall,
455 kCallOnSlowPath,
456 kCall
457 };
458
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800459 LocationSummary(HInstruction* instruction,
460 CallKind call_kind = kNoCall,
461 bool intrinsified = false);
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100462
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100463 void SetInAt(uint32_t at, Location location) {
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000464 DCHECK(inputs_.Get(at).IsUnallocated() || inputs_.Get(at).IsInvalid());
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100465 inputs_.Put(at, location);
466 }
467
468 Location InAt(uint32_t at) const {
469 return inputs_.Get(at);
470 }
471
472 size_t GetInputCount() const {
473 return inputs_.Size();
474 }
475
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000476 void SetOut(Location location, Location::OutputOverlap overlaps = Location::kOutputOverlap) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000477 DCHECK(output_.IsInvalid());
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100478 output_overlaps_ = overlaps;
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000479 output_ = location;
480 }
481
482 void UpdateOut(Location location) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000483 // There are two reasons for updating an output:
484 // 1) Parameters, where we only know the exact stack slot after
485 // doing full register allocation.
486 // 2) Unallocated location.
487 DCHECK(output_.IsStackSlot() || output_.IsDoubleStackSlot() || output_.IsUnallocated());
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000488 output_ = location;
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100489 }
490
491 void AddTemp(Location location) {
492 temps_.Add(location);
493 }
494
495 Location GetTemp(uint32_t at) const {
496 return temps_.Get(at);
497 }
498
499 void SetTempAt(uint32_t at, Location location) {
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000500 DCHECK(temps_.Get(at).IsUnallocated() || temps_.Get(at).IsInvalid());
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100501 temps_.Put(at, location);
502 }
503
504 size_t GetTempCount() const {
505 return temps_.Size();
506 }
507
Nicolas Geoffray39468442014-09-02 15:17:15 +0100508 void SetEnvironmentAt(uint32_t at, Location location) {
509 environment_.Put(at, location);
510 }
511
512 Location GetEnvironmentAt(uint32_t at) const {
513 return environment_.Get(at);
514 }
515
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100516 Location Out() const { return output_; }
517
Nicolas Geoffray39468442014-09-02 15:17:15 +0100518 bool CanCall() const { return call_kind_ != kNoCall; }
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100519 bool WillCall() const { return call_kind_ == kCall; }
520 bool OnlyCallsOnSlowPath() const { return call_kind_ == kCallOnSlowPath; }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100521 bool NeedsSafepoint() const { return CanCall(); }
522
523 void SetStackBit(uint32_t index) {
524 stack_mask_->SetBit(index);
525 }
526
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100527 void ClearStackBit(uint32_t index) {
528 stack_mask_->ClearBit(index);
529 }
530
Nicolas Geoffray39468442014-09-02 15:17:15 +0100531 void SetRegisterBit(uint32_t reg_id) {
532 register_mask_ |= (1 << reg_id);
533 }
534
Nicolas Geoffray98893962015-01-21 12:32:32 +0000535 uint32_t GetRegisterMask() const {
536 return register_mask_;
537 }
538
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100539 bool RegisterContainsObject(uint32_t reg_id) {
540 return RegisterSet::Contains(register_mask_, reg_id);
541 }
542
543 void AddLiveRegister(Location location) {
544 live_registers_.Add(location);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100545 }
546
547 BitVector* GetStackMask() const {
548 return stack_mask_;
549 }
550
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100551 RegisterSet* GetLiveRegisters() {
552 return &live_registers_;
553 }
554
Nicolas Geoffray87d03762014-11-19 15:17:56 +0000555 size_t GetNumberOfLiveRegisters() const {
556 return live_registers_.GetNumberOfRegisters();
557 }
558
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000559 bool OutputUsesSameAs(uint32_t input_index) const {
560 return (input_index == 0)
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100561 && output_.IsUnallocated()
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000562 && (output_.GetPolicy() == Location::kSameAsFirstInput);
563 }
564
565 bool IsFixedInput(uint32_t input_index) const {
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000566 Location input = inputs_.Get(input_index);
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000567 return input.IsRegister()
Nicolas Geoffray98893962015-01-21 12:32:32 +0000568 || input.IsFpuRegister()
569 || input.IsPair()
570 || input.IsStackSlot()
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000571 || input.IsDoubleStackSlot();
Nicolas Geoffray76905622014-09-25 14:39:26 +0100572 }
573
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000574 bool OutputCanOverlapWithInputs() const {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000575 return output_overlaps_ == Location::kOutputOverlap;
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100576 }
577
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800578 bool Intrinsified() const {
579 return intrinsified_;
580 }
581
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100582 private:
583 GrowableArray<Location> inputs_;
584 GrowableArray<Location> temps_;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100585 GrowableArray<Location> environment_;
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100586 // Whether the output overlaps with any of the inputs. If it overlaps, then it cannot
587 // share the same register as the inputs.
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000588 Location::OutputOverlap output_overlaps_;
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100589 Location output_;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100590 const CallKind call_kind_;
591
592 // Mask of objects that live in the stack.
593 BitVector* stack_mask_;
594
595 // Mask of objects that live in register.
596 uint32_t register_mask_;
597
598 // Registers that are in use at this position.
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100599 RegisterSet live_registers_;
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100600
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800601 // Whether these are locations for an intrinsified call.
602 const bool intrinsified_;
603
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000604 ART_FRIEND_TEST(RegisterAllocatorTest, ExpectedInRegisterHint);
605 ART_FRIEND_TEST(RegisterAllocatorTest, SameAsFirstInputHint);
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100606 DISALLOW_COPY_AND_ASSIGN(LocationSummary);
607};
608
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100609} // namespace art
610
611#endif // ART_COMPILER_OPTIMIZING_LOCATIONS_H_