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.
5 #ifndef V8_COMPILER_INSTRUCTION_H_
6 #define V8_COMPILER_INSTRUCTION_H_
13 #include "src/compiler/common-operator.h"
14 #include "src/compiler/frame.h"
15 #include "src/compiler/instruction-codes.h"
16 #include "src/compiler/opcodes.h"
17 #include "src/compiler/register-configuration.h"
18 #include "src/compiler/source-position.h"
19 #include "src/zone-allocator.h"
27 // A couple of reserved opcodes are used for internal use.
28 const InstructionCode kGapInstruction = -1;
29 const InstructionCode kSourcePositionInstruction = -2;
31 #define INSTRUCTION_OPERAND_LIST(V) \
32 V(Constant, CONSTANT) \
33 V(Immediate, IMMEDIATE) \
34 V(StackSlot, STACK_SLOT) \
35 V(DoubleStackSlot, DOUBLE_STACK_SLOT) \
36 V(Register, REGISTER) \
37 V(DoubleRegister, DOUBLE_REGISTER)
39 class InstructionOperand {
41 static const int kInvalidVirtualRegister = -1;
54 InstructionOperand() { ConvertTo(INVALID, 0, kInvalidVirtualRegister); }
56 InstructionOperand(Kind kind, int index) {
57 DCHECK(kind != UNALLOCATED && kind != INVALID);
58 ConvertTo(kind, index, kInvalidVirtualRegister);
61 static InstructionOperand* New(Zone* zone, Kind kind, int index) {
62 return New(zone, InstructionOperand(kind, index));
65 Kind kind() const { return KindField::decode(value_); }
66 // TODO(dcarney): move this to subkind operand.
68 DCHECK(kind() != UNALLOCATED && kind() != INVALID);
69 return static_cast<int64_t>(value_) >> IndexField::kShift;
71 #define INSTRUCTION_OPERAND_PREDICATE(name, type) \
72 bool Is##name() const { return kind() == type; }
73 INSTRUCTION_OPERAND_LIST(INSTRUCTION_OPERAND_PREDICATE)
74 INSTRUCTION_OPERAND_PREDICATE(Unallocated, UNALLOCATED)
75 INSTRUCTION_OPERAND_PREDICATE(Invalid, INVALID)
76 #undef INSTRUCTION_OPERAND_PREDICATE
77 bool Equals(const InstructionOperand* other) const {
78 return value_ == other->value_;
81 void ConvertTo(Kind kind, int index) {
82 DCHECK(kind != UNALLOCATED && kind != INVALID);
83 ConvertTo(kind, index, kInvalidVirtualRegister);
86 // Useful for map/set keys.
87 bool operator<(const InstructionOperand& op) const {
88 return value_ < op.value_;
92 template <typename SubKindOperand>
93 static SubKindOperand* New(Zone* zone, const SubKindOperand& op) {
94 void* buffer = zone->New(sizeof(op));
95 return new (buffer) SubKindOperand(op);
98 InstructionOperand(Kind kind, int index, int virtual_register) {
99 ConvertTo(kind, index, virtual_register);
102 void ConvertTo(Kind kind, int index, int virtual_register) {
103 if (kind == REGISTER || kind == DOUBLE_REGISTER) DCHECK(index >= 0);
104 if (kind != UNALLOCATED) {
105 DCHECK(virtual_register == kInvalidVirtualRegister);
107 value_ = KindField::encode(kind);
109 VirtualRegisterField::encode(static_cast<uint32_t>(virtual_register));
110 value_ |= static_cast<int64_t>(index) << IndexField::kShift;
111 DCHECK(((kind == UNALLOCATED || kind == INVALID) && index == 0) ||
112 this->index() == index);
115 typedef BitField64<Kind, 0, 3> KindField;
116 typedef BitField64<uint32_t, 3, 32> VirtualRegisterField;
117 typedef BitField64<int32_t, 35, 29> IndexField;
122 struct PrintableInstructionOperand {
123 const RegisterConfiguration* register_configuration_;
124 const InstructionOperand* op_;
127 std::ostream& operator<<(std::ostream& os,
128 const PrintableInstructionOperand& op);
130 class UnallocatedOperand : public InstructionOperand {
132 enum BasicPolicy { FIXED_SLOT, EXTENDED_POLICY };
134 enum ExtendedPolicy {
138 FIXED_DOUBLE_REGISTER,
144 // Lifetime of operand inside the instruction.
146 // USED_AT_START operand is guaranteed to be live only at
147 // instruction start. Register allocator is free to assign the same register
148 // to some other operand used inside instruction (i.e. temporary or
152 // USED_AT_END operand is treated as live until the end of
153 // instruction. This means that register allocator will not reuse it's
154 // register for any other operand inside instruction.
158 UnallocatedOperand(ExtendedPolicy policy, int virtual_register)
159 : InstructionOperand(UNALLOCATED, 0, virtual_register) {
160 value_ |= BasicPolicyField::encode(EXTENDED_POLICY);
161 value_ |= ExtendedPolicyField::encode(policy);
162 value_ |= LifetimeField::encode(USED_AT_END);
165 UnallocatedOperand(BasicPolicy policy, int index, int virtual_register)
166 : InstructionOperand(UNALLOCATED, 0, virtual_register) {
167 DCHECK(policy == FIXED_SLOT);
168 value_ |= BasicPolicyField::encode(policy);
169 value_ |= static_cast<int64_t>(index) << FixedSlotIndexField::kShift;
170 DCHECK(this->fixed_slot_index() == index);
173 UnallocatedOperand(ExtendedPolicy policy, int index, int virtual_register)
174 : InstructionOperand(UNALLOCATED, 0, virtual_register) {
175 DCHECK(policy == FIXED_REGISTER || policy == FIXED_DOUBLE_REGISTER);
176 value_ |= BasicPolicyField::encode(EXTENDED_POLICY);
177 value_ |= ExtendedPolicyField::encode(policy);
178 value_ |= LifetimeField::encode(USED_AT_END);
179 value_ |= FixedRegisterField::encode(index);
182 UnallocatedOperand(ExtendedPolicy policy, Lifetime lifetime,
183 int virtual_register)
184 : InstructionOperand(UNALLOCATED, 0, virtual_register) {
185 value_ |= BasicPolicyField::encode(EXTENDED_POLICY);
186 value_ |= ExtendedPolicyField::encode(policy);
187 value_ |= LifetimeField::encode(lifetime);
190 UnallocatedOperand* Copy(Zone* zone) { return New(zone, *this); }
192 UnallocatedOperand* CopyUnconstrained(Zone* zone) {
193 return New(zone, UnallocatedOperand(ANY, virtual_register()));
196 static const UnallocatedOperand* cast(const InstructionOperand* op) {
197 DCHECK(op->IsUnallocated());
198 return static_cast<const UnallocatedOperand*>(op);
201 static UnallocatedOperand* cast(InstructionOperand* op) {
202 DCHECK(op->IsUnallocated());
203 return static_cast<UnallocatedOperand*>(op);
206 static UnallocatedOperand cast(const InstructionOperand& op) {
207 DCHECK(op.IsUnallocated());
208 return *static_cast<const UnallocatedOperand*>(&op);
211 // The encoding used for UnallocatedOperand operands depends on the policy
213 // stored within the operand. The FIXED_SLOT policy uses a compact encoding
214 // because it accommodates a larger pay-load.
216 // For FIXED_SLOT policy:
217 // +------------------------------------------------+
218 // | slot_index | 0 | virtual_register | 001 |
219 // +------------------------------------------------+
221 // For all other (extended) policies:
222 // +-----------------------------------------------------+
223 // | reg_index | L | PPP | 1 | virtual_register | 001 |
224 // +-----------------------------------------------------+
228 // The slot index is a signed value which requires us to decode it manually
229 // instead of using the BitField utility class.
231 // All bits fit into the index field.
232 STATIC_ASSERT(IndexField::kShift == 35);
234 // BitFields for all unallocated operands.
235 class BasicPolicyField : public BitField64<BasicPolicy, 35, 1> {};
237 // BitFields specific to BasicPolicy::FIXED_SLOT.
238 class FixedSlotIndexField : public BitField64<int, 36, 28> {};
240 // BitFields specific to BasicPolicy::EXTENDED_POLICY.
241 class ExtendedPolicyField : public BitField64<ExtendedPolicy, 36, 3> {};
242 class LifetimeField : public BitField64<Lifetime, 39, 1> {};
243 class FixedRegisterField : public BitField64<int, 40, 6> {};
245 // Predicates for the operand policy.
246 bool HasAnyPolicy() const {
247 return basic_policy() == EXTENDED_POLICY && extended_policy() == ANY;
249 bool HasFixedPolicy() const {
250 return basic_policy() == FIXED_SLOT ||
251 extended_policy() == FIXED_REGISTER ||
252 extended_policy() == FIXED_DOUBLE_REGISTER;
254 bool HasRegisterPolicy() const {
255 return basic_policy() == EXTENDED_POLICY &&
256 extended_policy() == MUST_HAVE_REGISTER;
258 bool HasSlotPolicy() const {
259 return basic_policy() == EXTENDED_POLICY &&
260 extended_policy() == MUST_HAVE_SLOT;
262 bool HasSameAsInputPolicy() const {
263 return basic_policy() == EXTENDED_POLICY &&
264 extended_policy() == SAME_AS_FIRST_INPUT;
266 bool HasFixedSlotPolicy() const { return basic_policy() == FIXED_SLOT; }
267 bool HasFixedRegisterPolicy() const {
268 return basic_policy() == EXTENDED_POLICY &&
269 extended_policy() == FIXED_REGISTER;
271 bool HasFixedDoubleRegisterPolicy() const {
272 return basic_policy() == EXTENDED_POLICY &&
273 extended_policy() == FIXED_DOUBLE_REGISTER;
276 // [basic_policy]: Distinguish between FIXED_SLOT and all other policies.
277 BasicPolicy basic_policy() const {
278 DCHECK_EQ(UNALLOCATED, kind());
279 return BasicPolicyField::decode(value_);
282 // [extended_policy]: Only for non-FIXED_SLOT. The finer-grained policy.
283 ExtendedPolicy extended_policy() const {
284 DCHECK(basic_policy() == EXTENDED_POLICY);
285 return ExtendedPolicyField::decode(value_);
288 // [fixed_slot_index]: Only for FIXED_SLOT.
289 int fixed_slot_index() const {
290 DCHECK(HasFixedSlotPolicy());
291 return static_cast<int>(static_cast<int64_t>(value_) >>
292 FixedSlotIndexField::kShift);
295 // [fixed_register_index]: Only for FIXED_REGISTER or FIXED_DOUBLE_REGISTER.
296 int fixed_register_index() const {
297 DCHECK(HasFixedRegisterPolicy() || HasFixedDoubleRegisterPolicy());
298 return FixedRegisterField::decode(value_);
301 // [virtual_register]: The virtual register ID for this operand.
302 int32_t virtual_register() const {
303 DCHECK_EQ(UNALLOCATED, kind());
304 return static_cast<int32_t>(VirtualRegisterField::decode(value_));
307 // TODO(dcarney): remove this.
308 void set_virtual_register(int32_t id) {
309 DCHECK_EQ(UNALLOCATED, kind());
310 value_ = VirtualRegisterField::update(value_, static_cast<uint32_t>(id));
313 // [lifetime]: Only for non-FIXED_SLOT.
314 bool IsUsedAtStart() const {
315 DCHECK(basic_policy() == EXTENDED_POLICY);
316 return LifetimeField::decode(value_) == USED_AT_START;
321 class MoveOperands FINAL {
323 MoveOperands(InstructionOperand* source, InstructionOperand* destination)
324 : source_(source), destination_(destination) {}
326 InstructionOperand* source() const { return source_; }
327 void set_source(InstructionOperand* operand) { source_ = operand; }
329 InstructionOperand* destination() const { return destination_; }
330 void set_destination(InstructionOperand* operand) { destination_ = operand; }
332 // The gap resolver marks moves as "in-progress" by clearing the
333 // destination (but not the source).
334 bool IsPending() const { return destination_ == NULL && source_ != NULL; }
336 // True if this move a move into the given destination operand.
337 bool Blocks(InstructionOperand* operand) const {
338 return !IsEliminated() && source()->Equals(operand);
341 // A move is redundant if it's been eliminated, if its source and
342 // destination are the same, or if its destination is constant.
343 bool IsRedundant() const {
344 return IsEliminated() || source_->Equals(destination_) ||
345 (destination_ != NULL && destination_->IsConstant());
348 // We clear both operands to indicate move that's been eliminated.
349 void Eliminate() { source_ = destination_ = NULL; }
350 bool IsEliminated() const {
351 DCHECK(source_ != NULL || destination_ == NULL);
352 return source_ == NULL;
356 InstructionOperand* source_;
357 InstructionOperand* destination_;
361 struct PrintableMoveOperands {
362 const RegisterConfiguration* register_configuration_;
363 const MoveOperands* move_operands_;
367 std::ostream& operator<<(std::ostream& os, const PrintableMoveOperands& mo);
370 #define INSTRUCTION_SUBKIND_OPERAND_CLASS(SubKind, kOperandKind) \
371 class SubKind##Operand FINAL : public InstructionOperand { \
373 explicit SubKind##Operand(int index) \
374 : InstructionOperand(kOperandKind, index) {} \
376 static SubKind##Operand* New(int index, Zone* zone) { \
377 return InstructionOperand::New(zone, SubKind##Operand(index)); \
380 static SubKind##Operand* cast(InstructionOperand* op) { \
381 DCHECK(op->kind() == kOperandKind); \
382 return reinterpret_cast<SubKind##Operand*>(op); \
385 static const SubKind##Operand* cast(const InstructionOperand* op) { \
386 DCHECK(op->kind() == kOperandKind); \
387 return reinterpret_cast<const SubKind##Operand*>(op); \
390 static SubKind##Operand cast(const InstructionOperand& op) { \
391 DCHECK(op.kind() == kOperandKind); \
392 return *static_cast<const SubKind##Operand*>(&op); \
395 INSTRUCTION_OPERAND_LIST(INSTRUCTION_SUBKIND_OPERAND_CLASS)
396 #undef INSTRUCTION_SUBKIND_OPERAND_CLASS
399 class ParallelMove FINAL : public ZoneObject {
401 explicit ParallelMove(Zone* zone) : move_operands_(4, zone) {}
403 void AddMove(InstructionOperand* from, InstructionOperand* to, Zone* zone) {
404 move_operands_.Add(MoveOperands(from, to), zone);
407 bool IsRedundant() const;
409 ZoneList<MoveOperands>* move_operands() { return &move_operands_; }
410 const ZoneList<MoveOperands>* move_operands() const {
411 return &move_operands_;
414 // Prepare this ParallelMove to insert move as if it happened in a subsequent
415 // ParallelMove. move->source() may be changed. The MoveOperand returned
416 // must be Eliminated and, as it points directly into move_operands_, it must
417 // be Eliminated before any further mutation.
418 MoveOperands* PrepareInsertAfter(MoveOperands* move) const;
421 ZoneList<MoveOperands> move_operands_;
425 struct PrintableParallelMove {
426 const RegisterConfiguration* register_configuration_;
427 const ParallelMove* parallel_move_;
431 std::ostream& operator<<(std::ostream& os, const PrintableParallelMove& pm);
434 class PointerMap FINAL : public ZoneObject {
436 explicit PointerMap(Zone* zone)
437 : pointer_operands_(8, zone),
438 untagged_operands_(0, zone),
439 instruction_position_(-1) {}
441 const ZoneList<InstructionOperand*>* GetNormalizedOperands() {
442 for (int i = 0; i < untagged_operands_.length(); ++i) {
443 RemovePointer(untagged_operands_[i]);
445 untagged_operands_.Clear();
446 return &pointer_operands_;
448 int instruction_position() const { return instruction_position_; }
450 void set_instruction_position(int pos) {
451 DCHECK(instruction_position_ == -1);
452 instruction_position_ = pos;
455 void RecordPointer(InstructionOperand* op, Zone* zone);
456 void RemovePointer(InstructionOperand* op);
457 void RecordUntagged(InstructionOperand* op, Zone* zone);
460 friend std::ostream& operator<<(std::ostream& os, const PointerMap& pm);
462 ZoneList<InstructionOperand*> pointer_operands_;
463 ZoneList<InstructionOperand*> untagged_operands_;
464 int instruction_position_;
467 std::ostream& operator<<(std::ostream& os, const PointerMap& pm);
469 // TODO(titzer): s/PointerMap/ReferenceMap/
472 size_t OutputCount() const { return OutputCountField::decode(bit_field_); }
473 const InstructionOperand* OutputAt(size_t i) const {
474 DCHECK(i < OutputCount());
475 return &operands_[i];
477 InstructionOperand* OutputAt(size_t i) {
478 DCHECK(i < OutputCount());
479 return &operands_[i];
482 bool HasOutput() const { return OutputCount() == 1; }
483 const InstructionOperand* Output() const { return OutputAt(0); }
484 InstructionOperand* Output() { return OutputAt(0); }
486 size_t InputCount() const { return InputCountField::decode(bit_field_); }
487 const InstructionOperand* InputAt(size_t i) const {
488 DCHECK(i < InputCount());
489 return &operands_[OutputCount() + i];
491 InstructionOperand* InputAt(size_t i) {
492 DCHECK(i < InputCount());
493 return &operands_[OutputCount() + i];
496 size_t TempCount() const { return TempCountField::decode(bit_field_); }
497 const InstructionOperand* TempAt(size_t i) const {
498 DCHECK(i < TempCount());
499 return &operands_[OutputCount() + InputCount() + i];
501 InstructionOperand* TempAt(size_t i) {
502 DCHECK(i < TempCount());
503 return &operands_[OutputCount() + InputCount() + i];
506 InstructionCode opcode() const { return opcode_; }
507 ArchOpcode arch_opcode() const { return ArchOpcodeField::decode(opcode()); }
508 AddressingMode addressing_mode() const {
509 return AddressingModeField::decode(opcode());
511 FlagsMode flags_mode() const { return FlagsModeField::decode(opcode()); }
512 FlagsCondition flags_condition() const {
513 return FlagsConditionField::decode(opcode());
516 // TODO(titzer): make call into a flags.
517 static Instruction* New(Zone* zone, InstructionCode opcode) {
518 return New(zone, opcode, 0, NULL, 0, NULL, 0, NULL);
521 static Instruction* New(Zone* zone, InstructionCode opcode,
522 size_t output_count, InstructionOperand* outputs,
523 size_t input_count, InstructionOperand* inputs,
524 size_t temp_count, InstructionOperand* temps) {
526 DCHECK(output_count == 0 || outputs != NULL);
527 DCHECK(input_count == 0 || inputs != NULL);
528 DCHECK(temp_count == 0 || temps != NULL);
529 size_t total_extra_ops = output_count + input_count + temp_count;
530 if (total_extra_ops != 0) total_extra_ops--;
531 int size = static_cast<int>(
532 RoundUp(sizeof(Instruction), sizeof(InstructionOperand)) +
533 total_extra_ops * sizeof(InstructionOperand));
534 return new (zone->New(size)) Instruction(
535 opcode, output_count, outputs, input_count, inputs, temp_count, temps);
538 Instruction* MarkAsCall() {
539 bit_field_ = IsCallField::update(bit_field_, true);
542 bool IsCall() const { return IsCallField::decode(bit_field_); }
543 bool NeedsPointerMap() const { return IsCall(); }
544 bool HasPointerMap() const { return pointer_map_ != NULL; }
546 bool IsGapMoves() const { return opcode() == kGapInstruction; }
547 bool IsSourcePosition() const {
548 return opcode() == kSourcePositionInstruction;
551 bool ClobbersRegisters() const { return IsCall(); }
552 bool ClobbersTemps() const { return IsCall(); }
553 bool ClobbersDoubleRegisters() const { return IsCall(); }
554 PointerMap* pointer_map() const { return pointer_map_; }
556 void set_pointer_map(PointerMap* map) {
557 DCHECK(NeedsPointerMap());
558 DCHECK(!pointer_map_);
562 void OverwriteWithNop() {
563 opcode_ = ArchOpcodeField::encode(kArchNop);
569 return arch_opcode() == kArchNop && InputCount() == 0 &&
570 OutputCount() == 0 && TempCount() == 0;
574 explicit Instruction(InstructionCode opcode);
575 Instruction(InstructionCode opcode, size_t output_count,
576 InstructionOperand* outputs, size_t input_count,
577 InstructionOperand* inputs, size_t temp_count,
578 InstructionOperand* temps);
580 typedef BitField<size_t, 0, 8> OutputCountField;
581 typedef BitField<size_t, 8, 16> InputCountField;
582 typedef BitField<size_t, 24, 6> TempCountField;
583 typedef BitField<bool, 30, 1> IsCallField;
585 InstructionCode opcode_;
587 PointerMap* pointer_map_;
588 InstructionOperand operands_[1];
591 DISALLOW_COPY_AND_ASSIGN(Instruction);
595 struct PrintableInstruction {
596 const RegisterConfiguration* register_configuration_;
597 const Instruction* instr_;
599 std::ostream& operator<<(std::ostream& os, const PrintableInstruction& instr);
602 // Represents moves inserted before an instruction due to register allocation.
603 // TODO(titzer): squash GapInstruction back into Instruction, since essentially
604 // every instruction can possibly have moves inserted before it.
605 class GapInstruction : public Instruction {
610 FIRST_INNER_POSITION = START,
611 LAST_INNER_POSITION = END
614 ParallelMove* GetOrCreateParallelMove(InnerPosition pos, Zone* zone) {
615 if (parallel_moves_[pos] == NULL) {
616 parallel_moves_[pos] = new (zone) ParallelMove(zone);
618 return parallel_moves_[pos];
621 ParallelMove* GetParallelMove(InnerPosition pos) {
622 return parallel_moves_[pos];
625 const ParallelMove* GetParallelMove(InnerPosition pos) const {
626 return parallel_moves_[pos];
629 bool IsRedundant() const;
631 ParallelMove** parallel_moves() { return parallel_moves_; }
633 static GapInstruction* New(Zone* zone) {
634 void* buffer = zone->New(sizeof(GapInstruction));
635 return new (buffer) GapInstruction(kGapInstruction);
638 static GapInstruction* cast(Instruction* instr) {
639 DCHECK(instr->IsGapMoves());
640 return static_cast<GapInstruction*>(instr);
643 static const GapInstruction* cast(const Instruction* instr) {
644 DCHECK(instr->IsGapMoves());
645 return static_cast<const GapInstruction*>(instr);
649 explicit GapInstruction(InstructionCode opcode) : Instruction(opcode) {
650 parallel_moves_[START] = NULL;
651 parallel_moves_[END] = NULL;
655 friend std::ostream& operator<<(std::ostream& os,
656 const PrintableInstruction& instr);
657 ParallelMove* parallel_moves_[LAST_INNER_POSITION + 1];
661 class SourcePositionInstruction FINAL : public Instruction {
663 static SourcePositionInstruction* New(Zone* zone, SourcePosition position) {
664 void* buffer = zone->New(sizeof(SourcePositionInstruction));
665 return new (buffer) SourcePositionInstruction(position);
668 SourcePosition source_position() const { return source_position_; }
670 static SourcePositionInstruction* cast(Instruction* instr) {
671 DCHECK(instr->IsSourcePosition());
672 return static_cast<SourcePositionInstruction*>(instr);
675 static const SourcePositionInstruction* cast(const Instruction* instr) {
676 DCHECK(instr->IsSourcePosition());
677 return static_cast<const SourcePositionInstruction*>(instr);
681 explicit SourcePositionInstruction(SourcePosition source_position)
682 : Instruction(kSourcePositionInstruction),
683 source_position_(source_position) {
684 DCHECK(!source_position_.IsInvalid());
685 DCHECK(!source_position_.IsUnknown());
688 SourcePosition source_position_;
692 class RpoNumber FINAL {
694 static const int kInvalidRpoNumber = -1;
699 size_t ToSize() const {
701 return static_cast<size_t>(index_);
703 bool IsValid() const { return index_ >= 0; }
704 static RpoNumber FromInt(int index) { return RpoNumber(index); }
705 static RpoNumber Invalid() { return RpoNumber(kInvalidRpoNumber); }
707 bool IsNext(const RpoNumber other) const {
709 return other.index_ == this->index_ + 1;
712 bool operator==(RpoNumber other) const {
713 return this->index_ == other.index_;
717 explicit RpoNumber(int32_t index) : index_(index) {}
722 std::ostream& operator<<(std::ostream&, const RpoNumber&);
725 class Constant FINAL {
737 explicit Constant(int32_t v);
738 explicit Constant(int64_t v) : type_(kInt64), value_(v) {}
739 explicit Constant(float v) : type_(kFloat32), value_(bit_cast<int32_t>(v)) {}
740 explicit Constant(double v) : type_(kFloat64), value_(bit_cast<int64_t>(v)) {}
741 explicit Constant(ExternalReference ref)
742 : type_(kExternalReference), value_(bit_cast<intptr_t>(ref)) {}
743 explicit Constant(Handle<HeapObject> obj)
744 : type_(kHeapObject), value_(bit_cast<intptr_t>(obj)) {}
745 explicit Constant(RpoNumber rpo) : type_(kRpoNumber), value_(rpo.ToInt()) {}
747 Type type() const { return type_; }
749 int32_t ToInt32() const {
750 DCHECK(type() == kInt32 || type() == kInt64);
751 const int32_t value = static_cast<int32_t>(value_);
752 DCHECK_EQ(value_, static_cast<int64_t>(value));
756 int64_t ToInt64() const {
757 if (type() == kInt32) return ToInt32();
758 DCHECK_EQ(kInt64, type());
762 float ToFloat32() const {
763 DCHECK_EQ(kFloat32, type());
764 return bit_cast<float>(static_cast<int32_t>(value_));
767 double ToFloat64() const {
768 if (type() == kInt32) return ToInt32();
769 DCHECK_EQ(kFloat64, type());
770 return bit_cast<double>(value_);
773 ExternalReference ToExternalReference() const {
774 DCHECK_EQ(kExternalReference, type());
775 return bit_cast<ExternalReference>(static_cast<intptr_t>(value_));
778 RpoNumber ToRpoNumber() const {
779 DCHECK_EQ(kRpoNumber, type());
780 return RpoNumber::FromInt(static_cast<int>(value_));
783 Handle<HeapObject> ToHeapObject() const {
784 DCHECK_EQ(kHeapObject, type());
785 return bit_cast<Handle<HeapObject> >(static_cast<intptr_t>(value_));
794 class FrameStateDescriptor : public ZoneObject {
796 FrameStateDescriptor(Zone* zone, const FrameStateCallInfo& state_info,
797 size_t parameters_count, size_t locals_count,
799 FrameStateDescriptor* outer_state = NULL);
801 FrameStateType type() const { return type_; }
802 BailoutId bailout_id() const { return bailout_id_; }
803 OutputFrameStateCombine state_combine() const { return frame_state_combine_; }
804 size_t parameters_count() const { return parameters_count_; }
805 size_t locals_count() const { return locals_count_; }
806 size_t stack_count() const { return stack_count_; }
807 FrameStateDescriptor* outer_state() const { return outer_state_; }
808 MaybeHandle<JSFunction> jsfunction() const { return jsfunction_; }
809 bool HasContext() const { return type_ == JS_FRAME; }
811 size_t GetSize(OutputFrameStateCombine combine =
812 OutputFrameStateCombine::Ignore()) const;
813 size_t GetTotalSize() const;
814 size_t GetFrameCount() const;
815 size_t GetJSFrameCount() const;
817 MachineType GetType(size_t index) const;
818 void SetType(size_t index, MachineType type);
821 FrameStateType type_;
822 BailoutId bailout_id_;
823 OutputFrameStateCombine frame_state_combine_;
824 size_t parameters_count_;
825 size_t locals_count_;
827 ZoneVector<MachineType> types_;
828 FrameStateDescriptor* outer_state_;
829 MaybeHandle<JSFunction> jsfunction_;
832 std::ostream& operator<<(std::ostream& os, const Constant& constant);
835 class PhiInstruction FINAL : public ZoneObject {
837 typedef ZoneVector<InstructionOperand> Inputs;
839 PhiInstruction(Zone* zone, int virtual_register, size_t input_count);
841 void SetInput(size_t offset, int virtual_register);
843 int virtual_register() const { return virtual_register_; }
844 const IntVector& operands() const { return operands_; }
846 const InstructionOperand& output() const { return output_; }
847 InstructionOperand& output() { return output_; }
848 const Inputs& inputs() const { return inputs_; }
849 Inputs& inputs() { return inputs_; }
852 // TODO(dcarney): some of these fields are only for verification, move them to
854 const int virtual_register_;
855 InstructionOperand output_;
861 // Analogue of BasicBlock for Instructions instead of Nodes.
862 class InstructionBlock FINAL : public ZoneObject {
864 InstructionBlock(Zone* zone, RpoNumber rpo_number, RpoNumber loop_header,
865 RpoNumber loop_end, bool deferred);
867 // Instruction indexes (used by the register allocator).
868 int first_instruction_index() const {
869 DCHECK(code_start_ >= 0);
870 DCHECK(code_end_ > 0);
871 DCHECK(code_end_ >= code_start_);
874 int last_instruction_index() const {
875 DCHECK(code_start_ >= 0);
876 DCHECK(code_end_ > 0);
877 DCHECK(code_end_ >= code_start_);
878 return code_end_ - 1;
881 int32_t code_start() const { return code_start_; }
882 void set_code_start(int32_t start) { code_start_ = start; }
884 int32_t code_end() const { return code_end_; }
885 void set_code_end(int32_t end) { code_end_ = end; }
887 bool IsDeferred() const { return deferred_; }
889 RpoNumber ao_number() const { return ao_number_; }
890 RpoNumber rpo_number() const { return rpo_number_; }
891 RpoNumber loop_header() const { return loop_header_; }
892 RpoNumber loop_end() const {
893 DCHECK(IsLoopHeader());
896 inline bool IsLoopHeader() const { return loop_end_.IsValid(); }
898 typedef ZoneVector<RpoNumber> Predecessors;
899 Predecessors& predecessors() { return predecessors_; }
900 const Predecessors& predecessors() const { return predecessors_; }
901 size_t PredecessorCount() const { return predecessors_.size(); }
902 size_t PredecessorIndexOf(RpoNumber rpo_number) const;
904 typedef ZoneVector<RpoNumber> Successors;
905 Successors& successors() { return successors_; }
906 const Successors& successors() const { return successors_; }
907 size_t SuccessorCount() const { return successors_.size(); }
909 typedef ZoneVector<PhiInstruction*> PhiInstructions;
910 const PhiInstructions& phis() const { return phis_; }
911 void AddPhi(PhiInstruction* phi) { phis_.push_back(phi); }
913 void set_ao_number(RpoNumber ao_number) { ao_number_ = ao_number; }
916 Successors successors_;
917 Predecessors predecessors_;
918 PhiInstructions phis_;
919 RpoNumber ao_number_; // Assembly order number.
920 const RpoNumber rpo_number_;
921 const RpoNumber loop_header_;
922 const RpoNumber loop_end_;
923 int32_t code_start_; // start index of arch-specific code.
924 int32_t code_end_; // end index of arch-specific code.
925 const bool deferred_; // Block contains deferred code.
928 typedef ZoneDeque<Constant> ConstantDeque;
929 typedef std::map<int, Constant, std::less<int>,
930 zone_allocator<std::pair<int, Constant> > > ConstantMap;
932 typedef ZoneDeque<Instruction*> InstructionDeque;
933 typedef ZoneDeque<PointerMap*> PointerMapDeque;
934 typedef ZoneVector<FrameStateDescriptor*> DeoptimizationVector;
935 typedef ZoneVector<InstructionBlock*> InstructionBlocks;
937 struct PrintableInstructionSequence;
940 // Represents architecture-specific generated code before, during, and after
941 // register allocation.
942 // TODO(titzer): s/IsDouble/IsFloat64/
943 class InstructionSequence FINAL : public ZoneObject {
945 static InstructionBlocks* InstructionBlocksFor(Zone* zone,
946 const Schedule* schedule);
947 // Puts the deferred blocks last.
948 static void ComputeAssemblyOrder(InstructionBlocks* blocks);
950 InstructionSequence(Isolate* isolate, Zone* zone,
951 InstructionBlocks* instruction_blocks);
953 int NextVirtualRegister();
954 int VirtualRegisterCount() const { return next_virtual_register_; }
956 const InstructionBlocks& instruction_blocks() const {
957 return *instruction_blocks_;
960 int InstructionBlockCount() const {
961 return static_cast<int>(instruction_blocks_->size());
964 InstructionBlock* InstructionBlockAt(RpoNumber rpo_number) {
965 return instruction_blocks_->at(rpo_number.ToSize());
968 int LastLoopInstructionIndex(const InstructionBlock* block) {
969 return instruction_blocks_->at(block->loop_end().ToSize() - 1)
970 ->last_instruction_index();
973 const InstructionBlock* InstructionBlockAt(RpoNumber rpo_number) const {
974 return instruction_blocks_->at(rpo_number.ToSize());
977 const InstructionBlock* GetInstructionBlock(int instruction_index) const;
979 bool IsReference(int virtual_register) const;
980 bool IsDouble(int virtual_register) const;
982 void MarkAsReference(int virtual_register);
983 void MarkAsDouble(int virtual_register);
985 void AddGapMove(int index, InstructionOperand* from, InstructionOperand* to);
987 GapInstruction* GetBlockStart(RpoNumber rpo) const;
989 typedef InstructionDeque::const_iterator const_iterator;
990 const_iterator begin() const { return instructions_.begin(); }
991 const_iterator end() const { return instructions_.end(); }
992 const InstructionDeque& instructions() const { return instructions_; }
994 GapInstruction* GapAt(int index) const {
995 return GapInstruction::cast(InstructionAt(index));
997 bool IsGapAt(int index) const { return InstructionAt(index)->IsGapMoves(); }
998 Instruction* InstructionAt(int index) const {
1000 DCHECK(index < static_cast<int>(instructions_.size()));
1001 return instructions_[index];
1004 Isolate* isolate() const { return isolate_; }
1005 const PointerMapDeque* pointer_maps() const { return &pointer_maps_; }
1006 Zone* zone() const { return zone_; }
1008 // Used by the instruction selector while adding instructions.
1009 int AddInstruction(Instruction* instr);
1010 void StartBlock(RpoNumber rpo);
1011 void EndBlock(RpoNumber rpo);
1013 int AddConstant(int virtual_register, Constant constant) {
1014 // TODO(titzer): allow RPO numbers as constants?
1015 DCHECK(constant.type() != Constant::kRpoNumber);
1016 DCHECK(virtual_register >= 0 && virtual_register < next_virtual_register_);
1017 DCHECK(constants_.find(virtual_register) == constants_.end());
1018 constants_.insert(std::make_pair(virtual_register, constant));
1019 return virtual_register;
1021 Constant GetConstant(int virtual_register) const {
1022 ConstantMap::const_iterator it = constants_.find(virtual_register);
1023 DCHECK(it != constants_.end());
1024 DCHECK_EQ(virtual_register, it->first);
1028 typedef ZoneVector<Constant> Immediates;
1029 Immediates& immediates() { return immediates_; }
1031 int AddImmediate(Constant constant) {
1032 int index = static_cast<int>(immediates_.size());
1033 immediates_.push_back(constant);
1036 Constant GetImmediate(int index) const {
1038 DCHECK(index < static_cast<int>(immediates_.size()));
1039 return immediates_[index];
1044 static StateId FromInt(int id) { return StateId(id); }
1045 int ToInt() const { return id_; }
1048 explicit StateId(int id) : id_(id) {}
1052 StateId AddFrameStateDescriptor(FrameStateDescriptor* descriptor);
1053 FrameStateDescriptor* GetFrameStateDescriptor(StateId deoptimization_id);
1054 int GetFrameStateDescriptorCount();
1056 RpoNumber InputRpo(Instruction* instr, size_t index) {
1057 InstructionOperand* operand = instr->InputAt(index);
1058 Constant constant = operand->IsImmediate() ? GetImmediate(operand->index())
1059 : GetConstant(operand->index());
1060 return constant.ToRpoNumber();
1064 friend std::ostream& operator<<(std::ostream& os,
1065 const PrintableInstructionSequence& code);
1067 typedef std::set<int, std::less<int>, ZoneIntAllocator> VirtualRegisterSet;
1071 InstructionBlocks* const instruction_blocks_;
1072 IntVector block_starts_;
1073 ConstantMap constants_;
1074 Immediates immediates_;
1075 InstructionDeque instructions_;
1076 int next_virtual_register_;
1077 PointerMapDeque pointer_maps_;
1078 VirtualRegisterSet doubles_;
1079 VirtualRegisterSet references_;
1080 DeoptimizationVector deoptimization_entries_;
1082 DISALLOW_COPY_AND_ASSIGN(InstructionSequence);
1086 struct PrintableInstructionSequence {
1087 const RegisterConfiguration* register_configuration_;
1088 const InstructionSequence* sequence_;
1092 std::ostream& operator<<(std::ostream& os,
1093 const PrintableInstructionSequence& code);
1095 } // namespace compiler
1096 } // namespace internal
1099 #endif // V8_COMPILER_INSTRUCTION_H_