Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1 | // Copyright 2013 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_SCHEDULER_H_ |
| 6 | #define V8_COMPILER_SCHEDULER_H_ |
| 7 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8 | #include "src/base/flags.h" |
| 9 | #include "src/compiler/node.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10 | #include "src/compiler/opcodes.h" |
| 11 | #include "src/compiler/schedule.h" |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 12 | #include "src/compiler/zone-pool.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13 | #include "src/zone-containers.h" |
| 14 | |
| 15 | namespace v8 { |
| 16 | namespace internal { |
| 17 | namespace compiler { |
| 18 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 19 | // Forward declarations. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 20 | class CFGBuilder; |
| 21 | class ControlEquivalence; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 22 | class Graph; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 23 | class SpecialRPONumberer; |
| 24 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 25 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 26 | // Computes a schedule from a graph, placing nodes into basic blocks and |
| 27 | // ordering the basic blocks in the special RPO order. |
| 28 | class Scheduler { |
| 29 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 30 | // Flags that control the mode of operation. |
| 31 | enum Flag { kNoFlags = 0u, kSplitNodes = 1u << 1 }; |
| 32 | typedef base::Flags<Flag> Flags; |
| 33 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 34 | // The complete scheduling algorithm. Creates a new schedule and places all |
| 35 | // nodes from the graph into it. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 36 | static Schedule* ComputeSchedule(Zone* zone, Graph* graph, Flags flags); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 37 | |
| 38 | // Compute the RPO of blocks in an existing schedule. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 39 | static BasicBlockVector* ComputeSpecialRPO(Zone* zone, Schedule* schedule); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 40 | |
| 41 | private: |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 42 | // Placement of a node changes during scheduling. The placement state |
| 43 | // transitions over time while the scheduler is choosing a position: |
| 44 | // |
| 45 | // +---------------------+-----+----> kFixed |
| 46 | // / / / |
| 47 | // kUnknown ----+------> kCoupled ----+ / |
| 48 | // \ / |
| 49 | // +----> kSchedulable ----+--------> kScheduled |
| 50 | // |
| 51 | // 1) GetPlacement(): kUnknown -> kCoupled|kSchedulable|kFixed |
| 52 | // 2) UpdatePlacement(): kCoupled|kSchedulable -> kFixed|kScheduled |
| 53 | enum Placement { kUnknown, kSchedulable, kFixed, kCoupled, kScheduled }; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 54 | |
| 55 | // Per-node data tracked during scheduling. |
| 56 | struct SchedulerData { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 57 | BasicBlock* minimum_block_; // Minimum legal RPO placement. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 58 | int unscheduled_count_; // Number of unscheduled uses of this node. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 59 | Placement placement_; // Whether the node is fixed, schedulable, |
| 60 | // coupled to another node, or not yet known. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | Zone* zone_; |
| 64 | Graph* graph_; |
| 65 | Schedule* schedule_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 66 | Flags flags_; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 67 | NodeVectorVector scheduled_nodes_; // Per-block list of nodes in reverse. |
| 68 | NodeVector schedule_root_nodes_; // Fixed root nodes seed the worklist. |
| 69 | ZoneQueue<Node*> schedule_queue_; // Worklist of schedulable nodes. |
| 70 | 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. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 74 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 75 | Scheduler(Zone* zone, Graph* graph, Schedule* schedule, Flags flags); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 76 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 77 | inline SchedulerData DefaultSchedulerData(); |
| 78 | inline SchedulerData* GetData(Node* node); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 79 | |
| 80 | Placement GetPlacement(Node* node); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 81 | void UpdatePlacement(Node* node, Placement placement); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 82 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 83 | inline bool IsCoupledControlEdge(Node* node, int index); |
| 84 | void IncrementUnscheduledUseCount(Node* node, int index, Node* from); |
| 85 | void DecrementUnscheduledUseCount(Node* node, int index, Node* from); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 86 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 87 | void PropagateImmediateDominators(BasicBlock* block); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 88 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 89 | // Phase 1: Build control-flow graph. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 90 | friend class CFGBuilder; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 91 | void BuildCFG(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 92 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 93 | // Phase 2: Compute special RPO and dominator tree. |
| 94 | friend class SpecialRPONumberer; |
| 95 | void ComputeSpecialRPONumbering(); |
| 96 | void GenerateImmediateDominatorTree(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 97 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 98 | // Phase 3: Prepare use counts for nodes. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 99 | friend class PrepareUsesVisitor; |
| 100 | void PrepareUses(); |
| 101 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 102 | // Phase 4: Schedule nodes early. |
| 103 | friend class ScheduleEarlyNodeVisitor; |
| 104 | void ScheduleEarly(); |
| 105 | |
| 106 | // Phase 5: Schedule nodes late. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 107 | friend class ScheduleLateNodeVisitor; |
| 108 | void ScheduleLate(); |
| 109 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 110 | // Phase 6: Seal the final schedule. |
| 111 | void SealFinalSchedule(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 112 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 113 | void FuseFloatingControl(BasicBlock* block, Node* node); |
| 114 | void MovePlannedNodes(BasicBlock* from, BasicBlock* to); |
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 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 117 | |
| 118 | DEFINE_OPERATORS_FOR_FLAGS(Scheduler::Flags) |
| 119 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 120 | } // namespace compiler |
| 121 | } // namespace internal |
| 122 | } // namespace v8 |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 123 | |
| 124 | #endif // V8_COMPILER_SCHEDULER_H_ |