dominator_analysis.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright (c) 2017 Google Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef SOURCE_OPT_DOMINATOR_ANALYSIS_H_
  15. #define SOURCE_OPT_DOMINATOR_ANALYSIS_H_
  16. #include <cstdint>
  17. #include <map>
  18. #include "source/opt/dominator_tree.h"
  19. namespace spvtools {
  20. namespace opt {
  21. // Interface to perform dominator or postdominator analysis on a given function.
  22. class DominatorAnalysisBase {
  23. public:
  24. explicit DominatorAnalysisBase(bool is_post_dom) : tree_(is_post_dom) {}
  25. // Calculates the dominator (or postdominator) tree for given function |f|.
  26. inline void InitializeTree(const CFG& cfg, const Function* f) {
  27. tree_.InitializeTree(cfg, f);
  28. }
  29. // Returns true if BasicBlock |a| dominates BasicBlock |b|.
  30. inline bool Dominates(const BasicBlock* a, const BasicBlock* b) const {
  31. if (!a || !b) return false;
  32. return Dominates(a->id(), b->id());
  33. }
  34. // Returns true if BasicBlock |a| dominates BasicBlock |b|. Same as above only
  35. // using the BasicBlock IDs.
  36. inline bool Dominates(uint32_t a, uint32_t b) const {
  37. return tree_.Dominates(a, b);
  38. }
  39. // Returns true if instruction |a| dominates instruction |b|.
  40. bool Dominates(Instruction* a, Instruction* b) const;
  41. // Returns true if BasicBlock |a| strictly dominates BasicBlock |b|.
  42. inline bool StrictlyDominates(const BasicBlock* a,
  43. const BasicBlock* b) const {
  44. if (!a || !b) return false;
  45. return StrictlyDominates(a->id(), b->id());
  46. }
  47. // Returns true if BasicBlock |a| strictly dominates BasicBlock |b|. Same as
  48. // above only using the BasicBlock IDs.
  49. inline bool StrictlyDominates(uint32_t a, uint32_t b) const {
  50. return tree_.StrictlyDominates(a, b);
  51. }
  52. // Returns the immediate dominator of |node| or returns nullptr if it is has
  53. // no dominator.
  54. inline BasicBlock* ImmediateDominator(const BasicBlock* node) const {
  55. if (!node) return nullptr;
  56. return tree_.ImmediateDominator(node);
  57. }
  58. // Returns the immediate dominator of |node_id| or returns nullptr if it is
  59. // has no dominator. Same as above but operates on IDs.
  60. inline BasicBlock* ImmediateDominator(uint32_t node_id) const {
  61. return tree_.ImmediateDominator(node_id);
  62. }
  63. // Returns true if |node| is reachable from the entry.
  64. inline bool IsReachable(const BasicBlock* node) const {
  65. if (!node) return false;
  66. return tree_.ReachableFromRoots(node->id());
  67. }
  68. // Returns true if |node_id| is reachable from the entry.
  69. inline bool IsReachable(uint32_t node_id) const {
  70. return tree_.ReachableFromRoots(node_id);
  71. }
  72. // Dump the tree structure into the given |out| stream in the dot format.
  73. inline void DumpAsDot(std::ostream& out) const { tree_.DumpTreeAsDot(out); }
  74. // Returns true if this is a postdomiator tree.
  75. inline bool IsPostDominator() const { return tree_.IsPostDominator(); }
  76. // Returns the tree itself for manual operations, such as traversing the
  77. // roots.
  78. // For normal dominance relationships the methods above should be used.
  79. inline DominatorTree& GetDomTree() { return tree_; }
  80. inline const DominatorTree& GetDomTree() const { return tree_; }
  81. // Force the dominator tree to be removed
  82. inline void ClearTree() { tree_.ClearTree(); }
  83. // Applies the std::function |func| to dominator tree nodes in dominator
  84. // order.
  85. void Visit(std::function<bool(DominatorTreeNode*)> func) {
  86. tree_.Visit(func);
  87. }
  88. // Applies the std::function |func| to dominator tree nodes in dominator
  89. // order.
  90. void Visit(std::function<bool(const DominatorTreeNode*)> func) const {
  91. tree_.Visit(func);
  92. }
  93. // Returns the most immediate basic block that dominates both |b1| and |b2|.
  94. // If there is no such basic block, nullptr is returned.
  95. BasicBlock* CommonDominator(BasicBlock* b1, BasicBlock* b2) const;
  96. protected:
  97. DominatorTree tree_;
  98. };
  99. // Derived class for normal dominator analysis.
  100. class DominatorAnalysis : public DominatorAnalysisBase {
  101. public:
  102. DominatorAnalysis() : DominatorAnalysisBase(false) {}
  103. };
  104. // Derived class for postdominator analysis.
  105. class PostDominatorAnalysis : public DominatorAnalysisBase {
  106. public:
  107. PostDominatorAnalysis() : DominatorAnalysisBase(true) {}
  108. };
  109. } // namespace opt
  110. } // namespace spvtools
  111. #endif // SOURCE_OPT_DOMINATOR_ANALYSIS_H_