51 #include <boost/graph/reverse_graph.hpp>
52 #include <boost/graph/depth_first_search.hpp>
53 #include <boost/graph/breadth_first_search.hpp>
54 #include <boost/graph/properties.hpp>
55 #include <boost/graph/strong_components.hpp>
56 #include <boost/graph/graph_utility.hpp>
57 #include <boost/timer.hpp>
58 #include <boost/format.hpp>
100 startAddress_(
TTAProgram::NullAddress::instance()),
132 std::vector<Node*> cdNodes;
143 for (
unsigned int i = 0; i <
iDomTree_.size(); i++) {
144 Node* cNode = cdNodes[i];
145 if (cNode->isEntryNode() || cNode->isExitNode()) {
150 DependenceMap::iterator rItr;
151 rItr = regionNodes.begin();
158 while (rItr != regionNodes.end()) {
161 "Node %s of procedure %s is missing dependencies. "
162 "Most likely CFG with unreachable BB. Can not create CDG!")
163 % cNode->toString() %
name()).str();
167 dependencies[cNode], (*rItr).second, (*rItr).first)){
172 if (found ==
true && dependencies[cNode]->size() == 1) {
176 *dependencies[cNode]->at(0).first, *cNode);
179 if (dependencies[cNode]->size() > 0) {
186 std::pair<Node*, DependentOn*>(newNode, dOn));
192 DependenceMap::iterator regionItr;
193 regionItr = regionNodes.begin();
194 while (regionItr != regionNodes.end()) {
197 for (
unsigned int i = 0; i < (*regionItr).second->size(); i++) {
199 *(*regionItr).second->at(i).first, *(*regionItr).first,
200 (*regionItr).second->at(i).second);
210 if (testNode.isExitNode()) {
218 if (testNode.isEntryNode()) {
221 "Entry node of procedure %s has more then one child node."
222 "Invalid graph!") %
name()).str();
258 if (subSet->size() > wholeSet->size()) {
261 std::vector<SourceType> missing;
262 unsigned int numberOfFound = 0;
263 for (
unsigned int i = 0; i < wholeSet->size(); i++) {
266 for (
unsigned int j = 0; j < subSet->size(); j++) {
268 if (test.first == tmp.first && test.second == tmp.second) {
273 if (found ==
false) {
274 missing.push_back(test);
277 if (numberOfFound != subSet->size()) {
281 for (
unsigned int i = 0; i < missing.size(); i++) {
282 wholeSet->push_back(missing[i]);
305 std::vector<int>& iDom,
312 while (finger1 < finger2) {
313 finger1 = iDom[finger1];
315 while (finger2 < finger1) {
316 finger2 = iDom[finger2];
318 }
while (finger1 != finger2);
335 Edge::CDGEdgeType edgeValue) {
343 theEdge =
new Edge(edgeValue);
371 bool modified =
false;
373 std::vector<ControlFlowEdge*> addedEdges;
380 boost::time_stamper<PostOrder, int, boost::on_finish_vertex>
381 pOrderStamper(postOrder, tStamp);
385 boost::depth_first_visit(
387 boost::make_dfs_visitor(pOrderStamper), colors);
393 std::vector<BasicBlockNode*> postZero;
397 postZero.push_back(testedNode);
400 if (postZero.size() > 1) {
401 for (
unsigned int i = 0; i < postZero.size(); i++) {
409 addedEdges.push_back(
edge);
430 for (
unsigned int i = 0; i < nodes.size(); i++) {
463 "Missing postOrder number of predecessor!";
467 for (predIndex = predIndex + 1;
483 for (
unsigned int i = 0; i < addedEdges.size(); i++) {
507 "Graph %s has node %s with no input edges which is "
508 "not the entry node!\n") %
name()
516 "Corrupted graph. Found multiple entry nodes!");
518 result =
dynamic_cast<Node*
>(&
node(i));
522 if (found ==
false || result == NULL) {
524 "Graph %s does not have entry node or has mutliple nodes with"
525 " no input edges!") %
name()).str();
548 std::vector<Node*>& cdNodes,
552 std::map<BasicBlockNode*, Node*> BBCD;
559 if (nodeOutDegree == 2 && (!bbNode->
isEntryBB())) {
563 }
else if(nodeOutDegree > 2){
570 "Basic block %s has out degree of %d. Most likely "
571 "indirect jump. Can not create CDG for that atm.")
572 % bbNode->
toString() % nodeOutDegree).str();
575 cd =
new Node(typeOfNode, bbNode);
577 BBCD.insert(std::pair<BasicBlockNode*,Node*>(
580 if (nodeOutDegree < 2) {
583 }
else if(nodeOutDegree > 2){
590 "Basic block %s has out degree of %d. Most likely "
591 "indirect jump. Can not create CDG for that atm.")
592 % bbNode->
toString() % nodeOutDegree).str();
595 for (
int j = 0 ; j < nodeOutDegree; j++) {
617 MapTools::valueForKey<Node*>(
618 BBCD, bbNode), edgeType);
620 while (nPoDom != runnerPo) {
623 Node* runnerCD = NULL;
625 runnerCD = MapTools::valueForKey<Node*>(
626 BBCD, nodes[runnerPo]);
629 !nodes[runnerPo]->isEntryBB()) {
640 std::pair<BasicBlockNode*,Node*>(
641 nodes[runnerPo], runnerCD));
644 DependentOn* dep = MapTools::valueForKey<DependentOn*>(
645 dependencies, runnerCD);
646 dep->push_back(newSource);
649 dep->push_back(newSource);
651 std::pair<Node*, DependentOn*>(
659 for (
unsigned int i = 0; i < nodes.size(); i++) {
661 cdNode = MapTools::valueForKey<Node*>(
663 cdNodes.push_back(cdNode);
696 std::vector<Node*> trueLinks;
697 std::vector<Node*> falseLinks;
700 for (
int j = 0; j < currDegree; j++) {
703 trueLinks.push_back(&
headNode(*currentOutEdge));
707 falseLinks.push_back(&
headNode(*currentOutEdge));
712 if (trueLinks.size() > 1) {
717 for (
unsigned int j = 0; j < trueLinks.size(); j++) {
723 if (falseLinks.size() > 1) {
728 for (
unsigned int j = 0; j < falseLinks.size(); j++) {
752 "\tStarting CDG serialization for %s with %d nodes and %d edges.\n")
765 elapsed =
static_cast<long>(timer.elapsed());
768 "\t\tStrong components: %d components, %d minutes and %d seconds.\n")
778 Color colorsDFS(colorMap);
781 boost::time_stamper<CDGOrder, int, boost::on_finish_vertex>
782 lastOrderStamper(lastOrder, fStamp);
785 boost::depth_first_visit(
787 boost::make_dfs_visitor(lastOrderStamper), colorsDFS);
788 elapsed =
static_cast<long>(timer.elapsed());
791 "\t\tPost order: %d minutes and %d seconds.\n")
792 % (elapsed/60) % (elapsed%60)).str();
800 elapsed =
static_cast<long>(timer.elapsed());
803 "\t\tRegion: %d minutes and %d seconds.\n")
804 % (elapsed/60) % (elapsed%60)).str();
812 elapsed =
static_cast<long>(timer.elapsed());
815 "\t\tEEC: %d minutes and %d seconds.\n")
816 % (elapsed/60) % (elapsed%60)).str();
824 elapsed =
static_cast<long>(timer.elapsed());
827 "\t\tRelations: %d minutes and %d seconds.\n")
828 % (elapsed/60) % (elapsed%60)).str();
856 std::vector<std::pair<Node*, Node*> > backEdges;
858 int currentComponents = 0;
862 CDGOrder componentOrder(components);
864 currentComponents = 0;
866 currentComponents = boost::strong_components(
867 graph_, componentOrder, boost::root_map(componentRoots));
870 std::vector<std::set<Node*> > componentVector;
883 for (CDGOrderMap::iterator iterA = components.begin();
884 iterA != components.end(); iterA ++) {
886 componentVector[(*iterA).second].insert(cNode);
889 for (
unsigned int i =
componentCount; i < componentVector.size(); i++) {
890 if (componentVector[i].size() > 1) {
891 std::set<Node*>& vector = componentVector[i];
894 for (std::set<Node*>::iterator iterB = vector.begin();
895 iterB != vector.end();
899 if ((*iterB)->component() == -1){
900 (*iterB)->setComponent(componentsSize);
910 std::vector<std::pair<Node*,int> > newEntry;
913 std::map<Node*, std::vector<Node*> > incomingToEntry;
918 for (std::set<Node*>::iterator iterD = nodes.begin();
919 iterD != nodes.end();
922 for (EdgeSet::iterator ei = edges.begin();
926 if (nodes.find(testedNode) == nodes.end()) {
927 if (!(*iterD)->isLoopEntryNode(i)) {
929 (*iterD)->setLoopEntryNode(i);
931 std::pair<Node*,int>(*iterD,i));
932 incomingToEntry[*iterD].push_back(testedNode);
937 incomingToEntry[*iterD].push_back(testedNode);
946 for (
unsigned int j = 0; j < newEntry.size(); j++) {
947 Node* loopNode = newEntry[j].first;
948 std::set<Node*>& nodes =
952 close->setComponent(newEntry[j].second);
960 std::vector<Edge*> storeEdges;
961 for (EdgeSet::iterator ei = edges.begin();
965 if (nodes.find(sourceNode) != nodes.end()){
966 storeEdges.push_back(*ei);
970 for (
unsigned int counter = 0;
971 counter < storeEdges.size();
973 moveInEdge(*loopNode, *close, *storeEdges[counter]);
977 std::pair<Node*, Node*>(close, loopNode));
982 "Close node for loop entry node %s was not connected!\n")
983 % loopNode->toString()).str();
989 std::vector<Node*> tmp =
990 MapTools::valueForKey<std::vector<Node*> >(
991 incomingToEntry, loopNode);
992 if (tmp.size() > 1) {
995 for (
unsigned int i = 0; i < tmp.size(); i++) {
996 Node* input = tmp[i];
1011 for (
unsigned int i = 0; i < backEdges.size(); i++) {
1034 int mapSize = orderMap.size();
1037 "No nodes in CDG graph for " +
name() +
"!");
1040 for (
int i = mapSize -1 ; i >= 0 ; i--) {
1042 MapTools::keyForValue<NodeDescriptor>(orderMap,i);
1050 for (Node::NodesInfo::iterator k = finalNodesInfo.begin();
1051 k != finalNodesInfo.end(); k++) {
1060 std::vector<Node*> entries;
1062 for (std::set<Node*>::iterator si = component.begin();
1063 si != component.end(); si++) {
1068 entries.push_back(*si);
1072 for (
unsigned int i = 0; i < entries.size(); i++) {
1076 for (
unsigned j = 0; j < entries.size(); j++) {
1077 Node* result = entries[j];
1078 for (Node::NodesInfo::iterator k = finalNodesInfo.begin();
1079 k != finalNodesInfo.end(); k++) {
1080 result->addToRegion(**k);
1101 int mapSize = orderMap.size();
1102 for (
int i = 0; i < mapSize; i++) {
1104 MapTools::keyForValue<NodeDescriptor>(
1114 std::vector<Node*> closeNodes;
1117 for (std::set<Node*>::iterator si = component.begin();
1118 si != component.end(); si++) {
1119 if ((*si)->isLoopCloseNode()) {
1120 closeNodes.push_back(*si);
1121 }
else if ((*si)->isRegionNode()
1122 || (*si)->isPredicateNode()) {
1123 (*si)->setLastNode();
1129 for (
unsigned int i = 0; i < closeNodes.size(); i++) {
1131 finalInfo, closeNodes[i]->region(), storeResult);
1132 finalInfo.swap(storeResult);
1133 storeResult.clear();
1136 for (
unsigned j = 0; j < closeNodes.size(); j++) {
1137 Node* result = closeNodes[j];
1138 if(result->eec().size() != 0) {
1140 "Close node %s in %s already has eec!\n")
1144 for (Node::NodesInfo::iterator k = finalInfo.begin();
1145 k != finalInfo.end(); k++) {
1146 result->addToEEC(**k);
1154 for (Node::NodesInfo::iterator t = regionX.begin();
1155 t != regionX.end(); t++) {
1165 for (Node::NodesInfo::iterator t = regionX.begin();
1166 t != regionX.end(); t++) {
1178 for (NodeSet::iterator su = succ.begin();
1179 su != succ.end(); su++) {
1180 childNodes.insert(*su);
1186 (*(childNodes.begin()))->eec().begin(),
1187 (*(childNodes.begin()))->eec().end());
1189 for(Node::NodesInfo::iterator j = childNodes.begin();
1190 j != childNodes.end(); j++ ) {
1193 finalEEC.swap(storeResult);
1194 storeResult.clear();
1196 std::vector<Node*> result(finalEEC.size(), NULL);
1199 std::vector<Node*>::iterator resultEnd =
1200 std::set_difference(finalEEC.begin(), finalEEC.end(),
1201 childNodes.begin(), childNodes.end(), result.begin());
1203 for (std::vector<Node*>::iterator t = result.begin();
1204 t != resultEnd; t++) {
1222 if (a->isBBNode() && b->isBBNode()
1223 && !a->isPredicateNode() && !b->isPredicateNode()) {
1239 if ((a->isLoopEntryNode() || a->isPredicateNode())
1240 && (b->isBBNode() && !b->isPredicateNode())
1242 if (a->isLastNode()) {
1247 if ((a->isLoopEntryNode() || a->isPredicateNode())
1248 && (b->isLoopEntryNode() || b->isPredicateNode())
1249 && AinA ==
true && BinB ==
true) {
1250 if (a->isLastNode()) {
1253 if (b->isLastNode()) {
1258 if ((a->isLoopEntryNode() || a->isPredicateNode())
1259 && (b->isBBNode() && !b->isPredicateNode())
1263 if ((a->isLoopEntryNode() || a->isPredicateNode())
1264 && (b->isLoopEntryNode() || b->isPredicateNode())
1265 && AinA ==
false && BinB ==
true) {
1268 if ((a->isLoopEntryNode() || a->isPredicateNode())
1269 && (b->isLoopEntryNode() || b->isPredicateNode())
1270 && AinA ==
false && BinB ==
false) {
1273 if ((a->isRegionNode() && AinB ==
true)
1274 && (b->isBBNode() || b->isLoopEntryNode() || b->isPredicateNode())) {
1277 if ((a->isRegionNode() && AinB ==
false)
1278 && (b->isLoopEntryNode() || b->isPredicateNode())
1282 if ((a->isRegionNode() && AinB ==
false)
1283 && (b->isRegionNode() && BinA ==
true)) {
1286 if ((a->isRegionNode() && AinB ==
false)
1287 && (b->isRegionNode() && BinA ==
false)) {
1290 if ((a->isLoopCloseNode() && AinB ==
true)
1291 && (b->isBBNode() || b->isPredicateNode() || b->isLoopEntryNode()
1292 || b->isRegionNode())) {
1295 if ((a->isLoopCloseNode() && AinB ==
false)
1296 && (b->isPredicateNode() || b->isLoopEntryNode()
1297 || b->isRegionNode())) {
1307 if ((a->isRegionNode() && AinB ==
true)
1308 && (b->isRegionNode() && BinA ==
true)) {
1309 if (a->isLastNode()) {
1312 if (b->isLastNode()) {
1334 std::vector<Node::NodesInfo> tmpResult;
1337 for (EdgeSet::iterator ei = edges.begin();
1341 if (previous->isRegionNode()
1342 || previous->isLoopEntryNode()
1343 || previous->isEntryNode()) {
1344 if (previous->region().size() == 0 &&
1345 !previous->isEntryNode()) {
1350 for (Node::NodesInfo::iterator k = addedNodesInfo.begin();
1351 k != addedNodesInfo.end();
1353 previous->addToRegion(**k);
1359 for (EdgeSet::iterator ei = outgoingEdges.begin();
1360 ei != outgoingEdges.end();
1363 tmp.insert(testedNode);
1365 tmpResult.push_back(tmp);
1366 }
else if (previous->isPredicateNode()){
1369 tmpResult.push_back(tmp);
1371 if (!previous->isLoopCloseNode()) {
1373 "Node: %s , parent %s.")
1380 if (tmpResult.size() > 0) {
1381 finalNodesInfo.insert(
1382 tmpResult[0].begin(), tmpResult[0].end());
1387 for (
unsigned int l = 1; l < tmpResult.size(); l++) {
1390 finalNodesInfo, tmpResult[l], storeResult);
1391 finalNodesInfo.swap(storeResult);
1392 storeResult.clear();
1406 int mapSize = orderMap.size();
1407 for (
int i = 0; i < mapSize; i++) {
1409 MapTools::keyForValue<NodeDescriptor>(orderMap, i);
1435 std::vector<std::pair<Node*, Node*> > newEdges;
1436 for (EdgeSet::iterator ei1 = edges.begin();
1443 EdgeSet::iterator ei2 = ei1;
1445 for (; ei2 != edges.end(); ++ei2) {
1450 if (result ==
ERROR) {
1455 newEdges.push_back(std::pair<Node*, Node*>(node1, node2));
1458 newEdges.push_back(std::pair<Node*, Node*>(node2, node1));
1463 "Nodes %s and %s can not be put into any order.\n")
1464 % node1->toString()% node2->toString()).str();
1471 __FILE__, __LINE__,
__func__, (boost::format(
1472 "Ordering error for A='%s' and B='%s'.")
1473 % node1->toString() % node2->toString()).str());
1483 for (
unsigned int i = 0; i < newEdges.size(); i++) {
1485 connectNodes(*newEdges[i].first, *newEdges[i].second, *direction);