CFGDiff.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //===- CFGDiff.h - Define a CFG snapshot. -----------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file defines specializations of GraphTraits that allows generic
  10. // algorithms to see a different snapshot of a CFG.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_SUPPORT_CFGDIFF_H
  14. #define LLVM_SUPPORT_CFGDIFF_H
  15. #include "llvm/ADT/GraphTraits.h"
  16. #include "llvm/ADT/iterator.h"
  17. #include "llvm/ADT/iterator_range.h"
  18. #include "llvm/Support/CFGUpdate.h"
  19. #include "llvm/Support/type_traits.h"
  20. #include <cassert>
  21. #include <cstddef>
  22. #include <iterator>
  23. // Two booleans are used to define orders in graphs:
  24. // InverseGraph defines when we need to reverse the whole graph and is as such
  25. // also equivalent to applying updates in reverse.
  26. // InverseEdge defines whether we want to change the edges direction. E.g., for
  27. // a non-inversed graph, the children are naturally the successors when
  28. // InverseEdge is false and the predecessors when InverseEdge is true.
  29. namespace llvm {
  30. namespace detail {
  31. template <typename Range>
  32. auto reverse_if_helper(Range &&R, std::integral_constant<bool, false>) {
  33. return std::forward<Range>(R);
  34. }
  35. template <typename Range>
  36. auto reverse_if_helper(Range &&R, std::integral_constant<bool, true>) {
  37. return llvm::reverse(std::forward<Range>(R));
  38. }
  39. template <bool B, typename Range> auto reverse_if(Range &&R) {
  40. return reverse_if_helper(std::forward<Range>(R),
  41. std::integral_constant<bool, B>{});
  42. }
  43. } // namespace detail
  44. // GraphDiff defines a CFG snapshot: given a set of Update<NodePtr>, provides
  45. // a getChildren method to get a Node's children based on the additional updates
  46. // in the snapshot. The current diff treats the CFG as a graph rather than a
  47. // multigraph. Added edges are pruned to be unique, and deleted edges will
  48. // remove all existing edges between two blocks.
  49. template <typename NodePtr, bool InverseGraph = false> class GraphDiff {
  50. struct DeletesInserts {
  51. SmallVector<NodePtr, 2> DI[2];
  52. };
  53. using UpdateMapType = SmallDenseMap<NodePtr, DeletesInserts>;
  54. UpdateMapType Succ;
  55. UpdateMapType Pred;
  56. // By default, it is assumed that, given a CFG and a set of updates, we wish
  57. // to apply these updates as given. If UpdatedAreReverseApplied is set, the
  58. // updates will be applied in reverse: deleted edges are considered re-added
  59. // and inserted edges are considered deleted when returning children.
  60. bool UpdatedAreReverseApplied;
  61. // Keep the list of legalized updates for a deterministic order of updates
  62. // when using a GraphDiff for incremental updates in the DominatorTree.
  63. // The list is kept in reverse to allow popping from end.
  64. SmallVector<cfg::Update<NodePtr>, 4> LegalizedUpdates;
  65. void printMap(raw_ostream &OS, const UpdateMapType &M) const {
  66. StringRef DIText[2] = {"Delete", "Insert"};
  67. for (auto Pair : M) {
  68. for (unsigned IsInsert = 0; IsInsert <= 1; ++IsInsert) {
  69. OS << DIText[IsInsert] << " edges: \n";
  70. for (auto Child : Pair.second.DI[IsInsert]) {
  71. OS << "(";
  72. Pair.first->printAsOperand(OS, false);
  73. OS << ", ";
  74. Child->printAsOperand(OS, false);
  75. OS << ") ";
  76. }
  77. }
  78. }
  79. OS << "\n";
  80. }
  81. public:
  82. GraphDiff() : UpdatedAreReverseApplied(false) {}
  83. GraphDiff(ArrayRef<cfg::Update<NodePtr>> Updates,
  84. bool ReverseApplyUpdates = false) {
  85. cfg::LegalizeUpdates<NodePtr>(Updates, LegalizedUpdates, InverseGraph);
  86. for (auto U : LegalizedUpdates) {
  87. unsigned IsInsert =
  88. (U.getKind() == cfg::UpdateKind::Insert) == !ReverseApplyUpdates;
  89. Succ[U.getFrom()].DI[IsInsert].push_back(U.getTo());
  90. Pred[U.getTo()].DI[IsInsert].push_back(U.getFrom());
  91. }
  92. UpdatedAreReverseApplied = ReverseApplyUpdates;
  93. }
  94. auto getLegalizedUpdates() const {
  95. return make_range(LegalizedUpdates.begin(), LegalizedUpdates.end());
  96. }
  97. unsigned getNumLegalizedUpdates() const { return LegalizedUpdates.size(); }
  98. cfg::Update<NodePtr> popUpdateForIncrementalUpdates() {
  99. assert(!LegalizedUpdates.empty() && "No updates to apply!");
  100. auto U = LegalizedUpdates.pop_back_val();
  101. unsigned IsInsert =
  102. (U.getKind() == cfg::UpdateKind::Insert) == !UpdatedAreReverseApplied;
  103. auto &SuccDIList = Succ[U.getFrom()];
  104. auto &SuccList = SuccDIList.DI[IsInsert];
  105. assert(SuccList.back() == U.getTo());
  106. SuccList.pop_back();
  107. if (SuccList.empty() && SuccDIList.DI[!IsInsert].empty())
  108. Succ.erase(U.getFrom());
  109. auto &PredDIList = Pred[U.getTo()];
  110. auto &PredList = PredDIList.DI[IsInsert];
  111. assert(PredList.back() == U.getFrom());
  112. PredList.pop_back();
  113. if (PredList.empty() && PredDIList.DI[!IsInsert].empty())
  114. Pred.erase(U.getTo());
  115. return U;
  116. }
  117. using VectRet = SmallVector<NodePtr, 8>;
  118. template <bool InverseEdge> VectRet getChildren(NodePtr N) const {
  119. using DirectedNodeT =
  120. std::conditional_t<InverseEdge, Inverse<NodePtr>, NodePtr>;
  121. auto R = children<DirectedNodeT>(N);
  122. VectRet Res = VectRet(detail::reverse_if<!InverseEdge>(R));
  123. // Remove nullptr children for clang.
  124. llvm::erase_value(Res, nullptr);
  125. auto &Children = (InverseEdge != InverseGraph) ? Pred : Succ;
  126. auto It = Children.find(N);
  127. if (It == Children.end())
  128. return Res;
  129. // Remove children present in the CFG but not in the snapshot.
  130. for (auto *Child : It->second.DI[0])
  131. llvm::erase_value(Res, Child);
  132. // Add children present in the snapshot for not in the real CFG.
  133. auto &AddedChildren = It->second.DI[1];
  134. llvm::append_range(Res, AddedChildren);
  135. return Res;
  136. }
  137. void print(raw_ostream &OS) const {
  138. OS << "===== GraphDiff: CFG edge changes to create a CFG snapshot. \n"
  139. "===== (Note: notion of children/inverse_children depends on "
  140. "the direction of edges and the graph.)\n";
  141. OS << "Children to delete/insert:\n\t";
  142. printMap(OS, Succ);
  143. OS << "Inverse_children to delete/insert:\n\t";
  144. printMap(OS, Pred);
  145. OS << "\n";
  146. }
  147. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  148. LLVM_DUMP_METHOD void dump() const { print(dbgs()); }
  149. #endif
  150. };
  151. } // end namespace llvm
  152. #endif // LLVM_SUPPORT_CFGDIFF_H