PhiValues.h 5.4 KB

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