PhiValues.h 5.3 KB

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