| OLD | NEW |
| (Empty) |
| 1 // Copyright 2008 the V8 project authors. All rights reserved. | |
| 2 // Redistribution and use in source and binary forms, with or without | |
| 3 // modification, are permitted provided that the following conditions are | |
| 4 // met: | |
| 5 // | |
| 6 // * Redistributions of source code must retain the above copyright | |
| 7 // notice, this list of conditions and the following disclaimer. | |
| 8 // * Redistributions in binary form must reproduce the above | |
| 9 // copyright notice, this list of conditions and the following | |
| 10 // disclaimer in the documentation and/or other materials provided | |
| 11 // with the distribution. | |
| 12 // * Neither the name of Google Inc. nor the names of its | |
| 13 // contributors may be used to endorse or promote products derived | |
| 14 // from this software without specific prior written permission. | |
| 15 // | |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 27 | |
| 28 #ifndef V8_REGISTER_ALLOCATOR_H_ | |
| 29 #define V8_REGISTER_ALLOCATOR_H_ | |
| 30 | |
| 31 #include "macro-assembler.h" | |
| 32 #include "type-info.h" | |
| 33 | |
| 34 #if V8_TARGET_ARCH_IA32 | |
| 35 #include "ia32/register-allocator-ia32.h" | |
| 36 #elif V8_TARGET_ARCH_X64 | |
| 37 #include "x64/register-allocator-x64.h" | |
| 38 #elif V8_TARGET_ARCH_ARM | |
| 39 #include "arm/register-allocator-arm.h" | |
| 40 #elif V8_TARGET_ARCH_MIPS | |
| 41 #include "mips/register-allocator-mips.h" | |
| 42 #else | |
| 43 #error Unsupported target architecture. | |
| 44 #endif | |
| 45 | |
| 46 namespace v8 { | |
| 47 namespace internal { | |
| 48 | |
| 49 | |
| 50 // ------------------------------------------------------------------------- | |
| 51 // Results | |
| 52 // | |
| 53 // Results encapsulate the compile-time values manipulated by the code | |
| 54 // generator. They can represent registers or constants. | |
| 55 | |
| 56 class Result BASE_EMBEDDED { | |
| 57 public: | |
| 58 enum Type { | |
| 59 INVALID, | |
| 60 REGISTER, | |
| 61 CONSTANT | |
| 62 }; | |
| 63 | |
| 64 // Construct an invalid result. | |
| 65 Result() { invalidate(); } | |
| 66 | |
| 67 // Construct a register Result. | |
| 68 explicit Result(Register reg, TypeInfo info = TypeInfo::Unknown()); | |
| 69 | |
| 70 // Construct a Result whose value is a compile-time constant. | |
| 71 explicit Result(Handle<Object> value) { | |
| 72 ZoneObjectList* constant_list = Isolate::Current()->result_constant_list(); | |
| 73 TypeInfo info = TypeInfo::TypeFromValue(value); | |
| 74 value_ = TypeField::encode(CONSTANT) | |
| 75 | TypeInfoField::encode(info.ToInt()) | |
| 76 | IsUntaggedInt32Field::encode(false) | |
| 77 | DataField::encode(constant_list->length()); | |
| 78 constant_list->Add(value); | |
| 79 } | |
| 80 | |
| 81 // The copy constructor and assignment operators could each create a new | |
| 82 // register reference. | |
| 83 inline Result(const Result& other); | |
| 84 | |
| 85 inline Result& operator=(const Result& other); | |
| 86 | |
| 87 inline ~Result(); | |
| 88 | |
| 89 inline void Unuse(); | |
| 90 | |
| 91 Type type() const { return TypeField::decode(value_); } | |
| 92 | |
| 93 void invalidate() { value_ = TypeField::encode(INVALID); } | |
| 94 | |
| 95 inline TypeInfo type_info() const; | |
| 96 inline void set_type_info(TypeInfo info); | |
| 97 inline bool is_number() const; | |
| 98 inline bool is_smi() const; | |
| 99 inline bool is_integer32() const; | |
| 100 inline bool is_double() const; | |
| 101 | |
| 102 bool is_valid() const { return type() != INVALID; } | |
| 103 bool is_register() const { return type() == REGISTER; } | |
| 104 bool is_constant() const { return type() == CONSTANT; } | |
| 105 | |
| 106 // An untagged int32 Result contains a signed int32 in a register | |
| 107 // or as a constant. These are only allowed in a side-effect-free | |
| 108 // int32 calculation, and if a non-int32 input shows up or an overflow | |
| 109 // occurs, we bail out and drop all the int32 values. Constants are | |
| 110 // not converted to int32 until they are loaded into a register. | |
| 111 bool is_untagged_int32() const { | |
| 112 return IsUntaggedInt32Field::decode(value_); | |
| 113 } | |
| 114 void set_untagged_int32(bool value) { | |
| 115 value_ &= ~IsUntaggedInt32Field::mask(); | |
| 116 value_ |= IsUntaggedInt32Field::encode(value); | |
| 117 } | |
| 118 | |
| 119 Register reg() const { | |
| 120 ASSERT(is_register()); | |
| 121 uint32_t reg = DataField::decode(value_); | |
| 122 Register result; | |
| 123 result.code_ = reg; | |
| 124 return result; | |
| 125 } | |
| 126 | |
| 127 Handle<Object> handle() const { | |
| 128 ASSERT(type() == CONSTANT); | |
| 129 return Isolate::Current()->result_constant_list()-> | |
| 130 at(DataField::decode(value_)); | |
| 131 } | |
| 132 | |
| 133 // Move this result to an arbitrary register. The register is not | |
| 134 // necessarily spilled from the frame or even singly-referenced outside | |
| 135 // it. | |
| 136 void ToRegister(); | |
| 137 | |
| 138 // Move this result to a specified register. The register is spilled from | |
| 139 // the frame, and the register is singly-referenced (by this result) | |
| 140 // outside the frame. | |
| 141 void ToRegister(Register reg); | |
| 142 | |
| 143 private: | |
| 144 uint32_t value_; | |
| 145 | |
| 146 // Declare BitFields with template parameters <type, start, size>. | |
| 147 class TypeField: public BitField<Type, 0, 2> {}; | |
| 148 class TypeInfoField : public BitField<int, 2, 6> {}; | |
| 149 class IsUntaggedInt32Field : public BitField<bool, 8, 1> {}; | |
| 150 class DataField: public BitField<uint32_t, 9, 32 - 9> {}; | |
| 151 | |
| 152 inline void CopyTo(Result* destination) const; | |
| 153 | |
| 154 friend class CodeGeneratorScope; | |
| 155 }; | |
| 156 | |
| 157 | |
| 158 // ------------------------------------------------------------------------- | |
| 159 // Register file | |
| 160 // | |
| 161 // The register file tracks reference counts for the processor registers. | |
| 162 // It is used by both the register allocator and the virtual frame. | |
| 163 | |
| 164 class RegisterFile BASE_EMBEDDED { | |
| 165 public: | |
| 166 RegisterFile() { Reset(); } | |
| 167 | |
| 168 void Reset() { | |
| 169 for (int i = 0; i < kNumRegisters; i++) { | |
| 170 ref_counts_[i] = 0; | |
| 171 } | |
| 172 } | |
| 173 | |
| 174 // Predicates and accessors for the reference counts. | |
| 175 bool is_used(int num) { | |
| 176 ASSERT(0 <= num && num < kNumRegisters); | |
| 177 return ref_counts_[num] > 0; | |
| 178 } | |
| 179 | |
| 180 int count(int num) { | |
| 181 ASSERT(0 <= num && num < kNumRegisters); | |
| 182 return ref_counts_[num]; | |
| 183 } | |
| 184 | |
| 185 // Record a use of a register by incrementing its reference count. | |
| 186 void Use(int num) { | |
| 187 ASSERT(0 <= num && num < kNumRegisters); | |
| 188 ref_counts_[num]++; | |
| 189 } | |
| 190 | |
| 191 // Record that a register will no longer be used by decrementing its | |
| 192 // reference count. | |
| 193 void Unuse(int num) { | |
| 194 ASSERT(is_used(num)); | |
| 195 ref_counts_[num]--; | |
| 196 } | |
| 197 | |
| 198 // Copy the reference counts from this register file to the other. | |
| 199 void CopyTo(RegisterFile* other) { | |
| 200 for (int i = 0; i < kNumRegisters; i++) { | |
| 201 other->ref_counts_[i] = ref_counts_[i]; | |
| 202 } | |
| 203 } | |
| 204 | |
| 205 private: | |
| 206 // C++ doesn't like zero length arrays, so we make the array length 1 even if | |
| 207 // we don't need it. | |
| 208 static const int kNumRegisters = | |
| 209 (RegisterAllocatorConstants::kNumRegisters == 0) ? | |
| 210 1 : RegisterAllocatorConstants::kNumRegisters; | |
| 211 | |
| 212 int ref_counts_[kNumRegisters]; | |
| 213 | |
| 214 // Very fast inlined loop to find a free register. Used in | |
| 215 // RegisterAllocator::AllocateWithoutSpilling. Returns | |
| 216 // kInvalidRegister if no free register found. | |
| 217 int ScanForFreeRegister() { | |
| 218 for (int i = 0; i < RegisterAllocatorConstants::kNumRegisters; i++) { | |
| 219 if (!is_used(i)) return i; | |
| 220 } | |
| 221 return RegisterAllocatorConstants::kInvalidRegister; | |
| 222 } | |
| 223 | |
| 224 friend class RegisterAllocator; | |
| 225 }; | |
| 226 | |
| 227 | |
| 228 // ------------------------------------------------------------------------- | |
| 229 // Register allocator | |
| 230 // | |
| 231 | |
| 232 class RegisterAllocator BASE_EMBEDDED { | |
| 233 public: | |
| 234 static const int kNumRegisters = | |
| 235 RegisterAllocatorConstants::kNumRegisters; | |
| 236 static const int kInvalidRegister = | |
| 237 RegisterAllocatorConstants::kInvalidRegister; | |
| 238 | |
| 239 explicit RegisterAllocator(CodeGenerator* cgen) : cgen_(cgen) {} | |
| 240 | |
| 241 // True if the register is reserved by the code generator, false if it | |
| 242 // can be freely used by the allocator Defined in the | |
| 243 // platform-specific XXX-inl.h files.. | |
| 244 static inline bool IsReserved(Register reg); | |
| 245 | |
| 246 // Convert between (unreserved) assembler registers and allocator | |
| 247 // numbers. Defined in the platform-specific XXX-inl.h files. | |
| 248 static inline int ToNumber(Register reg); | |
| 249 static inline Register ToRegister(int num); | |
| 250 | |
| 251 // Predicates and accessors for the registers' reference counts. | |
| 252 bool is_used(int num) { return registers_.is_used(num); } | |
| 253 inline bool is_used(Register reg); | |
| 254 | |
| 255 int count(int num) { return registers_.count(num); } | |
| 256 inline int count(Register reg); | |
| 257 | |
| 258 // Explicitly record a reference to a register. | |
| 259 void Use(int num) { registers_.Use(num); } | |
| 260 inline void Use(Register reg); | |
| 261 | |
| 262 // Explicitly record that a register will no longer be used. | |
| 263 void Unuse(int num) { registers_.Unuse(num); } | |
| 264 inline void Unuse(Register reg); | |
| 265 | |
| 266 // Reset the register reference counts to free all non-reserved registers. | |
| 267 void Reset() { registers_.Reset(); } | |
| 268 | |
| 269 // Initialize the register allocator for entry to a JS function. On | |
| 270 // entry, the (non-reserved) registers used by the JS calling | |
| 271 // convention are referenced and the other (non-reserved) registers | |
| 272 // are free. | |
| 273 inline void Initialize(); | |
| 274 | |
| 275 // Allocate a free register and return a register result if possible or | |
| 276 // fail and return an invalid result. | |
| 277 Result Allocate(); | |
| 278 | |
| 279 // Allocate a specific register if possible, spilling it from the | |
| 280 // current frame if necessary, or else fail and return an invalid | |
| 281 // result. | |
| 282 Result Allocate(Register target); | |
| 283 | |
| 284 // Allocate a free register without spilling any from the current | |
| 285 // frame or fail and return an invalid result. | |
| 286 Result AllocateWithoutSpilling(); | |
| 287 | |
| 288 // Allocate a free byte register without spilling any from the current | |
| 289 // frame or fail and return an invalid result. | |
| 290 Result AllocateByteRegisterWithoutSpilling(); | |
| 291 | |
| 292 // Copy the internal state to a register file, to be restored later by | |
| 293 // RestoreFrom. | |
| 294 void SaveTo(RegisterFile* register_file) { | |
| 295 registers_.CopyTo(register_file); | |
| 296 } | |
| 297 | |
| 298 // Restore the internal state. | |
| 299 void RestoreFrom(RegisterFile* register_file) { | |
| 300 register_file->CopyTo(®isters_); | |
| 301 } | |
| 302 | |
| 303 private: | |
| 304 CodeGenerator* cgen_; | |
| 305 RegisterFile registers_; | |
| 306 }; | |
| 307 | |
| 308 } } // namespace v8::internal | |
| 309 | |
| 310 #endif // V8_REGISTER_ALLOCATOR_H_ | |
| OLD | NEW |