blob: 5f85b6a0e15cd047862620407d2b7ef3334c9141 [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"
Nicolas Geoffray76716a62014-05-23 10:14:19 +010022#include "utils/allocation.h"
23#include "utils/growable_array.h"
24#include "utils/managed_register.h"
25
26namespace art {
27
Nicolas Geoffray96f89a22014-07-11 10:57:49 +010028class HConstant;
Nicolas Geoffray76716a62014-05-23 10:14:19 +010029class HInstruction;
30
31/**
32 * A Location is an abstraction over the potential location
33 * of an instruction. It could be in register or stack.
34 */
35class Location : public ValueObject {
36 public:
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +010037 static constexpr bool kDiesAtEntry = true;
38
Nicolas Geoffray76716a62014-05-23 10:14:19 +010039 enum Kind {
40 kInvalid = 0,
Nicolas Geoffray96f89a22014-07-11 10:57:49 +010041 kConstant = 1,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010042 kStackSlot = 2, // 32bit stack slot.
Nicolas Geoffray96f89a22014-07-11 10:57:49 +010043 kDoubleStackSlot = 3, // 64bit stack slot.
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010044
45 kRegister = 4, // Core register.
46
47 // We do not use the value 5 because it conflicts with kLocationConstantMask.
48 kDoNotUse = 5,
49
50 kFpuRegister = 6, // Floating point processor.
51
Nicolas Geoffray76716a62014-05-23 10:14:19 +010052 // On 32bits architectures, quick can pass a long where the
53 // low bits are in the last parameter register, and the high
54 // bits are in a stack slot. The kQuickParameter kind is for
55 // handling this special case.
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010056 kQuickParameter = 7,
Nicolas Geoffray76716a62014-05-23 10:14:19 +010057
58 // Unallocated location represents a location that is not fixed and can be
59 // allocated by a register allocator. Each unallocated location has
60 // a policy that specifies what kind of location is suitable. Payload
61 // contains register allocation policy.
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010062 kUnallocated = 8,
Nicolas Geoffray76716a62014-05-23 10:14:19 +010063 };
64
65 Location() : value_(kInvalid) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010066 // Verify that non-constant location kinds do not interfere with kConstant.
67 COMPILE_ASSERT((kInvalid & kLocationConstantMask) != kConstant, TagError);
68 COMPILE_ASSERT((kUnallocated & kLocationConstantMask) != kConstant, TagError);
69 COMPILE_ASSERT((kStackSlot & kLocationConstantMask) != kConstant, TagError);
70 COMPILE_ASSERT((kDoubleStackSlot & kLocationConstantMask) != kConstant, TagError);
71 COMPILE_ASSERT((kRegister & kLocationConstantMask) != kConstant, TagError);
72 COMPILE_ASSERT((kQuickParameter & kLocationConstantMask) != kConstant, TagError);
73 COMPILE_ASSERT((kFpuRegister & kLocationConstantMask) != kConstant, TagError);
74 COMPILE_ASSERT((kConstant & kLocationConstantMask) == kConstant, TagError);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +010075
Nicolas Geoffray76716a62014-05-23 10:14:19 +010076 DCHECK(!IsValid());
77 }
78
79 Location(const Location& other) : ValueObject(), value_(other.value_) {}
80
81 Location& operator=(const Location& other) {
82 value_ = other.value_;
83 return *this;
84 }
85
Nicolas Geoffray96f89a22014-07-11 10:57:49 +010086 bool IsConstant() const {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010087 return (value_ & kLocationConstantMask) == kConstant;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +010088 }
89
90 static Location ConstantLocation(HConstant* constant) {
91 DCHECK(constant != nullptr);
92 return Location(kConstant | reinterpret_cast<uword>(constant));
93 }
94
95 HConstant* GetConstant() const {
96 DCHECK(IsConstant());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010097 return reinterpret_cast<HConstant*>(value_ & ~kLocationConstantMask);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +010098 }
99
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100100 bool IsValid() const {
101 return value_ != kInvalid;
102 }
103
104 bool IsInvalid() const {
105 return !IsValid();
106 }
107
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100108 // Empty location. Used if there the location should be ignored.
109 static Location NoLocation() {
110 return Location();
111 }
112
113 // Register locations.
114 static Location RegisterLocation(ManagedRegister reg) {
115 return Location(kRegister, reg.RegId());
116 }
117
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100118 static Location FpuRegisterLocation(ManagedRegister reg) {
119 return Location(kFpuRegister, reg.RegId());
120 }
121
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100122 bool IsRegister() const {
123 return GetKind() == kRegister;
124 }
125
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100126 bool IsFpuRegister() const {
127 return GetKind() == kFpuRegister;
128 }
129
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100130 ManagedRegister reg() const {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100131 DCHECK(IsRegister() || IsFpuRegister());
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100132 return static_cast<ManagedRegister>(GetPayload());
133 }
134
135 static uword EncodeStackIndex(intptr_t stack_index) {
136 DCHECK(-kStackIndexBias <= stack_index);
137 DCHECK(stack_index < kStackIndexBias);
138 return static_cast<uword>(kStackIndexBias + stack_index);
139 }
140
141 static Location StackSlot(intptr_t stack_index) {
142 uword payload = EncodeStackIndex(stack_index);
143 Location loc(kStackSlot, payload);
144 // Ensure that sign is preserved.
145 DCHECK_EQ(loc.GetStackIndex(), stack_index);
146 return loc;
147 }
148
149 bool IsStackSlot() const {
150 return GetKind() == kStackSlot;
151 }
152
153 static Location DoubleStackSlot(intptr_t stack_index) {
154 uword payload = EncodeStackIndex(stack_index);
155 Location loc(kDoubleStackSlot, payload);
156 // Ensure that sign is preserved.
157 DCHECK_EQ(loc.GetStackIndex(), stack_index);
158 return loc;
159 }
160
161 bool IsDoubleStackSlot() const {
162 return GetKind() == kDoubleStackSlot;
163 }
164
165 intptr_t GetStackIndex() const {
166 DCHECK(IsStackSlot() || IsDoubleStackSlot());
167 // Decode stack index manually to preserve sign.
168 return GetPayload() - kStackIndexBias;
169 }
170
171 intptr_t GetHighStackIndex(uintptr_t word_size) const {
172 DCHECK(IsDoubleStackSlot());
173 // Decode stack index manually to preserve sign.
174 return GetPayload() - kStackIndexBias + word_size;
175 }
176
177 static Location QuickParameter(uint32_t parameter_index) {
178 return Location(kQuickParameter, parameter_index);
179 }
180
181 uint32_t GetQuickParameterIndex() const {
182 DCHECK(IsQuickParameter());
183 return GetPayload();
184 }
185
186 bool IsQuickParameter() const {
187 return GetKind() == kQuickParameter;
188 }
189
190 arm::ArmManagedRegister AsArm() const;
191 x86::X86ManagedRegister AsX86() const;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100192 x86_64::X86_64ManagedRegister AsX86_64() const;
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100193
194 Kind GetKind() const {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100195 return IsConstant() ? kConstant : KindField::Decode(value_);
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100196 }
197
198 bool Equals(Location other) const {
199 return value_ == other.value_;
200 }
201
202 const char* DebugString() const {
203 switch (GetKind()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100204 case kInvalid: return "I";
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100205 case kRegister: return "R";
206 case kStackSlot: return "S";
207 case kDoubleStackSlot: return "DS";
208 case kQuickParameter: return "Q";
209 case kUnallocated: return "U";
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100210 case kConstant: return "C";
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100211 case kFpuRegister: return "F";
212 case kDoNotUse:
213 LOG(FATAL) << "Should not use this location kind";
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100214 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100215 UNREACHABLE();
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100216 return "?";
217 }
218
219 // Unallocated locations.
220 enum Policy {
221 kAny,
222 kRequiresRegister,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100223 kRequiresFpuRegister,
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100224 kSameAsFirstInput,
225 };
226
227 bool IsUnallocated() const {
228 return GetKind() == kUnallocated;
229 }
230
231 static Location UnallocatedLocation(Policy policy) {
232 return Location(kUnallocated, PolicyField::Encode(policy));
233 }
234
235 // Any free register is suitable to replace this unallocated location.
236 static Location Any() {
237 return UnallocatedLocation(kAny);
238 }
239
240 static Location RequiresRegister() {
241 return UnallocatedLocation(kRequiresRegister);
242 }
243
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100244 static Location RequiresFpuRegister() {
245 return UnallocatedLocation(kRequiresFpuRegister);
246 }
247
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100248 static Location RegisterOrConstant(HInstruction* instruction);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +0100249 static Location ByteRegisterOrConstant(ManagedRegister reg, HInstruction* instruction);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100250
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100251 // The location of the first input to the instruction will be
252 // used to replace this unallocated location.
253 static Location SameAsFirstInput() {
254 return UnallocatedLocation(kSameAsFirstInput);
255 }
256
257 Policy GetPolicy() const {
258 DCHECK(IsUnallocated());
259 return PolicyField::Decode(GetPayload());
260 }
261
262 uword GetEncoding() const {
263 return GetPayload();
264 }
265
266 private:
267 // Number of bits required to encode Kind value.
268 static constexpr uint32_t kBitsForKind = 4;
269 static constexpr uint32_t kBitsForPayload = kWordSize * kBitsPerByte - kBitsForKind;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100270 static constexpr uword kLocationConstantMask = 0x3;
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100271
272 explicit Location(uword value) : value_(value) {}
273
274 Location(Kind kind, uword payload)
275 : value_(KindField::Encode(kind) | PayloadField::Encode(payload)) {}
276
277 uword GetPayload() const {
278 return PayloadField::Decode(value_);
279 }
280
281 typedef BitField<Kind, 0, kBitsForKind> KindField;
282 typedef BitField<uword, kBitsForKind, kBitsForPayload> PayloadField;
283
284 // Layout for kUnallocated locations payload.
285 typedef BitField<Policy, 0, 3> PolicyField;
286
287 // Layout for stack slots.
288 static const intptr_t kStackIndexBias =
289 static_cast<intptr_t>(1) << (kBitsForPayload - 1);
290
291 // Location either contains kind and payload fields or a tagged handle for
292 // a constant locations. Values of enumeration Kind are selected in such a
293 // way that none of them can be interpreted as a kConstant tag.
294 uword value_;
295};
296
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100297class RegisterSet : public ValueObject {
298 public:
299 RegisterSet() : core_registers_(0), floating_point_registers_(0) {}
300
301 void Add(Location loc) {
302 // TODO: floating point registers.
303 core_registers_ |= (1 << loc.reg().RegId());
304 }
305
306 bool ContainsCoreRegister(uint32_t id) {
307 return Contains(core_registers_, id);
308 }
309
310 bool ContainsFloatingPointRegister(uint32_t id) {
311 return Contains(floating_point_registers_, id);
312 }
313
314 static bool Contains(uint32_t register_set, uint32_t reg) {
315 return (register_set & (1 << reg)) != 0;
316 }
317
318 private:
319 uint32_t core_registers_;
320 uint32_t floating_point_registers_;
321
322 DISALLOW_COPY_AND_ASSIGN(RegisterSet);
323};
324
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100325/**
326 * The code generator computes LocationSummary for each instruction so that
327 * the instruction itself knows what code to generate: where to find the inputs
328 * and where to place the result.
329 *
330 * The intent is to have the code for generating the instruction independent of
331 * register allocation. A register allocator just has to provide a LocationSummary.
332 */
333class LocationSummary : public ArenaObject {
334 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100335 enum CallKind {
336 kNoCall,
337 kCallOnSlowPath,
338 kCall
339 };
340
Roland Levillain5799fc02014-09-25 12:15:20 +0100341 LocationSummary(HInstruction* instruction, CallKind call_kind = kNoCall);
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100342
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +0100343 void SetInAt(uint32_t at, Location location, bool dies_at_entry = false) {
344 dies_at_entry_.Put(at, dies_at_entry);
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100345 inputs_.Put(at, location);
346 }
347
348 Location InAt(uint32_t at) const {
349 return inputs_.Get(at);
350 }
351
352 size_t GetInputCount() const {
353 return inputs_.Size();
354 }
355
356 void SetOut(Location location) {
357 output_ = Location(location);
358 }
359
360 void AddTemp(Location location) {
361 temps_.Add(location);
362 }
363
364 Location GetTemp(uint32_t at) const {
365 return temps_.Get(at);
366 }
367
368 void SetTempAt(uint32_t at, Location location) {
369 temps_.Put(at, location);
370 }
371
372 size_t GetTempCount() const {
373 return temps_.Size();
374 }
375
Nicolas Geoffray39468442014-09-02 15:17:15 +0100376 void SetEnvironmentAt(uint32_t at, Location location) {
377 environment_.Put(at, location);
378 }
379
380 Location GetEnvironmentAt(uint32_t at) const {
381 return environment_.Get(at);
382 }
383
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100384 Location Out() const { return output_; }
385
Nicolas Geoffray39468442014-09-02 15:17:15 +0100386 bool CanCall() const { return call_kind_ != kNoCall; }
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100387 bool WillCall() const { return call_kind_ == kCall; }
388 bool OnlyCallsOnSlowPath() const { return call_kind_ == kCallOnSlowPath; }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100389 bool NeedsSafepoint() const { return CanCall(); }
390
391 void SetStackBit(uint32_t index) {
392 stack_mask_->SetBit(index);
393 }
394
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100395 void ClearStackBit(uint32_t index) {
396 stack_mask_->ClearBit(index);
397 }
398
Nicolas Geoffray39468442014-09-02 15:17:15 +0100399 void SetRegisterBit(uint32_t reg_id) {
400 register_mask_ |= (1 << reg_id);
401 }
402
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100403 bool RegisterContainsObject(uint32_t reg_id) {
404 return RegisterSet::Contains(register_mask_, reg_id);
405 }
406
407 void AddLiveRegister(Location location) {
408 live_registers_.Add(location);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100409 }
410
411 BitVector* GetStackMask() const {
412 return stack_mask_;
413 }
414
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100415 RegisterSet* GetLiveRegisters() {
416 return &live_registers_;
417 }
418
Nicolas Geoffray76905622014-09-25 14:39:26 +0100419 bool InputOverlapsWithOutputOrTemp(uint32_t input, bool is_environment) const {
420 if (is_environment) return true;
421 Location location = Out();
Nicolas Geoffray76905622014-09-25 14:39:26 +0100422 if (input == 0 && location.IsUnallocated() && location.GetPolicy() == Location::kSameAsFirstInput) {
423 return false;
424 }
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +0100425 if (dies_at_entry_.Get(input)) {
426 return false;
427 }
Nicolas Geoffray76905622014-09-25 14:39:26 +0100428 return true;
429 }
430
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100431 private:
432 GrowableArray<Location> inputs_;
433 GrowableArray<Location> temps_;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100434 GrowableArray<Location> environment_;
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +0100435 GrowableArray<bool> dies_at_entry_;
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100436 Location output_;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100437 const CallKind call_kind_;
438
439 // Mask of objects that live in the stack.
440 BitVector* stack_mask_;
441
442 // Mask of objects that live in register.
443 uint32_t register_mask_;
444
445 // Registers that are in use at this position.
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100446 RegisterSet live_registers_;
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100447
448 DISALLOW_COPY_AND_ASSIGN(LocationSummary);
449};
450
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +0100451std::ostream& operator<<(std::ostream& os, const Location& location);
452
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100453} // namespace art
454
455#endif // ART_COMPILER_OPTIMIZING_LOCATIONS_H_