40 #include <boost/graph/depth_first_search.hpp>
41 #include <boost/graph/properties.hpp>
42 #include <boost/graph/strong_components.hpp>
43 #include <boost/graph/graph_utility.hpp>
44 #include <boost/graph/topological_sort.hpp>
45 #include <boost/graph/exception.hpp>
46 #include <boost/timer.hpp>
47 #include <boost/format.hpp>
48 #include <boost/graph/graphviz.hpp>
111 for (
int i = 0; i < cdg.
nodeCount(); i++) {
134 std::pair<ControlDependenceNode*, ProgramDependenceNode*>(
138 std::pair<BasicBlockNode*, ControlDependenceNode*>(
144 std::pair<BasicBlockNode*, ControlDependenceNode*>(
150 for (
int i = 0; i < cdg.
edgeCount(); i++) {
168 sourceNode = MapTools::valueForKey<ProgramDependenceNode*>(
170 targetNode = MapTools::valueForKey<ProgramDependenceNode*>(
183 for (
int i = 0; i < ddg.
nodeCount(); i++) {
184 Node* newNode = NULL;
189 node.move().isControlFlowMove() &&
190 !
node.move().isUnconditional()) {
192 }
else if (
node.isMove() &&
193 node.move().isControlFlowMove() &&
194 node.move().isJump() &&
195 !
node.move().isReturn()){
204 movePD.insert(std::pair<MoveNode*, ProgramDependenceNode*>(
209 for (
int j = 0; j < ddg.
edgeCount(); j++) {
216 pSource = MapTools::valueForKey<ProgramDependenceNode*>(
221 pTarget = MapTools::valueForKey<ProgramDependenceNode*>(
229 for (
int i = 0; i < ddg.
nodeCount(); i++) {
242 "No Control Dependence Node for Basic Block Node!");
245 cNode = MapTools::valueForKey<ControlDependenceNode*>(
254 pNode = MapTools::valueForKey<ProgramDependenceNode*>(
269 std::vector<Node*> tmp;
270 tmp.push_back(pNode);
271 moveToCD[cNode] = tmp;
273 std::vector<Node*> tmp = moveToCD[cNode];
274 tmp.push_back(pNode);
275 moveToCD[cNode] = tmp;
280 for (
int j = 0; j < cdg.
inDegree(*cNode); j++) {
292 pSource = MapTools::valueForKey<ProgramDependenceNode*>(
338 "Guarded jump (%s) has inDegree different from 2! Degree =%d.\n")
341 for (EdgeSet::iterator ei = e.begin(); ei != e.end(); ei++) {
344 << (*ei)->toString() <<
" ";
348 for (
int i = 0; i <
inDegree(pNode); i++) {
354 if (guardSource == NULL) {
357 "Guarded jump did not have source of guard defined!");
377 pTarget = MapTools::valueForKey<ProgramDependenceNode*>(
388 "Basic block with guarded jump does not have"
389 " Region nodes as control dependent!");
405 "\tStarting PDG serialization for %s with %d nodes and %d edges.\n")
420 componentMap, rootMap, filteredCDG);
421 elapsed =
static_cast<long>(timer.elapsed());
424 "\t\tStrong components:%d components, %d minutes and %d seconds.\n")
425 % componentCount % (elapsed/60) % (elapsed%60)).str();
434 Color colorsDFS(colorMap);
437 boost::time_stamper<PDGOrder, int, boost::on_finish_vertex>
438 lastOrderStamper(lastOrder, fStamp);
441 boost::depth_first_visit(
443 boost::make_dfs_visitor(lastOrderStamper), colorsDFS);
444 elapsed =
static_cast<long>(timer.elapsed());
447 "\t\tPost order: %d minutes and %d seconds.\n")
448 % (elapsed/60) % (elapsed%60)).str();
455 elapsed =
static_cast<long>(timer.elapsed());
458 "\t\tRegion: %d minutes and %d seconds.\n")
459 % (elapsed/60) % (elapsed%60)).str();
465 elapsed =
static_cast<long>(timer.elapsed());
468 "\t\tEEC: %d minutes and %d seconds.\n")
469 % (elapsed/60) % (elapsed%60)).str();
480 for (Node::NodesInfo::iterator k = finalNodesInfo.begin();
481 k != finalNodesInfo.end(); k++) {
492 elapsed =
static_cast<long>(timer.elapsed());
495 "\t\tRelations: %d minutes and %d seconds.\n")
496 % (elapsed/60) % (elapsed%60)).str();
500 "Procedure %s has %d nodes and %d edges.\n")
529 std::vector<std::pair<Node*, Node*> > backEdges;
530 int componentCount = 0;
531 int currentComponents = 0;
532 int numberOfNodes = 0;
537 PDGOrder componentOrder(components);
539 currentComponents = 0;
541 numberOfNodes = boost::num_vertices(cdg);
542 currentComponents = boost::strong_components(
543 cdg, componentOrder, boost::root_map(componentRoots));
546 std::vector<std::set<Node*> > componentVector;
547 componentVector.resize(componentCount + currentComponents);
551 if (currentComponents == numberOfNodes) {
560 for (PDGOrderMap::iterator iterA = components.begin();
561 iterA != components.end(); iterA ++) {
562 Node* cNode = cdg[(*iterA).first];
563 componentVector[(*iterA).second].insert(cNode);
565 for (
unsigned int i = componentCount; i < componentVector.size(); i++){
566 if (componentVector[i].size() > 1) {
567 std::set<Node*>& vector = componentVector[i];
570 for (std::set<Node*>::iterator iterB = vector.begin();
571 iterB != vector.end();
575 if ((*iterB)->component() == -1){
576 (*iterB)->setComponent(componentsSize);
586 std::vector<std::pair<Node*,int> > newEntry;
589 std::map<Node*, std::vector<Node*> > incomingToEntry;
590 for (
unsigned int i = componentCount;
594 for (std::set<Node*>::iterator iterD = nodes.begin();
595 iterD != nodes.end();
605 Node* testedNode = cdg[boost::source(*ei, cdg)];
606 if (nodes.find(testedNode) == nodes.end()) {
609 if (!(*iterD)->isLoopEntryNode(i)) {
612 std::pair<Node*,int>(*iterD,i));
613 incomingToEntry[*iterD].push_back(testedNode);
618 incomingToEntry[*iterD].push_back(testedNode);
627 for (
unsigned int j = 0; j < newEntry.size(); j++) {
628 Node* loopNode = newEntry[j].first;
629 std::set<Node*>& nodes =
642 std::vector<Edge*> storeEdges;
648 Node* sourceNode = cdg[boost::source(*ei, cdg)];
649 if (nodes.find(sourceNode) != nodes.end()){
652 storeEdges.push_back(
edge);
656 for (
unsigned int counter = 0;
657 counter < storeEdges.size();
659 moveInEdge(*loopNode, *close, *storeEdges[counter]);
663 std::pair<Node*, Node*>(close, loopNode));
668 TCEString msg =
"Close node for loop entry node ";
669 msg += loopNode->
toString() +
" was not connected!";
676 std::vector<Node*> tmp =
677 MapTools::valueForKey<std::vector<Node*> >(
678 incomingToEntry, loopNode);
679 if (tmp.size() > 1) {
684 for (
unsigned int i = 0; i < tmp.size(); i++) {
685 Node* input = tmp[i];
700 for (
unsigned int i = 0; i < backEdges.size(); i++) {
704 connectNodes(*backEdges[i].first, *backEdges[i].second, *pdgEdge);
709 std::cerr <<
"\tComponent: " << i << std::endl;
712 std::cerr <<
"\t\t" << (*iter)->toString() << std::endl;
714 std::cerr << std::endl;
717 return componentCount;
738 int mapSize = orderMap.size();
741 "No nodes in CDG graph for " +
name() +
"!");
746 for (
int i = mapSize -1 ; i >= 0 ; i--) {
748 MapTools::keyForValue<NodeDescriptor>(orderMap,i);
755 for (Node::NodesInfo::iterator k = finalNodesInfo.begin();
756 k != finalNodesInfo.end(); k++) {
766 std::vector<Node*> entries;
768 for (std::set<Node*>::iterator si = component.begin();
769 si != component.end(); si++) {
772 entries.push_back(*si);
776 for (
unsigned int i = 0; i < entries.size(); i++) {
781 for (
unsigned j = 0; j < entries.size(); j++) {
782 Node* result = entries[j];
783 for (Node::NodesInfo::iterator k = finalNodesInfo.begin();
784 k != finalNodesInfo.end();
810 int mapSize = orderMap.size();
812 for (
int i = 0; i < mapSize; i++) {
814 MapTools::keyForValue<NodeDescriptor>(
824 std::vector<Node*> closeNodes;
827 for (std::set<Node*>::iterator si = component.begin();
828 si != component.end(); si++) {
829 if ((*si)->isLoopCloseNode()) {
830 closeNodes.push_back(*si);
831 }
else if ((*si)->isRegionNode()
832 || (*si)->isPredicateMoveNode()) {
833 (*si)->setLastNode();
839 for (
unsigned int i = 0; i < closeNodes.size(); i++) {
841 finalInfo, closeNodes[i]->region(), storeResult);
842 finalInfo.swap(storeResult);
846 for (
unsigned j = 0; j < closeNodes.size(); j++) {
847 Node* result = closeNodes[j];
849 if(result->
eec().size() != 0) {
851 "Close node %s in %s already has eec!\n")
855 for (Node::NodesInfo::iterator k = finalInfo.begin();
856 k != finalInfo.end(); k++) {
861 }
else if (boost::out_degree(
descriptor(*
node), filteredCDG) == 0) {
864 for (Node::NodesInfo::iterator t = regionX.begin();
865 t != regionX.end(); t++) {
877 Node* testedNode = filteredCDG[boost::target(*ei, filteredCDG)];
878 childNodes.insert(testedNode);
884 (*(childNodes.begin()))->eec().begin(),
885 (*(childNodes.begin()))->eec().end());
887 for(Node::NodesInfo::iterator j = childNodes.begin();
888 j != childNodes.end(); j++ ) {
891 finalEEC.swap(storeResult);
894 std::vector<Node*> result(finalEEC.size(), NULL);
897 std::vector<Node*>::iterator resultEnd =
898 std::set_difference(finalEEC.begin(), finalEEC.end(),
899 childNodes.begin(), childNodes.end(), result.begin());
901 for (std::vector<Node*>::iterator t = result.begin();
902 t != resultEnd; t++) {
925 #ifdef AVOID_COMPILER_WARNINGS_REMOVE_THESE_LINES
970 && AinA ==
true && BinB ==
true) {
991 && AinA ==
false && BinB ==
true) {
996 && AinA ==
true && BinB ==
false) {
1001 && AinA ==
false && BinB ==
false) {
1073 "Found two regions with identical control dependencies. "
1074 "Known issue with CDG detection not reusing regions.\n")).str();
1095 "Trying to get entry node before it was defined!");
1116 std::vector<Node::NodesInfo> tmpResult;
1122 Node* previous = cdg[boost::source(*ei, cdg)];
1126 if (previous->
region().size() == 0 &&
1132 for (Node::NodesInfo::iterator k = addedNodesInfo.begin();
1133 k != addedNodesInfo.end();
1149 boost::out_edges(
descriptor(*previous), cdg);
1153 Node* testedNode = cdg[boost::target(*ei, cdg)];
1154 tmp.insert(testedNode);
1156 tmpResult.push_back(tmp);
1160 tmpResult.push_back(tmp);
1166 if (tmpResult.size() > 0) {
1167 finalNodesInfo.insert(
1168 tmpResult[0].begin(), tmpResult[0].end());
1173 for (
unsigned int l = 1; l < tmpResult.size(); l++) {
1176 finalNodesInfo, tmpResult[l], storeResult);
1177 finalNodesInfo.swap(storeResult);
1178 storeResult.clear();
1200 subgraphNodes.insert(regionNode);
1201 std::vector<std::pair<Node*, Node*> > newEdges;
1202 std::vector<std::pair<Node*, Node*> > unorderable;
1204 ei1 != edges.second;
1208 subgraphNodes.insert(node1);
1214 for (; ei2 != edges.second; ++ei2) {
1215 Node* node2 =
graph_[boost::target(*ei2, filteredCDG)];
1228 "Ordering error for A='%s' and B='%s'")
1231 newEdges.push_back(std::pair<Node*, Node*>(node1, node2));
1234 newEdges.push_back(std::pair<Node*, Node*>(node2, node1));
1239 "Nodes (%s) and (%s) can not "
1240 "be put into any order.\n")
1243 unorderable.push_back(
1244 std::pair<Node*, Node*>(node1, node2));
1256 for (
unsigned int i = 0; i < newEdges.size(); i++) {
1258 connectNodes(*newEdges[i].first, *newEdges[i].second, *direction);
1264 regionNode, subgraphNodes,
graph_);
1270 std::vector<NodeDescriptor> sorted;
1272 boost::topological_sort(
sub, std::back_inserter(sorted));
1273 }
catch (boost::not_a_dag & x) {
1280 std::ofstream output(outName.c_str());
1281 boost::write_graphviz(output,
sub,lb,lb);
1284 "Topological sort of %s(%s) failed.\n"
1285 "Most likely loop is present.\n")
1290 "Topological sort of %s(%s) failed.\n"
1291 "Most likely loop is present.\n")
1297 std::vector<BasicBlockNode*> leafBlocks;
1298 bool changeBB =
false;
1300 for (std::vector<NodeDescriptor>::reverse_iterator iter = sorted.rbegin();
1301 iter != sorted.rend(); iter++) {
1310 assert(leafBlocks.size() == 0 && lastBB == NULL);
1315 if (lastBB != NULL) {
1316 assert(leafBlocks.size() == 0);
1321 if (changeBB ==
true) {
1333 std::cerr <<
"Adding for lastBB != NULL" << std::endl;
1359 if (lastBB == NULL || changeBB ==
true) {
1377 if (changeBB ==
true && lastBB != NULL) {
1383 assert(leafBlocks.size() == 0);
1402 if (leafBlocks.size() > 0) {
1413 (boost::format(
" Undetected case! %s inside %s\n")
1420 if (leafBlocks.size() > 0) {
1424 if (lastBB != NULL) {
1433 unorderable.clear();
1450 int mapSize = orderMap.size();
1455 for (
int i = 0; i < mapSize; i++) {
1457 MapTools::keyForValue<NodeDescriptor>(
1495 for (NodeSet::iterator iter = subgraphNodes.begin();
1496 iter != subgraphNodes.end();
1500 if ((*iter) == &root) {
1505 bool copyInsteadOfMove =
false;
1506 if (!(*iter)->isMoveNode()) {
1509 int parentCount = boost::in_degree(
descriptor(**iter), filteredCDG);
1510 if (parentCount > 1) {
1511 copyInsteadOfMove =
true;
1516 for (EdgeSet::iterator ein = inputs.begin();
1517 ein != inputs.end();
1522 if (!(*ein)->isDataDependence() || (*ein)->fixed()) {
1527 Edge* testedEdge = *ein;
1529 bool duplicateEdge =
false;
1530 for (EdgeSet::iterator testIter = inputEdges.begin();
1531 testIter != inputEdges.end();
1533 if (!(*testIter)->isDataDependence()) {
1537 (*testIter)->dataDependenceEdge().dependenceType())
1540 (*testIter)->dataDependenceEdge().edgeReason())) {
1543 duplicateEdge =
true;
1546 if (copyInsteadOfMove && !duplicateEdge) {
1550 }
else if (duplicateEdge) {
1564 for (EdgeSet::iterator eout = outputs.begin();
1565 eout != outputs.end();
1570 if (!(*eout)->isDataDependence() || (*eout)->fixed()) {
1575 Edge* testedEdge = *eout;
1577 bool duplicateEdge =
false;
1578 for (EdgeSet::iterator testIter = outputEdges.begin();
1579 testIter != outputEdges.end();
1581 if (!(*testIter)->isDataDependence()) {
1585 (*testIter)->dataDependenceEdge().dependenceType())
1588 (*testIter)->dataDependenceEdge().edgeReason())) {
1591 duplicateEdge =
true;
1594 if (copyInsteadOfMove && !duplicateEdge) {
1598 }
else if (duplicateEdge) {
1607 (*eout)->setFixed();
1658 MapTools::valueForKey<ControlDependenceNode*>(
1659 bBlockToCDNode, BB);
1662 "No CD node for basic block!" + BB->
toString());
1667 std::vector<Node*> nodesInBB = moveToCD[cNode];
1668 int currentComponent = cNode->
component();
1669 for (
unsigned int i = 0; i < nodesInBB.size(); i++) {
1672 nodesInBB[i]->setComponent(currentComponent);
1678 for (ControlDependenceNode::NodesInfo::iterator iter = rInfo.begin();
1679 iter != rInfo.end();
1682 if ((*iter)->isRegionNode()
1683 || (*iter)->isLoopCloseNode()
1684 || (*iter)->isLoopEntryNode()) {
1685 Node* target = cDNodeToPNode[*iter];
1689 std::vector<Node*> nodesInBB = moveToCD[*iter];
1690 for (
unsigned int i = 0; i < nodesInBB.size(); i++) {
1704 eecInfo = cNode->
eec();
1713 for (ControlDependenceNode::NodesInfo::iterator iter =
1714 eecInfo.begin(); iter != eecInfo.end(); iter++) {
1715 if ((*iter)->isRegionNode()
1716 || (*iter)->isLoopCloseNode()
1717 || (*iter)->isLoopEntryNode()) {
1718 Node* target = cDNodeToPNode[*iter];
1721 std::vector<Node*> nodesInBB = moveToCD[*iter];
1722 for (
unsigned int i = 0; i < nodesInBB.size(); i++) {
1770 subgraphNodes.insert(predicate);
1789 bool hasTrue =
false;
1790 bool hasFalse =
false;
1792 ei1 != edges.second;
1800 subgraphNodes.insert(
node);
1821 std::cerr <<
"Adding for predicate" << std::endl;
1825 if(boost::out_degree(des, filteredCDG) != 0) {
1827 "Found a node without first BB set. %s for predicate %s"
1828 " in procedure %s\n")
1843 if (!(hasTrue && hasFalse)) {
1859 std::vector<BasicBlockNode*> leafBlocks,
1861 for (
unsigned int i = 0; i < leafBlocks.size(); i++) {
1870 for(ControlFlowGraph::EdgeSet::iterator cfe = edges.begin();
1871 cfe != edges.end(); cfe++) {
1872 if ((*cfe)->isTrueEdge()) {
1876 if ((*cfe)->isFalseEdge()) {
1884 "Adding leaf edge from %s to %s in %s\n")
1885 % leafBlocks[i]->toString() % bb->
toString() %
name()).str();
1887 std::cerr <<
"Adding for leaf edges" << std::endl;
1922 for(NodeSet::iterator ni = inputs.begin(); ni != inputs.end();
1924 if((*ni)->isLoopCloseNode()
1925 && (*ni)->newFirstBB() != NULL
1931 edge->setBackEdge();
1933 std::cerr <<
"Adding for loop entry" << std::endl;
1946 std::vector<ControlFlowGraph::NodeDescriptor> sorted;
1948 boost::topological_sort(*
newCFG_, std::back_inserter(sorted));
1951 "CFG %s can not be topologically sorted\n")
1954 for (std::vector<ControlFlowGraph::NodeDescriptor>::reverse_iterator iter =
1956 iter != sorted.rend(); iter++) {
1958 if (
node->isNormalBB()) {
1960 POMDIsassembler::disassemble(
node->basicBlock());
1965 "Trying out %s with node count %d\n")
1983 for (
int i = 0; i < count; i++) {
2009 "Creating jump from %s to %s\n")
2026 auto jumpMove = std::make_shared<TTAProgram::Move>(
2027 jump0Terminal, jump1Terminal,
2033 for (
int i = 0; i < busNav.
count(); i++) {
2035 for (
int i = 0; i < bus->
guardCount(); i++) {
2038 if (regGuard != NULL &&
2050 for (
int i = 0; i < busNav.
count(); i++) {
2052 for (
int i = 0; i < bus->
guardCount(); i++) {
2055 if (regGuard != NULL &&
2072 std::cerr <<
" before result " << inst->
toString() << std::endl;
2074 std::cerr <<
" after result " << inst->
toString() << std::endl;
2077 "Failed to add jump for %s %s, %s, %s\n")