PhiValues.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //===- PhiValues.h - Phi Value Analysis -------------------------*- 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 the PhiValues class, and associated passes, which can be
  10. // used to find the underlying values of the phis in a function, i.e. the
  11. // non-phi values that can be found by traversing the phi graph.
  12. //
  13. // This information is computed lazily and cached. If new phis are added to the
  14. // function they are handled correctly, but if an existing phi has its operands
  15. // modified PhiValues has to be notified by calling invalidateValue.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_ANALYSIS_PHIVALUES_H
  19. #define LLVM_ANALYSIS_PHIVALUES_H
  20. #include "llvm/ADT/DenseMap.h"
  21. #include "llvm/ADT/DenseSet.h"
  22. #include "llvm/ADT/SetVector.h"
  23. #include "llvm/ADT/SmallVector.h"
  24. #include "llvm/IR/PassManager.h"
  25. #include "llvm/IR/ValueHandle.h"
  26. #include "llvm/Pass.h"
  27. namespace llvm {
  28. class Value;
  29. class PHINode;
  30. class Function;
  31. /// Class for calculating and caching the underlying values of phis in a
  32. /// function.
  33. ///
  34. /// Initially the PhiValues is empty, and gets incrementally populated whenever
  35. /// it is queried.
  36. class PhiValues {
  37. public:
  38. using ValueSet = SmallSetVector<Value *, 4>;
  39. /// Construct an empty PhiValues.
  40. PhiValues(const Function &F) : F(F) {}
  41. /// Get the underlying values of a phi.
  42. ///
  43. /// This returns the cached value if PN has previously been processed,
  44. /// otherwise it processes it first.
  45. const ValueSet &getValuesForPhi(const PHINode *PN);
  46. /// Notify PhiValues that the cached information using V is no longer valid
  47. ///
  48. /// Whenever a phi has its operands modified the cached values for that phi
  49. /// (and the phis that use that phi) become invalid. A user of PhiValues has
  50. /// to notify it of this by calling invalidateValue on either the operand or
  51. /// the phi, which will then clear the relevant cached information.
  52. void invalidateValue(const Value *V);
  53. /// Free the memory used by this class.
  54. void releaseMemory();
  55. /// Print out the values currently in the cache.
  56. void print(raw_ostream &OS) const;
  57. /// Handle invalidation events in the new pass manager.
  58. bool invalidate(Function &, const PreservedAnalyses &,
  59. FunctionAnalysisManager::Invalidator &);
  60. private:
  61. using ConstValueSet = SmallSetVector<const Value *, 4>;
  62. /// The next depth number to be used by processPhi.
  63. unsigned int NextDepthNumber = 1;
  64. /// Depth numbers of phis. Phis with the same depth number are part of the
  65. /// same strongly connected component.
  66. DenseMap<const PHINode *, unsigned int> DepthMap;
  67. /// Non-phi values reachable from each component.
  68. DenseMap<unsigned int, ValueSet> NonPhiReachableMap;
  69. /// All values reachable from each component.
  70. DenseMap<unsigned int, ConstValueSet> ReachableMap;
  71. /// A CallbackVH to notify PhiValues when a value is deleted or replaced, so
  72. /// that the cached information for that value can be cleared to avoid
  73. /// dangling pointers to invalid values.
  74. class PhiValuesCallbackVH final : public CallbackVH {
  75. PhiValues *PV;
  76. void deleted() override;
  77. void allUsesReplacedWith(Value *New) override;
  78. public:
  79. PhiValuesCallbackVH(Value *V, PhiValues *PV = nullptr)
  80. : CallbackVH(V), PV(PV) {}
  81. };
  82. /// A set of callbacks to the values that processPhi has seen.
  83. DenseSet<PhiValuesCallbackVH, DenseMapInfo<Value *>> TrackedValues;
  84. /// The function that the PhiValues is for.
  85. const Function &F;
  86. /// Process a phi so that its entries in the depth and reachable maps are
  87. /// fully populated.
  88. void processPhi(const PHINode *PN, SmallVectorImpl<const PHINode *> &Stack);
  89. };
  90. /// The analysis pass which yields a PhiValues
  91. ///
  92. /// The analysis does nothing by itself, and just returns an empty PhiValues
  93. /// which will get filled in as it's used.
  94. class PhiValuesAnalysis : public AnalysisInfoMixin<PhiValuesAnalysis> {
  95. friend AnalysisInfoMixin<PhiValuesAnalysis>;
  96. static AnalysisKey Key;
  97. public:
  98. using Result = PhiValues;
  99. PhiValues run(Function &F, FunctionAnalysisManager &);
  100. };
  101. /// A pass for printing the PhiValues for a function.
  102. ///
  103. /// This pass doesn't print whatever information the PhiValues happens to hold,
  104. /// but instead first uses the PhiValues to analyze all the phis in the function
  105. /// so the complete information is printed.
  106. class PhiValuesPrinterPass : public PassInfoMixin<PhiValuesPrinterPass> {
  107. raw_ostream &OS;
  108. public:
  109. explicit PhiValuesPrinterPass(raw_ostream &OS) : OS(OS) {}
  110. PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
  111. };
  112. /// Wrapper pass for the legacy pass manager
  113. class PhiValuesWrapperPass : public FunctionPass {
  114. std::unique_ptr<PhiValues> Result;
  115. public:
  116. static char ID;
  117. PhiValuesWrapperPass();
  118. PhiValues &getResult() { return *Result; }
  119. const PhiValues &getResult() const { return *Result; }
  120. bool runOnFunction(Function &F) override;
  121. void releaseMemory() override;
  122. void getAnalysisUsage(AnalysisUsage &AU) const override;
  123. };
  124. } // namespace llvm
  125. #endif