2 Copyright (c) 2002-2009 Tampere University.
4 This file is part of TTA-Based Codesign Environment (TCE).
6 Permission is hereby granted, free of charge, to any person obtaining a
7 copy of this software and associated documentation files (the "Software"),
8 to deal in the Software without restriction, including without limitation
9 the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 and/or sell copies of the Software, and to permit persons to whom the
11 Software is furnished to do so, subject to the following conditions:
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 DEALINGS IN THE SOFTWARE.
25 * @file DDGMoveNodeSelector.icc
27 * Template Implementation of DDGMoveNodeSelector interface.
29 * @author Pekka Jääskeläinen 2007 (pekka.jaaskelainen-no.spam-tut.fi)
30 * @author Heikki Kultala 2008 (heikki.kultala-no.spam-tut.fi)
34 #include "DataDependenceGraph.hh"
35 #include "DataDependenceGraphBuilder.hh"
36 #include "POMDisassembler.hh"
37 #include "ProgramOperation.hh"
38 #include "Procedure.hh"
39 #include "BasicBlock.hh"
40 #include "SpecialRegisterPort.hh"
41 #include "Terminal.hh"
43 //#define DEBUG_OUTPUT__
44 //#define WRITE_DOT_SNAPSHOTS
47 * Add the unscheduled root nodes of the DDG to the ready list
49 template <class PQType>
51 DDGMoveNodeSelector<PQType>::initializeReadylist() {
52 DataDependenceGraph::NodeSet roots = ddg_->rootNodes();
53 for (DataDependenceGraph::NodeSet::iterator i = roots.begin();
58 // a hack to avoid adding the root operations multiple times
59 // (the count of input moves): only "announce" the move to the first
60 // operand (every operation should have one input operand)
61 if (!node.isDestinationOperation())
63 else if (node.move().destination().operationIndex() == 1) {
71 * Constructor. Creates subgraph of the given big graph
73 * @param bigDDG big ddg containing more than just the basic block
74 * @param bb basic block for this selector.
76 template <class PQType>
77 DDGMoveNodeSelector<PQType>::DDGMoveNodeSelector(
78 DataDependenceGraph& bigDDG, TTAProgram::BasicBlock& bb,
79 const TTAMachine::Machine& machine)
82 ddg_ = bigDDG.createSubgraph(bb);
83 // TODO: do this only for the big graph once!
84 ddg_->setMachine(machine);
85 } catch (InstanceNotFound& inf) {
87 __FILE__,__LINE__,__func__,"Creation of subgraph failed");
91 initializeReadylist();
97 * @param bb The basic block from which to select moves.
99 template <class PQType>
100 DDGMoveNodeSelector<PQType>::DDGMoveNodeSelector(
101 TTAProgram::BasicBlock& bb, const TTAMachine::Machine& machine) :
104 DataDependenceGraphBuilder ddgBuilder;
105 ddg_ = ddgBuilder.build(
106 bb,DataDependenceGraph::INTRA_BB_ANTIDEPS, machine);
107 // ddg_->setMachine(machine);
109 #ifdef WRITE_DOT_SNAPSHOTS
110 ddg_->setCycleGrouping(true);
111 ddg_->writeToDotFile("ddg.dot");
114 initializeReadylist();
120 * @param ddg The data dependence graph from which to select moves.
121 * Selector does not take the ownership of the ddg.
123 template <class PQType>
124 DDGMoveNodeSelector<PQType>::DDGMoveNodeSelector(
125 DataDependenceGraph& ddg, const TTAMachine::Machine& machine) :
126 ddg_(&ddg), ddgOwned_(false) {
128 ddg_->setMachine(machine);
130 #ifdef WRITE_DOT_SNAPSHOTS
131 ddg_->setCycleGrouping(true);
132 ddg_->writeToDotFile("ddg.dot");
135 initializeReadylist();
141 template <class PQType>
142 DDGMoveNodeSelector<PQType>::~DDGMoveNodeSelector() {
149 * Returns a group of move nodes which should be scheduled next.
151 * @return Move node group.
153 template <class PQType>
155 DDGMoveNodeSelector<PQType>::candidates() {
157 // find a MoveNodeGroup with unscheduled MoveNodes
158 while (!readyList_.empty()) {
159 MoveNodeGroup moves = readyList_.top();
160 if (!moves.isAlive() || moves.isPlaced())
165 // nothing in ready list, let's see if there are "orphan" nodes
167 if (ddg_->nodeCount() - ddg_->scheduledNodeCount() > 0) {
168 DataDependenceGraph::NodeSet unscheduled = ddg_->unscheduledMoves();
169 for (DataDependenceGraph::NodeSet::iterator i = unscheduled.begin();
170 i != unscheduled.end();
172 MoveNode& node = **i;
177 // did we find new nodes?
178 while (!readyList_.empty()) {
179 MoveNodeGroup moves = readyList_.top();
180 if (!moves.isAlive() || moves.isPlaced())
186 // return an empty move node group
187 return MoveNodeGroup();
191 * Returns the DDG used internally.
193 * This is needed temporarily only until the MoveNodeManager is done.
197 template <class PQType>
199 DDGMoveNodeSelector<PQType>::dataDependenceGraph() {
204 * This should be called by the client as soon as a MoveNode is scheduled
205 * in order to update the internal state of the selector.
207 * @param node The scheduled MoveNode.
209 template <class PQType>
211 DDGMoveNodeSelector<PQType>::notifyScheduled(MoveNode& node) {
213 if (!node.isPlaced()) {
214 ddg_->writeToDotFile("notifying_unsched_ddg.dot");
215 std::cerr << "The node being notified: " << node.toString() << std::endl;
218 assert(node.isPlaced() && "Notifying scheduled even though it isn't.");
219 DataDependenceGraph::NodeSet succ = ddg_->successors(node);
220 for (DataDependenceGraph::NodeSet::iterator i = succ.begin();
221 i != succ.end(); ++i) {
222 MoveNode& successor = **i;
224 // we schedule operations as entities, so if the successor is a
225 // (result) move, it's already in the ready list along with the
227 if (!successor.inSameOperation(node))
231 #ifdef WRITE_DOT_SNAPSHOTS
232 ddg_->setCycleGrouping(true);
233 ddg_->writeToDotFile("ddg.dot");
238 * Adds the given move node (along with the other possible move nodes in the
239 * same operation) to the ready list in case all its parents in the DDG have
242 * In case the node belongs to an operation, also checks that the other
243 * operand moves are also ready. In that case adds all the nodes in the said
244 * MoveOperation to the ready list in a single MoveNodeGroup.
246 * @param node Move node that might be ready.
248 template <class PQType>
250 DDGMoveNodeSelector<PQType>::mightBeReady(MoveNode& node) {
252 if (node.isPlaced()) {
256 if (!isReadyToBeScheduled(node))
259 if (node.isDestinationOperation() || node.isSourceOperation()) {
260 // it's a trigger, result, or operand move, let's see if all the
261 // moves of the operation are ready to be scheduled
262 ProgramOperation& operation =
263 (node.isDestinationOperation()?
264 (node.destinationOperation()) : node.sourceOperation());
265 ProgramOperationPtr operationPtr =
266 (node.isDestinationOperation()?
267 (node.destinationOperationPtr()) : node.sourceOperationPtr());
269 bool allReady = true;
270 MoveNodeGroup moves(*ddg_);
271 moves.setProgramOperationPtr(operationPtr);
272 for (int inputIndex = 0; inputIndex < operation.inputMoveCount();
274 MoveNode& m = operation.inputMove(inputIndex);
276 if (!isReadyToBeScheduled(m)) {
285 // let's add also the output move(s) to the MoveNodeGroup
286 for (int outputIndex = 0;
287 outputIndex < operation.outputMoveCount();
289 MoveNode& m = operation.outputMove(outputIndex);
291 if (!isReadyToBeScheduled(m)) {
300 readyList_.push(moves);
303 } else if ((node.isSourceVariable() || node.move().source().isRA()) &&
304 (node.isDestinationVariable() ||
305 node.move().destination().isRA())) {
306 // it's a register to register move, we can always schedule these
307 // as soon as all the dependencies are satisfied
308 // handle RA -> ireg also as a register to register move
309 if (isReadyToBeScheduled(node)) {
310 MoveNodeGroup move(*ddg_);
312 readyList_.push(move);
314 } else if (node.isSourceConstant() &&
315 (node.isDestinationVariable() ||
316 node.move().destination().isRA())) {
317 if (isReadyToBeScheduled(node)) {
318 MoveNodeGroup move(*ddg_);
320 readyList_.push(move);
324 throw IllegalProgram(
325 __FILE__, __LINE__, __func__,
326 (boost::format("Illegal move '%s'.") %
327 POMDisassembler::disassemble(node.move())).str());
332 * Returns true in case the move is "data ready", that is, all its
333 * predecessors have been scheduled.
335 * It should be noted that moves within same operation are treated
336 * specially. Result move is considered ready even if the operands
337 * moves are not to allow scheduling all moves in the same operation
338 * as a single entity. Additionally, as we are considering a basic block
339 * at a time, the branch operation is considered never ready before
340 * all the other moves in the basic block have been scheduled.
342 * @param node Move to check.
343 * @return True if the move is ready to be scheduled.
345 template <class PQType>
347 DDGMoveNodeSelector<PQType>::isReadyToBeScheduled(MoveNode& node)
350 // the control flow move(s) are ready only if all other moves have been
351 // scheduled. In rare case of conditional branching with SPU operation set
352 // branch operation can have 2 moves, condition register and destination.
353 if (node.move().isControlFlowMove() &&
354 ddg_->nodeCount() - ddg_->scheduledNodeCount() > 1) {
355 DataDependenceGraph::NodeSet unscheduledMoves = ddg_->unscheduledMoves();
356 for (DataDependenceGraph::NodeSet::iterator i = unscheduledMoves.begin();
357 i != unscheduledMoves.end(); ++i) {
358 if (!(*i)->move().isControlFlowMove()) {
363 return ddg_->predecessorsReady(node);