Advertisement
Sokolmish

oPtIMizaTiOn

Feb 4th, 2022
1,422
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.61 KB | None | 0 0
  1. // Without recursion, wrong
  2. void GraphInfo::dfs(UtilNode &startNode) {
  3.     /* Second element in pair is false -- if current node is a target to visit
  4.      * and false -- if it is a mark from already visited node, means that
  5.      * all subrees of this node are already visited
  6.      */
  7.     std::stack<std::pair<UtilNode*, bool>> stack;
  8.     stack.push({ &startNode, false });
  9.     std::set<int> visited;
  10.  
  11.     while (!stack.empty()) {
  12.         UtilNode &node = *stack.top().first;
  13.         bool isCompletion = stack.top().second;
  14.         stack.pop();
  15.  
  16.         if (isCompletion) {
  17.             node.timeOut = globalTime++;
  18.             continue;
  19.         }
  20.  
  21.         if (visited.contains(node.id))
  22.             continue;
  23.         visited.insert(node.id);
  24.  
  25.         funBlocksIds.push_back(node.id);
  26.         node.timeIn = globalTime++;
  27.         stack.push({ &node, true });
  28.  
  29.         IR_Block &block = cfg->block(node.id);
  30.         for (int nextId : block.next) {
  31.             auto it = utilNodes.lower_bound(nextId);
  32.             if (it == utilNodes.end() || it->first != nextId) {
  33.                 arcsClasses.emplace(std::make_pair(node.id, nextId), GraphInfo::TREE);
  34.  
  35.                 auto &domNode = domData.emplace(nextId, DomNode()).first->second;
  36.                 auto &newNode = utilNodes.emplace_hint(it, nextId, UtilNode(nextId, &domNode))->second;
  37.  
  38.                 newNode.parent = &node;
  39.                 stack.push({ &newNode, false });
  40.             }
  41.             else {
  42.                 unprocArcs.push_back(std::make_pair(&node, &it->second));
  43.  
  44.                 if (!visited.contains(it->second.parent->id)) {
  45.                     it->second.parent = &node;
  46.                     // TODO: Relink TREE arc...
  47.                     stack.push({ &it->second, false });
  48.                 }
  49.             }
  50.         }
  51.     }
  52. }
  53.  
  54.  
  55. // With recursion, correct
  56. void GraphInfo::dfs(UtilNode &node) {
  57.     funBlocksIds.push_back(node.id);
  58.     node.timeIn = globalTime++;
  59.  
  60.     IR_Block &block = cfg->block(node.id);
  61.     for (int nextId : block.next) {
  62.         auto it = utilNodes.lower_bound(nextId);
  63.         if (it == utilNodes.end() || it->first != nextId) {
  64.             arcsClasses.emplace(std::make_pair(node.id, nextId), GraphInfo::TREE);
  65.  
  66.             auto &domNode = domData.emplace(nextId, DomNode()).first->second;
  67.             auto &newNode = utilNodes.emplace_hint(it, nextId, UtilNode(nextId, &domNode))->second;
  68.  
  69.             newNode.parent = &node;
  70.             dfs(newNode);
  71.         }
  72.         else {
  73.             unprocArcs.push_back(std::make_pair(&node, &it->second));
  74.         }
  75.     }
  76.     node.timeOut = globalTime++;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement