LLVM  10.0.0
PhiValues.cpp
Go to the documentation of this file.
1 //===- PhiValues.cpp - Phi Value Analysis ---------------------------------===//
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 
10 #include "llvm/ADT/SmallPtrSet.h"
11 #include "llvm/ADT/SmallVector.h"
12 #include "llvm/IR/Instructions.h"
13 #include "llvm/InitializePasses.h"
14 
15 using namespace llvm;
16 
17 void PhiValues::PhiValuesCallbackVH::deleted() {
18  PV->invalidateValue(getValPtr());
19 }
20 
21 void PhiValues::PhiValuesCallbackVH::allUsesReplacedWith(Value *) {
22  // We could potentially update the cached values we have with the new value,
23  // but it's simpler to just treat the old value as invalidated.
24  PV->invalidateValue(getValPtr());
25 }
26 
29  // PhiValues is invalidated if it isn't preserved.
30  auto PAC = PA.getChecker<PhiValuesAnalysis>();
31  return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>());
32 }
33 
34 // The goal here is to find all of the non-phi values reachable from this phi,
35 // and to do the same for all of the phis reachable from this phi, as doing so
36 // is necessary anyway in order to get the values for this phi. We do this using
37 // Tarjan's algorithm with Nuutila's improvements to find the strongly connected
38 // components of the phi graph rooted in this phi:
39 // * All phis in a strongly connected component will have the same reachable
40 // non-phi values. The SCC may not be the maximal subgraph for that set of
41 // reachable values, but finding out that isn't really necessary (it would
42 // only reduce the amount of memory needed to store the values).
43 // * Tarjan's algorithm completes components in a bottom-up manner, i.e. it
44 // never completes a component before the components reachable from it have
45 // been completed. This means that when we complete a component we have
46 // everything we need to collect the values reachable from that component.
47 // * We collect both the non-phi values reachable from each SCC, as that's what
48 // we're ultimately interested in, and all of the reachable values, i.e.
49 // including phis, as that makes invalidateValue easier.
50 void PhiValues::processPhi(const PHINode *Phi,
52  // Initialize the phi with the next depth number.
53  assert(DepthMap.lookup(Phi) == 0);
54  assert(NextDepthNumber != UINT_MAX);
55  unsigned int RootDepthNumber = ++NextDepthNumber;
56  DepthMap[Phi] = RootDepthNumber;
57 
58  // Recursively process the incoming phis of this phi.
59  TrackedValues.insert(PhiValuesCallbackVH(const_cast<PHINode *>(Phi), this));
60  for (Value *PhiOp : Phi->incoming_values()) {
61  if (PHINode *PhiPhiOp = dyn_cast<PHINode>(PhiOp)) {
62  // Recurse if the phi has not yet been visited.
63  unsigned int OpDepthNumber = DepthMap.lookup(PhiPhiOp);
64  if (OpDepthNumber == 0) {
65  processPhi(PhiPhiOp, Stack);
66  OpDepthNumber = DepthMap.lookup(PhiPhiOp);
67  assert(OpDepthNumber != 0);
68  }
69  // If the phi did not become part of a component then this phi and that
70  // phi are part of the same component, so adjust the depth number.
71  if (!ReachableMap.count(OpDepthNumber))
72  DepthMap[Phi] = std::min(DepthMap[Phi], OpDepthNumber);
73  } else {
74  TrackedValues.insert(PhiValuesCallbackVH(PhiOp, this));
75  }
76  }
77 
78  // Now that incoming phis have been handled, push this phi to the stack.
79  Stack.push_back(Phi);
80 
81  // If the depth number has not changed then we've finished collecting the phis
82  // of a strongly connected component.
83  if (DepthMap[Phi] == RootDepthNumber) {
84  // Collect the reachable values for this component. The phis of this
85  // component will be those on top of the depth stack with the same or
86  // greater depth number.
87  ConstValueSet &Reachable = ReachableMap[RootDepthNumber];
88  while (true) {
89  const PHINode *ComponentPhi = Stack.pop_back_val();
90  Reachable.insert(ComponentPhi);
91 
92  for (Value *Op : ComponentPhi->incoming_values()) {
93  if (PHINode *PhiOp = dyn_cast<PHINode>(Op)) {
94  // If this phi is not part of the same component then that component
95  // is guaranteed to have been completed before this one. Therefore we
96  // can just add its reachable values to the reachable values of this
97  // component.
98  unsigned int OpDepthNumber = DepthMap[PhiOp];
99  if (OpDepthNumber != RootDepthNumber) {
100  auto It = ReachableMap.find(OpDepthNumber);
101  if (It != ReachableMap.end())
102  Reachable.insert(It->second.begin(), It->second.end());
103  }
104  } else
105  Reachable.insert(Op);
106  }
107 
108  if (Stack.empty())
109  break;
110 
111  unsigned int &ComponentDepthNumber = DepthMap[Stack.back()];
112  if (ComponentDepthNumber < RootDepthNumber)
113  break;
114 
115  ComponentDepthNumber = RootDepthNumber;
116  }
117 
118  // Filter out phis to get the non-phi reachable values.
119  ValueSet &NonPhi = NonPhiReachableMap[RootDepthNumber];
120  for (const Value *V : Reachable)
121  if (!isa<PHINode>(V))
122  NonPhi.insert(const_cast<Value *>(V));
123  }
124 }
125 
127  unsigned int DepthNumber = DepthMap.lookup(PN);
128  if (DepthNumber == 0) {
130  processPhi(PN, Stack);
131  DepthNumber = DepthMap.lookup(PN);
132  assert(Stack.empty());
133  assert(DepthNumber != 0);
134  }
135  return NonPhiReachableMap[DepthNumber];
136 }
137 
139  // Components that can reach V are invalid.
140  SmallVector<unsigned int, 8> InvalidComponents;
141  for (auto &Pair : ReachableMap)
142  if (Pair.second.count(V))
143  InvalidComponents.push_back(Pair.first);
144 
145  for (unsigned int N : InvalidComponents) {
146  for (const Value *V : ReachableMap[N])
147  if (const PHINode *PN = dyn_cast<PHINode>(V))
148  DepthMap.erase(PN);
149  NonPhiReachableMap.erase(N);
150  ReachableMap.erase(N);
151  }
152  // This value is no longer tracked
153  auto It = TrackedValues.find_as(V);
154  if (It != TrackedValues.end())
155  TrackedValues.erase(It);
156 }
157 
159  DepthMap.clear();
160  NonPhiReachableMap.clear();
161  ReachableMap.clear();
162 }
163 
164 void PhiValues::print(raw_ostream &OS) const {
165  // Iterate through the phi nodes of the function rather than iterating through
166  // DepthMap in order to get predictable ordering.
167  for (const BasicBlock &BB : F) {
168  for (const PHINode &PN : BB.phis()) {
169  OS << "PHI ";
170  PN.printAsOperand(OS, false);
171  OS << " has values:\n";
172  unsigned int N = DepthMap.lookup(&PN);
173  auto It = NonPhiReachableMap.find(N);
174  if (It == NonPhiReachableMap.end())
175  OS << " UNKNOWN\n";
176  else if (It->second.empty())
177  OS << " NONE\n";
178  else
179  for (Value *V : It->second)
180  // Printing of an instruction prints two spaces at the start, so
181  // handle instructions and everything else slightly differently in
182  // order to get consistent indenting.
183  if (Instruction *I = dyn_cast<Instruction>(V))
184  OS << *I << "\n";
185  else
186  OS << " " << *V << "\n";
187  }
188  }
189 }
190 
191 AnalysisKey PhiValuesAnalysis::Key;
193  return PhiValues(F);
194 }
195 
198  OS << "PHI Values for function: " << F.getName() << "\n";
200  for (const BasicBlock &BB : F)
201  for (const PHINode &PN : BB.phis())
202  PI.getValuesForPhi(&PN);
203  PI.print(OS);
204  return PreservedAnalyses::all();
205 }
206 
209 }
210 
212  Result.reset(new PhiValues(F));
213  return false;
214 }
215 
217  Result->releaseMemory();
218 }
219 
221  AU.setPreservesAll();
222 }
223 
224 char PhiValuesWrapperPass::ID = 0;
225 
226 INITIALIZE_PASS(PhiValuesWrapperPass, "phi-values", "Phi Values Analysis", false,
227  true)
void releaseMemory()
Free the memory used by this class.
Definition: PhiValues.cpp:158
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:777
This class represents lattice values for constants.
Definition: AllocatorList.h:23
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:216
F(f)
const ValueSet & getValuesForPhi(const PHINode *PN)
Get the underlying values of a phi.
Definition: PhiValues.cpp:126
The analysis pass which yields a PhiValues.
Definition: PhiValues.h:118
PreservedAnalysisChecker getChecker() const
Build a checker for this PreservedAnalyses and the specified analysis type.
Definition: PassManager.h:312
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:42
Wrapper pass for the legacy pass manager.
Definition: PhiValues.h:141
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: PhiValues.cpp:220
bool invalidate(Function &, const PreservedAnalyses &, FunctionAnalysisManager::Invalidator &)
Handle invalidation events in the new pass manager.
Definition: PhiValues.cpp:27
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:154
LLVM Basic Block Representation.
Definition: BasicBlock.h:57
void invalidateValue(const Value *V)
Notify PhiValues that the cached information using V is no longer valid.
Definition: PhiValues.cpp:138
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:370
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:282
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:160
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:33
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:443
PhiValues run(Function &F, FunctionAnalysisManager &)
Definition: PhiValues.cpp:192
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass...
Definition: PhiValues.cpp:211
LLVM_NODISCARD T pop_back_val()
Definition: SmallVector.h:374
void setPreservesAll()
Set by analyses that do not transform their input at all.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition: PhiValues.cpp:196
void print(raw_ostream &OS) const
Print out the values currently in the cache.
Definition: PhiValues.cpp:164
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:55
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:215
Class for calculating and caching the underlying values of phis in a function.
Definition: PhiValues.h:42
#define I(x, y, z)
Definition: MD5.cpp:58
#define N
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:649
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This templated class represents "all analyses that operate over <a particular IR unit>" (e...
Definition: PassManager.h:92
LLVM Value Representation.
Definition: Value.h:74
void initializePhiValuesWrapperPassPass(PassRegistry &)
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
A container for analyses that lazily runs them and caches their results.
op_range incoming_values()
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: PassManager.h:71
PhiValues(const Function &F)
Construct an empty PhiValues.
Definition: PhiValues.h:47