control_dependence.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Copyright (c) 2021 Google LLC.
  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. #include "source/opt/control_dependence.h"
  15. #include <cassert>
  16. #include <tuple>
  17. #include <utility>
  18. #include <vector>
  19. #include "source/opt/basic_block.h"
  20. #include "source/opt/cfg.h"
  21. #include "source/opt/dominator_analysis.h"
  22. #include "source/opt/function.h"
  23. #include "source/opt/instruction.h"
  24. // Computes the control dependence graph (CDG) using the algorithm in Cytron
  25. // 1991, "Efficiently Computing Static Single Assignment Form and the Control
  26. // Dependence Graph." It relies on the fact that the control dependence sources
  27. // (blocks on which a block is control dependent) are exactly the post-dominance
  28. // frontier for that block. The explanation and proofs are given in Section 6 of
  29. // that paper.
  30. // Link: https://www.cs.utexas.edu/~pingali/CS380C/2010/papers/ssaCytron.pdf
  31. //
  32. // The algorithm in Section 4.2 of the same paper is used to construct the
  33. // dominance frontier. It uses the post-dominance tree, which is available in
  34. // the IR context.
  35. namespace spvtools {
  36. namespace opt {
  37. constexpr uint32_t ControlDependenceAnalysis::kPseudoEntryBlock;
  38. uint32_t ControlDependence::GetConditionID(const CFG& cfg) const {
  39. if (source_bb_id() == 0) {
  40. // Entry dependence; return 0.
  41. return 0;
  42. }
  43. const BasicBlock* source_bb = cfg.block(source_bb_id());
  44. const Instruction* branch = source_bb->terminator();
  45. assert((branch->opcode() == spv::Op::OpBranchConditional ||
  46. branch->opcode() == spv::Op::OpSwitch) &&
  47. "invalid control dependence; last instruction must be conditional "
  48. "branch or switch");
  49. return branch->GetSingleWordInOperand(0);
  50. }
  51. bool ControlDependence::operator<(const ControlDependence& other) const {
  52. return std::tie(source_bb_id_, target_bb_id_, branch_target_bb_id_) <
  53. std::tie(other.source_bb_id_, other.target_bb_id_,
  54. other.branch_target_bb_id_);
  55. }
  56. bool ControlDependence::operator==(const ControlDependence& other) const {
  57. return std::tie(source_bb_id_, target_bb_id_, branch_target_bb_id_) ==
  58. std::tie(other.source_bb_id_, other.target_bb_id_,
  59. other.branch_target_bb_id_);
  60. }
  61. std::ostream& operator<<(std::ostream& os, const ControlDependence& dep) {
  62. os << dep.source_bb_id() << "->" << dep.target_bb_id();
  63. if (dep.branch_target_bb_id() != dep.target_bb_id()) {
  64. os << " through " << dep.branch_target_bb_id();
  65. }
  66. return os;
  67. }
  68. void ControlDependenceAnalysis::ComputePostDominanceFrontiers(
  69. const CFG& cfg, const PostDominatorAnalysis& pdom) {
  70. // Compute post-dominance frontiers (reverse graph).
  71. // The dominance frontier for a block X is equal to (Equation 4)
  72. // DF_local(X) U { B in DF_up(Z) | X = ipdom(Z) }
  73. // (ipdom(Z) is the immediate post-dominator of Z.)
  74. // where
  75. // DF_local(X) = { Y | X -> Y in CFG, X does not strictly post-dominate Y }
  76. // represents the contribution of X's predecessors to the DF, and
  77. // DF_up(Z) = { Y | Y in DF(Z), ipdom(Z) does not strictly post-dominate Y }
  78. // (note: ipdom(Z) = X.)
  79. // represents the contribution of a block to its immediate post-
  80. // dominator's DF.
  81. // This is computed in one pass through a post-order traversal of the
  82. // post-dominator tree.
  83. // Assert that there is a block other than the pseudo exit in the pdom tree,
  84. // as we need one to get the function entry point (as the pseudo exit is not
  85. // actually part of the function.)
  86. assert(!cfg.IsPseudoExitBlock(pdom.GetDomTree().post_begin()->bb_));
  87. Function* function = pdom.GetDomTree().post_begin()->bb_->GetParent();
  88. uint32_t function_entry = function->entry()->id();
  89. // Explicitly initialize pseudo-entry block, as it doesn't depend on anything,
  90. // so it won't be initialized in the following loop.
  91. reverse_nodes_[kPseudoEntryBlock] = {};
  92. for (auto it = pdom.GetDomTree().post_cbegin();
  93. it != pdom.GetDomTree().post_cend(); ++it) {
  94. ComputePostDominanceFrontierForNode(cfg, pdom, function_entry, *it);
  95. }
  96. }
  97. void ControlDependenceAnalysis::ComputePostDominanceFrontierForNode(
  98. const CFG& cfg, const PostDominatorAnalysis& pdom, uint32_t function_entry,
  99. const DominatorTreeNode& pdom_node) {
  100. const uint32_t label = pdom_node.id();
  101. ControlDependenceList& edges = reverse_nodes_[label];
  102. for (uint32_t pred : cfg.preds(label)) {
  103. if (!pdom.StrictlyDominates(label, pred)) {
  104. edges.push_back(ControlDependence(pred, label));
  105. }
  106. }
  107. if (label == function_entry) {
  108. // Add edge from pseudo-entry to entry.
  109. // In CDG construction, an edge is added from entry to exit, so only the
  110. // exit node can post-dominate entry.
  111. edges.push_back(ControlDependence(kPseudoEntryBlock, label));
  112. }
  113. for (DominatorTreeNode* child : pdom_node) {
  114. // Note: iterate dependences by value, as we need a copy.
  115. for (const ControlDependence& dep : reverse_nodes_[child->id()]) {
  116. // Special-case pseudo-entry, as above.
  117. if (dep.source_bb_id() == kPseudoEntryBlock ||
  118. !pdom.StrictlyDominates(label, dep.source_bb_id())) {
  119. edges.push_back(ControlDependence(dep.source_bb_id(), label,
  120. dep.branch_target_bb_id()));
  121. }
  122. }
  123. }
  124. }
  125. void ControlDependenceAnalysis::ComputeControlDependenceGraph(
  126. const CFG& cfg, const PostDominatorAnalysis& pdom) {
  127. ComputePostDominanceFrontiers(cfg, pdom);
  128. ComputeForwardGraphFromReverse();
  129. }
  130. void ControlDependenceAnalysis::ComputeForwardGraphFromReverse() {
  131. for (const auto& entry : reverse_nodes_) {
  132. // Ensure an entry is created for each node.
  133. forward_nodes_[entry.first];
  134. for (const ControlDependence& dep : entry.second) {
  135. forward_nodes_[dep.source_bb_id()].push_back(dep);
  136. }
  137. }
  138. }
  139. } // namespace opt
  140. } // namespace spvtools