control_dependence.cpp 6.1 KB

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