scheduler.h 4.19 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2013 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_COMPILER_SCHEDULER_H_
#define V8_COMPILER_SCHEDULER_H_

8
#include "src/base/flags.h"
9
#include "src/compiler/node.h"
10 11
#include "src/compiler/opcodes.h"
#include "src/compiler/schedule.h"
12
#include "src/compiler/zone-pool.h"
13 14 15 16 17 18
#include "src/zone-containers.h"

namespace v8 {
namespace internal {
namespace compiler {

19
// Forward declarations.
20
class CFGBuilder;
21
class ControlEquivalence;
22
class Graph;
23 24
class SpecialRPONumberer;

25

26 27
// Computes a schedule from a graph, placing nodes into basic blocks and
// ordering the basic blocks in the special RPO order.
28 29
class Scheduler {
 public:
30 31 32 33
  // Flags that control the mode of operation.
  enum Flag { kNoFlags = 0u, kSplitNodes = 1u << 1 };
  typedef base::Flags<Flag> Flags;

34 35
  // The complete scheduling algorithm. Creates a new schedule and places all
  // nodes from the graph into it.
36
  static Schedule* ComputeSchedule(Zone* zone, Graph* graph, Flags flags);
37

38
  // Compute the RPO of blocks in an existing schedule.
39
  static BasicBlockVector* ComputeSpecialRPO(Zone* zone, Schedule* schedule);
40 41

 private:
42 43 44 45 46 47 48 49 50 51 52 53
  // Placement of a node changes during scheduling. The placement state
  // transitions over time while the scheduler is choosing a position:
  //
  //                   +---------------------+-----+----> kFixed
  //                  /                     /     /
  //    kUnknown ----+------> kCoupled ----+     /
  //                  \                         /
  //                   +----> kSchedulable ----+--------> kScheduled
  //
  // 1) GetPlacement(): kUnknown -> kCoupled|kSchedulable|kFixed
  // 2) UpdatePlacement(): kCoupled|kSchedulable -> kFixed|kScheduled
  enum Placement { kUnknown, kSchedulable, kFixed, kCoupled, kScheduled };
54 55 56

  // Per-node data tracked during scheduling.
  struct SchedulerData {
57
    BasicBlock* minimum_block_;  // Minimum legal RPO placement.
58
    int unscheduled_count_;      // Number of unscheduled uses of this node.
59 60
    Placement placement_;        // Whether the node is fixed, schedulable,
                                 // coupled to another node, or not yet known.
61 62
  };

63
  Zone* zone_;
64 65
  Graph* graph_;
  Schedule* schedule_;
66
  Flags flags_;
67 68 69 70
  NodeVectorVector scheduled_nodes_;     // Per-block list of nodes in reverse.
  NodeVector schedule_root_nodes_;       // Fixed root nodes seed the worklist.
  ZoneQueue<Node*> schedule_queue_;      // Worklist of schedulable nodes.
  ZoneVector<SchedulerData> node_data_;  // Per-node data for all nodes.
71
  CFGBuilder* control_flow_builder_;     // Builds basic blocks for controls.
72
  SpecialRPONumberer* special_rpo_;      // Special RPO numbering of blocks.
73
  ControlEquivalence* equivalence_;      // Control dependence equivalence.
74

75
  Scheduler(Zone* zone, Graph* graph, Schedule* schedule, Flags flags);
76

77 78
  inline SchedulerData DefaultSchedulerData();
  inline SchedulerData* GetData(Node* node);
79 80

  Placement GetPlacement(Node* node);
81
  void UpdatePlacement(Node* node, Placement placement);
82

83 84 85
  inline bool IsCoupledControlEdge(Node* node, int index);
  void IncrementUnscheduledUseCount(Node* node, int index, Node* from);
  void DecrementUnscheduledUseCount(Node* node, int index, Node* from);
86

87
  void PropagateImmediateDominators(BasicBlock* block);
88

89
  // Phase 1: Build control-flow graph.
90
  friend class CFGBuilder;
91
  void BuildCFG();
92 93 94 95

  // Phase 2: Compute special RPO and dominator tree.
  friend class SpecialRPONumberer;
  void ComputeSpecialRPONumbering();
96
  void GenerateImmediateDominatorTree();
97

98
  // Phase 3: Prepare use counts for nodes.
99 100 101
  friend class PrepareUsesVisitor;
  void PrepareUses();

102
  // Phase 4: Schedule nodes early.
103 104 105
  friend class ScheduleEarlyNodeVisitor;
  void ScheduleEarly();

106
  // Phase 5: Schedule nodes late.
107 108
  friend class ScheduleLateNodeVisitor;
  void ScheduleLate();
109

110 111 112
  // Phase 6: Seal the final schedule.
  void SealFinalSchedule();

113 114
  void FuseFloatingControl(BasicBlock* block, Node* node);
  void MovePlannedNodes(BasicBlock* from, BasicBlock* to);
115
};
116

117 118 119

DEFINE_OPERATORS_FOR_FLAGS(Scheduler::Flags)

120 121 122
}  // namespace compiler
}  // namespace internal
}  // namespace v8
123 124

#endif  // V8_COMPILER_SCHEDULER_H_