OpenASIP  2.0
MoveNodeDuplicator.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2015 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 /**
26  * This class is used for copying moves from a basic block to another
27  * corresponding basic block, copying also the programoperation structure.
28  */
29 
30 #include "MoveNodeDuplicator.hh"
31 
32 #include "BasicBlockNode.hh"
33 #include "AssocTools.hh"
34 #include "DataDependenceGraph.hh"
35 #include "ProgramAnnotation.hh"
36 #include "HWOperation.hh"
37 #include "Operation.hh"
38 #include "UniversalMachine.hh"
39 #include "Terminal.hh"
40 #include "MoveGuard.hh"
41 #include "UniversalFunctionUnit.hh"
42 #include "Move.hh"
43 #include "TerminalFUPort.hh"
44 #include "ControlUnit.hh"
45 #include "SpecialRegisterPort.hh"
46 
47 
49  DataDependenceGraph& oldDDG, DataDependenceGraph& newDDG) :
50  oldDDG_(&oldDDG), newDDG_(&newDDG),
51  bigDDG_(static_cast<DataDependenceGraph*>(oldDDG.rootGraph())) { }
52 
54  if (newMN == NULL) {
55  return;
56  }
57  if (newMN->isScheduled()) {
58  std::cerr << "\t\t\t\t\tCannot dispose scheduled mn: "
59  << newMN->toString() << std::endl;
60  assert(false);
61  }
62  assert(!newMN->isScheduled());
63  std::map<MoveNode*,MoveNode*,MoveNode::Comparator>::iterator i =
64  oldMoveNodes_.find(newMN);
65  // already disposed?
66  if (i == oldMoveNodes_.end()) {
67  return;
68  }
69  MoveNode* old = i->second;
70  moveNodes_.erase(old);
71  oldMoveNodes_.erase(newMN);
72  TTAProgram::Move* oldMove = &old->move();
73  moves_.erase(oldMove);
74  if (newDDG_->hasNode(*newMN)) {
75  newDDG_->removeNode(*newMN);
76  }
77  delete newMN;
78 }
79 
80 /*
81  * Gets already duplicated move, but does not create a new one.
82  * If none found, returns null
83  */
84 MoveNode*
86  std::map<MoveNode*,MoveNode*,MoveNode::Comparator>::iterator iter =
87  moveNodes_.find(&old);
88 if (iter != moveNodes_.end()) {
89  return iter->second;
90  }
91  return NULL;
92 }
93 
94 
95 /**
96  *
97  * Duplicates a corresponding MoveNode a given move in the next BB.
98  *
99  * If no corresponding MoveNode created, creates one
100  *
101  * @param old ProgramOperation in jump target BB.
102  * @return new MoveNode for this BB.
103  */
104 
105 std::pair<MoveNode*, bool>
107  MoveNode& old, bool addToDDG, bool ignoreSameBBBackEdges) {
108  std::map<MoveNode*,MoveNode*,MoveNode::Comparator>::iterator iter =
109  moveNodes_.find(&old);
110 
111  if (iter != moveNodes_.end()) {
112  MoveNode* node = iter->second;
113  if (addToDDG) {
114  if (!newDDG_->hasNode(*node)) {
115  newDDG_->addNode(*node, *bbn_);
116  bigDDG_->copyDependencies(old,*node, ignoreSameBBBackEdges, false);
117  }
118  }
119  }
121 #ifdef DEBUG_BUBBLEFISH_SCHEDULER
122  std::cerr << "\t\t\t\t\told mn found: "
123  << moveNodes_[&old]->toString() << std::endl;
124 #endif
125  return std::make_pair(moveNodes_[&old], false);
126  } else {
127  auto movePtr = duplicateMove(old.move());
128  MoveNode *newMN = new MoveNode(movePtr);
129 
130  // TODO: only add to ddg if really scheduling, not for testing?
131  if (addToDDG) {
132  newDDG_->addNode(*newMN, *bbn_);
133  bigDDG_->copyDependencies(old,*newMN, ignoreSameBBBackEdges, false);
134  }
135 
136  moveNodes_[&old] = newMN;
137  oldMoveNodes_[newMN] = &old;
138  if (old.isSourceOperation()) {
139  newMN->setSourceOperationPtr(
141  newMN->sourceOperation().addOutputNode(*newMN);
142  assert(newMN->isSourceOperation());
143  }
144  if (old.isDestinationOperation()) {
145  for (unsigned int i = 0;
146  i < old.destinationOperationCount(); i++) {
149  old.destinationOperationPtr(i)));
150  newMN->destinationOperation(i).addInputNode(*newMN);
151  }
152  assert(newMN->isDestinationOperation());
153  }
154  if (old.isGuardOperation()) {
155  newMN->setGuardOperationPtr(
157  newMN->guardOperation().addGuardOutputNode(*newMN);
158  assert(newMN->isGuardOperation());
159  }
160  return std::make_pair(newMN, true);
161  }
162 }
163 
164 /**
165  * Gets a corresponding ProgramOperation to a given move in the next BB.
166  *
167  * If no corresponding ProgramOperation created, creates one
168  *
169  * @param old ProgramOperation in jump target BB.
170  * @return new ProgramOperation for this BB.
171  */
174  ProgramOperationPtr old) { // , BasicBlockNode& bbn) {
176  return programOperations_[old.get()];
177  } else {
180  new ProgramOperation(old->operation()));
181  programOperations_[old.get()] = po;
182  oldProgramOperations_[po.get()] = old;
183  return po;
184  }
185 }
186 
189  auto i = programOperations_.find(old.get());
190  if (i != programOperations_.end()) {
191  return i->second;
192  } else {
193  return ProgramOperationPtr(nullptr);
194  }
195 }
196 
197 
198 std::shared_ptr<TTAProgram::Move>
200  if (AssocTools::containsKey(moves_,&old)) {
201  return moves_[&old];
202  } else {
203  MoveNode& oldMN = oldDDG_->nodeOfMove(old);
204  std::shared_ptr<TTAProgram::Move> newMove(old.copy());
205  newMove->setBus(UniversalMachine::instance().universalBus());
206 
207  TTAProgram::Terminal& source = newMove->source();
208  if (oldMN.isSourceOperation()) {
209  if (oldMN.isScheduled()) {
210  assert(source.isFUPort());
211  std::string fuName = source.functionUnit().name();
212  //TODO: which is the correct annotation here?
215  fuName);
216  newMove->setAnnotation(srcUnit);
217  const Operation &srcOp = oldMN.sourceOperation().operation();
218  const TTAMachine::HWOperation& hwop =
220  operation(srcOp.name());
222  hwop, old.source().operationIndex()));
223  }
224  } else {
225  if (source.isRA()) {
226  newMove->setSource(
228  *UniversalMachine::instance().controlUnit()->
229  returnAddressPort()));
230  }
231  }
232 
233  TTAProgram::Terminal& dest = newMove->destination();
234  if (oldMN.isDestinationOperation()) {
235  assert(dest.isFUPort());
236 
237  if (oldMN.isScheduled()) {
238  std::string fuName = dest.functionUnit().name();
239  //TODO: which is the correct annotation here?
242  fuName);
243  newMove->setAnnotation(dstUnit);
244  const Operation &dstOp =
248  operation(dstOp.name());
250  hwop, old.destination().
251  operationIndex()));
252  } else {
253  if (dest.isRA()) {
254  newMove->setDestination(
256  *UniversalMachine::instance().controlUnit()->
257  returnAddressPort()));
258  }
259  }
260  }
261 
262  moves_[&old] = newMove;
263 
264  // set guard of new move to be same as old move.
265  if (!old.isUnconditional()) {
266  TTAProgram::MoveGuard* g = old.guard().copy();
267  newMove->setGuard(g);
268  }
269  return newMove;
270  }
271 }
272 
274  newDDG_->writeToDotFile("prolog_ddg.dot");
275 }
TTAProgram::Move::copy
std::shared_ptr< Move > copy() const
Definition: Move.cc:413
ProgramOperation::operation
const Operation & operation() const
Definition: ProgramOperation.cc:590
TTAProgram::Terminal::isFUPort
virtual bool isFUPort() const
Definition: Terminal.cc:118
MoveNodeDuplicator::newDDG_
DataDependenceGraph * newDDG_
Definition: MoveNodeDuplicator.hh:79
ProgramOperation::addGuardOutputNode
void addGuardOutputNode(MoveNode &node)
Definition: ProgramOperation.cc:201
MoveNodeDuplicator::bigDDG_
DataDependenceGraph * bigDDG_
Definition: MoveNodeDuplicator.hh:80
TTAMachine::Component::name
virtual TCEString name() const
Definition: MachinePart.cc:125
MoveNodeDuplicator::programOperations_
std::map< ProgramOperation *, ProgramOperationPtr, ProgramOperation::Comparator > programOperations_
Definition: MoveNodeDuplicator.hh:65
MoveNode::toString
std::string toString() const
Definition: MoveNode.cc:576
MoveNodeDuplicator::dumpDDG
void dumpDDG()
Definition: MoveNodeDuplicator.cc:273
MoveNode::guardOperationPtr
ProgramOperationPtr guardOperationPtr() const
Definition: MoveNode.cc:484
TTAMachine::HWOperation
Definition: HWOperation.hh:52
MoveNodeDuplicator::MoveNodeDuplicator
MoveNodeDuplicator(DataDependenceGraph &oldDDG, DataDependenceGraph &newDDG)
Definition: MoveNodeDuplicator.cc:48
MoveNode::isDestinationOperation
bool isDestinationOperation() const
AssocTools::containsKey
static bool containsKey(const ContainerType &aContainer, const KeyType &aKey)
TTAProgram::Move::isUnconditional
bool isUnconditional() const
Definition: Move.cc:154
TTAProgram::AnnotatedInstructionElement::setAnnotation
void setAnnotation(const ProgramAnnotation &annotation)
Definition: AnnotatedInstructionElement.cc:79
BasicBlockNode.hh
TTAProgram::Move::destination
Terminal & destination() const
Definition: Move.cc:323
MoveNodeDuplicator::moves_
std::map< TTAProgram::Move *, std::shared_ptr< TTAProgram::Move > > moves_
Definition: MoveNodeDuplicator.hh:75
UniversalMachine::instance
static UniversalMachine & instance()
Definition: UniversalMachine.cc:73
DataDependenceGraph.hh
ProgramOperation
Definition: ProgramOperation.hh:70
TTAProgram::Move::setGuard
void setGuard(MoveGuard *guard)
Definition: Move.cc:360
MoveNode
Definition: MoveNode.hh:65
Terminal.hh
MoveNodeDuplicator::duplicateProgramOperationPtr
ProgramOperationPtr duplicateProgramOperationPtr(ProgramOperationPtr old)
Definition: MoveNodeDuplicator.cc:173
Operation::name
virtual TCEString name() const
Definition: Operation.cc:93
MoveNodeDuplicator::bbn_
BasicBlockNode * bbn_
Definition: MoveNodeDuplicator.hh:77
ProgramOperationPtr
std::shared_ptr< ProgramOperation > ProgramOperationPtr
Definition: MoveNode.hh:52
ProgramOperation::addOutputNode
void addOutputNode(MoveNode &node, int outputIndex)
Definition: ProgramOperation.cc:167
MoveNodeDuplicator::duplicateMoveNode
std::pair< MoveNode *, bool > duplicateMoveNode(MoveNode &mn, bool addToDDG, bool ignoreSameBBBackEdges)
Definition: MoveNodeDuplicator.cc:106
MoveNode::setSourceOperationPtr
void setSourceOperationPtr(ProgramOperationPtr po)
Definition: MoveNode.cc:541
MoveNode::sourceOperation
ProgramOperation & sourceOperation() const
Definition: MoveNode.cc:453
assert
#define assert(condition)
Definition: Application.hh:86
UniversalMachine.hh
TTAProgram::Move::setDestination
void setDestination(Terminal *dst)
Definition: Move.cc:333
MoveNode::isGuardOperation
bool isGuardOperation() const
Definition: MoveNode.cc:181
TTAProgram::ProgramAnnotation::ANN_CONN_CANDIDATE_UNIT_SRC
@ ANN_CONN_CANDIDATE_UNIT_SRC
Src. unit candidate.
Definition: ProgramAnnotation.hh:115
TTAProgram::Terminal::operationIndex
virtual int operationIndex() const
Definition: Terminal.cc:364
HWOperation.hh
MoveNode::addDestinationOperationPtr
void addDestinationOperationPtr(ProgramOperationPtr po)
Definition: MoveNode.cc:533
UniversalFunctionUnit.hh
MoveNodeDuplicator::disposeMoveNode
void disposeMoveNode(MoveNode *newMN)
Definition: MoveNodeDuplicator.cc:53
TTAProgram::Move::guard
MoveGuard & guard() const
Definition: Move.cc:345
DataDependenceGraph::nodeOfMove
MoveNode & nodeOfMove(const TTAProgram::Move &move)
Definition: DataDependenceGraph.cc:3600
MoveNodeDuplicator::oldDDG_
DataDependenceGraph * oldDDG_
Definition: MoveNodeDuplicator.hh:78
DataDependenceGraph::removeNode
void removeNode(MoveNode &node)
Definition: DataDependenceGraph.cc:2843
Operation.hh
MoveNode::isSourceOperation
bool isSourceOperation() const
Definition: MoveNode.cc:168
TerminalFUPort.hh
TTAProgram::Move
Definition: Move.hh:55
MoveNode::destinationOperationCount
unsigned int destinationOperationCount() const
MoveNodeDuplicator::getProgramOperation
ProgramOperationPtr getProgramOperation(ProgramOperationPtr old)
Definition: MoveNodeDuplicator.cc:188
BoostGraph::hasNode
bool hasNode(const Node &) const
Operation
Definition: Operation.hh:59
MoveNodeDuplicator.hh
GraphBase::writeToDotFile
virtual void writeToDotFile(const TCEString &fileName) const
TTAProgram::TerminalFUPort
Definition: TerminalFUPort.hh:56
TTAProgram::Terminal::functionUnit
virtual const TTAMachine::FunctionUnit & functionUnit() const
Definition: Terminal.cc:251
MoveNodeDuplicator::moveNodes_
std::map< MoveNode *, MoveNode *, MoveNode::Comparator > moveNodes_
Definition: MoveNodeDuplicator.hh:72
UniversalMachine::universalFunctionUnit
UniversalFunctionUnit & universalFunctionUnit() const
Definition: UniversalMachine.cc:205
MoveNodeDuplicator::getMoveNode
MoveNode * getMoveNode(MoveNode &mn)
Definition: MoveNodeDuplicator.cc:85
MoveNode::destinationOperation
ProgramOperation & destinationOperation(unsigned int index=0) const
DataDependenceGraph::addNode
void addNode(MoveNode &moveNode)
Definition: DataDependenceGraph.cc:144
ProgramOperation::addInputNode
void addInputNode(MoveNode &node)
Definition: ProgramOperation.cc:144
MoveNode::destinationOperationPtr
ProgramOperationPtr destinationOperationPtr(unsigned int index=0) const
MoveNode::move
TTAProgram::Move & move()
MoveNodeDuplicator::oldProgramOperations_
std::map< ProgramOperation *, ProgramOperationPtr, ProgramOperation::Comparator > oldProgramOperations_
Definition: MoveNodeDuplicator.hh:70
MoveNodeDuplicator::oldMoveNodes_
std::map< MoveNode *, MoveNode *, MoveNode::Comparator > oldMoveNodes_
Definition: MoveNodeDuplicator.hh:74
AssocTools.hh
ControlUnit.hh
DataDependenceGraph
Definition: DataDependenceGraph.hh:67
SpecialRegisterPort.hh
MoveNode::setGuardOperationPtr
void setGuardOperationPtr(ProgramOperationPtr po)
Definition: MoveNode.cc:550
MoveNode::sourceOperationPtr
ProgramOperationPtr sourceOperationPtr() const
Definition: MoveNode.cc:458
TTAProgram::Terminal
Definition: Terminal.hh:60
MoveNode::isScheduled
bool isScheduled() const
Definition: MoveNode.cc:409
TTAProgram::Move::source
Terminal & source() const
Definition: Move.cc:302
TTAProgram::MoveGuard::copy
MoveGuard * copy() const
Definition: MoveGuard.cc:96
TTAProgram::ProgramAnnotation
Definition: ProgramAnnotation.hh:49
Move.hh
TTAProgram::Terminal::isRA
virtual bool isRA() const
Definition: Terminal.cc:129
TTAProgram::MoveGuard
Definition: MoveGuard.hh:47
TTAProgram::Move::setBus
void setBus(const TTAMachine::Bus &bus)
Definition: Move.cc:383
ProgramAnnotation.hh
DataDependenceGraph::copyDependencies
void copyDependencies(const MoveNode &src, MoveNode &dst, bool ignoreSameBBBackedges, bool moveOverLoopEdge=true)
Definition: DataDependenceGraph.cc:3816
TTAProgram::Move::setSource
void setSource(Terminal *src)
Definition: Move.cc:312
MoveNodeDuplicator::duplicateMove
std::shared_ptr< TTAProgram::Move > duplicateMove(TTAProgram::Move &old)
Definition: MoveNodeDuplicator.cc:199
TTAProgram::ProgramAnnotation::ANN_CONN_CANDIDATE_UNIT_DST
@ ANN_CONN_CANDIDATE_UNIT_DST
Dst. unit candidate.
Definition: ProgramAnnotation.hh:116
MoveNode::guardOperation
ProgramOperation & guardOperation() const
Definition: MoveNode.cc:479
MoveGuard.hh