xref: /netbsd-src/external/apache2/llvm/dist/llvm/lib/Analysis/PhiValues.cpp (revision 82d56013d7b633d116a93943de88e08335357a7c)
17330f729Sjoerg //===- PhiValues.cpp - Phi Value Analysis ---------------------------------===//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg 
97330f729Sjoerg #include "llvm/Analysis/PhiValues.h"
107330f729Sjoerg #include "llvm/ADT/SmallVector.h"
117330f729Sjoerg #include "llvm/IR/Instructions.h"
12*82d56013Sjoerg #include "llvm/InitializePasses.h"
137330f729Sjoerg 
147330f729Sjoerg using namespace llvm;
157330f729Sjoerg 
deleted()167330f729Sjoerg void PhiValues::PhiValuesCallbackVH::deleted() {
177330f729Sjoerg   PV->invalidateValue(getValPtr());
187330f729Sjoerg }
197330f729Sjoerg 
allUsesReplacedWith(Value *)207330f729Sjoerg void PhiValues::PhiValuesCallbackVH::allUsesReplacedWith(Value *) {
217330f729Sjoerg   // We could potentially update the cached values we have with the new value,
227330f729Sjoerg   // but it's simpler to just treat the old value as invalidated.
237330f729Sjoerg   PV->invalidateValue(getValPtr());
247330f729Sjoerg }
257330f729Sjoerg 
invalidate(Function &,const PreservedAnalyses & PA,FunctionAnalysisManager::Invalidator &)267330f729Sjoerg bool PhiValues::invalidate(Function &, const PreservedAnalyses &PA,
277330f729Sjoerg                            FunctionAnalysisManager::Invalidator &) {
287330f729Sjoerg   // PhiValues is invalidated if it isn't preserved.
297330f729Sjoerg   auto PAC = PA.getChecker<PhiValuesAnalysis>();
307330f729Sjoerg   return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>());
317330f729Sjoerg }
327330f729Sjoerg 
337330f729Sjoerg // The goal here is to find all of the non-phi values reachable from this phi,
347330f729Sjoerg // and to do the same for all of the phis reachable from this phi, as doing so
357330f729Sjoerg // is necessary anyway in order to get the values for this phi. We do this using
367330f729Sjoerg // Tarjan's algorithm with Nuutila's improvements to find the strongly connected
377330f729Sjoerg // components of the phi graph rooted in this phi:
387330f729Sjoerg //  * All phis in a strongly connected component will have the same reachable
397330f729Sjoerg //    non-phi values. The SCC may not be the maximal subgraph for that set of
407330f729Sjoerg //    reachable values, but finding out that isn't really necessary (it would
417330f729Sjoerg //    only reduce the amount of memory needed to store the values).
427330f729Sjoerg //  * Tarjan's algorithm completes components in a bottom-up manner, i.e. it
437330f729Sjoerg //    never completes a component before the components reachable from it have
447330f729Sjoerg //    been completed. This means that when we complete a component we have
457330f729Sjoerg //    everything we need to collect the values reachable from that component.
467330f729Sjoerg //  * We collect both the non-phi values reachable from each SCC, as that's what
477330f729Sjoerg //    we're ultimately interested in, and all of the reachable values, i.e.
487330f729Sjoerg //    including phis, as that makes invalidateValue easier.
processPhi(const PHINode * Phi,SmallVectorImpl<const PHINode * > & Stack)497330f729Sjoerg void PhiValues::processPhi(const PHINode *Phi,
50*82d56013Sjoerg                            SmallVectorImpl<const PHINode *> &Stack) {
517330f729Sjoerg   // Initialize the phi with the next depth number.
527330f729Sjoerg   assert(DepthMap.lookup(Phi) == 0);
537330f729Sjoerg   assert(NextDepthNumber != UINT_MAX);
54*82d56013Sjoerg   unsigned int RootDepthNumber = ++NextDepthNumber;
55*82d56013Sjoerg   DepthMap[Phi] = RootDepthNumber;
567330f729Sjoerg 
577330f729Sjoerg   // Recursively process the incoming phis of this phi.
587330f729Sjoerg   TrackedValues.insert(PhiValuesCallbackVH(const_cast<PHINode *>(Phi), this));
597330f729Sjoerg   for (Value *PhiOp : Phi->incoming_values()) {
607330f729Sjoerg     if (PHINode *PhiPhiOp = dyn_cast<PHINode>(PhiOp)) {
617330f729Sjoerg       // Recurse if the phi has not yet been visited.
62*82d56013Sjoerg       unsigned int OpDepthNumber = DepthMap.lookup(PhiPhiOp);
63*82d56013Sjoerg       if (OpDepthNumber == 0) {
647330f729Sjoerg         processPhi(PhiPhiOp, Stack);
65*82d56013Sjoerg         OpDepthNumber = DepthMap.lookup(PhiPhiOp);
66*82d56013Sjoerg         assert(OpDepthNumber != 0);
67*82d56013Sjoerg       }
687330f729Sjoerg       // If the phi did not become part of a component then this phi and that
697330f729Sjoerg       // phi are part of the same component, so adjust the depth number.
70*82d56013Sjoerg       if (!ReachableMap.count(OpDepthNumber))
71*82d56013Sjoerg         DepthMap[Phi] = std::min(DepthMap[Phi], OpDepthNumber);
727330f729Sjoerg     } else {
737330f729Sjoerg       TrackedValues.insert(PhiValuesCallbackVH(PhiOp, this));
747330f729Sjoerg     }
757330f729Sjoerg   }
767330f729Sjoerg 
777330f729Sjoerg   // Now that incoming phis have been handled, push this phi to the stack.
787330f729Sjoerg   Stack.push_back(Phi);
797330f729Sjoerg 
807330f729Sjoerg   // If the depth number has not changed then we've finished collecting the phis
817330f729Sjoerg   // of a strongly connected component.
82*82d56013Sjoerg   if (DepthMap[Phi] == RootDepthNumber) {
837330f729Sjoerg     // Collect the reachable values for this component. The phis of this
84*82d56013Sjoerg     // component will be those on top of the depth stack with the same or
857330f729Sjoerg     // greater depth number.
86*82d56013Sjoerg     ConstValueSet &Reachable = ReachableMap[RootDepthNumber];
87*82d56013Sjoerg     while (true) {
887330f729Sjoerg       const PHINode *ComponentPhi = Stack.pop_back_val();
897330f729Sjoerg       Reachable.insert(ComponentPhi);
90*82d56013Sjoerg 
917330f729Sjoerg       for (Value *Op : ComponentPhi->incoming_values()) {
927330f729Sjoerg         if (PHINode *PhiOp = dyn_cast<PHINode>(Op)) {
937330f729Sjoerg           // If this phi is not part of the same component then that component
947330f729Sjoerg           // is guaranteed to have been completed before this one. Therefore we
957330f729Sjoerg           // can just add its reachable values to the reachable values of this
967330f729Sjoerg           // component.
97*82d56013Sjoerg           unsigned int OpDepthNumber = DepthMap[PhiOp];
98*82d56013Sjoerg           if (OpDepthNumber != RootDepthNumber) {
99*82d56013Sjoerg             auto It = ReachableMap.find(OpDepthNumber);
1007330f729Sjoerg             if (It != ReachableMap.end())
1017330f729Sjoerg               Reachable.insert(It->second.begin(), It->second.end());
102*82d56013Sjoerg           }
103*82d56013Sjoerg         } else
1047330f729Sjoerg           Reachable.insert(Op);
1057330f729Sjoerg       }
106*82d56013Sjoerg 
107*82d56013Sjoerg       if (Stack.empty())
108*82d56013Sjoerg         break;
109*82d56013Sjoerg 
110*82d56013Sjoerg       unsigned int &ComponentDepthNumber = DepthMap[Stack.back()];
111*82d56013Sjoerg       if (ComponentDepthNumber < RootDepthNumber)
112*82d56013Sjoerg         break;
113*82d56013Sjoerg 
114*82d56013Sjoerg       ComponentDepthNumber = RootDepthNumber;
1157330f729Sjoerg     }
1167330f729Sjoerg 
1177330f729Sjoerg     // Filter out phis to get the non-phi reachable values.
118*82d56013Sjoerg     ValueSet &NonPhi = NonPhiReachableMap[RootDepthNumber];
1197330f729Sjoerg     for (const Value *V : Reachable)
1207330f729Sjoerg       if (!isa<PHINode>(V))
1217330f729Sjoerg         NonPhi.insert(const_cast<Value *>(V));
1227330f729Sjoerg   }
1237330f729Sjoerg }
1247330f729Sjoerg 
getValuesForPhi(const PHINode * PN)1257330f729Sjoerg const PhiValues::ValueSet &PhiValues::getValuesForPhi(const PHINode *PN) {
126*82d56013Sjoerg   unsigned int DepthNumber = DepthMap.lookup(PN);
127*82d56013Sjoerg   if (DepthNumber == 0) {
1287330f729Sjoerg     SmallVector<const PHINode *, 8> Stack;
1297330f729Sjoerg     processPhi(PN, Stack);
130*82d56013Sjoerg     DepthNumber = DepthMap.lookup(PN);
1317330f729Sjoerg     assert(Stack.empty());
132*82d56013Sjoerg     assert(DepthNumber != 0);
1337330f729Sjoerg   }
134*82d56013Sjoerg   return NonPhiReachableMap[DepthNumber];
1357330f729Sjoerg }
1367330f729Sjoerg 
invalidateValue(const Value * V)1377330f729Sjoerg void PhiValues::invalidateValue(const Value *V) {
1387330f729Sjoerg   // Components that can reach V are invalid.
1397330f729Sjoerg   SmallVector<unsigned int, 8> InvalidComponents;
1407330f729Sjoerg   for (auto &Pair : ReachableMap)
1417330f729Sjoerg     if (Pair.second.count(V))
1427330f729Sjoerg       InvalidComponents.push_back(Pair.first);
1437330f729Sjoerg 
1447330f729Sjoerg   for (unsigned int N : InvalidComponents) {
1457330f729Sjoerg     for (const Value *V : ReachableMap[N])
1467330f729Sjoerg       if (const PHINode *PN = dyn_cast<PHINode>(V))
1477330f729Sjoerg         DepthMap.erase(PN);
1487330f729Sjoerg     NonPhiReachableMap.erase(N);
1497330f729Sjoerg     ReachableMap.erase(N);
1507330f729Sjoerg   }
1517330f729Sjoerg   // This value is no longer tracked
1527330f729Sjoerg   auto It = TrackedValues.find_as(V);
1537330f729Sjoerg   if (It != TrackedValues.end())
1547330f729Sjoerg     TrackedValues.erase(It);
1557330f729Sjoerg }
1567330f729Sjoerg 
releaseMemory()1577330f729Sjoerg void PhiValues::releaseMemory() {
1587330f729Sjoerg   DepthMap.clear();
1597330f729Sjoerg   NonPhiReachableMap.clear();
1607330f729Sjoerg   ReachableMap.clear();
1617330f729Sjoerg }
1627330f729Sjoerg 
print(raw_ostream & OS) const1637330f729Sjoerg void PhiValues::print(raw_ostream &OS) const {
1647330f729Sjoerg   // Iterate through the phi nodes of the function rather than iterating through
1657330f729Sjoerg   // DepthMap in order to get predictable ordering.
1667330f729Sjoerg   for (const BasicBlock &BB : F) {
1677330f729Sjoerg     for (const PHINode &PN : BB.phis()) {
1687330f729Sjoerg       OS << "PHI ";
1697330f729Sjoerg       PN.printAsOperand(OS, false);
1707330f729Sjoerg       OS << " has values:\n";
1717330f729Sjoerg       unsigned int N = DepthMap.lookup(&PN);
1727330f729Sjoerg       auto It = NonPhiReachableMap.find(N);
1737330f729Sjoerg       if (It == NonPhiReachableMap.end())
1747330f729Sjoerg         OS << "  UNKNOWN\n";
1757330f729Sjoerg       else if (It->second.empty())
1767330f729Sjoerg         OS << "  NONE\n";
1777330f729Sjoerg       else
1787330f729Sjoerg         for (Value *V : It->second)
1797330f729Sjoerg           // Printing of an instruction prints two spaces at the start, so
1807330f729Sjoerg           // handle instructions and everything else slightly differently in
1817330f729Sjoerg           // order to get consistent indenting.
1827330f729Sjoerg           if (Instruction *I = dyn_cast<Instruction>(V))
1837330f729Sjoerg             OS << *I << "\n";
1847330f729Sjoerg           else
1857330f729Sjoerg             OS << "  " << *V << "\n";
1867330f729Sjoerg     }
1877330f729Sjoerg   }
1887330f729Sjoerg }
1897330f729Sjoerg 
1907330f729Sjoerg AnalysisKey PhiValuesAnalysis::Key;
run(Function & F,FunctionAnalysisManager &)1917330f729Sjoerg PhiValues PhiValuesAnalysis::run(Function &F, FunctionAnalysisManager &) {
1927330f729Sjoerg   return PhiValues(F);
1937330f729Sjoerg }
1947330f729Sjoerg 
run(Function & F,FunctionAnalysisManager & AM)1957330f729Sjoerg PreservedAnalyses PhiValuesPrinterPass::run(Function &F,
1967330f729Sjoerg                                             FunctionAnalysisManager &AM) {
1977330f729Sjoerg   OS << "PHI Values for function: " << F.getName() << "\n";
1987330f729Sjoerg   PhiValues &PI = AM.getResult<PhiValuesAnalysis>(F);
1997330f729Sjoerg   for (const BasicBlock &BB : F)
2007330f729Sjoerg     for (const PHINode &PN : BB.phis())
2017330f729Sjoerg       PI.getValuesForPhi(&PN);
2027330f729Sjoerg   PI.print(OS);
2037330f729Sjoerg   return PreservedAnalyses::all();
2047330f729Sjoerg }
2057330f729Sjoerg 
PhiValuesWrapperPass()2067330f729Sjoerg PhiValuesWrapperPass::PhiValuesWrapperPass() : FunctionPass(ID) {
2077330f729Sjoerg   initializePhiValuesWrapperPassPass(*PassRegistry::getPassRegistry());
2087330f729Sjoerg }
2097330f729Sjoerg 
runOnFunction(Function & F)2107330f729Sjoerg bool PhiValuesWrapperPass::runOnFunction(Function &F) {
2117330f729Sjoerg   Result.reset(new PhiValues(F));
2127330f729Sjoerg   return false;
2137330f729Sjoerg }
2147330f729Sjoerg 
releaseMemory()2157330f729Sjoerg void PhiValuesWrapperPass::releaseMemory() {
2167330f729Sjoerg   Result->releaseMemory();
2177330f729Sjoerg }
2187330f729Sjoerg 
getAnalysisUsage(AnalysisUsage & AU) const2197330f729Sjoerg void PhiValuesWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
2207330f729Sjoerg   AU.setPreservesAll();
2217330f729Sjoerg }
2227330f729Sjoerg 
2237330f729Sjoerg char PhiValuesWrapperPass::ID = 0;
2247330f729Sjoerg 
2257330f729Sjoerg INITIALIZE_PASS(PhiValuesWrapperPass, "phi-values", "Phi Values Analysis", false,
2267330f729Sjoerg                 true)
227