OpenASIP  2.0
DDGMoveNodeSelector.icc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2009 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
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:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
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.
23  */
24 /**
25  * @file DDGMoveNodeSelector.icc
26  *
27  * Template Implementation of DDGMoveNodeSelector interface.
28  *
29  * @author Pekka Jääskeläinen 2007 (pekka.jaaskelainen-no.spam-tut.fi)
30  * @author Heikki Kultala 2008 (heikki.kultala-no.spam-tut.fi)
31  * @note rating: red
32  */
33 
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"
42 
43 //#define DEBUG_OUTPUT__
44 //#define WRITE_DOT_SNAPSHOTS
45 
46 /**
47  * Add the unscheduled root nodes of the DDG to the ready list
48  */
49 template <class PQType>
50 void
51 DDGMoveNodeSelector<PQType>::initializeReadylist() {
52  DataDependenceGraph::NodeSet roots = ddg_->rootNodes();
53  for (DataDependenceGraph::NodeSet::iterator i = roots.begin();
54  i != roots.end();
55  ++i) {
56  MoveNode& node = **i;
57 
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())
62  mightBeReady(**i);
63  else if (node.move().destination().operationIndex() == 1) {
64  mightBeReady(**i);
65  }
66  }
67 
68 }
69 
70 /**
71  * Constructor. Creates subgraph of the given big graph
72  *
73  * @param bigDDG big ddg containing more than just the basic block
74  * @param bb basic block for this selector.
75  */
76 template <class PQType>
77 DDGMoveNodeSelector<PQType>::DDGMoveNodeSelector(
78  DataDependenceGraph& bigDDG, TTAProgram::BasicBlock& bb,
79  const TTAMachine::Machine& machine)
80  : ddgOwned_(true) {
81  try {
82  ddg_ = bigDDG.createSubgraph(bb);
83  // TODO: do this only for the big graph once!
84  ddg_->setMachine(machine);
85  } catch (InstanceNotFound& inf) {
86  ModuleRunTimeError e(
87  __FILE__,__LINE__,__func__,"Creation of subgraph failed");
88  e.setCause(inf);
89  throw e;
90  }
91  initializeReadylist();
92 }
93 
94 /**
95  * Constructor.
96  *
97  * @param bb The basic block from which to select moves.
98  */
99 template <class PQType>
100 DDGMoveNodeSelector<PQType>::DDGMoveNodeSelector(
101  TTAProgram::BasicBlock& bb, const TTAMachine::Machine& machine) :
102  ddgOwned_(true) {
103 
104  DataDependenceGraphBuilder ddgBuilder;
105  ddg_ = ddgBuilder.build(
106  bb,DataDependenceGraph::INTRA_BB_ANTIDEPS, machine);
107 // ddg_->setMachine(machine);
108 
109 #ifdef WRITE_DOT_SNAPSHOTS
110  ddg_->setCycleGrouping(true);
111  ddg_->writeToDotFile("ddg.dot");
112 #endif
113 
114  initializeReadylist();
115 }
116 
117 /**
118  * Constructor.
119  *
120  * @param ddg The data dependence graph from which to select moves.
121  * Selector does not take the ownership of the ddg.
122  */
123 template <class PQType>
124 DDGMoveNodeSelector<PQType>::DDGMoveNodeSelector(
125  DataDependenceGraph& ddg, const TTAMachine::Machine& machine) :
126  ddg_(&ddg), ddgOwned_(false) {
127 
128  ddg_->setMachine(machine);
129 
130 #ifdef WRITE_DOT_SNAPSHOTS
131  ddg_->setCycleGrouping(true);
132  ddg_->writeToDotFile("ddg.dot");
133 #endif
134 
135  initializeReadylist();
136 }
137 
138 /**
139  * Destructor.
140  */
141 template <class PQType>
142 DDGMoveNodeSelector<PQType>::~DDGMoveNodeSelector() {
143  if (ddgOwned_) {
144  delete ddg_;
145  }
146 }
147 
148 /**
149  * Returns a group of move nodes which should be scheduled next.
150  *
151  * @return Move node group.
152  */
153 template <class PQType>
154 MoveNodeGroup
155 DDGMoveNodeSelector<PQType>::candidates() {
156 
157  // find a MoveNodeGroup with unscheduled MoveNodes
158  while (!readyList_.empty()) {
159  MoveNodeGroup moves = readyList_.top();
160  if (!moves.isAlive() || moves.isPlaced())
161  readyList_.pop();
162  else
163  return moves;
164  }
165  // nothing in ready list, let's see if there are "orphan" nodes
166  // still to schedule
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();
171  ++i) {
172  MoveNode& node = **i;
173  mightBeReady(node);
174  }
175  }
176 
177  // did we find new nodes?
178  while (!readyList_.empty()) {
179  MoveNodeGroup moves = readyList_.top();
180  if (!moves.isAlive() || moves.isPlaced())
181  readyList_.pop();
182  else
183  return moves;
184  }
185 
186  // return an empty move node group
187  return MoveNodeGroup();
188 }
189 
190 /**
191  * Returns the DDG used internally.
192  *
193  * This is needed temporarily only until the MoveNodeManager is done.
194  *
195  * @return The DDG.
196  */
197 template <class PQType>
198 DataDependenceGraph&
199 DDGMoveNodeSelector<PQType>::dataDependenceGraph() {
200  return *ddg_;
201 }
202 
203 /**
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.
206  *
207  * @param node The scheduled MoveNode.
208  */
209 template <class PQType>
210 void
211 DDGMoveNodeSelector<PQType>::notifyScheduled(MoveNode& node) {
212 
213  if (!node.isPlaced()) {
214  ddg_->writeToDotFile("notifying_unsched_ddg.dot");
215  std::cerr << "The node being notified: " << node.toString() << std::endl;
216  }
217 
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;
223 
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
226  // move itself
227  if (!successor.inSameOperation(node))
228  mightBeReady(**i);
229  }
230 
231 #ifdef WRITE_DOT_SNAPSHOTS
232  ddg_->setCycleGrouping(true);
233  ddg_->writeToDotFile("ddg.dot");
234 #endif
235 }
236 
237 /**
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
240  * been scheduled.
241  *
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.
245  *
246  * @param node Move node that might be ready.
247  */
248 template <class PQType>
249 void
250 DDGMoveNodeSelector<PQType>::mightBeReady(MoveNode& node) {
251 
252  if (node.isPlaced()) {
253  return;
254  }
255 
256  if (!isReadyToBeScheduled(node))
257  return;
258 
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());
268 
269  bool allReady = true;
270  MoveNodeGroup moves(*ddg_);
271  moves.setProgramOperationPtr(operationPtr);
272  for (int inputIndex = 0; inputIndex < operation.inputMoveCount();
273  ++inputIndex) {
274  MoveNode& m = operation.inputMove(inputIndex);
275  if (&m != &node) {
276  if (!isReadyToBeScheduled(m)) {
277  allReady = false;
278  break;
279  }
280  }
281  if (!m.isPlaced())
282  moves.addNode(m);
283  }
284  if (allReady) {
285  // let's add also the output move(s) to the MoveNodeGroup
286  for (int outputIndex = 0;
287  outputIndex < operation.outputMoveCount();
288  ++outputIndex) {
289  MoveNode& m = operation.outputMove(outputIndex);
290  if (&m != &node) {
291  if (!isReadyToBeScheduled(m)) {
292  allReady = false;
293  break;
294  }
295  }
296  if (!m.isPlaced())
297  moves.addNode(m);
298  }
299  if (allReady) {
300  readyList_.push(moves);
301  }
302  }
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_);
311  move.addNode(node);
312  readyList_.push(move);
313  }
314  } else if (node.isSourceConstant() &&
315  (node.isDestinationVariable() ||
316  node.move().destination().isRA())) {
317  if (isReadyToBeScheduled(node)) {
318  MoveNodeGroup move(*ddg_);
319  move.addNode(node);
320  readyList_.push(move);
321  }
322 
323  } else {
324  throw IllegalProgram(
325  __FILE__, __LINE__, __func__,
326  (boost::format("Illegal move '%s'.") %
327  POMDisassembler::disassemble(node.move())).str());
328  }
329 }
330 
331 /**
332  * Returns true in case the move is "data ready", that is, all its
333  * predecessors have been scheduled.
334  *
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.
341  *
342  * @param node Move to check.
343  * @return True if the move is ready to be scheduled.
344  */
345 template <class PQType>
346 bool
347 DDGMoveNodeSelector<PQType>::isReadyToBeScheduled(MoveNode& node)
348  const {
349 
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()) {
359  return false;
360  }
361  }
362  }
363  return ddg_->predecessorsReady(node);
364 }