IteratedDominanceFrontier.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //===- IteratedDominanceFrontier.h - Calculate IDF --------------*- C++ -*-===//
  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. /// The algorithm used here is based on:
  13. ///
  14. /// Sreedhar and Gao. A linear time algorithm for placing phi-nodes.
  15. /// In Proceedings of the 22nd ACM SIGPLAN-SIGACT Symposium on Principles of
  16. /// Programming Languages
  17. /// POPL '95. ACM, New York, NY, 62-73.
  18. ///
  19. /// It has been modified to not explicitly use the DJ graph data structure and
  20. /// to directly compute pruned SSA using per-variable liveness information.
  21. //
  22. //===----------------------------------------------------------------------===//
  23. #ifndef LLVM_ANALYSIS_IDF_H
  24. #define LLVM_ANALYSIS_IDF_H
  25. #include "llvm/ADT/ArrayRef.h"
  26. #include "llvm/ADT/DenseMap.h"
  27. #include "llvm/ADT/SmallPtrSet.h"
  28. #include "llvm/ADT/SmallVector.h"
  29. namespace llvm {
  30. class BasicBlock;
  31. template <class T> class DomTreeNodeBase;
  32. typedef DomTreeNodeBase<BasicBlock> DomTreeNode;
  33. class DominatorTree;
  34. /// \brief Determine the iterated dominance frontier, given a set of defining
  35. /// blocks, and optionally, a set of live-in blocks.
  36. ///
  37. /// In turn, the results can be used to place phi nodes.
  38. ///
  39. /// This algorithm is a linear time computation of Iterated Dominance Frontiers,
  40. /// pruned using the live-in set.
  41. /// By default, liveness is not used to prune the IDF computation.
  42. class IDFCalculator {
  43. public:
  44. IDFCalculator(DominatorTree &DT) : DT(DT), useLiveIn(false) {}
  45. /// \brief Give the IDF calculator the set of blocks in which the value is
  46. /// defined. This is equivalent to the set of starting blocks it should be
  47. /// calculating the IDF for (though later gets pruned based on liveness).
  48. ///
  49. /// Note: This set *must* live for the entire lifetime of the IDF calculator.
  50. void setDefiningBlocks(const SmallPtrSetImpl<BasicBlock *> &Blocks) {
  51. DefBlocks = &Blocks;
  52. }
  53. /// \brief Give the IDF calculator the set of blocks in which the value is
  54. /// live on entry to the block. This is used to prune the IDF calculation to
  55. /// not include blocks where any phi insertion would be dead.
  56. ///
  57. /// Note: This set *must* live for the entire lifetime of the IDF calculator.
  58. void setLiveInBlocks(const SmallPtrSetImpl<BasicBlock *> &Blocks) {
  59. LiveInBlocks = &Blocks;
  60. useLiveIn = true;
  61. }
  62. /// \brief Reset the live-in block set to be empty, and tell the IDF
  63. /// calculator to not use liveness anymore.
  64. void resetLiveInBlocks() {
  65. LiveInBlocks = nullptr;
  66. useLiveIn = false;
  67. }
  68. /// \brief Calculate iterated dominance frontiers
  69. ///
  70. /// This uses the linear-time phi algorithm based on DJ-graphs mentioned in
  71. /// the file-level comment. It performs DF->IDF pruning using the live-in
  72. /// set, to avoid computing the IDF for blocks where an inserted PHI node
  73. /// would be dead.
  74. void calculate(SmallVectorImpl<BasicBlock *> &IDFBlocks);
  75. private:
  76. DominatorTree &DT;
  77. bool useLiveIn;
  78. DenseMap<DomTreeNode *, unsigned> DomLevels;
  79. const SmallPtrSetImpl<BasicBlock *> *LiveInBlocks;
  80. const SmallPtrSetImpl<BasicBlock *> *DefBlocks;
  81. SmallVector<BasicBlock *, 32> PHIBlocks;
  82. };
  83. }
  84. #endif