1bdbbd838SJohn Brawn //===- PhiValues.cpp - Phi Value Analysis ---------------------------------===//
2bdbbd838SJohn Brawn //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6bdbbd838SJohn Brawn //
7bdbbd838SJohn Brawn //===----------------------------------------------------------------------===//
8bdbbd838SJohn Brawn
9bdbbd838SJohn Brawn #include "llvm/Analysis/PhiValues.h"
10bdbbd838SJohn Brawn #include "llvm/ADT/SmallVector.h"
11bdbbd838SJohn Brawn #include "llvm/IR/Instructions.h"
1205da2fe5SReid Kleckner #include "llvm/InitializePasses.h"
13bdbbd838SJohn Brawn
14bdbbd838SJohn Brawn using namespace llvm;
15bdbbd838SJohn Brawn
deleted()16980da83fSJohn Brawn void PhiValues::PhiValuesCallbackVH::deleted() {
17980da83fSJohn Brawn PV->invalidateValue(getValPtr());
18980da83fSJohn Brawn }
19980da83fSJohn Brawn
allUsesReplacedWith(Value *)20980da83fSJohn Brawn void PhiValues::PhiValuesCallbackVH::allUsesReplacedWith(Value *) {
21980da83fSJohn Brawn // We could potentially update the cached values we have with the new value,
22980da83fSJohn Brawn // but it's simpler to just treat the old value as invalidated.
23980da83fSJohn Brawn PV->invalidateValue(getValPtr());
24980da83fSJohn Brawn }
25980da83fSJohn Brawn
invalidate(Function &,const PreservedAnalyses & PA,FunctionAnalysisManager::Invalidator &)26bdbbd838SJohn Brawn bool PhiValues::invalidate(Function &, const PreservedAnalyses &PA,
27bdbbd838SJohn Brawn FunctionAnalysisManager::Invalidator &) {
28bdbbd838SJohn Brawn // PhiValues is invalidated if it isn't preserved.
29bdbbd838SJohn Brawn auto PAC = PA.getChecker<PhiValuesAnalysis>();
30bdbbd838SJohn Brawn return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>());
31bdbbd838SJohn Brawn }
32bdbbd838SJohn Brawn
33bdbbd838SJohn Brawn // The goal here is to find all of the non-phi values reachable from this phi,
34bdbbd838SJohn Brawn // and to do the same for all of the phis reachable from this phi, as doing so
35bdbbd838SJohn Brawn // is necessary anyway in order to get the values for this phi. We do this using
36bdbbd838SJohn Brawn // Tarjan's algorithm with Nuutila's improvements to find the strongly connected
37bdbbd838SJohn Brawn // components of the phi graph rooted in this phi:
38bdbbd838SJohn Brawn // * All phis in a strongly connected component will have the same reachable
39bdbbd838SJohn Brawn // non-phi values. The SCC may not be the maximal subgraph for that set of
40bdbbd838SJohn Brawn // reachable values, but finding out that isn't really necessary (it would
41bdbbd838SJohn Brawn // only reduce the amount of memory needed to store the values).
42bdbbd838SJohn Brawn // * Tarjan's algorithm completes components in a bottom-up manner, i.e. it
43bdbbd838SJohn Brawn // never completes a component before the components reachable from it have
44bdbbd838SJohn Brawn // been completed. This means that when we complete a component we have
45bdbbd838SJohn Brawn // everything we need to collect the values reachable from that component.
46bdbbd838SJohn Brawn // * We collect both the non-phi values reachable from each SCC, as that's what
47bdbbd838SJohn Brawn // we're ultimately interested in, and all of the reachable values, i.e.
48bdbbd838SJohn Brawn // including phis, as that makes invalidateValue easier.
processPhi(const PHINode * Phi,SmallVectorImpl<const PHINode * > & Stack)49bdbbd838SJohn Brawn void PhiValues::processPhi(const PHINode *Phi,
50*91aa5daeSEhud Katz SmallVectorImpl<const PHINode *> &Stack) {
51bdbbd838SJohn Brawn // Initialize the phi with the next depth number.
52bdbbd838SJohn Brawn assert(DepthMap.lookup(Phi) == 0);
53bdbbd838SJohn Brawn assert(NextDepthNumber != UINT_MAX);
54*91aa5daeSEhud Katz unsigned int RootDepthNumber = ++NextDepthNumber;
55*91aa5daeSEhud Katz DepthMap[Phi] = RootDepthNumber;
56bdbbd838SJohn Brawn
57bdbbd838SJohn Brawn // Recursively process the incoming phis of this phi.
58980da83fSJohn Brawn TrackedValues.insert(PhiValuesCallbackVH(const_cast<PHINode *>(Phi), this));
59bdbbd838SJohn Brawn for (Value *PhiOp : Phi->incoming_values()) {
60bdbbd838SJohn Brawn if (PHINode *PhiPhiOp = dyn_cast<PHINode>(PhiOp)) {
61bdbbd838SJohn Brawn // Recurse if the phi has not yet been visited.
62*91aa5daeSEhud Katz unsigned int OpDepthNumber = DepthMap.lookup(PhiPhiOp);
63*91aa5daeSEhud Katz if (OpDepthNumber == 0) {
64bdbbd838SJohn Brawn processPhi(PhiPhiOp, Stack);
65*91aa5daeSEhud Katz OpDepthNumber = DepthMap.lookup(PhiPhiOp);
66*91aa5daeSEhud Katz assert(OpDepthNumber != 0);
67*91aa5daeSEhud Katz }
68bdbbd838SJohn Brawn // If the phi did not become part of a component then this phi and that
69bdbbd838SJohn Brawn // phi are part of the same component, so adjust the depth number.
70*91aa5daeSEhud Katz if (!ReachableMap.count(OpDepthNumber))
71*91aa5daeSEhud Katz DepthMap[Phi] = std::min(DepthMap[Phi], OpDepthNumber);
72980da83fSJohn Brawn } else {
73980da83fSJohn Brawn TrackedValues.insert(PhiValuesCallbackVH(PhiOp, this));
74bdbbd838SJohn Brawn }
75bdbbd838SJohn Brawn }
76bdbbd838SJohn Brawn
77bdbbd838SJohn Brawn // Now that incoming phis have been handled, push this phi to the stack.
78bdbbd838SJohn Brawn Stack.push_back(Phi);
79bdbbd838SJohn Brawn
80bdbbd838SJohn Brawn // If the depth number has not changed then we've finished collecting the phis
81bdbbd838SJohn Brawn // of a strongly connected component.
82*91aa5daeSEhud Katz if (DepthMap[Phi] == RootDepthNumber) {
83bdbbd838SJohn Brawn // Collect the reachable values for this component. The phis of this
84*91aa5daeSEhud Katz // component will be those on top of the depth stack with the same or
85bdbbd838SJohn Brawn // greater depth number.
86*91aa5daeSEhud Katz ConstValueSet &Reachable = ReachableMap[RootDepthNumber];
87*91aa5daeSEhud Katz while (true) {
88bdbbd838SJohn Brawn const PHINode *ComponentPhi = Stack.pop_back_val();
89bdbbd838SJohn Brawn Reachable.insert(ComponentPhi);
90*91aa5daeSEhud Katz
91bdbbd838SJohn Brawn for (Value *Op : ComponentPhi->incoming_values()) {
92bdbbd838SJohn Brawn if (PHINode *PhiOp = dyn_cast<PHINode>(Op)) {
93bdbbd838SJohn Brawn // If this phi is not part of the same component then that component
94bdbbd838SJohn Brawn // is guaranteed to have been completed before this one. Therefore we
95bdbbd838SJohn Brawn // can just add its reachable values to the reachable values of this
96bdbbd838SJohn Brawn // component.
97*91aa5daeSEhud Katz unsigned int OpDepthNumber = DepthMap[PhiOp];
98*91aa5daeSEhud Katz if (OpDepthNumber != RootDepthNumber) {
99*91aa5daeSEhud Katz auto It = ReachableMap.find(OpDepthNumber);
100bdbbd838SJohn Brawn if (It != ReachableMap.end())
101bdbbd838SJohn Brawn Reachable.insert(It->second.begin(), It->second.end());
102*91aa5daeSEhud Katz }
103*91aa5daeSEhud Katz } else
104bdbbd838SJohn Brawn Reachable.insert(Op);
105bdbbd838SJohn Brawn }
106*91aa5daeSEhud Katz
107*91aa5daeSEhud Katz if (Stack.empty())
108*91aa5daeSEhud Katz break;
109*91aa5daeSEhud Katz
110*91aa5daeSEhud Katz unsigned int &ComponentDepthNumber = DepthMap[Stack.back()];
111*91aa5daeSEhud Katz if (ComponentDepthNumber < RootDepthNumber)
112*91aa5daeSEhud Katz break;
113*91aa5daeSEhud Katz
114*91aa5daeSEhud Katz ComponentDepthNumber = RootDepthNumber;
115bdbbd838SJohn Brawn }
116bdbbd838SJohn Brawn
117bdbbd838SJohn Brawn // Filter out phis to get the non-phi reachable values.
118*91aa5daeSEhud Katz ValueSet &NonPhi = NonPhiReachableMap[RootDepthNumber];
119bdbbd838SJohn Brawn for (const Value *V : Reachable)
120bdbbd838SJohn Brawn if (!isa<PHINode>(V))
121bdbbd838SJohn Brawn NonPhi.insert(const_cast<Value *>(V));
122bdbbd838SJohn Brawn }
123bdbbd838SJohn Brawn }
124bdbbd838SJohn Brawn
getValuesForPhi(const PHINode * PN)125bdbbd838SJohn Brawn const PhiValues::ValueSet &PhiValues::getValuesForPhi(const PHINode *PN) {
126*91aa5daeSEhud Katz unsigned int DepthNumber = DepthMap.lookup(PN);
127*91aa5daeSEhud Katz if (DepthNumber == 0) {
128bdbbd838SJohn Brawn SmallVector<const PHINode *, 8> Stack;
129bdbbd838SJohn Brawn processPhi(PN, Stack);
130*91aa5daeSEhud Katz DepthNumber = DepthMap.lookup(PN);
131bdbbd838SJohn Brawn assert(Stack.empty());
132*91aa5daeSEhud Katz assert(DepthNumber != 0);
133bdbbd838SJohn Brawn }
134*91aa5daeSEhud Katz return NonPhiReachableMap[DepthNumber];
135bdbbd838SJohn Brawn }
136bdbbd838SJohn Brawn
invalidateValue(const Value * V)137bdbbd838SJohn Brawn void PhiValues::invalidateValue(const Value *V) {
138bdbbd838SJohn Brawn // Components that can reach V are invalid.
139bdbbd838SJohn Brawn SmallVector<unsigned int, 8> InvalidComponents;
140bdbbd838SJohn Brawn for (auto &Pair : ReachableMap)
141bdbbd838SJohn Brawn if (Pair.second.count(V))
142bdbbd838SJohn Brawn InvalidComponents.push_back(Pair.first);
143bdbbd838SJohn Brawn
144bdbbd838SJohn Brawn for (unsigned int N : InvalidComponents) {
145bdbbd838SJohn Brawn for (const Value *V : ReachableMap[N])
146bdbbd838SJohn Brawn if (const PHINode *PN = dyn_cast<PHINode>(V))
147bdbbd838SJohn Brawn DepthMap.erase(PN);
148bdbbd838SJohn Brawn NonPhiReachableMap.erase(N);
149bdbbd838SJohn Brawn ReachableMap.erase(N);
150bdbbd838SJohn Brawn }
151980da83fSJohn Brawn // This value is no longer tracked
152980da83fSJohn Brawn auto It = TrackedValues.find_as(V);
153980da83fSJohn Brawn if (It != TrackedValues.end())
154980da83fSJohn Brawn TrackedValues.erase(It);
155bdbbd838SJohn Brawn }
156bdbbd838SJohn Brawn
releaseMemory()157bdbbd838SJohn Brawn void PhiValues::releaseMemory() {
158bdbbd838SJohn Brawn DepthMap.clear();
159bdbbd838SJohn Brawn NonPhiReachableMap.clear();
160bdbbd838SJohn Brawn ReachableMap.clear();
161bdbbd838SJohn Brawn }
162bdbbd838SJohn Brawn
print(raw_ostream & OS) const163bdbbd838SJohn Brawn void PhiValues::print(raw_ostream &OS) const {
164bdbbd838SJohn Brawn // Iterate through the phi nodes of the function rather than iterating through
165bdbbd838SJohn Brawn // DepthMap in order to get predictable ordering.
166bdbbd838SJohn Brawn for (const BasicBlock &BB : F) {
167bdbbd838SJohn Brawn for (const PHINode &PN : BB.phis()) {
168bdbbd838SJohn Brawn OS << "PHI ";
169bdbbd838SJohn Brawn PN.printAsOperand(OS, false);
170bdbbd838SJohn Brawn OS << " has values:\n";
171bdbbd838SJohn Brawn unsigned int N = DepthMap.lookup(&PN);
172bdbbd838SJohn Brawn auto It = NonPhiReachableMap.find(N);
173bdbbd838SJohn Brawn if (It == NonPhiReachableMap.end())
174bdbbd838SJohn Brawn OS << " UNKNOWN\n";
175bdbbd838SJohn Brawn else if (It->second.empty())
176bdbbd838SJohn Brawn OS << " NONE\n";
177bdbbd838SJohn Brawn else
178bdbbd838SJohn Brawn for (Value *V : It->second)
179bdbbd838SJohn Brawn // Printing of an instruction prints two spaces at the start, so
180bdbbd838SJohn Brawn // handle instructions and everything else slightly differently in
181bdbbd838SJohn Brawn // order to get consistent indenting.
182bdbbd838SJohn Brawn if (Instruction *I = dyn_cast<Instruction>(V))
183bdbbd838SJohn Brawn OS << *I << "\n";
184bdbbd838SJohn Brawn else
185bdbbd838SJohn Brawn OS << " " << *V << "\n";
186bdbbd838SJohn Brawn }
187bdbbd838SJohn Brawn }
188bdbbd838SJohn Brawn }
189bdbbd838SJohn Brawn
190bdbbd838SJohn Brawn AnalysisKey PhiValuesAnalysis::Key;
run(Function & F,FunctionAnalysisManager &)191bdbbd838SJohn Brawn PhiValues PhiValuesAnalysis::run(Function &F, FunctionAnalysisManager &) {
192bdbbd838SJohn Brawn return PhiValues(F);
193bdbbd838SJohn Brawn }
194bdbbd838SJohn Brawn
run(Function & F,FunctionAnalysisManager & AM)195bdbbd838SJohn Brawn PreservedAnalyses PhiValuesPrinterPass::run(Function &F,
196bdbbd838SJohn Brawn FunctionAnalysisManager &AM) {
197bdbbd838SJohn Brawn OS << "PHI Values for function: " << F.getName() << "\n";
198bdbbd838SJohn Brawn PhiValues &PI = AM.getResult<PhiValuesAnalysis>(F);
199bdbbd838SJohn Brawn for (const BasicBlock &BB : F)
200bdbbd838SJohn Brawn for (const PHINode &PN : BB.phis())
201bdbbd838SJohn Brawn PI.getValuesForPhi(&PN);
202bdbbd838SJohn Brawn PI.print(OS);
203bdbbd838SJohn Brawn return PreservedAnalyses::all();
204bdbbd838SJohn Brawn }
205bdbbd838SJohn Brawn
PhiValuesWrapperPass()206bdbbd838SJohn Brawn PhiValuesWrapperPass::PhiValuesWrapperPass() : FunctionPass(ID) {
207bdbbd838SJohn Brawn initializePhiValuesWrapperPassPass(*PassRegistry::getPassRegistry());
208bdbbd838SJohn Brawn }
209bdbbd838SJohn Brawn
runOnFunction(Function & F)210bdbbd838SJohn Brawn bool PhiValuesWrapperPass::runOnFunction(Function &F) {
211bdbbd838SJohn Brawn Result.reset(new PhiValues(F));
212bdbbd838SJohn Brawn return false;
213bdbbd838SJohn Brawn }
214bdbbd838SJohn Brawn
releaseMemory()215bdbbd838SJohn Brawn void PhiValuesWrapperPass::releaseMemory() {
216bdbbd838SJohn Brawn Result->releaseMemory();
217bdbbd838SJohn Brawn }
218bdbbd838SJohn Brawn
getAnalysisUsage(AnalysisUsage & AU) const219bdbbd838SJohn Brawn void PhiValuesWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
220bdbbd838SJohn Brawn AU.setPreservesAll();
221bdbbd838SJohn Brawn }
222bdbbd838SJohn Brawn
223bdbbd838SJohn Brawn char PhiValuesWrapperPass::ID = 0;
224bdbbd838SJohn Brawn
225bdbbd838SJohn Brawn INITIALIZE_PASS(PhiValuesWrapperPass, "phi-values", "Phi Values Analysis", false,
226bdbbd838SJohn Brawn true)
227