OpenASIP  2.0
BFRemoveLoopChecks.cc
Go to the documentation of this file.
1 #include "BFRemoveLoopChecks.hh"
2 #include "DataDependenceGraph.hh"
3 #include "Move.hh"
4 #include "Operation.hh"
5 
6 bool
8 
9  POSet queuedToRemovePo;
10  POSet maybeRemovePo;
11  POSet queuedAlivePo;
12  POSet finishedAlivePo;
13 
14  DataDependenceGraph::NodeSet queuedToRemoveMove;
15  DataDependenceGraph::NodeSet maybeRemoveMove;
16  DataDependenceGraph::NodeSet queuedAliveMoves;
17  DataDependenceGraph::NodeSet finishedAliveMoves;
18 
19  DataDependenceGraph& rootddg = static_cast<DataDependenceGraph&>
20  (*ddg().rootGraph());
21 
22  int count = 0;
23  for (int i = 0; i < ddg().nodeCount(); i++) {
24  MoveNode& mn = ddg().node(i);
25  if (mn.move().isJump()) {
27  queuedToRemovePo.insert(mn.destinationOperationPtr());
28  continue;
29  }
30 
31  // Mark other nodes alive
32  if (mn.isDestinationOperation()) {
34  const Operation& op = po.operation();
35  if (op.hasSideEffects() || op.writesMemory() ||
37  queueAliveMove(mn, queuedAlivePo, finishedAlivePo,
38  queuedAliveMoves, finishedAliveMoves);
39  continue;
40  }
41  }
42 
43  int outd = rootddg.outDegree(mn);
44  for (int j = 0; j < outd; j++) {
45  DataDependenceEdge& e = rootddg.outEdge(mn,j);
48  continue;
49  }
50 
51  // edges out from this BB are considered alive
52  MoveNode& head = rootddg.headNode(e);
53  if (!ddg().hasNode(head)) {
54  queueAliveMove(mn, queuedAlivePo, finishedAlivePo,
55  queuedAliveMoves, finishedAliveMoves);
56  }
57  }
58  }
59 
60  // propagate alive status
61  while (!queuedAlivePo.empty() || !queuedAliveMoves.empty()) {
62  if (!queuedAlivePo.empty()) {
63  checkAlivePO(queuedAlivePo, finishedAlivePo, queuedAliveMoves, finishedAliveMoves);
64  }
65  if (!queuedAliveMoves.empty()) {
66  checkAliveMove(queuedAliveMoves, finishedAliveMoves, queuedAlivePo, finishedAlivePo);
67  }
68  }
69 
70  // then remove POs and movenodes that are not alive
71  while (!queuedToRemovePo.empty() || !maybeRemovePo.empty() ||
72  !queuedToRemoveMove.empty() || !maybeRemoveMove.empty()) {
73  if (!queuedToRemovePo.empty()) {
74  removePoFromQueue(queuedToRemovePo, maybeRemovePo,
75  queuedToRemoveMove, maybeRemoveMove);
76  count++;
77  continue;
78  }
79 
80  if (!queuedToRemoveMove.empty()) {
81  removeMoveFromQueue(queuedToRemoveMove, maybeRemoveMove,
82  queuedToRemovePo, maybeRemovePo);
83  count++;
84  continue;
85  }
86 
87  if (!maybeRemovePo.empty()) {
88  checkMaybePos(maybeRemovePo, queuedToRemovePo, finishedAlivePo);
89  }
90 
91  if (!maybeRemoveMove.empty()) {
92  checkMaybeMoves(maybeRemoveMove, queuedToRemoveMove, finishedAliveMoves);
93  }
94  }
95  for (auto i: removedPOs_) {
97  }
98 
99 #ifdef DEBUG_BUBBLEFISH_SCHEDULER
100  std::cerr << "count of removed POs: " << count << std::endl;
101 #endif
102  return count!=0;
103 }
104 
106  POSet& queuedAlivePo, POSet& finishedAlivePo,
107  DataDependenceGraph::NodeSet& queuedAliveMoves,
108  DataDependenceGraph::NodeSet& finishedAliveMoves) {
109  ProgramOperationPtr po = *queuedAlivePo.begin();
110  for (int i = 0; i < po->inputMoveCount(); i++) {
111  MoveNode& mn = po->inputMove(i);
112  int ind = ddg().inDegree(mn);
113  for (int j = 0; j < ind; j++) {
114  DataDependenceEdge& e = ddg().inEdge(mn,j);
117  continue;
118  }
119  queueAliveMove(ddg().tailNode(e), queuedAlivePo, finishedAlivePo,
120  queuedAliveMoves, finishedAliveMoves);
121  }
122  }
123  queuedAlivePo.erase(po);
124  finishedAlivePo.insert(po);
125 }
126 
128  MoveNode& mn,
129  POSet& queuedAlivePo, POSet& finishedAlivePo,
130  DataDependenceGraph::NodeSet& queuedAliveMoves,
131  DataDependenceGraph::NodeSet& finishedAliveMoves) {
132 
133  if (mn.isSourceOperation()) {
134  if (finishedAlivePo.find(mn.sourceOperationPtr()) == finishedAlivePo.end()) {
135 #ifdef DEBUG_BUBBLEFISH_SCHEDULER
136  std::cerr << "alive PO: " << mn.toString() << std::endl;
137 #endif
138  queuedAlivePo.insert(mn.sourceOperationPtr());
139  }
140  } else if (mn.isDestinationOperation()) {
141  if (finishedAlivePo.find(mn.destinationOperationPtr()) == finishedAlivePo.end()) {
142 #ifdef DEBUG_BUBBLEFISH_SCHEDULER
143  std::cerr << "alive PO: " << mn.toString() << std::endl;
144 #endif
145  queuedAlivePo.insert(mn.destinationOperationPtr());
146  }
147  } else {
148  if (finishedAliveMoves.find(&mn) == finishedAliveMoves.end()) {
149 #ifdef DEBUG_BUBBLEFISH_SCHEDULER
150  std::cerr << "alive MN: " << mn.toString() << std::endl;
151 #endif
152  queuedAliveMoves.insert(&mn);
153  }
154  }
155 }
156 
158  DataDependenceGraph::NodeSet& queuedAliveMoves,
159  DataDependenceGraph::NodeSet& finishedAliveMoves,
160  POSet& queuedAlivePo, POSet& finishedAlivePo) {
161  MoveNode* mn = *queuedAliveMoves.begin();
162  int ind = ddg().inDegree(*mn);
163  for (int j = 0; j < ind; j++) {
164  DataDependenceEdge& e = ddg().inEdge(*mn,j);
167  continue;
168  }
169  queueAliveMove(ddg().tailNode(e), queuedAlivePo, finishedAlivePo,
170  queuedAliveMoves, finishedAliveMoves);
171  }
172  queuedAliveMoves.erase(mn);
173  finishedAliveMoves.insert(mn);
174 }
175 
176 
177 
178 
179 
181  POSet& queuedToRemovePo, POSet& maybeRemovePo,
182  DataDependenceGraph::NodeSet& queuedToRemoveMove,
183  DataDependenceGraph::NodeSet& maybeRemoveMove) {
184  ProgramOperationPtr po = *queuedToRemovePo.begin();
185 #ifdef DEBUG_BUBBLEFISH_SCHEDULER
186  std::cerr << "Removing programoperation: " << po->toString()
187  << std::endl;
188 #endif
189  for (int i = 0; i < po->inputMoveCount(); i++) {
190  MoveNode& mn = po->inputMove(i);
191  int ind = ddg().inDegree(mn);
192  for (int j = 0; j < ind; j++) {
193  DataDependenceEdge& e = ddg().inEdge(mn,j);
196  continue;
197  }
198  MoveNode& tail = ddg().tailNode(e);
199  if (!tail.isSourceOperation()) {
200  if (tail.isDestinationOperation()) {
202  if (queuedToRemovePo.find(tailOp) == queuedToRemovePo.end()) {
203  maybeRemovePo.insert(tailOp);
204  }
205  } else {
206  if (queuedToRemoveMove.find(&tail) == queuedToRemoveMove.end()) {
207  maybeRemoveMove.insert(&tail);
208  }
209  }
210  } else {
211  ProgramOperationPtr tailOp = tail.sourceOperationPtr();
212  // if already is to be remoed, no need to put tho
213  // checking queue again
214  if (queuedToRemovePo.find(tailOp) == queuedToRemovePo.end()) {
215  maybeRemovePo.insert(tailOp);
216  }
217  }
218  }
219  ddg().dropNode(mn);
220  removedMoves_.insert(&mn);
221  }
222 
223  for (int i = 0; i < po->outputMoveCount(); i++) {
224  MoveNode& mn = po->outputMove(i);
225  ddg().dropNode(mn);
226  removedMoves_.insert(&mn);
227  }
228 
229  // TODO: remove the PO from the ddg!
230  removedPOs_.insert(po);
231  queuedToRemovePo.erase(queuedToRemovePo.begin());
232 }
233 
235  DataDependenceGraph::NodeSet& queuedToRemoveMove,
236  DataDependenceGraph::NodeSet& maybeRemoveMove,
237  POSet& queuedToRemovePo, POSet& maybeRemovePo) {
238  MoveNode* mn = *queuedToRemoveMove.begin();
239 #ifdef DEBUG_BUBBLEFISH_SCHEDULER
240  std::cerr << "\tRemoving move: " << mn->toString()
241  << std::endl;
242 #endif
243  int ind = ddg().inDegree(*mn);
244  for (int j = 0; j < ind; j++) {
245  DataDependenceEdge& e = ddg().inEdge(*mn,j);
248  continue;
249  }
250  MoveNode& tail = ddg().tailNode(e);
251  if (!tail.isSourceOperation()) {
252  if (tail.isDestinationOperation()) {
254  // if already is to be remoed, no need to put tho
255  // checking queue again
256  if (queuedToRemovePo.find(tailOp) == queuedToRemovePo.end()) {
257  maybeRemovePo.insert(tailOp);
258  }
259  } else {
260  if (queuedToRemoveMove.find(&tail) == queuedToRemoveMove.end()) {
261  maybeRemoveMove.insert(&tail);
262  }
263  }
264  } else {
265  ProgramOperationPtr tailOp = tail.sourceOperationPtr();
266  // if already is to be remoed, no need to put tho
267  // checking queue again
268  if (queuedToRemovePo.find(tailOp) == queuedToRemovePo.end()) {
269  maybeRemovePo.insert(tailOp);
270  }
271  }
272  }
273  ddg().dropNode(*mn);
274  removedMoves_.insert(mn);
275 
276  // TODO: remove the PO from the ddg!
277  removedMoves_.insert(mn);
278  queuedToRemoveMove.erase(queuedToRemoveMove.begin());
279 }
280 
281 void BFRemoveLoopChecksAndJump::checkMaybePos(POSet& maybeRemove, POSet& queuedToRemove, POSet& alivePos) {
282  ProgramOperationPtr po = *maybeRemove.begin();
283  if (alivePos.find(po) == alivePos.end()) {
284  queuedToRemove.insert(po);
285  }
286  maybeRemove.erase(maybeRemove.begin());
287 }
288 
290  DataDependenceGraph::NodeSet& maybeRemove,
291  DataDependenceGraph::NodeSet& queuedToRemove,
292  DataDependenceGraph::NodeSet& aliveNodes) {
293  MoveNode* mn = *maybeRemove.begin();
294  if (aliveNodes.find(mn) == aliveNodes.end()) {
295  queuedToRemove.insert(mn);
296  }
297  maybeRemove.erase(mn);
298 }
299 
300 void
302  for (auto i: removedMoves_) {
303 #ifdef DEBUG_BUBBLEFISH_SCHEDULER
304  std::cerr << "Restoring node: " << (*i).toString() << std::endl;
305 #endif
307  }
308  removedMoves_.clear();
309 
310  for (auto i: removedPOs_) {
312  }
313 }
ProgramOperation::operation
const Operation & operation() const
Definition: ProgramOperation.cc:590
BoostGraph::outEdge
virtual Edge & outEdge(const Node &node, const int index) const
Operation::writesMemory
virtual bool writesMemory() const
Definition: Operation.cc:252
BFRemoveLoopChecksAndJump::checkAliveMove
void checkAliveMove(DataDependenceGraph::NodeSet &queuedAliveMoves, DataDependenceGraph::NodeSet &finishedAliveMoves, POSet &queuedAlivePo, POSet &finishedAlivePo)
Definition: BFRemoveLoopChecks.cc:157
BoostGraph::tailNode
virtual Node & tailNode(const Edge &edge) const
MoveNode::toString
std::string toString() const
Definition: MoveNode.cc:576
Operation::hasSideEffects
virtual bool hasSideEffects() const
Definition: Operation.cc:272
BFRemoveLoopChecksAndJump::checkMaybePos
void checkMaybePos(POSet &maybeRemove, POSet &queuedToRemove, POSet &finishedAlivePo)
Definition: BFRemoveLoopChecks.cc:281
BoostGraph::headNode
virtual Node & headNode(const Edge &edge) const
BoostGraph::node
Node & node(const int index) const
MoveNode::isDestinationOperation
bool isDestinationOperation() const
BoostGraph< MoveNode, DataDependenceEdge >::NodeSet
std::set< MoveNode *, typename MoveNode ::Comparator > NodeSet
Definition: BoostGraph.hh:86
BFRemoveLoopChecksAndJump::queueAliveMove
void queueAliveMove(MoveNode &mn, POSet &queuedAlivePo, POSet &finishedAlivePo, DataDependenceGraph::NodeSet &queuedAliveMoves, DataDependenceGraph::NodeSet &finishedAliveMoves)
Definition: BFRemoveLoopChecks.cc:127
DataDependenceGraph::addProgramOperation
void addProgramOperation(ProgramOperationPtr po)
Definition: DataDependenceGraph.cc:298
BFRemoveLoopChecksAndJump::removeMoveFromQueue
void removeMoveFromQueue(DataDependenceGraph::NodeSet &queuedToRemoveMove, DataDependenceGraph::NodeSet &maybeRemoveMove, POSet &queuedToRemovePo, POSet &maybeRemovePo)
Definition: BFRemoveLoopChecks.cc:234
DataDependenceGraph.hh
ProgramOperation
Definition: ProgramOperation.hh:70
MoveNode
Definition: MoveNode.hh:65
BFRemoveLoopChecks.hh
BFRemoveLoopChecksAndJump::removePoFromQueue
void removePoFromQueue(POSet &queuedToRemovePo, POSet &maybeRemovePo, DataDependenceGraph::NodeSet &queuedToRemoveMove, DataDependenceGraph::NodeSet &maybeRemoveMove)
Definition: BFRemoveLoopChecks.cc:180
DataDependenceEdge::dependenceType
DependenceType dependenceType() const
Definition: DataDependenceEdge.hh:88
BFRemoveLoopChecksAndJump::removedPOs_
std::set< ProgramOperationPtr, ProgramOperationPtrComparator > removedPOs_
Definition: BFRemoveLoopChecks.hh:49
BoostGraph::dropNode
virtual void dropNode(Node &node)
ProgramOperationPtr
std::shared_ptr< ProgramOperation > ProgramOperationPtr
Definition: MoveNode.hh:52
BoostGraph::outDegree
virtual int outDegree(const Node &node) const
assert
#define assert(condition)
Definition: Application.hh:86
BFRemoveLoopChecksAndJump::removedMoves_
DataDependenceGraph::NodeSet removedMoves_
Definition: BFRemoveLoopChecks.hh:48
BoostGraph::rootGraph
BoostGraph * rootGraph()
BoostGraph::inEdge
virtual Edge & inEdge(const Node &node, const int index) const
Operation.hh
MoveNode::isSourceOperation
bool isSourceOperation() const
Definition: MoveNode.cc:168
BFOptimization::ddg
DataDependenceGraph & ddg()
Definition: BFOptimization.cc:70
MoveNode::destinationOperationCount
unsigned int destinationOperationCount() const
BoostGraph::inDegree
virtual int inDegree(const Node &node) const
DataDependenceEdge::DEP_WAW
@ DEP_WAW
Definition: DataDependenceEdge.hh:49
Operation
Definition: Operation.hh:59
BFRemoveLoopChecksAndJump::checkAlivePO
void checkAlivePO(POSet &queuedAlivePo, POSet &finishedAlivePo, DataDependenceGraph::NodeSet &queuedAliveMoves, DataDependenceGraph::NodeSet &finishedAliveMoves)
Definition: BFRemoveLoopChecks.cc:105
MoveNode::destinationOperation
ProgramOperation & destinationOperation(unsigned int index=0) const
MoveNode::destinationOperationPtr
ProgramOperationPtr destinationOperationPtr(unsigned int index=0) const
BFRemoveLoopChecksAndJump::undoOnlyMe
void undoOnlyMe()
Definition: BFRemoveLoopChecks.cc:301
MoveNode::move
TTAProgram::Move & move()
BFRemoveLoopChecksAndJump::operator()
bool operator()()
Definition: BFRemoveLoopChecks.cc:7
DataDependenceGraph
Definition: DataDependenceGraph.hh:67
MoveNode::sourceOperationPtr
ProgramOperationPtr sourceOperationPtr() const
Definition: MoveNode.cc:458
DataDependenceEdge
Definition: DataDependenceEdge.hh:43
TTAProgram::Move::isJump
bool isJump() const
Definition: Move.cc:164
Move.hh
DataDependenceEdge::DEP_WAR
@ DEP_WAR
Definition: DataDependenceEdge.hh:48
BoostGraph::nodeCount
int nodeCount() const
BFRemoveLoopChecksAndJump::checkMaybeMoves
void checkMaybeMoves(DataDependenceGraph::NodeSet &maybeRemove, DataDependenceGraph::NodeSet &queuedToRemove, DataDependenceGraph::NodeSet &aliveNodes)
Definition: BFRemoveLoopChecks.cc:289
Operation::isControlFlowOperation
virtual bool isControlFlowOperation() const
Definition: Operation.cc:294
DataDependenceGraph::dropProgramOperation
void dropProgramOperation(ProgramOperationPtr po)
Definition: DataDependenceGraph.cc:361
BoostGraph::restoreNodeFromParent
void restoreNodeFromParent(GraphNode &node)
BFRemoveLoopChecksAndJump::POSet
std::set< ProgramOperationPtr, ProgramOperationPtrComparator > POSet
Definition: BFRemoveLoopChecks.hh:13