LLVM  15.0.0git
PhiValues.h
Go to the documentation of this file.
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 
19 #ifndef LLVM_ANALYSIS_PHIVALUES_H
20 #define LLVM_ANALYSIS_PHIVALUES_H
21 
22 #include "llvm/ADT/DenseMap.h"
23 #include "llvm/ADT/DenseSet.h"
24 #include "llvm/ADT/SetVector.h"
25 #include "llvm/IR/PassManager.h"
26 #include "llvm/IR/ValueHandle.h"
27 #include "llvm/Pass.h"
28 
29 namespace llvm {
30 
31 class Value;
32 class PHINode;
33 class Function;
34 
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:
43 
44  /// Construct an empty PhiValues.
45  PhiValues(const Function &F) : F(F) {}
46 
47  /// Get the underlying values of a phi.
48  ///
49  /// This returns the cached value if PN has previously been processed,
50  /// otherwise it processes it first.
51  const ValueSet &getValuesForPhi(const PHINode *PN);
52 
53  /// Notify PhiValues that the cached information using V is no longer valid
54  ///
55  /// Whenever a phi has its operands modified the cached values for that phi
56  /// (and the phis that use that phi) become invalid. A user of PhiValues has
57  /// to notify it of this by calling invalidateValue on either the operand or
58  /// the phi, which will then clear the relevant cached information.
59  void invalidateValue(const Value *V);
60 
61  /// Free the memory used by this class.
62  void releaseMemory();
63 
64  /// Print out the values currently in the cache.
65  void print(raw_ostream &OS) const;
66 
67  /// Handle invalidation events in the new pass manager.
68  bool invalidate(Function &, const PreservedAnalyses &,
70 
71 private:
72  using ConstValueSet = SmallSetVector<const Value *, 4>;
73 
74  /// The next depth number to be used by processPhi.
75  unsigned int NextDepthNumber = 1;
76 
77  /// Depth numbers of phis. Phis with the same depth number are part of the
78  /// same strongly connected component.
80 
81  /// Non-phi values reachable from each component.
82  DenseMap<unsigned int, ValueSet> NonPhiReachableMap;
83 
84  /// All values reachable from each component.
86 
87  /// A CallbackVH to notify PhiValues when a value is deleted or replaced, so
88  /// that the cached information for that value can be cleared to avoid
89  /// dangling pointers to invalid values.
90  class PhiValuesCallbackVH final : public CallbackVH {
91  PhiValues *PV;
92  void deleted() override;
93  void allUsesReplacedWith(Value *New) override;
94 
95  public:
96  PhiValuesCallbackVH(Value *V, PhiValues *PV = nullptr)
97  : CallbackVH(V), PV(PV) {}
98  };
99 
100  /// A set of callbacks to the values that processPhi has seen.
101  DenseSet<PhiValuesCallbackVH, DenseMapInfo<Value *>> TrackedValues;
102 
103  /// The function that the PhiValues is for.
104  const Function &F;
105 
106  /// Process a phi so that its entries in the depth and reachable maps are
107  /// fully populated.
108  void processPhi(const PHINode *PN, SmallVectorImpl<const PHINode *> &Stack);
109 };
110 
111 /// The analysis pass which yields a PhiValues
112 ///
113 /// The analysis does nothing by itself, and just returns an empty PhiValues
114 /// which will get filled in as it's used.
115 class PhiValuesAnalysis : public AnalysisInfoMixin<PhiValuesAnalysis> {
117  static AnalysisKey Key;
118 
119 public:
120  using Result = PhiValues;
122 };
123 
124 /// A pass for printing the PhiValues for a function.
125 ///
126 /// This pass doesn't print whatever information the PhiValues happens to hold,
127 /// but instead first uses the PhiValues to analyze all the phis in the function
128 /// so the complete information is printed.
129 class PhiValuesPrinterPass : public PassInfoMixin<PhiValuesPrinterPass> {
130  raw_ostream &OS;
131 
132 public:
133  explicit PhiValuesPrinterPass(raw_ostream &OS) : OS(OS) {}
135 };
136 
137 /// Wrapper pass for the legacy pass manager
139  std::unique_ptr<PhiValues> Result;
140 
141 public:
142  static char ID;
144 
145  PhiValues &getResult() { return *Result; }
146  const PhiValues &getResult() const { return *Result; }
147 
148  bool runOnFunction(Function &F) override;
149  void releaseMemory() override;
150  void getAnalysisUsage(AnalysisUsage &AU) const override;
151 };
152 
153 } // namespace llvm
154 
155 #endif
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:152
llvm::PhiValues::invalidate
bool invalidate(Function &, const PreservedAnalyses &, FunctionAnalysisManager::Invalidator &)
Handle invalidation events in the new pass manager.
Definition: PhiValues.cpp:26
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::PhiValuesWrapperPass::getResult
const PhiValues & getResult() const
Definition: PhiValues.h:146
llvm::PhiValuesWrapperPass::getResult
PhiValues & getResult()
Definition: PhiValues.h:145
llvm::PhiValuesPrinterPass::run
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition: PhiValues.cpp:195
llvm::PhiValuesPrinterPass::PhiValuesPrinterPass
PhiValuesPrinterPass(raw_ostream &OS)
Definition: PhiValues.h:133
llvm::PassInfoMixin
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:371
llvm::Function
Definition: Function.h:60
llvm::PhiValues::ValueSet
SmallSetVector< Value *, 4 > ValueSet
Definition: PhiValues.h:42
Pass.h
llvm::PhiValues::print
void print(raw_ostream &OS) const
Print out the values currently in the cache.
Definition: PhiValues.cpp:163
llvm::PhiValuesWrapperPass::getAnalysisUsage
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: PhiValues.cpp:219
llvm::PhiValuesWrapperPass
Wrapper pass for the legacy pass manager.
Definition: PhiValues.h:138
DenseMap.h
llvm::PhiValues::PhiValues
PhiValues(const Function &F)
Construct an empty PhiValues.
Definition: PhiValues.h:45
F
#define F(x, y, z)
Definition: MD5.cpp:55
llvm::PhiValuesAnalysis::run
PhiValues run(Function &F, FunctionAnalysisManager &)
Definition: PhiValues.cpp:191
llvm::PhiValuesWrapperPass::runOnFunction
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
Definition: PhiValues.cpp:210
llvm::PhiValuesWrapperPass::releaseMemory
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
Definition: PhiValues.cpp:215
llvm::PhiValues::releaseMemory
void releaseMemory()
Free the memory used by this class.
Definition: PhiValues.cpp:157
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition: PassAnalysisSupport.h:47
DenseSet.h
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:54
llvm::AnalysisManager::Invalidator
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:667
llvm::PhiValues::getValuesForPhi
const ValueSet & getValuesForPhi(const PHINode *PN)
Get the underlying values of a phi.
Definition: PhiValues.cpp:125
llvm::PhiValuesPrinterPass
A pass for printing the PhiValues for a function.
Definition: PhiValues.h:129
llvm::DenseMap
Definition: DenseMap.h:716
llvm::AnalysisKey
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: PassManager.h:69
llvm::PhiValuesAnalysis
The analysis pass which yields a PhiValues.
Definition: PhiValues.h:115
llvm::PhiValuesWrapperPass::ID
static char ID
Definition: PhiValues.h:142
llvm::AnalysisInfoMixin
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition: PassManager.h:394
ValueHandle.h
llvm::PhiValues::invalidateValue
void invalidateValue(const Value *V)
Notify PhiValues that the cached information using V is no longer valid.
Definition: PhiValues.cpp:137
llvm::TargetStackID::Value
Value
Definition: TargetFrameLowering.h:27
PassManager.h
llvm::CallbackVH
Value handle with callbacks on RAUW and destruction.
Definition: ValueHandle.h:383
llvm::PhiValues
Class for calculating and caching the underlying values of phis in a function.
Definition: PhiValues.h:40
llvm::PHINode
Definition: Instructions.h:2651
llvm::SmallSetVector
A SetVector that performs no allocations if smaller than a certain size.
Definition: SetVector.h:307
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:42
llvm::FunctionPass
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:308
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
llvm::codeview::PublicSymFlags::Function
@ Function
llvm::PhiValuesWrapperPass::PhiValuesWrapperPass
PhiValuesWrapperPass()
Definition: PhiValues.cpp:206
SetVector.h