IteratedDominanceFrontier.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //===- IteratedDominanceFrontier.cpp - Compute IDF ------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. /// \brief Compute iterated dominance frontiers using a linear time algorithm.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Analysis/IteratedDominanceFrontier.h"
  14. #include "llvm/IR/CFG.h"
  15. #include "llvm/IR/Dominators.h"
  16. #include <queue>
  17. using namespace llvm;
  18. void IDFCalculator::calculate(SmallVectorImpl<BasicBlock *> &PHIBlocks) {
  19. // If we haven't computed dominator tree levels, do so now.
  20. if (DomLevels.empty()) {
  21. for (auto DFI = df_begin(DT.getRootNode()), DFE = df_end(DT.getRootNode());
  22. DFI != DFE; ++DFI) {
  23. DomLevels[*DFI] = DFI.getPathLength() - 1;
  24. }
  25. }
  26. // Use a priority queue keyed on dominator tree level so that inserted nodes
  27. // are handled from the bottom of the dominator tree upwards.
  28. typedef std::pair<DomTreeNode *, unsigned> DomTreeNodePair;
  29. typedef std::priority_queue<DomTreeNodePair, SmallVector<DomTreeNodePair, 32>,
  30. less_second> IDFPriorityQueue;
  31. IDFPriorityQueue PQ;
  32. for (BasicBlock *BB : *DefBlocks) {
  33. if (DomTreeNode *Node = DT.getNode(BB))
  34. PQ.push(std::make_pair(Node, DomLevels.lookup(Node)));
  35. }
  36. SmallVector<DomTreeNode *, 32> Worklist;
  37. SmallPtrSet<DomTreeNode *, 32> VisitedPQ;
  38. SmallPtrSet<DomTreeNode *, 32> VisitedWorklist;
  39. while (!PQ.empty()) {
  40. DomTreeNodePair RootPair = PQ.top();
  41. PQ.pop();
  42. DomTreeNode *Root = RootPair.first;
  43. unsigned RootLevel = RootPair.second;
  44. // Walk all dominator tree children of Root, inspecting their CFG edges with
  45. // targets elsewhere on the dominator tree. Only targets whose level is at
  46. // most Root's level are added to the iterated dominance frontier of the
  47. // definition set.
  48. Worklist.clear();
  49. Worklist.push_back(Root);
  50. VisitedWorklist.insert(Root);
  51. while (!Worklist.empty()) {
  52. DomTreeNode *Node = Worklist.pop_back_val();
  53. BasicBlock *BB = Node->getBlock();
  54. for (auto Succ : successors(BB)) {
  55. DomTreeNode *SuccNode = DT.getNode(Succ);
  56. // Quickly skip all CFG edges that are also dominator tree edges instead
  57. // of catching them below.
  58. if (SuccNode->getIDom() == Node)
  59. continue;
  60. unsigned SuccLevel = DomLevels.lookup(SuccNode);
  61. if (SuccLevel > RootLevel)
  62. continue;
  63. if (!VisitedPQ.insert(SuccNode).second)
  64. continue;
  65. BasicBlock *SuccBB = SuccNode->getBlock();
  66. if (useLiveIn && !LiveInBlocks->count(SuccBB))
  67. continue;
  68. PHIBlocks.emplace_back(SuccBB);
  69. if (!DefBlocks->count(SuccBB))
  70. PQ.push(std::make_pair(SuccNode, SuccLevel));
  71. }
  72. for (auto DomChild : *Node) {
  73. if (VisitedWorklist.insert(DomChild).second)
  74. Worklist.push_back(DomChild);
  75. }
  76. }
  77. }
  78. }