Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1 | // Copyright 2014 the V8 project authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef V8_COMPILER_INSTRUCTION_H_ |
| 6 | #define V8_COMPILER_INSTRUCTION_H_ |
| 7 | |
| 8 | #include <deque> |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9 | #include <iosfwd> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10 | #include <map> |
| 11 | #include <set> |
| 12 | |
| 13 | #include "src/compiler/common-operator.h" |
| 14 | #include "src/compiler/frame.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15 | #include "src/compiler/instruction-codes.h" |
| 16 | #include "src/compiler/opcodes.h" |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 17 | #include "src/compiler/source-position.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18 | #include "src/macro-assembler.h" |
| 19 | #include "src/register-configuration.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 20 | #include "src/zone-allocator.h" |
| 21 | |
| 22 | namespace v8 { |
| 23 | namespace internal { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 24 | namespace compiler { |
| 25 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 26 | // Forward declarations. |
| 27 | class Schedule; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 28 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 29 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 30 | class InstructionOperand { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 31 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 32 | static const int kInvalidVirtualRegister = -1; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 33 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 34 | // TODO(dcarney): recover bit. INVALID can be represented as UNALLOCATED with |
| 35 | // kInvalidVirtualRegister and some DCHECKS. |
| 36 | enum Kind { INVALID, UNALLOCATED, CONSTANT, IMMEDIATE, EXPLICIT, ALLOCATED }; |
| 37 | |
| 38 | InstructionOperand() : InstructionOperand(INVALID) {} |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 39 | |
| 40 | Kind kind() const { return KindField::decode(value_); } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 41 | |
| 42 | #define INSTRUCTION_OPERAND_PREDICATE(name, type) \ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 43 | bool Is##name() const { return kind() == type; } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 44 | INSTRUCTION_OPERAND_PREDICATE(Invalid, INVALID) |
| 45 | // UnallocatedOperands are place-holder operands created before register |
| 46 | // allocation. They later are assigned registers and become AllocatedOperands. |
| 47 | INSTRUCTION_OPERAND_PREDICATE(Unallocated, UNALLOCATED) |
| 48 | // Constant operands participate in register allocation. They are allocated to |
| 49 | // registers but have a special "spilling" behavior. When a ConstantOperand |
| 50 | // value must be rematerialized, it is loaded from an immediate constant |
| 51 | // rather from an unspilled slot. |
| 52 | INSTRUCTION_OPERAND_PREDICATE(Constant, CONSTANT) |
| 53 | // ImmediateOperands do not participate in register allocation and are only |
| 54 | // embedded directly in instructions, e.g. small integers and on some |
| 55 | // platforms Objects. |
| 56 | INSTRUCTION_OPERAND_PREDICATE(Immediate, IMMEDIATE) |
| 57 | // ExplicitOperands do not participate in register allocation. They are |
| 58 | // created by the instruction selector for direct access to registers and |
| 59 | // stack slots, completely bypassing the register allocator. They are never |
| 60 | // associated with a virtual register |
| 61 | INSTRUCTION_OPERAND_PREDICATE(Explicit, EXPLICIT) |
| 62 | // AllocatedOperands are registers or stack slots that are assigned by the |
| 63 | // register allocator and are always associated with a virtual register. |
| 64 | INSTRUCTION_OPERAND_PREDICATE(Allocated, ALLOCATED) |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 65 | #undef INSTRUCTION_OPERAND_PREDICATE |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 66 | |
| 67 | inline bool IsAnyRegister() const; |
| 68 | inline bool IsRegister() const; |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 69 | inline bool IsFPRegister() const; |
| 70 | inline bool IsFloatRegister() const; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 71 | inline bool IsDoubleRegister() const; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 72 | inline bool IsSimd128Register() const; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 73 | inline bool IsStackSlot() const; |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 74 | inline bool IsFPStackSlot() const; |
| 75 | inline bool IsFloatStackSlot() const; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 76 | inline bool IsDoubleStackSlot() const; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 77 | inline bool IsSimd128StackSlot() const; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 78 | |
| 79 | template <typename SubKindOperand> |
| 80 | static SubKindOperand* New(Zone* zone, const SubKindOperand& op) { |
| 81 | void* buffer = zone->New(sizeof(op)); |
| 82 | return new (buffer) SubKindOperand(op); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 85 | static void ReplaceWith(InstructionOperand* dest, |
| 86 | const InstructionOperand* src) { |
| 87 | *dest = *src; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 90 | bool Equals(const InstructionOperand& that) const { |
| 91 | return this->value_ == that.value_; |
| 92 | } |
| 93 | |
| 94 | bool Compare(const InstructionOperand& that) const { |
| 95 | return this->value_ < that.value_; |
| 96 | } |
| 97 | |
| 98 | bool EqualsCanonicalized(const InstructionOperand& that) const { |
| 99 | return this->GetCanonicalizedValue() == that.GetCanonicalizedValue(); |
| 100 | } |
| 101 | |
| 102 | bool CompareCanonicalized(const InstructionOperand& that) const { |
| 103 | return this->GetCanonicalizedValue() < that.GetCanonicalizedValue(); |
| 104 | } |
| 105 | |
| 106 | void Print(const RegisterConfiguration* config) const; |
| 107 | void Print() const; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 108 | |
| 109 | protected: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 110 | explicit InstructionOperand(Kind kind) : value_(KindField::encode(kind)) {} |
| 111 | |
| 112 | inline uint64_t GetCanonicalizedValue() const; |
| 113 | |
| 114 | class KindField : public BitField64<Kind, 0, 3> {}; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 115 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 116 | uint64_t value_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 117 | }; |
| 118 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 119 | |
| 120 | typedef ZoneVector<InstructionOperand> InstructionOperandVector; |
| 121 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 122 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 123 | struct PrintableInstructionOperand { |
| 124 | const RegisterConfiguration* register_configuration_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 125 | InstructionOperand op_; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 126 | }; |
| 127 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 128 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 129 | std::ostream& operator<<(std::ostream& os, |
| 130 | const PrintableInstructionOperand& op); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 131 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 132 | |
| 133 | #define INSTRUCTION_OPERAND_CASTS(OperandType, OperandKind) \ |
| 134 | \ |
| 135 | static OperandType* cast(InstructionOperand* op) { \ |
| 136 | DCHECK_EQ(OperandKind, op->kind()); \ |
| 137 | return static_cast<OperandType*>(op); \ |
| 138 | } \ |
| 139 | \ |
| 140 | static const OperandType* cast(const InstructionOperand* op) { \ |
| 141 | DCHECK_EQ(OperandKind, op->kind()); \ |
| 142 | return static_cast<const OperandType*>(op); \ |
| 143 | } \ |
| 144 | \ |
| 145 | static OperandType cast(const InstructionOperand& op) { \ |
| 146 | DCHECK_EQ(OperandKind, op.kind()); \ |
| 147 | return *static_cast<const OperandType*>(&op); \ |
| 148 | } |
| 149 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 150 | class UnallocatedOperand : public InstructionOperand { |
| 151 | public: |
| 152 | enum BasicPolicy { FIXED_SLOT, EXTENDED_POLICY }; |
| 153 | |
| 154 | enum ExtendedPolicy { |
| 155 | NONE, |
| 156 | ANY, |
| 157 | FIXED_REGISTER, |
| 158 | FIXED_DOUBLE_REGISTER, |
| 159 | MUST_HAVE_REGISTER, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 160 | MUST_HAVE_SLOT, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 161 | SAME_AS_FIRST_INPUT |
| 162 | }; |
| 163 | |
| 164 | // Lifetime of operand inside the instruction. |
| 165 | enum Lifetime { |
| 166 | // USED_AT_START operand is guaranteed to be live only at |
| 167 | // instruction start. Register allocator is free to assign the same register |
| 168 | // to some other operand used inside instruction (i.e. temporary or |
| 169 | // output). |
| 170 | USED_AT_START, |
| 171 | |
| 172 | // USED_AT_END operand is treated as live until the end of |
| 173 | // instruction. This means that register allocator will not reuse it's |
| 174 | // register for any other operand inside instruction. |
| 175 | USED_AT_END |
| 176 | }; |
| 177 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 178 | UnallocatedOperand(ExtendedPolicy policy, int virtual_register) |
| 179 | : UnallocatedOperand(virtual_register) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 180 | value_ |= BasicPolicyField::encode(EXTENDED_POLICY); |
| 181 | value_ |= ExtendedPolicyField::encode(policy); |
| 182 | value_ |= LifetimeField::encode(USED_AT_END); |
| 183 | } |
| 184 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 185 | UnallocatedOperand(BasicPolicy policy, int index, int virtual_register) |
| 186 | : UnallocatedOperand(virtual_register) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 187 | DCHECK(policy == FIXED_SLOT); |
| 188 | value_ |= BasicPolicyField::encode(policy); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 189 | value_ |= static_cast<int64_t>(index) << FixedSlotIndexField::kShift; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 190 | DCHECK(this->fixed_slot_index() == index); |
| 191 | } |
| 192 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 193 | UnallocatedOperand(ExtendedPolicy policy, int index, int virtual_register) |
| 194 | : UnallocatedOperand(virtual_register) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 195 | DCHECK(policy == FIXED_REGISTER || policy == FIXED_DOUBLE_REGISTER); |
| 196 | value_ |= BasicPolicyField::encode(EXTENDED_POLICY); |
| 197 | value_ |= ExtendedPolicyField::encode(policy); |
| 198 | value_ |= LifetimeField::encode(USED_AT_END); |
| 199 | value_ |= FixedRegisterField::encode(index); |
| 200 | } |
| 201 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 202 | UnallocatedOperand(ExtendedPolicy policy, Lifetime lifetime, |
| 203 | int virtual_register) |
| 204 | : UnallocatedOperand(virtual_register) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 205 | value_ |= BasicPolicyField::encode(EXTENDED_POLICY); |
| 206 | value_ |= ExtendedPolicyField::encode(policy); |
| 207 | value_ |= LifetimeField::encode(lifetime); |
| 208 | } |
| 209 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 210 | UnallocatedOperand(int reg_id, int slot_id, int virtual_register) |
| 211 | : UnallocatedOperand(FIXED_REGISTER, reg_id, virtual_register) { |
| 212 | value_ |= HasSecondaryStorageField::encode(true); |
| 213 | value_ |= SecondaryStorageField::encode(slot_id); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 216 | // Predicates for the operand policy. |
| 217 | bool HasAnyPolicy() const { |
| 218 | return basic_policy() == EXTENDED_POLICY && extended_policy() == ANY; |
| 219 | } |
| 220 | bool HasFixedPolicy() const { |
| 221 | return basic_policy() == FIXED_SLOT || |
| 222 | extended_policy() == FIXED_REGISTER || |
| 223 | extended_policy() == FIXED_DOUBLE_REGISTER; |
| 224 | } |
| 225 | bool HasRegisterPolicy() const { |
| 226 | return basic_policy() == EXTENDED_POLICY && |
| 227 | extended_policy() == MUST_HAVE_REGISTER; |
| 228 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 229 | bool HasSlotPolicy() const { |
| 230 | return basic_policy() == EXTENDED_POLICY && |
| 231 | extended_policy() == MUST_HAVE_SLOT; |
| 232 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 233 | bool HasSameAsInputPolicy() const { |
| 234 | return basic_policy() == EXTENDED_POLICY && |
| 235 | extended_policy() == SAME_AS_FIRST_INPUT; |
| 236 | } |
| 237 | bool HasFixedSlotPolicy() const { return basic_policy() == FIXED_SLOT; } |
| 238 | bool HasFixedRegisterPolicy() const { |
| 239 | return basic_policy() == EXTENDED_POLICY && |
| 240 | extended_policy() == FIXED_REGISTER; |
| 241 | } |
| 242 | bool HasFixedDoubleRegisterPolicy() const { |
| 243 | return basic_policy() == EXTENDED_POLICY && |
| 244 | extended_policy() == FIXED_DOUBLE_REGISTER; |
| 245 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 246 | bool HasSecondaryStorage() const { |
| 247 | return basic_policy() == EXTENDED_POLICY && |
| 248 | extended_policy() == FIXED_REGISTER && |
| 249 | HasSecondaryStorageField::decode(value_); |
| 250 | } |
| 251 | int GetSecondaryStorage() const { |
| 252 | DCHECK(HasSecondaryStorage()); |
| 253 | return SecondaryStorageField::decode(value_); |
| 254 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 255 | |
| 256 | // [basic_policy]: Distinguish between FIXED_SLOT and all other policies. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 257 | BasicPolicy basic_policy() const { |
| 258 | DCHECK_EQ(UNALLOCATED, kind()); |
| 259 | return BasicPolicyField::decode(value_); |
| 260 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 261 | |
| 262 | // [extended_policy]: Only for non-FIXED_SLOT. The finer-grained policy. |
| 263 | ExtendedPolicy extended_policy() const { |
| 264 | DCHECK(basic_policy() == EXTENDED_POLICY); |
| 265 | return ExtendedPolicyField::decode(value_); |
| 266 | } |
| 267 | |
| 268 | // [fixed_slot_index]: Only for FIXED_SLOT. |
| 269 | int fixed_slot_index() const { |
| 270 | DCHECK(HasFixedSlotPolicy()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 271 | return static_cast<int>(static_cast<int64_t>(value_) >> |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 272 | FixedSlotIndexField::kShift); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | // [fixed_register_index]: Only for FIXED_REGISTER or FIXED_DOUBLE_REGISTER. |
| 276 | int fixed_register_index() const { |
| 277 | DCHECK(HasFixedRegisterPolicy() || HasFixedDoubleRegisterPolicy()); |
| 278 | return FixedRegisterField::decode(value_); |
| 279 | } |
| 280 | |
| 281 | // [virtual_register]: The virtual register ID for this operand. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 282 | int32_t virtual_register() const { |
| 283 | DCHECK_EQ(UNALLOCATED, kind()); |
| 284 | return static_cast<int32_t>(VirtualRegisterField::decode(value_)); |
| 285 | } |
| 286 | |
| 287 | // TODO(dcarney): remove this. |
| 288 | void set_virtual_register(int32_t id) { |
| 289 | DCHECK_EQ(UNALLOCATED, kind()); |
| 290 | value_ = VirtualRegisterField::update(value_, static_cast<uint32_t>(id)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | // [lifetime]: Only for non-FIXED_SLOT. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 294 | bool IsUsedAtStart() const { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 295 | DCHECK(basic_policy() == EXTENDED_POLICY); |
| 296 | return LifetimeField::decode(value_) == USED_AT_START; |
| 297 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 298 | |
| 299 | INSTRUCTION_OPERAND_CASTS(UnallocatedOperand, UNALLOCATED); |
| 300 | |
| 301 | // The encoding used for UnallocatedOperand operands depends on the policy |
| 302 | // that is |
| 303 | // stored within the operand. The FIXED_SLOT policy uses a compact encoding |
| 304 | // because it accommodates a larger pay-load. |
| 305 | // |
| 306 | // For FIXED_SLOT policy: |
| 307 | // +------------------------------------------------+ |
| 308 | // | slot_index | 0 | virtual_register | 001 | |
| 309 | // +------------------------------------------------+ |
| 310 | // |
| 311 | // For all other (extended) policies: |
| 312 | // +-----------------------------------------------------+ |
| 313 | // | reg_index | L | PPP | 1 | virtual_register | 001 | |
| 314 | // +-----------------------------------------------------+ |
| 315 | // L ... Lifetime |
| 316 | // P ... Policy |
| 317 | // |
| 318 | // The slot index is a signed value which requires us to decode it manually |
| 319 | // instead of using the BitField utility class. |
| 320 | |
| 321 | STATIC_ASSERT(KindField::kSize == 3); |
| 322 | |
| 323 | class VirtualRegisterField : public BitField64<uint32_t, 3, 32> {}; |
| 324 | |
| 325 | // BitFields for all unallocated operands. |
| 326 | class BasicPolicyField : public BitField64<BasicPolicy, 35, 1> {}; |
| 327 | |
| 328 | // BitFields specific to BasicPolicy::FIXED_SLOT. |
| 329 | class FixedSlotIndexField : public BitField64<int, 36, 28> {}; |
| 330 | |
| 331 | // BitFields specific to BasicPolicy::EXTENDED_POLICY. |
| 332 | class ExtendedPolicyField : public BitField64<ExtendedPolicy, 36, 3> {}; |
| 333 | class LifetimeField : public BitField64<Lifetime, 39, 1> {}; |
| 334 | class HasSecondaryStorageField : public BitField64<bool, 40, 1> {}; |
| 335 | class FixedRegisterField : public BitField64<int, 41, 6> {}; |
| 336 | class SecondaryStorageField : public BitField64<int, 47, 3> {}; |
| 337 | |
| 338 | private: |
| 339 | explicit UnallocatedOperand(int virtual_register) |
| 340 | : InstructionOperand(UNALLOCATED) { |
| 341 | value_ |= |
| 342 | VirtualRegisterField::encode(static_cast<uint32_t>(virtual_register)); |
| 343 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 344 | }; |
| 345 | |
| 346 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 347 | class ConstantOperand : public InstructionOperand { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 348 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 349 | explicit ConstantOperand(int virtual_register) |
| 350 | : InstructionOperand(CONSTANT) { |
| 351 | value_ |= |
| 352 | VirtualRegisterField::encode(static_cast<uint32_t>(virtual_register)); |
| 353 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 354 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 355 | int32_t virtual_register() const { |
| 356 | return static_cast<int32_t>(VirtualRegisterField::decode(value_)); |
| 357 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 358 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 359 | static ConstantOperand* New(Zone* zone, int virtual_register) { |
| 360 | return InstructionOperand::New(zone, ConstantOperand(virtual_register)); |
| 361 | } |
| 362 | |
| 363 | INSTRUCTION_OPERAND_CASTS(ConstantOperand, CONSTANT); |
| 364 | |
| 365 | STATIC_ASSERT(KindField::kSize == 3); |
| 366 | class VirtualRegisterField : public BitField64<uint32_t, 3, 32> {}; |
| 367 | }; |
| 368 | |
| 369 | |
| 370 | class ImmediateOperand : public InstructionOperand { |
| 371 | public: |
| 372 | enum ImmediateType { INLINE, INDEXED }; |
| 373 | |
| 374 | explicit ImmediateOperand(ImmediateType type, int32_t value) |
| 375 | : InstructionOperand(IMMEDIATE) { |
| 376 | value_ |= TypeField::encode(type); |
| 377 | value_ |= static_cast<int64_t>(value) << ValueField::kShift; |
| 378 | } |
| 379 | |
| 380 | ImmediateType type() const { return TypeField::decode(value_); } |
| 381 | |
| 382 | int32_t inline_value() const { |
| 383 | DCHECK_EQ(INLINE, type()); |
| 384 | return static_cast<int64_t>(value_) >> ValueField::kShift; |
| 385 | } |
| 386 | |
| 387 | int32_t indexed_value() const { |
| 388 | DCHECK_EQ(INDEXED, type()); |
| 389 | return static_cast<int64_t>(value_) >> ValueField::kShift; |
| 390 | } |
| 391 | |
| 392 | static ImmediateOperand* New(Zone* zone, ImmediateType type, int32_t value) { |
| 393 | return InstructionOperand::New(zone, ImmediateOperand(type, value)); |
| 394 | } |
| 395 | |
| 396 | INSTRUCTION_OPERAND_CASTS(ImmediateOperand, IMMEDIATE); |
| 397 | |
| 398 | STATIC_ASSERT(KindField::kSize == 3); |
| 399 | class TypeField : public BitField64<ImmediateType, 3, 1> {}; |
| 400 | class ValueField : public BitField64<int32_t, 32, 32> {}; |
| 401 | }; |
| 402 | |
| 403 | |
| 404 | class LocationOperand : public InstructionOperand { |
| 405 | public: |
| 406 | enum LocationKind { REGISTER, STACK_SLOT }; |
| 407 | |
| 408 | LocationOperand(InstructionOperand::Kind operand_kind, |
| 409 | LocationOperand::LocationKind location_kind, |
| 410 | MachineRepresentation rep, int index) |
| 411 | : InstructionOperand(operand_kind) { |
| 412 | DCHECK_IMPLIES(location_kind == REGISTER, index >= 0); |
| 413 | DCHECK(IsSupportedRepresentation(rep)); |
| 414 | value_ |= LocationKindField::encode(location_kind); |
| 415 | value_ |= RepresentationField::encode(rep); |
| 416 | value_ |= static_cast<int64_t>(index) << IndexField::kShift; |
| 417 | } |
| 418 | |
| 419 | int index() const { |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 420 | DCHECK(IsStackSlot() || IsFPStackSlot()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 421 | return static_cast<int64_t>(value_) >> IndexField::kShift; |
| 422 | } |
| 423 | |
| 424 | Register GetRegister() const { |
| 425 | DCHECK(IsRegister()); |
| 426 | return Register::from_code(static_cast<int64_t>(value_) >> |
| 427 | IndexField::kShift); |
| 428 | } |
| 429 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 430 | FloatRegister GetFloatRegister() const { |
| 431 | DCHECK(IsFloatRegister()); |
| 432 | return FloatRegister::from_code(static_cast<int64_t>(value_) >> |
| 433 | IndexField::kShift); |
| 434 | } |
| 435 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 436 | DoubleRegister GetDoubleRegister() const { |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 437 | // TODO(bbudge) Tighten this test to IsDoubleRegister when all code |
| 438 | // generators are changed to use the correct Get*Register method. |
| 439 | DCHECK(IsFPRegister()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 440 | return DoubleRegister::from_code(static_cast<int64_t>(value_) >> |
| 441 | IndexField::kShift); |
| 442 | } |
| 443 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 444 | Simd128Register GetSimd128Register() const { |
| 445 | DCHECK(IsSimd128Register()); |
| 446 | return Simd128Register::from_code(static_cast<int64_t>(value_) >> |
| 447 | IndexField::kShift); |
| 448 | } |
| 449 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 450 | LocationKind location_kind() const { |
| 451 | return LocationKindField::decode(value_); |
| 452 | } |
| 453 | |
| 454 | MachineRepresentation representation() const { |
| 455 | return RepresentationField::decode(value_); |
| 456 | } |
| 457 | |
| 458 | static bool IsSupportedRepresentation(MachineRepresentation rep) { |
| 459 | switch (rep) { |
| 460 | case MachineRepresentation::kWord32: |
| 461 | case MachineRepresentation::kWord64: |
| 462 | case MachineRepresentation::kFloat32: |
| 463 | case MachineRepresentation::kFloat64: |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 464 | case MachineRepresentation::kSimd128: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 465 | case MachineRepresentation::kTagged: |
| 466 | return true; |
| 467 | case MachineRepresentation::kBit: |
| 468 | case MachineRepresentation::kWord8: |
| 469 | case MachineRepresentation::kWord16: |
| 470 | case MachineRepresentation::kNone: |
| 471 | return false; |
| 472 | } |
| 473 | UNREACHABLE(); |
| 474 | return false; |
| 475 | } |
| 476 | |
| 477 | static LocationOperand* cast(InstructionOperand* op) { |
| 478 | DCHECK(ALLOCATED == op->kind() || EXPLICIT == op->kind()); |
| 479 | return static_cast<LocationOperand*>(op); |
| 480 | } |
| 481 | |
| 482 | static const LocationOperand* cast(const InstructionOperand* op) { |
| 483 | DCHECK(ALLOCATED == op->kind() || EXPLICIT == op->kind()); |
| 484 | return static_cast<const LocationOperand*>(op); |
| 485 | } |
| 486 | |
| 487 | static LocationOperand cast(const InstructionOperand& op) { |
| 488 | DCHECK(ALLOCATED == op.kind() || EXPLICIT == op.kind()); |
| 489 | return *static_cast<const LocationOperand*>(&op); |
| 490 | } |
| 491 | |
| 492 | STATIC_ASSERT(KindField::kSize == 3); |
| 493 | class LocationKindField : public BitField64<LocationKind, 3, 2> {}; |
| 494 | class RepresentationField : public BitField64<MachineRepresentation, 5, 8> {}; |
| 495 | class IndexField : public BitField64<int32_t, 35, 29> {}; |
| 496 | }; |
| 497 | |
| 498 | |
| 499 | class ExplicitOperand : public LocationOperand { |
| 500 | public: |
| 501 | ExplicitOperand(LocationKind kind, MachineRepresentation rep, int index); |
| 502 | |
| 503 | static ExplicitOperand* New(Zone* zone, LocationKind kind, |
| 504 | MachineRepresentation rep, int index) { |
| 505 | return InstructionOperand::New(zone, ExplicitOperand(kind, rep, index)); |
| 506 | } |
| 507 | |
| 508 | INSTRUCTION_OPERAND_CASTS(ExplicitOperand, EXPLICIT); |
| 509 | }; |
| 510 | |
| 511 | |
| 512 | class AllocatedOperand : public LocationOperand { |
| 513 | public: |
| 514 | AllocatedOperand(LocationKind kind, MachineRepresentation rep, int index) |
| 515 | : LocationOperand(ALLOCATED, kind, rep, index) {} |
| 516 | |
| 517 | static AllocatedOperand* New(Zone* zone, LocationKind kind, |
| 518 | MachineRepresentation rep, int index) { |
| 519 | return InstructionOperand::New(zone, AllocatedOperand(kind, rep, index)); |
| 520 | } |
| 521 | |
| 522 | INSTRUCTION_OPERAND_CASTS(AllocatedOperand, ALLOCATED); |
| 523 | }; |
| 524 | |
| 525 | |
| 526 | #undef INSTRUCTION_OPERAND_CASTS |
| 527 | |
| 528 | |
| 529 | bool InstructionOperand::IsAnyRegister() const { |
| 530 | return (IsAllocated() || IsExplicit()) && |
| 531 | LocationOperand::cast(this)->location_kind() == |
| 532 | LocationOperand::REGISTER; |
| 533 | } |
| 534 | |
| 535 | |
| 536 | bool InstructionOperand::IsRegister() const { |
| 537 | return IsAnyRegister() && |
| 538 | !IsFloatingPoint(LocationOperand::cast(this)->representation()); |
| 539 | } |
| 540 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 541 | bool InstructionOperand::IsFPRegister() const { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 542 | return IsAnyRegister() && |
| 543 | IsFloatingPoint(LocationOperand::cast(this)->representation()); |
| 544 | } |
| 545 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 546 | bool InstructionOperand::IsFloatRegister() const { |
| 547 | return IsAnyRegister() && |
| 548 | LocationOperand::cast(this)->representation() == |
| 549 | MachineRepresentation::kFloat32; |
| 550 | } |
| 551 | |
| 552 | bool InstructionOperand::IsDoubleRegister() const { |
| 553 | return IsAnyRegister() && |
| 554 | LocationOperand::cast(this)->representation() == |
| 555 | MachineRepresentation::kFloat64; |
| 556 | } |
| 557 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 558 | bool InstructionOperand::IsSimd128Register() const { |
| 559 | return IsAnyRegister() && |
| 560 | LocationOperand::cast(this)->representation() == |
| 561 | MachineRepresentation::kSimd128; |
| 562 | } |
| 563 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 564 | bool InstructionOperand::IsStackSlot() const { |
| 565 | return (IsAllocated() || IsExplicit()) && |
| 566 | LocationOperand::cast(this)->location_kind() == |
| 567 | LocationOperand::STACK_SLOT && |
| 568 | !IsFloatingPoint(LocationOperand::cast(this)->representation()); |
| 569 | } |
| 570 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 571 | bool InstructionOperand::IsFPStackSlot() const { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 572 | return (IsAllocated() || IsExplicit()) && |
| 573 | LocationOperand::cast(this)->location_kind() == |
| 574 | LocationOperand::STACK_SLOT && |
| 575 | IsFloatingPoint(LocationOperand::cast(this)->representation()); |
| 576 | } |
| 577 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 578 | bool InstructionOperand::IsFloatStackSlot() const { |
| 579 | return (IsAllocated() || IsExplicit()) && |
| 580 | LocationOperand::cast(this)->location_kind() == |
| 581 | LocationOperand::STACK_SLOT && |
| 582 | LocationOperand::cast(this)->representation() == |
| 583 | MachineRepresentation::kFloat32; |
| 584 | } |
| 585 | |
| 586 | bool InstructionOperand::IsDoubleStackSlot() const { |
| 587 | return (IsAllocated() || IsExplicit()) && |
| 588 | LocationOperand::cast(this)->location_kind() == |
| 589 | LocationOperand::STACK_SLOT && |
| 590 | LocationOperand::cast(this)->representation() == |
| 591 | MachineRepresentation::kFloat64; |
| 592 | } |
| 593 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 594 | bool InstructionOperand::IsSimd128StackSlot() const { |
| 595 | return (IsAllocated() || IsExplicit()) && |
| 596 | LocationOperand::cast(this)->location_kind() == |
| 597 | LocationOperand::STACK_SLOT && |
| 598 | LocationOperand::cast(this)->representation() == |
| 599 | MachineRepresentation::kSimd128; |
| 600 | } |
| 601 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 602 | uint64_t InstructionOperand::GetCanonicalizedValue() const { |
| 603 | if (IsAllocated() || IsExplicit()) { |
| 604 | // TODO(dcarney): put machine type last and mask. |
| 605 | MachineRepresentation canonicalized_representation = |
| 606 | IsFloatingPoint(LocationOperand::cast(this)->representation()) |
| 607 | ? MachineRepresentation::kFloat64 |
| 608 | : MachineRepresentation::kNone; |
| 609 | return InstructionOperand::KindField::update( |
| 610 | LocationOperand::RepresentationField::update( |
| 611 | this->value_, canonicalized_representation), |
| 612 | LocationOperand::EXPLICIT); |
| 613 | } |
| 614 | return this->value_; |
| 615 | } |
| 616 | |
| 617 | |
| 618 | // Required for maps that don't care about machine type. |
| 619 | struct CompareOperandModuloType { |
| 620 | bool operator()(const InstructionOperand& a, |
| 621 | const InstructionOperand& b) const { |
| 622 | return a.CompareCanonicalized(b); |
| 623 | } |
| 624 | }; |
| 625 | |
| 626 | |
| 627 | class MoveOperands final : public ZoneObject { |
| 628 | public: |
| 629 | MoveOperands(const InstructionOperand& source, |
| 630 | const InstructionOperand& destination) |
| 631 | : source_(source), destination_(destination) { |
| 632 | DCHECK(!source.IsInvalid() && !destination.IsInvalid()); |
| 633 | } |
| 634 | |
| 635 | const InstructionOperand& source() const { return source_; } |
| 636 | InstructionOperand& source() { return source_; } |
| 637 | void set_source(const InstructionOperand& operand) { source_ = operand; } |
| 638 | |
| 639 | const InstructionOperand& destination() const { return destination_; } |
| 640 | InstructionOperand& destination() { return destination_; } |
| 641 | void set_destination(const InstructionOperand& operand) { |
| 642 | destination_ = operand; |
| 643 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 644 | |
| 645 | // The gap resolver marks moves as "in-progress" by clearing the |
| 646 | // destination (but not the source). |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 647 | bool IsPending() const { |
| 648 | return destination_.IsInvalid() && !source_.IsInvalid(); |
| 649 | } |
| 650 | void SetPending() { destination_ = InstructionOperand(); } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 651 | |
| 652 | // True if this move a move into the given destination operand. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 653 | bool Blocks(const InstructionOperand& operand) const { |
| 654 | return !IsEliminated() && source().EqualsCanonicalized(operand); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 655 | } |
| 656 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 657 | // A move is redundant if it's been eliminated or if its source and |
| 658 | // destination are the same. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 659 | bool IsRedundant() const { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 660 | DCHECK_IMPLIES(!destination_.IsInvalid(), !destination_.IsConstant()); |
| 661 | return IsEliminated() || source_.EqualsCanonicalized(destination_); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 662 | } |
| 663 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 664 | // We clear both operands to indicate move that's been eliminated. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 665 | void Eliminate() { source_ = destination_ = InstructionOperand(); } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 666 | bool IsEliminated() const { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 667 | DCHECK_IMPLIES(source_.IsInvalid(), destination_.IsInvalid()); |
| 668 | return source_.IsInvalid(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 669 | } |
| 670 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 671 | void Print(const RegisterConfiguration* config) const; |
| 672 | void Print() const; |
| 673 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 674 | private: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 675 | InstructionOperand source_; |
| 676 | InstructionOperand destination_; |
| 677 | |
| 678 | DISALLOW_COPY_AND_ASSIGN(MoveOperands); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 679 | }; |
| 680 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 681 | |
| 682 | struct PrintableMoveOperands { |
| 683 | const RegisterConfiguration* register_configuration_; |
| 684 | const MoveOperands* move_operands_; |
| 685 | }; |
| 686 | |
| 687 | |
| 688 | std::ostream& operator<<(std::ostream& os, const PrintableMoveOperands& mo); |
| 689 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 690 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 691 | class ParallelMove final : public ZoneVector<MoveOperands*>, public ZoneObject { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 692 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 693 | explicit ParallelMove(Zone* zone) : ZoneVector<MoveOperands*>(zone) { |
| 694 | reserve(4); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 695 | } |
| 696 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 697 | MoveOperands* AddMove(const InstructionOperand& from, |
| 698 | const InstructionOperand& to) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 699 | Zone* zone = get_allocator().zone(); |
| 700 | return AddMove(from, to, zone); |
| 701 | } |
| 702 | |
| 703 | MoveOperands* AddMove(const InstructionOperand& from, |
| 704 | const InstructionOperand& to, |
| 705 | Zone* operand_allocation_zone) { |
| 706 | MoveOperands* move = new (operand_allocation_zone) MoveOperands(from, to); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 707 | push_back(move); |
| 708 | return move; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | bool IsRedundant() const; |
| 712 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 713 | // Prepare this ParallelMove to insert move as if it happened in a subsequent |
| 714 | // ParallelMove. move->source() may be changed. The MoveOperand returned |
| 715 | // must be Eliminated. |
| 716 | MoveOperands* PrepareInsertAfter(MoveOperands* move) const; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 717 | |
| 718 | private: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 719 | DISALLOW_COPY_AND_ASSIGN(ParallelMove); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 720 | }; |
| 721 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 722 | |
| 723 | struct PrintableParallelMove { |
| 724 | const RegisterConfiguration* register_configuration_; |
| 725 | const ParallelMove* parallel_move_; |
| 726 | }; |
| 727 | |
| 728 | |
| 729 | std::ostream& operator<<(std::ostream& os, const PrintableParallelMove& pm); |
| 730 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 731 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 732 | class ReferenceMap final : public ZoneObject { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 733 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 734 | explicit ReferenceMap(Zone* zone) |
| 735 | : reference_operands_(8, zone), instruction_position_(-1) {} |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 736 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 737 | const ZoneVector<InstructionOperand>& reference_operands() const { |
| 738 | return reference_operands_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 739 | } |
| 740 | int instruction_position() const { return instruction_position_; } |
| 741 | |
| 742 | void set_instruction_position(int pos) { |
| 743 | DCHECK(instruction_position_ == -1); |
| 744 | instruction_position_ = pos; |
| 745 | } |
| 746 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 747 | void RecordReference(const AllocatedOperand& op); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 748 | |
| 749 | private: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 750 | friend std::ostream& operator<<(std::ostream& os, const ReferenceMap& pm); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 751 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 752 | ZoneVector<InstructionOperand> reference_operands_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 753 | int instruction_position_; |
| 754 | }; |
| 755 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 756 | std::ostream& operator<<(std::ostream& os, const ReferenceMap& pm); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 757 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 758 | class InstructionBlock; |
| 759 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 760 | class Instruction final { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 761 | public: |
| 762 | size_t OutputCount() const { return OutputCountField::decode(bit_field_); } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 763 | const InstructionOperand* OutputAt(size_t i) const { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 764 | DCHECK(i < OutputCount()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 765 | return &operands_[i]; |
| 766 | } |
| 767 | InstructionOperand* OutputAt(size_t i) { |
| 768 | DCHECK(i < OutputCount()); |
| 769 | return &operands_[i]; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | bool HasOutput() const { return OutputCount() == 1; } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 773 | const InstructionOperand* Output() const { return OutputAt(0); } |
| 774 | InstructionOperand* Output() { return OutputAt(0); } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 775 | |
| 776 | size_t InputCount() const { return InputCountField::decode(bit_field_); } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 777 | const InstructionOperand* InputAt(size_t i) const { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 778 | DCHECK(i < InputCount()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 779 | return &operands_[OutputCount() + i]; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 780 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 781 | InstructionOperand* InputAt(size_t i) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 782 | DCHECK(i < InputCount()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 783 | return &operands_[OutputCount() + i]; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 784 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 785 | |
| 786 | size_t TempCount() const { return TempCountField::decode(bit_field_); } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 787 | const InstructionOperand* TempAt(size_t i) const { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 788 | DCHECK(i < TempCount()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 789 | return &operands_[OutputCount() + InputCount() + i]; |
| 790 | } |
| 791 | InstructionOperand* TempAt(size_t i) { |
| 792 | DCHECK(i < TempCount()); |
| 793 | return &operands_[OutputCount() + InputCount() + i]; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | InstructionCode opcode() const { return opcode_; } |
| 797 | ArchOpcode arch_opcode() const { return ArchOpcodeField::decode(opcode()); } |
| 798 | AddressingMode addressing_mode() const { |
| 799 | return AddressingModeField::decode(opcode()); |
| 800 | } |
| 801 | FlagsMode flags_mode() const { return FlagsModeField::decode(opcode()); } |
| 802 | FlagsCondition flags_condition() const { |
| 803 | return FlagsConditionField::decode(opcode()); |
| 804 | } |
| 805 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 806 | static Instruction* New(Zone* zone, InstructionCode opcode) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 807 | return New(zone, opcode, 0, nullptr, 0, nullptr, 0, nullptr); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | static Instruction* New(Zone* zone, InstructionCode opcode, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 811 | size_t output_count, InstructionOperand* outputs, |
| 812 | size_t input_count, InstructionOperand* inputs, |
| 813 | size_t temp_count, InstructionOperand* temps) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 814 | DCHECK(opcode >= 0); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 815 | DCHECK(output_count == 0 || outputs != nullptr); |
| 816 | DCHECK(input_count == 0 || inputs != nullptr); |
| 817 | DCHECK(temp_count == 0 || temps != nullptr); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 818 | // TODO(jarin/mstarzinger): Handle this gracefully. See crbug.com/582702. |
| 819 | CHECK(InputCountField::is_valid(input_count)); |
| 820 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 821 | size_t total_extra_ops = output_count + input_count + temp_count; |
| 822 | if (total_extra_ops != 0) total_extra_ops--; |
| 823 | int size = static_cast<int>( |
| 824 | RoundUp(sizeof(Instruction), sizeof(InstructionOperand)) + |
| 825 | total_extra_ops * sizeof(InstructionOperand)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 826 | return new (zone->New(size)) Instruction( |
| 827 | opcode, output_count, outputs, input_count, inputs, temp_count, temps); |
| 828 | } |
| 829 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 830 | Instruction* MarkAsCall() { |
| 831 | bit_field_ = IsCallField::update(bit_field_, true); |
| 832 | return this; |
| 833 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 834 | bool IsCall() const { return IsCallField::decode(bit_field_); } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 835 | bool NeedsReferenceMap() const { return IsCall(); } |
| 836 | bool HasReferenceMap() const { return reference_map_ != nullptr; } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 837 | |
| 838 | bool ClobbersRegisters() const { return IsCall(); } |
| 839 | bool ClobbersTemps() const { return IsCall(); } |
| 840 | bool ClobbersDoubleRegisters() const { return IsCall(); } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 841 | ReferenceMap* reference_map() const { return reference_map_; } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 842 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 843 | void set_reference_map(ReferenceMap* map) { |
| 844 | DCHECK(NeedsReferenceMap()); |
| 845 | DCHECK(!reference_map_); |
| 846 | reference_map_ = map; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 847 | } |
| 848 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 849 | void OverwriteWithNop() { |
| 850 | opcode_ = ArchOpcodeField::encode(kArchNop); |
| 851 | bit_field_ = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 852 | reference_map_ = nullptr; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | bool IsNop() const { |
| 856 | return arch_opcode() == kArchNop && InputCount() == 0 && |
| 857 | OutputCount() == 0 && TempCount() == 0; |
| 858 | } |
| 859 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 860 | bool IsDeoptimizeCall() const { |
| 861 | return arch_opcode() == ArchOpcode::kArchDeoptimize || |
| 862 | FlagsModeField::decode(opcode()) == kFlags_deoptimize; |
| 863 | } |
| 864 | |
| 865 | bool IsJump() const { return arch_opcode() == ArchOpcode::kArchJmp; } |
| 866 | bool IsRet() const { return arch_opcode() == ArchOpcode::kArchRet; } |
| 867 | bool IsTailCall() const { |
| 868 | return arch_opcode() == ArchOpcode::kArchTailCallCodeObject || |
| 869 | arch_opcode() == ArchOpcode::kArchTailCallCodeObjectFromJSFunction || |
| 870 | arch_opcode() == ArchOpcode::kArchTailCallJSFunction || |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 871 | arch_opcode() == ArchOpcode::kArchTailCallJSFunctionFromJSFunction || |
| 872 | arch_opcode() == ArchOpcode::kArchTailCallAddress; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 873 | } |
| 874 | bool IsThrow() const { |
| 875 | return arch_opcode() == ArchOpcode::kArchThrowTerminator; |
| 876 | } |
| 877 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 878 | enum GapPosition { |
| 879 | START, |
| 880 | END, |
| 881 | FIRST_GAP_POSITION = START, |
| 882 | LAST_GAP_POSITION = END |
| 883 | }; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 884 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 885 | ParallelMove* GetOrCreateParallelMove(GapPosition pos, Zone* zone) { |
| 886 | if (parallel_moves_[pos] == nullptr) { |
| 887 | parallel_moves_[pos] = new (zone) ParallelMove(zone); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 888 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 889 | return parallel_moves_[pos]; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 890 | } |
| 891 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 892 | ParallelMove* GetParallelMove(GapPosition pos) { |
| 893 | return parallel_moves_[pos]; |
| 894 | } |
| 895 | |
| 896 | const ParallelMove* GetParallelMove(GapPosition pos) const { |
| 897 | return parallel_moves_[pos]; |
| 898 | } |
| 899 | |
| 900 | bool AreMovesRedundant() const; |
| 901 | |
| 902 | ParallelMove* const* parallel_moves() const { return ¶llel_moves_[0]; } |
| 903 | ParallelMove** parallel_moves() { return ¶llel_moves_[0]; } |
| 904 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 905 | // The block_id may be invalidated in JumpThreading. It is only important for |
| 906 | // register allocation, to avoid searching for blocks from instruction |
| 907 | // indexes. |
| 908 | InstructionBlock* block() const { return block_; } |
| 909 | void set_block(InstructionBlock* block) { |
| 910 | DCHECK_NOT_NULL(block); |
| 911 | block_ = block; |
| 912 | } |
| 913 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 914 | void Print(const RegisterConfiguration* config) const; |
| 915 | void Print() const; |
| 916 | |
| 917 | private: |
| 918 | explicit Instruction(InstructionCode opcode); |
| 919 | |
| 920 | Instruction(InstructionCode opcode, size_t output_count, |
| 921 | InstructionOperand* outputs, size_t input_count, |
| 922 | InstructionOperand* inputs, size_t temp_count, |
| 923 | InstructionOperand* temps); |
| 924 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 925 | typedef BitField<size_t, 0, 8> OutputCountField; |
| 926 | typedef BitField<size_t, 8, 16> InputCountField; |
| 927 | typedef BitField<size_t, 24, 6> TempCountField; |
| 928 | typedef BitField<bool, 30, 1> IsCallField; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 929 | |
| 930 | InstructionCode opcode_; |
| 931 | uint32_t bit_field_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 932 | ParallelMove* parallel_moves_[2]; |
| 933 | ReferenceMap* reference_map_; |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 934 | InstructionBlock* block_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 935 | InstructionOperand operands_[1]; |
| 936 | |
| 937 | DISALLOW_COPY_AND_ASSIGN(Instruction); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 938 | }; |
| 939 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 940 | |
| 941 | struct PrintableInstruction { |
| 942 | const RegisterConfiguration* register_configuration_; |
| 943 | const Instruction* instr_; |
| 944 | }; |
| 945 | std::ostream& operator<<(std::ostream& os, const PrintableInstruction& instr); |
| 946 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 947 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 948 | class RpoNumber final { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 949 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 950 | static const int kInvalidRpoNumber = -1; |
| 951 | int ToInt() const { |
| 952 | DCHECK(IsValid()); |
| 953 | return index_; |
| 954 | } |
| 955 | size_t ToSize() const { |
| 956 | DCHECK(IsValid()); |
| 957 | return static_cast<size_t>(index_); |
| 958 | } |
| 959 | bool IsValid() const { return index_ >= 0; } |
| 960 | static RpoNumber FromInt(int index) { return RpoNumber(index); } |
| 961 | static RpoNumber Invalid() { return RpoNumber(kInvalidRpoNumber); } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 962 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 963 | bool IsNext(const RpoNumber other) const { |
| 964 | DCHECK(IsValid()); |
| 965 | return other.index_ == this->index_ + 1; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 966 | } |
| 967 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 968 | // Comparison operators. |
| 969 | bool operator==(RpoNumber other) const { return index_ == other.index_; } |
| 970 | bool operator!=(RpoNumber other) const { return index_ != other.index_; } |
| 971 | bool operator>(RpoNumber other) const { return index_ > other.index_; } |
| 972 | bool operator<(RpoNumber other) const { return index_ < other.index_; } |
| 973 | bool operator<=(RpoNumber other) const { return index_ <= other.index_; } |
| 974 | bool operator>=(RpoNumber other) const { return index_ >= other.index_; } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 975 | |
| 976 | private: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 977 | explicit RpoNumber(int32_t index) : index_(index) {} |
| 978 | int32_t index_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 979 | }; |
| 980 | |
| 981 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 982 | std::ostream& operator<<(std::ostream&, const RpoNumber&); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 983 | |
| 984 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 985 | class Constant final { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 986 | public: |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 987 | enum Type { |
| 988 | kInt32, |
| 989 | kInt64, |
| 990 | kFloat32, |
| 991 | kFloat64, |
| 992 | kExternalReference, |
| 993 | kHeapObject, |
| 994 | kRpoNumber |
| 995 | }; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 996 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 997 | explicit Constant(int32_t v); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 998 | explicit Constant(int64_t v) : type_(kInt64), value_(v) {} |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 999 | explicit Constant(float v) : type_(kFloat32), value_(bit_cast<int32_t>(v)) {} |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1000 | explicit Constant(double v) : type_(kFloat64), value_(bit_cast<int64_t>(v)) {} |
| 1001 | explicit Constant(ExternalReference ref) |
| 1002 | : type_(kExternalReference), value_(bit_cast<intptr_t>(ref)) {} |
| 1003 | explicit Constant(Handle<HeapObject> obj) |
| 1004 | : type_(kHeapObject), value_(bit_cast<intptr_t>(obj)) {} |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1005 | explicit Constant(RpoNumber rpo) : type_(kRpoNumber), value_(rpo.ToInt()) {} |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1006 | explicit Constant(RelocatablePtrConstantInfo info); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1007 | |
| 1008 | Type type() const { return type_; } |
| 1009 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1010 | RelocInfo::Mode rmode() const { return rmode_; } |
| 1011 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1012 | int32_t ToInt32() const { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1013 | DCHECK(type() == kInt32 || type() == kInt64); |
| 1014 | const int32_t value = static_cast<int32_t>(value_); |
| 1015 | DCHECK_EQ(value_, static_cast<int64_t>(value)); |
| 1016 | return value; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1017 | } |
| 1018 | |
| 1019 | int64_t ToInt64() const { |
| 1020 | if (type() == kInt32) return ToInt32(); |
| 1021 | DCHECK_EQ(kInt64, type()); |
| 1022 | return value_; |
| 1023 | } |
| 1024 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1025 | float ToFloat32() const { |
| 1026 | DCHECK_EQ(kFloat32, type()); |
| 1027 | return bit_cast<float>(static_cast<int32_t>(value_)); |
| 1028 | } |
| 1029 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1030 | double ToFloat64() const { |
| 1031 | if (type() == kInt32) return ToInt32(); |
| 1032 | DCHECK_EQ(kFloat64, type()); |
| 1033 | return bit_cast<double>(value_); |
| 1034 | } |
| 1035 | |
| 1036 | ExternalReference ToExternalReference() const { |
| 1037 | DCHECK_EQ(kExternalReference, type()); |
| 1038 | return bit_cast<ExternalReference>(static_cast<intptr_t>(value_)); |
| 1039 | } |
| 1040 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1041 | RpoNumber ToRpoNumber() const { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1042 | DCHECK_EQ(kRpoNumber, type()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1043 | return RpoNumber::FromInt(static_cast<int>(value_)); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1044 | } |
| 1045 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1046 | Handle<HeapObject> ToHeapObject() const; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1047 | |
| 1048 | private: |
| 1049 | Type type_; |
| 1050 | int64_t value_; |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1051 | #if V8_TARGET_ARCH_32_BIT |
| 1052 | RelocInfo::Mode rmode_ = RelocInfo::NONE32; |
| 1053 | #else |
| 1054 | RelocInfo::Mode rmode_ = RelocInfo::NONE64; |
| 1055 | #endif |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1056 | }; |
| 1057 | |
| 1058 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1059 | std::ostream& operator<<(std::ostream& os, const Constant& constant); |
| 1060 | |
| 1061 | |
| 1062 | // Forward declarations. |
| 1063 | class FrameStateDescriptor; |
| 1064 | |
| 1065 | |
| 1066 | enum class StateValueKind { kPlain, kNested, kDuplicate }; |
| 1067 | |
| 1068 | |
| 1069 | class StateValueDescriptor { |
| 1070 | public: |
| 1071 | explicit StateValueDescriptor(Zone* zone) |
| 1072 | : kind_(StateValueKind::kPlain), |
| 1073 | type_(MachineType::AnyTagged()), |
| 1074 | id_(0), |
| 1075 | fields_(zone) {} |
| 1076 | |
| 1077 | static StateValueDescriptor Plain(Zone* zone, MachineType type) { |
| 1078 | return StateValueDescriptor(StateValueKind::kPlain, zone, type, 0); |
| 1079 | } |
| 1080 | static StateValueDescriptor Recursive(Zone* zone, size_t id) { |
| 1081 | return StateValueDescriptor(StateValueKind::kNested, zone, |
| 1082 | MachineType::AnyTagged(), id); |
| 1083 | } |
| 1084 | static StateValueDescriptor Duplicate(Zone* zone, size_t id) { |
| 1085 | return StateValueDescriptor(StateValueKind::kDuplicate, zone, |
| 1086 | MachineType::AnyTagged(), id); |
| 1087 | } |
| 1088 | |
| 1089 | size_t size() { return fields_.size(); } |
| 1090 | ZoneVector<StateValueDescriptor>& fields() { return fields_; } |
| 1091 | int IsPlain() { return kind_ == StateValueKind::kPlain; } |
| 1092 | int IsNested() { return kind_ == StateValueKind::kNested; } |
| 1093 | int IsDuplicate() { return kind_ == StateValueKind::kDuplicate; } |
| 1094 | MachineType type() const { return type_; } |
| 1095 | MachineType GetOperandType(size_t index) const { |
| 1096 | return fields_[index].type_; |
| 1097 | } |
| 1098 | size_t id() const { return id_; } |
| 1099 | |
| 1100 | private: |
| 1101 | StateValueDescriptor(StateValueKind kind, Zone* zone, MachineType type, |
| 1102 | size_t id) |
| 1103 | : kind_(kind), type_(type), id_(id), fields_(zone) {} |
| 1104 | |
| 1105 | StateValueKind kind_; |
| 1106 | MachineType type_; |
| 1107 | size_t id_; |
| 1108 | ZoneVector<StateValueDescriptor> fields_; |
| 1109 | }; |
| 1110 | |
| 1111 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1112 | class FrameStateDescriptor : public ZoneObject { |
| 1113 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1114 | FrameStateDescriptor(Zone* zone, FrameStateType type, BailoutId bailout_id, |
| 1115 | OutputFrameStateCombine state_combine, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1116 | size_t parameters_count, size_t locals_count, |
| 1117 | size_t stack_count, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1118 | MaybeHandle<SharedFunctionInfo> shared_info, |
| 1119 | FrameStateDescriptor* outer_state = nullptr); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1120 | |
| 1121 | FrameStateType type() const { return type_; } |
| 1122 | BailoutId bailout_id() const { return bailout_id_; } |
| 1123 | OutputFrameStateCombine state_combine() const { return frame_state_combine_; } |
| 1124 | size_t parameters_count() const { return parameters_count_; } |
| 1125 | size_t locals_count() const { return locals_count_; } |
| 1126 | size_t stack_count() const { return stack_count_; } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1127 | MaybeHandle<SharedFunctionInfo> shared_info() const { return shared_info_; } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1128 | FrameStateDescriptor* outer_state() const { return outer_state_; } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1129 | bool HasContext() const { |
| 1130 | return FrameStateFunctionInfo::IsJSFunctionType(type_); |
| 1131 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1132 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1133 | size_t GetSize(OutputFrameStateCombine combine = |
| 1134 | OutputFrameStateCombine::Ignore()) const; |
| 1135 | size_t GetTotalSize() const; |
| 1136 | size_t GetFrameCount() const; |
| 1137 | size_t GetJSFrameCount() const; |
| 1138 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1139 | MachineType GetType(size_t index) const { |
| 1140 | return values_.GetOperandType(index); |
| 1141 | } |
| 1142 | StateValueDescriptor* GetStateValueDescriptor() { return &values_; } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1143 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1144 | private: |
| 1145 | FrameStateType type_; |
| 1146 | BailoutId bailout_id_; |
| 1147 | OutputFrameStateCombine frame_state_combine_; |
| 1148 | size_t parameters_count_; |
| 1149 | size_t locals_count_; |
| 1150 | size_t stack_count_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1151 | StateValueDescriptor values_; |
| 1152 | MaybeHandle<SharedFunctionInfo> const shared_info_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1153 | FrameStateDescriptor* outer_state_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1154 | }; |
| 1155 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1156 | |
| 1157 | typedef ZoneVector<FrameStateDescriptor*> DeoptimizationVector; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1158 | |
| 1159 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1160 | class PhiInstruction final : public ZoneObject { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1161 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1162 | typedef ZoneVector<InstructionOperand> Inputs; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1163 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1164 | PhiInstruction(Zone* zone, int virtual_register, size_t input_count); |
| 1165 | |
| 1166 | void SetInput(size_t offset, int virtual_register); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1167 | |
| 1168 | int virtual_register() const { return virtual_register_; } |
| 1169 | const IntVector& operands() const { return operands_; } |
| 1170 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1171 | // TODO(dcarney): this has no real business being here, since it's internal to |
| 1172 | // the register allocator, but putting it here was convenient. |
| 1173 | const InstructionOperand& output() const { return output_; } |
| 1174 | InstructionOperand& output() { return output_; } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1175 | |
| 1176 | private: |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1177 | const int virtual_register_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1178 | InstructionOperand output_; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1179 | IntVector operands_; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1180 | }; |
| 1181 | |
| 1182 | |
| 1183 | // Analogue of BasicBlock for Instructions instead of Nodes. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1184 | class InstructionBlock final : public ZoneObject { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1185 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1186 | InstructionBlock(Zone* zone, RpoNumber rpo_number, RpoNumber loop_header, |
| 1187 | RpoNumber loop_end, bool deferred, bool handler); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1188 | |
| 1189 | // Instruction indexes (used by the register allocator). |
| 1190 | int first_instruction_index() const { |
| 1191 | DCHECK(code_start_ >= 0); |
| 1192 | DCHECK(code_end_ > 0); |
| 1193 | DCHECK(code_end_ >= code_start_); |
| 1194 | return code_start_; |
| 1195 | } |
| 1196 | int last_instruction_index() const { |
| 1197 | DCHECK(code_start_ >= 0); |
| 1198 | DCHECK(code_end_ > 0); |
| 1199 | DCHECK(code_end_ >= code_start_); |
| 1200 | return code_end_ - 1; |
| 1201 | } |
| 1202 | |
| 1203 | int32_t code_start() const { return code_start_; } |
| 1204 | void set_code_start(int32_t start) { code_start_ = start; } |
| 1205 | |
| 1206 | int32_t code_end() const { return code_end_; } |
| 1207 | void set_code_end(int32_t end) { code_end_ = end; } |
| 1208 | |
| 1209 | bool IsDeferred() const { return deferred_; } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1210 | bool IsHandler() const { return handler_; } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1211 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1212 | RpoNumber ao_number() const { return ao_number_; } |
| 1213 | RpoNumber rpo_number() const { return rpo_number_; } |
| 1214 | RpoNumber loop_header() const { return loop_header_; } |
| 1215 | RpoNumber loop_end() const { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1216 | DCHECK(IsLoopHeader()); |
| 1217 | return loop_end_; |
| 1218 | } |
| 1219 | inline bool IsLoopHeader() const { return loop_end_.IsValid(); } |
| 1220 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1221 | typedef ZoneVector<RpoNumber> Predecessors; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1222 | Predecessors& predecessors() { return predecessors_; } |
| 1223 | const Predecessors& predecessors() const { return predecessors_; } |
| 1224 | size_t PredecessorCount() const { return predecessors_.size(); } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1225 | size_t PredecessorIndexOf(RpoNumber rpo_number) const; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1226 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1227 | typedef ZoneVector<RpoNumber> Successors; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1228 | Successors& successors() { return successors_; } |
| 1229 | const Successors& successors() const { return successors_; } |
| 1230 | size_t SuccessorCount() const { return successors_.size(); } |
| 1231 | |
| 1232 | typedef ZoneVector<PhiInstruction*> PhiInstructions; |
| 1233 | const PhiInstructions& phis() const { return phis_; } |
| 1234 | void AddPhi(PhiInstruction* phi) { phis_.push_back(phi); } |
| 1235 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1236 | void set_ao_number(RpoNumber ao_number) { ao_number_ = ao_number; } |
| 1237 | |
| 1238 | bool needs_frame() const { return needs_frame_; } |
| 1239 | void mark_needs_frame() { needs_frame_ = true; } |
| 1240 | |
| 1241 | bool must_construct_frame() const { return must_construct_frame_; } |
| 1242 | void mark_must_construct_frame() { must_construct_frame_ = true; } |
| 1243 | |
| 1244 | bool must_deconstruct_frame() const { return must_deconstruct_frame_; } |
| 1245 | void mark_must_deconstruct_frame() { must_deconstruct_frame_ = true; } |
| 1246 | |
| 1247 | void set_last_deferred(RpoNumber last) { last_deferred_ = last; } |
| 1248 | RpoNumber last_deferred() const { return last_deferred_; } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1249 | |
| 1250 | private: |
| 1251 | Successors successors_; |
| 1252 | Predecessors predecessors_; |
| 1253 | PhiInstructions phis_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1254 | RpoNumber ao_number_; // Assembly order number. |
| 1255 | const RpoNumber rpo_number_; |
| 1256 | const RpoNumber loop_header_; |
| 1257 | const RpoNumber loop_end_; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1258 | int32_t code_start_; // start index of arch-specific code. |
| 1259 | int32_t code_end_; // end index of arch-specific code. |
| 1260 | const bool deferred_; // Block contains deferred code. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1261 | const bool handler_; // Block is a handler entry point. |
| 1262 | bool needs_frame_; |
| 1263 | bool must_construct_frame_; |
| 1264 | bool must_deconstruct_frame_; |
| 1265 | RpoNumber last_deferred_; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1266 | }; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1267 | |
| 1268 | typedef ZoneDeque<Constant> ConstantDeque; |
| 1269 | typedef std::map<int, Constant, std::less<int>, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1270 | zone_allocator<std::pair<const int, Constant> > > ConstantMap; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1271 | |
| 1272 | typedef ZoneDeque<Instruction*> InstructionDeque; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1273 | typedef ZoneDeque<ReferenceMap*> ReferenceMapDeque; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1274 | typedef ZoneVector<InstructionBlock*> InstructionBlocks; |
| 1275 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1276 | |
| 1277 | // Forward declarations. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1278 | struct PrintableInstructionSequence; |
| 1279 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1280 | |
| 1281 | // Represents architecture-specific generated code before, during, and after |
| 1282 | // register allocation. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1283 | class InstructionSequence final : public ZoneObject { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1284 | public: |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1285 | static InstructionBlocks* InstructionBlocksFor(Zone* zone, |
| 1286 | const Schedule* schedule); |
| 1287 | // Puts the deferred blocks last. |
| 1288 | static void ComputeAssemblyOrder(InstructionBlocks* blocks); |
| 1289 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1290 | InstructionSequence(Isolate* isolate, Zone* zone, |
| 1291 | InstructionBlocks* instruction_blocks); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1292 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1293 | int NextVirtualRegister(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1294 | int VirtualRegisterCount() const { return next_virtual_register_; } |
| 1295 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1296 | const InstructionBlocks& instruction_blocks() const { |
| 1297 | return *instruction_blocks_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1298 | } |
| 1299 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1300 | int InstructionBlockCount() const { |
| 1301 | return static_cast<int>(instruction_blocks_->size()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1302 | } |
| 1303 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1304 | InstructionBlock* InstructionBlockAt(RpoNumber rpo_number) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1305 | return instruction_blocks_->at(rpo_number.ToSize()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1306 | } |
| 1307 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1308 | int LastLoopInstructionIndex(const InstructionBlock* block) { |
| 1309 | return instruction_blocks_->at(block->loop_end().ToSize() - 1) |
| 1310 | ->last_instruction_index(); |
| 1311 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1312 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1313 | const InstructionBlock* InstructionBlockAt(RpoNumber rpo_number) const { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1314 | return instruction_blocks_->at(rpo_number.ToSize()); |
| 1315 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1316 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1317 | InstructionBlock* GetInstructionBlock(int instruction_index) const; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1318 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1319 | static MachineRepresentation DefaultRepresentation() { |
| 1320 | return MachineType::PointerRepresentation(); |
| 1321 | } |
| 1322 | MachineRepresentation GetRepresentation(int virtual_register) const; |
| 1323 | void MarkAsRepresentation(MachineRepresentation rep, int virtual_register); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1324 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1325 | bool IsReference(int virtual_register) const { |
| 1326 | return GetRepresentation(virtual_register) == |
| 1327 | MachineRepresentation::kTagged; |
| 1328 | } |
| 1329 | bool IsFloat(int virtual_register) const { |
| 1330 | return IsFloatingPoint(GetRepresentation(virtual_register)); |
| 1331 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1332 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1333 | Instruction* GetBlockStart(RpoNumber rpo) const; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1334 | |
| 1335 | typedef InstructionDeque::const_iterator const_iterator; |
| 1336 | const_iterator begin() const { return instructions_.begin(); } |
| 1337 | const_iterator end() const { return instructions_.end(); } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1338 | const InstructionDeque& instructions() const { return instructions_; } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1339 | int LastInstructionIndex() const { |
| 1340 | return static_cast<int>(instructions().size()) - 1; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1341 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1342 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1343 | Instruction* InstructionAt(int index) const { |
| 1344 | DCHECK(index >= 0); |
| 1345 | DCHECK(index < static_cast<int>(instructions_.size())); |
| 1346 | return instructions_[index]; |
| 1347 | } |
| 1348 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1349 | Isolate* isolate() const { return isolate_; } |
| 1350 | const ReferenceMapDeque* reference_maps() const { return &reference_maps_; } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1351 | Zone* zone() const { return zone_; } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1352 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1353 | // Used by the instruction selector while adding instructions. |
| 1354 | int AddInstruction(Instruction* instr); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1355 | void StartBlock(RpoNumber rpo); |
| 1356 | void EndBlock(RpoNumber rpo); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1357 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1358 | int AddConstant(int virtual_register, Constant constant) { |
| 1359 | // TODO(titzer): allow RPO numbers as constants? |
| 1360 | DCHECK(constant.type() != Constant::kRpoNumber); |
| 1361 | DCHECK(virtual_register >= 0 && virtual_register < next_virtual_register_); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1362 | DCHECK(constants_.find(virtual_register) == constants_.end()); |
| 1363 | constants_.insert(std::make_pair(virtual_register, constant)); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1364 | return virtual_register; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1365 | } |
| 1366 | Constant GetConstant(int virtual_register) const { |
| 1367 | ConstantMap::const_iterator it = constants_.find(virtual_register); |
| 1368 | DCHECK(it != constants_.end()); |
| 1369 | DCHECK_EQ(virtual_register, it->first); |
| 1370 | return it->second; |
| 1371 | } |
| 1372 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1373 | typedef ZoneVector<Constant> Immediates; |
| 1374 | Immediates& immediates() { return immediates_; } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1375 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1376 | ImmediateOperand AddImmediate(const Constant& constant) { |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1377 | if (constant.type() == Constant::kInt32 && |
| 1378 | RelocInfo::IsNone(constant.rmode())) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1379 | return ImmediateOperand(ImmediateOperand::INLINE, constant.ToInt32()); |
| 1380 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1381 | int index = static_cast<int>(immediates_.size()); |
| 1382 | immediates_.push_back(constant); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1383 | return ImmediateOperand(ImmediateOperand::INDEXED, index); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1384 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1385 | |
| 1386 | Constant GetImmediate(const ImmediateOperand* op) const { |
| 1387 | switch (op->type()) { |
| 1388 | case ImmediateOperand::INLINE: |
| 1389 | return Constant(op->inline_value()); |
| 1390 | case ImmediateOperand::INDEXED: { |
| 1391 | int index = op->indexed_value(); |
| 1392 | DCHECK(index >= 0); |
| 1393 | DCHECK(index < static_cast<int>(immediates_.size())); |
| 1394 | return immediates_[index]; |
| 1395 | } |
| 1396 | } |
| 1397 | UNREACHABLE(); |
| 1398 | return Constant(static_cast<int32_t>(0)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1399 | } |
| 1400 | |
| 1401 | class StateId { |
| 1402 | public: |
| 1403 | static StateId FromInt(int id) { return StateId(id); } |
| 1404 | int ToInt() const { return id_; } |
| 1405 | |
| 1406 | private: |
| 1407 | explicit StateId(int id) : id_(id) {} |
| 1408 | int id_; |
| 1409 | }; |
| 1410 | |
| 1411 | StateId AddFrameStateDescriptor(FrameStateDescriptor* descriptor); |
| 1412 | FrameStateDescriptor* GetFrameStateDescriptor(StateId deoptimization_id); |
| 1413 | int GetFrameStateDescriptorCount(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1414 | DeoptimizationVector const& frame_state_descriptors() const { |
| 1415 | return deoptimization_entries_; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1416 | } |
| 1417 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1418 | RpoNumber InputRpo(Instruction* instr, size_t index); |
| 1419 | |
| 1420 | bool GetSourcePosition(const Instruction* instr, |
| 1421 | SourcePosition* result) const; |
| 1422 | void SetSourcePosition(const Instruction* instr, SourcePosition value); |
| 1423 | |
| 1424 | bool ContainsCall() const { |
| 1425 | for (Instruction* instr : instructions_) { |
| 1426 | if (instr->IsCall()) return true; |
| 1427 | } |
| 1428 | return false; |
| 1429 | } |
| 1430 | void Print(const RegisterConfiguration* config) const; |
| 1431 | void Print() const; |
| 1432 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1433 | void PrintBlock(const RegisterConfiguration* config, int block_id) const; |
| 1434 | void PrintBlock(int block_id) const; |
| 1435 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1436 | void ValidateEdgeSplitForm() const; |
| 1437 | void ValidateDeferredBlockExitPaths() const; |
| 1438 | void ValidateDeferredBlockEntryPaths() const; |
| 1439 | void ValidateSSA() const; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1440 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1441 | private: |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1442 | friend std::ostream& operator<<(std::ostream& os, |
| 1443 | const PrintableInstructionSequence& code); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1444 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1445 | typedef ZoneMap<const Instruction*, SourcePosition> SourcePositionMap; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1446 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1447 | Isolate* isolate_; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1448 | Zone* const zone_; |
| 1449 | InstructionBlocks* const instruction_blocks_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1450 | SourcePositionMap source_positions_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1451 | ConstantMap constants_; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1452 | Immediates immediates_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1453 | InstructionDeque instructions_; |
| 1454 | int next_virtual_register_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1455 | ReferenceMapDeque reference_maps_; |
| 1456 | ZoneVector<MachineRepresentation> representations_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1457 | DeoptimizationVector deoptimization_entries_; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1458 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1459 | // Used at construction time |
| 1460 | InstructionBlock* current_block_; |
| 1461 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1462 | DISALLOW_COPY_AND_ASSIGN(InstructionSequence); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1463 | }; |
| 1464 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1465 | |
| 1466 | struct PrintableInstructionSequence { |
| 1467 | const RegisterConfiguration* register_configuration_; |
| 1468 | const InstructionSequence* sequence_; |
| 1469 | }; |
| 1470 | |
| 1471 | |
| 1472 | std::ostream& operator<<(std::ostream& os, |
| 1473 | const PrintableInstructionSequence& code); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1474 | |
| 1475 | } // namespace compiler |
| 1476 | } // namespace internal |
| 1477 | } // namespace v8 |
| 1478 | |
| 1479 | #endif // V8_COMPILER_INSTRUCTION_H_ |