| OLD | NEW |
| 1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "src/compiler/instruction-scheduler.h" | 5 #include "src/compiler/instruction-scheduler.h" |
| 6 | 6 |
| 7 #include "src/base/adapters.h" | 7 #include "src/base/adapters.h" |
| 8 #include "src/base/utils/random-number-generator.h" |
| 8 | 9 |
| 9 namespace v8 { | 10 namespace v8 { |
| 10 namespace internal { | 11 namespace internal { |
| 11 namespace compiler { | 12 namespace compiler { |
| 12 | 13 |
| 14 // Compare the two nodes and return true if node1 is a better candidate than |
| 15 // node2 (i.e. node1 should be scheduled before node2). |
| 16 bool InstructionScheduler::CriticalPathFirstQueue::CompareNodes( |
| 17 ScheduleGraphNode *node1, ScheduleGraphNode *node2) const { |
| 18 return node1->total_latency() > node2->total_latency(); |
| 19 } |
| 20 |
| 21 |
| 22 InstructionScheduler::ScheduleGraphNode* |
| 23 InstructionScheduler::CriticalPathFirstQueue::PopBestCandidate(int cycle) { |
| 24 DCHECK(!IsEmpty()); |
| 25 auto candidate = nodes_.end(); |
| 26 for (auto iterator = nodes_.begin(); iterator != nodes_.end(); ++iterator) { |
| 27 // We only consider instructions that have all their operands ready and |
| 28 // we try to schedule the critical path first. |
| 29 if (cycle >= (*iterator)->start_cycle()) { |
| 30 if ((candidate == nodes_.end()) || CompareNodes(*iterator, *candidate)) { |
| 31 candidate = iterator; |
| 32 } |
| 33 } |
| 34 } |
| 35 |
| 36 if (candidate != nodes_.end()) { |
| 37 ScheduleGraphNode *result = *candidate; |
| 38 nodes_.erase(candidate); |
| 39 return result; |
| 40 } |
| 41 |
| 42 return nullptr; |
| 43 } |
| 44 |
| 45 |
| 46 InstructionScheduler::ScheduleGraphNode* |
| 47 InstructionScheduler::StressSchedulerQueue::PopBestCandidate(int cycle) { |
| 48 DCHECK(!IsEmpty()); |
| 49 // Choose a random element from the ready list. |
| 50 auto candidate = nodes_.begin(); |
| 51 std::advance(candidate, isolate()->random_number_generator()->NextInt( |
| 52 static_cast<int>(nodes_.size()))); |
| 53 ScheduleGraphNode *result = *candidate; |
| 54 nodes_.erase(candidate); |
| 55 return result; |
| 56 } |
| 57 |
| 58 |
| 13 InstructionScheduler::ScheduleGraphNode::ScheduleGraphNode( | 59 InstructionScheduler::ScheduleGraphNode::ScheduleGraphNode( |
| 14 Zone* zone, | 60 Zone* zone, |
| 15 Instruction* instr) | 61 Instruction* instr) |
| 16 : instr_(instr), | 62 : instr_(instr), |
| 17 successors_(zone), | 63 successors_(zone), |
| 18 unscheduled_predecessors_count_(0), | 64 unscheduled_predecessors_count_(0), |
| 19 latency_(GetInstructionLatency(instr)), | 65 latency_(GetInstructionLatency(instr)), |
| 20 total_latency_(-1), | 66 total_latency_(-1), |
| 21 start_cycle_(-1) { | 67 start_cycle_(-1) { |
| 22 } | 68 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 43 void InstructionScheduler::StartBlock(RpoNumber rpo) { | 89 void InstructionScheduler::StartBlock(RpoNumber rpo) { |
| 44 DCHECK(graph_.empty()); | 90 DCHECK(graph_.empty()); |
| 45 DCHECK(last_side_effect_instr_ == nullptr); | 91 DCHECK(last_side_effect_instr_ == nullptr); |
| 46 DCHECK(pending_loads_.empty()); | 92 DCHECK(pending_loads_.empty()); |
| 47 DCHECK(last_live_in_reg_marker_ == nullptr); | 93 DCHECK(last_live_in_reg_marker_ == nullptr); |
| 48 sequence()->StartBlock(rpo); | 94 sequence()->StartBlock(rpo); |
| 49 } | 95 } |
| 50 | 96 |
| 51 | 97 |
| 52 void InstructionScheduler::EndBlock(RpoNumber rpo) { | 98 void InstructionScheduler::EndBlock(RpoNumber rpo) { |
| 53 ScheduleBlock(); | 99 if (FLAG_turbo_stress_instruction_scheduling) { |
| 100 ScheduleBlock<StressSchedulerQueue>(); |
| 101 } else { |
| 102 ScheduleBlock<CriticalPathFirstQueue>(); |
| 103 } |
| 54 sequence()->EndBlock(rpo); | 104 sequence()->EndBlock(rpo); |
| 55 graph_.clear(); | 105 graph_.clear(); |
| 56 last_side_effect_instr_ = nullptr; | 106 last_side_effect_instr_ = nullptr; |
| 57 pending_loads_.clear(); | 107 pending_loads_.clear(); |
| 58 last_live_in_reg_marker_ = nullptr; | 108 last_live_in_reg_marker_ = nullptr; |
| 59 } | 109 } |
| 60 | 110 |
| 61 | 111 |
| 62 void InstructionScheduler::AddInstruction(Instruction* instr) { | 112 void InstructionScheduler::AddInstruction(Instruction* instr) { |
| 63 ScheduleGraphNode* new_node = new (zone()) ScheduleGraphNode(zone(), instr); | 113 ScheduleGraphNode* new_node = new (zone()) ScheduleGraphNode(zone(), instr); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 if (HasOperandDependency(node->instruction(), instr)) { | 153 if (HasOperandDependency(node->instruction(), instr)) { |
| 104 node->AddSuccessor(new_node); | 154 node->AddSuccessor(new_node); |
| 105 } | 155 } |
| 106 } | 156 } |
| 107 } | 157 } |
| 108 | 158 |
| 109 graph_.push_back(new_node); | 159 graph_.push_back(new_node); |
| 110 } | 160 } |
| 111 | 161 |
| 112 | 162 |
| 113 bool InstructionScheduler::CompareNodes(ScheduleGraphNode *node1, | 163 template <typename QueueType> |
| 114 ScheduleGraphNode *node2) const { | |
| 115 return node1->total_latency() > node2->total_latency(); | |
| 116 } | |
| 117 | |
| 118 | |
| 119 void InstructionScheduler::ScheduleBlock() { | 164 void InstructionScheduler::ScheduleBlock() { |
| 120 ZoneLinkedList<ScheduleGraphNode*> ready_list(zone()); | 165 QueueType ready_list(this); |
| 121 | 166 |
| 122 // Compute total latencies so that we can schedule the critical path first. | 167 // Compute total latencies so that we can schedule the critical path first. |
| 123 ComputeTotalLatencies(); | 168 ComputeTotalLatencies(); |
| 124 | 169 |
| 125 // Add nodes which don't have dependencies to the ready list. | 170 // Add nodes which don't have dependencies to the ready list. |
| 126 for (auto node : graph_) { | 171 for (auto node : graph_) { |
| 127 if (!node->HasUnscheduledPredecessor()) { | 172 if (!node->HasUnscheduledPredecessor()) { |
| 128 ready_list.push_back(node); | 173 ready_list.AddNode(node); |
| 129 } | 174 } |
| 130 } | 175 } |
| 131 | 176 |
| 132 // Go through the ready list and schedule the instructions. | 177 // Go through the ready list and schedule the instructions. |
| 133 int cycle = 0; | 178 int cycle = 0; |
| 134 while (!ready_list.empty()) { | 179 while (!ready_list.IsEmpty()) { |
| 135 auto candidate = ready_list.end(); | 180 auto candidate = ready_list.PopBestCandidate(cycle); |
| 136 for (auto iterator = ready_list.begin(); iterator != ready_list.end(); | 181 |
| 137 ++iterator) { | 182 if (candidate != nullptr) { |
| 138 // Look for the best candidate to schedule. | 183 sequence()->AddInstruction(candidate->instruction()); |
| 139 // We only consider instructions that have all their operands ready and | 184 |
| 140 // we try to schedule the critical path first (we look for the instruction | 185 for (auto successor : candidate->successors()) { |
| 141 // with the highest latency on the path to reach the end of the graph). | 186 successor->DropUnscheduledPredecessor(); |
| 142 if (cycle >= (*iterator)->start_cycle()) { | 187 successor->set_start_cycle( |
| 143 if ((candidate == ready_list.end()) || | 188 std::max(successor->start_cycle(), |
| 144 CompareNodes(*iterator, *candidate)) { | 189 cycle + candidate->latency())); |
| 145 candidate = iterator; | 190 |
| 191 if (!successor->HasUnscheduledPredecessor()) { |
| 192 ready_list.AddNode(successor); |
| 146 } | 193 } |
| 147 } | 194 } |
| 148 } | 195 } |
| 149 | 196 |
| 150 if (candidate != ready_list.end()) { | |
| 151 sequence()->AddInstruction((*candidate)->instruction()); | |
| 152 | |
| 153 for (auto successor : (*candidate)->successors()) { | |
| 154 successor->DropUnscheduledPredecessor(); | |
| 155 successor->set_start_cycle( | |
| 156 std::max(successor->start_cycle(), | |
| 157 cycle + (*candidate)->latency())); | |
| 158 | |
| 159 if (!successor->HasUnscheduledPredecessor()) { | |
| 160 ready_list.push_back(successor); | |
| 161 } | |
| 162 } | |
| 163 | |
| 164 ready_list.erase(candidate); | |
| 165 } | |
| 166 | |
| 167 cycle++; | 197 cycle++; |
| 168 } | 198 } |
| 169 } | 199 } |
| 170 | 200 |
| 171 | 201 |
| 172 int InstructionScheduler::GetInstructionFlags(const Instruction* instr) const { | 202 int InstructionScheduler::GetInstructionFlags(const Instruction* instr) const { |
| 173 switch (instr->arch_opcode()) { | 203 switch (instr->arch_opcode()) { |
| 174 case kArchNop: | 204 case kArchNop: |
| 175 case kArchStackPointer: | 205 case kArchStackPointer: |
| 176 case kArchFramePointer: | 206 case kArchFramePointer: |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 272 } | 302 } |
| 273 } | 303 } |
| 274 | 304 |
| 275 node->set_total_latency(max_latency + node->latency()); | 305 node->set_total_latency(max_latency + node->latency()); |
| 276 } | 306 } |
| 277 } | 307 } |
| 278 | 308 |
| 279 } // namespace compiler | 309 } // namespace compiler |
| 280 } // namespace internal | 310 } // namespace internal |
| 281 } // namespace v8 | 311 } // namespace v8 |
| OLD | NEW |