ControlDependence.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // ControlDependence.cpp //
  4. // Copyright (C) Microsoft Corporation. All rights reserved. //
  5. // This file is distributed under the University of Illinois Open Source //
  6. // License. See LICENSE.TXT for details. //
  7. // //
  8. // Control dependence is computed using algorithm in Figure 7.9 from [AK]. //
  9. // //
  10. // References //
  11. // [AK] Optimizing Compilers for Modern Architectures by Allen and Kennedy. //
  12. ///////////////////////////////////////////////////////////////////////////////
  13. #include "dxc/HLSL/ControlDependence.h"
  14. #include "dxc/Support/Global.h"
  15. #include "llvm/Support/Debug.h"
  16. using namespace llvm;
  17. using namespace hlsl;
  18. const BasicBlockSet &ControlDependence::GetCDBlocks(BasicBlock *pBB) const {
  19. auto it = m_ControlDependence.find(pBB);
  20. if (it != m_ControlDependence.end())
  21. return it->second;
  22. else
  23. return m_EmptyBBSet;
  24. }
  25. void ControlDependence::print(raw_ostream &OS) {
  26. OS << "Control dependence for function '" << m_pFunc->getName() << "'\n";
  27. for (auto &it : m_ControlDependence) {
  28. BasicBlock *pBB = it.first;
  29. OS << "Block " << pBB->getName() << ": { ";
  30. bool bFirst = true;
  31. for (BasicBlock *pBB2 : it.second) {
  32. if (!bFirst) OS << ", ";
  33. OS << pBB2->getName();
  34. bFirst = false;
  35. }
  36. OS << " }\n";
  37. }
  38. OS << "\n";
  39. }
  40. void ControlDependence::dump() {
  41. print(dbgs());
  42. }
  43. void ControlDependence::Compute(Function *F, PostDomRelationType &PostDomRel) {
  44. m_pFunc = F;
  45. // Compute reverse topological order of PDT.
  46. BasicBlockVector RevTopOrder;
  47. BasicBlockSet VisitedBBs;
  48. for (BasicBlock *pBB : PostDomRel.getRoots()) {
  49. ComputeRevTopOrderRec(PostDomRel, pBB, RevTopOrder, VisitedBBs);
  50. }
  51. DXASSERT_NOMSG(RevTopOrder.size() == VisitedBBs.size());
  52. // Compute control dependence relation.
  53. for (size_t iBB = 0; iBB < RevTopOrder.size(); iBB++) {
  54. BasicBlock *x = RevTopOrder[iBB];
  55. // For each y = pred(x): if ipostdom(y) != x then add "x is control dependent on y"
  56. for (auto itPred = pred_begin(x), endPred = pred_end(x); itPred != endPred; ++itPred) {
  57. BasicBlock *y = *itPred; // predecessor of x
  58. BasicBlock *pPredIDomBB = GetIPostDom(PostDomRel, y);
  59. if (pPredIDomBB != x) {
  60. m_ControlDependence[x].insert(y);
  61. }
  62. }
  63. // For all z such that ipostdom(z) = x
  64. for (DomTreeNode *child : PostDomRel.getNode(x)->getChildren()) {
  65. BasicBlock *z = child->getBlock();
  66. auto it = m_ControlDependence.find(z);
  67. if (it == m_ControlDependence.end())
  68. continue;
  69. // For all y in CDG(z)
  70. for (BasicBlock *y : it->second) {
  71. // if ipostdom(y) != x then add "x is control dependent on y"
  72. BasicBlock *pPredIDomBB = GetIPostDom(PostDomRel, y);
  73. if (pPredIDomBB != x) {
  74. m_ControlDependence[x].insert(y);
  75. }
  76. }
  77. }
  78. }
  79. }
  80. void ControlDependence::Clear() {
  81. m_pFunc = nullptr;
  82. m_ControlDependence.clear();
  83. m_EmptyBBSet.clear();
  84. }
  85. BasicBlock *ControlDependence::GetIPostDom(PostDomRelationType &PostDomRel, BasicBlock *pBB) {
  86. auto *pPDTNode = PostDomRel.getNode(pBB);
  87. auto *pIDomNode = pPDTNode->getIDom();
  88. BasicBlock *pIDomBB = pIDomNode != nullptr ? pIDomNode->getBlock() : nullptr;
  89. return pIDomBB;
  90. }
  91. void ControlDependence::ComputeRevTopOrderRec(PostDomRelationType &PostDomRel,
  92. BasicBlock *pBB,
  93. BasicBlockVector &RevTopOrder,
  94. BasicBlockSet &VisitedBBs) {
  95. if (VisitedBBs.find(pBB) != VisitedBBs.end()) {
  96. return;
  97. }
  98. VisitedBBs.insert(pBB);
  99. SmallVector<BasicBlock *, 8> Descendants;
  100. PostDomRel.getDescendants(pBB, Descendants);
  101. for (BasicBlock *pDescBB : Descendants) {
  102. if (pDescBB != pBB)
  103. ComputeRevTopOrderRec(PostDomRel, pDescBB, RevTopOrder, VisitedBBs);
  104. }
  105. RevTopOrder.emplace_back(pBB);
  106. }