10b57cec5SDimitry Andric //===- PhiValues.cpp - Phi Value Analysis ---------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric
90b57cec5SDimitry Andric #include "llvm/Analysis/PhiValues.h"
100b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
110b57cec5SDimitry Andric #include "llvm/IR/Instructions.h"
12*480093f4SDimitry Andric #include "llvm/InitializePasses.h"
130b57cec5SDimitry Andric
140b57cec5SDimitry Andric using namespace llvm;
150b57cec5SDimitry Andric
deleted()160b57cec5SDimitry Andric void PhiValues::PhiValuesCallbackVH::deleted() {
170b57cec5SDimitry Andric PV->invalidateValue(getValPtr());
180b57cec5SDimitry Andric }
190b57cec5SDimitry Andric
allUsesReplacedWith(Value *)200b57cec5SDimitry Andric void PhiValues::PhiValuesCallbackVH::allUsesReplacedWith(Value *) {
210b57cec5SDimitry Andric // We could potentially update the cached values we have with the new value,
220b57cec5SDimitry Andric // but it's simpler to just treat the old value as invalidated.
230b57cec5SDimitry Andric PV->invalidateValue(getValPtr());
240b57cec5SDimitry Andric }
250b57cec5SDimitry Andric
invalidate(Function &,const PreservedAnalyses & PA,FunctionAnalysisManager::Invalidator &)260b57cec5SDimitry Andric bool PhiValues::invalidate(Function &, const PreservedAnalyses &PA,
270b57cec5SDimitry Andric FunctionAnalysisManager::Invalidator &) {
280b57cec5SDimitry Andric // PhiValues is invalidated if it isn't preserved.
290b57cec5SDimitry Andric auto PAC = PA.getChecker<PhiValuesAnalysis>();
300b57cec5SDimitry Andric return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>());
310b57cec5SDimitry Andric }
320b57cec5SDimitry Andric
330b57cec5SDimitry Andric // The goal here is to find all of the non-phi values reachable from this phi,
340b57cec5SDimitry Andric // and to do the same for all of the phis reachable from this phi, as doing so
350b57cec5SDimitry Andric // is necessary anyway in order to get the values for this phi. We do this using
360b57cec5SDimitry Andric // Tarjan's algorithm with Nuutila's improvements to find the strongly connected
370b57cec5SDimitry Andric // components of the phi graph rooted in this phi:
380b57cec5SDimitry Andric // * All phis in a strongly connected component will have the same reachable
390b57cec5SDimitry Andric // non-phi values. The SCC may not be the maximal subgraph for that set of
400b57cec5SDimitry Andric // reachable values, but finding out that isn't really necessary (it would
410b57cec5SDimitry Andric // only reduce the amount of memory needed to store the values).
420b57cec5SDimitry Andric // * Tarjan's algorithm completes components in a bottom-up manner, i.e. it
430b57cec5SDimitry Andric // never completes a component before the components reachable from it have
440b57cec5SDimitry Andric // been completed. This means that when we complete a component we have
450b57cec5SDimitry Andric // everything we need to collect the values reachable from that component.
460b57cec5SDimitry Andric // * We collect both the non-phi values reachable from each SCC, as that's what
470b57cec5SDimitry Andric // we're ultimately interested in, and all of the reachable values, i.e.
480b57cec5SDimitry Andric // including phis, as that makes invalidateValue easier.
processPhi(const PHINode * Phi,SmallVectorImpl<const PHINode * > & Stack)490b57cec5SDimitry Andric void PhiValues::processPhi(const PHINode *Phi,
50*480093f4SDimitry Andric SmallVectorImpl<const PHINode *> &Stack) {
510b57cec5SDimitry Andric // Initialize the phi with the next depth number.
520b57cec5SDimitry Andric assert(DepthMap.lookup(Phi) == 0);
530b57cec5SDimitry Andric assert(NextDepthNumber != UINT_MAX);
54*480093f4SDimitry Andric unsigned int RootDepthNumber = ++NextDepthNumber;
55*480093f4SDimitry Andric DepthMap[Phi] = RootDepthNumber;
560b57cec5SDimitry Andric
570b57cec5SDimitry Andric // Recursively process the incoming phis of this phi.
580b57cec5SDimitry Andric TrackedValues.insert(PhiValuesCallbackVH(const_cast<PHINode *>(Phi), this));
590b57cec5SDimitry Andric for (Value *PhiOp : Phi->incoming_values()) {
600b57cec5SDimitry Andric if (PHINode *PhiPhiOp = dyn_cast<PHINode>(PhiOp)) {
610b57cec5SDimitry Andric // Recurse if the phi has not yet been visited.
62*480093f4SDimitry Andric unsigned int OpDepthNumber = DepthMap.lookup(PhiPhiOp);
63*480093f4SDimitry Andric if (OpDepthNumber == 0) {
640b57cec5SDimitry Andric processPhi(PhiPhiOp, Stack);
65*480093f4SDimitry Andric OpDepthNumber = DepthMap.lookup(PhiPhiOp);
66*480093f4SDimitry Andric assert(OpDepthNumber != 0);
67*480093f4SDimitry Andric }
680b57cec5SDimitry Andric // If the phi did not become part of a component then this phi and that
690b57cec5SDimitry Andric // phi are part of the same component, so adjust the depth number.
70*480093f4SDimitry Andric if (!ReachableMap.count(OpDepthNumber))
71*480093f4SDimitry Andric DepthMap[Phi] = std::min(DepthMap[Phi], OpDepthNumber);
720b57cec5SDimitry Andric } else {
730b57cec5SDimitry Andric TrackedValues.insert(PhiValuesCallbackVH(PhiOp, this));
740b57cec5SDimitry Andric }
750b57cec5SDimitry Andric }
760b57cec5SDimitry Andric
770b57cec5SDimitry Andric // Now that incoming phis have been handled, push this phi to the stack.
780b57cec5SDimitry Andric Stack.push_back(Phi);
790b57cec5SDimitry Andric
800b57cec5SDimitry Andric // If the depth number has not changed then we've finished collecting the phis
810b57cec5SDimitry Andric // of a strongly connected component.
82*480093f4SDimitry Andric if (DepthMap[Phi] == RootDepthNumber) {
830b57cec5SDimitry Andric // Collect the reachable values for this component. The phis of this
84*480093f4SDimitry Andric // component will be those on top of the depth stack with the same or
850b57cec5SDimitry Andric // greater depth number.
86*480093f4SDimitry Andric ConstValueSet &Reachable = ReachableMap[RootDepthNumber];
87*480093f4SDimitry Andric while (true) {
880b57cec5SDimitry Andric const PHINode *ComponentPhi = Stack.pop_back_val();
890b57cec5SDimitry Andric Reachable.insert(ComponentPhi);
90*480093f4SDimitry Andric
910b57cec5SDimitry Andric for (Value *Op : ComponentPhi->incoming_values()) {
920b57cec5SDimitry Andric if (PHINode *PhiOp = dyn_cast<PHINode>(Op)) {
930b57cec5SDimitry Andric // If this phi is not part of the same component then that component
940b57cec5SDimitry Andric // is guaranteed to have been completed before this one. Therefore we
950b57cec5SDimitry Andric // can just add its reachable values to the reachable values of this
960b57cec5SDimitry Andric // component.
97*480093f4SDimitry Andric unsigned int OpDepthNumber = DepthMap[PhiOp];
98*480093f4SDimitry Andric if (OpDepthNumber != RootDepthNumber) {
99*480093f4SDimitry Andric auto It = ReachableMap.find(OpDepthNumber);
1000b57cec5SDimitry Andric if (It != ReachableMap.end())
1010b57cec5SDimitry Andric Reachable.insert(It->second.begin(), It->second.end());
102*480093f4SDimitry Andric }
103*480093f4SDimitry Andric } else
1040b57cec5SDimitry Andric Reachable.insert(Op);
1050b57cec5SDimitry Andric }
106*480093f4SDimitry Andric
107*480093f4SDimitry Andric if (Stack.empty())
108*480093f4SDimitry Andric break;
109*480093f4SDimitry Andric
110*480093f4SDimitry Andric unsigned int &ComponentDepthNumber = DepthMap[Stack.back()];
111*480093f4SDimitry Andric if (ComponentDepthNumber < RootDepthNumber)
112*480093f4SDimitry Andric break;
113*480093f4SDimitry Andric
114*480093f4SDimitry Andric ComponentDepthNumber = RootDepthNumber;
1150b57cec5SDimitry Andric }
1160b57cec5SDimitry Andric
1170b57cec5SDimitry Andric // Filter out phis to get the non-phi reachable values.
118*480093f4SDimitry Andric ValueSet &NonPhi = NonPhiReachableMap[RootDepthNumber];
1190b57cec5SDimitry Andric for (const Value *V : Reachable)
1200b57cec5SDimitry Andric if (!isa<PHINode>(V))
1210b57cec5SDimitry Andric NonPhi.insert(const_cast<Value *>(V));
1220b57cec5SDimitry Andric }
1230b57cec5SDimitry Andric }
1240b57cec5SDimitry Andric
getValuesForPhi(const PHINode * PN)1250b57cec5SDimitry Andric const PhiValues::ValueSet &PhiValues::getValuesForPhi(const PHINode *PN) {
126*480093f4SDimitry Andric unsigned int DepthNumber = DepthMap.lookup(PN);
127*480093f4SDimitry Andric if (DepthNumber == 0) {
1280b57cec5SDimitry Andric SmallVector<const PHINode *, 8> Stack;
1290b57cec5SDimitry Andric processPhi(PN, Stack);
130*480093f4SDimitry Andric DepthNumber = DepthMap.lookup(PN);
1310b57cec5SDimitry Andric assert(Stack.empty());
132*480093f4SDimitry Andric assert(DepthNumber != 0);
1330b57cec5SDimitry Andric }
134*480093f4SDimitry Andric return NonPhiReachableMap[DepthNumber];
1350b57cec5SDimitry Andric }
1360b57cec5SDimitry Andric
invalidateValue(const Value * V)1370b57cec5SDimitry Andric void PhiValues::invalidateValue(const Value *V) {
1380b57cec5SDimitry Andric // Components that can reach V are invalid.
1390b57cec5SDimitry Andric SmallVector<unsigned int, 8> InvalidComponents;
1400b57cec5SDimitry Andric for (auto &Pair : ReachableMap)
1410b57cec5SDimitry Andric if (Pair.second.count(V))
1420b57cec5SDimitry Andric InvalidComponents.push_back(Pair.first);
1430b57cec5SDimitry Andric
1440b57cec5SDimitry Andric for (unsigned int N : InvalidComponents) {
1450b57cec5SDimitry Andric for (const Value *V : ReachableMap[N])
1460b57cec5SDimitry Andric if (const PHINode *PN = dyn_cast<PHINode>(V))
1470b57cec5SDimitry Andric DepthMap.erase(PN);
1480b57cec5SDimitry Andric NonPhiReachableMap.erase(N);
1490b57cec5SDimitry Andric ReachableMap.erase(N);
1500b57cec5SDimitry Andric }
1510b57cec5SDimitry Andric // This value is no longer tracked
1520b57cec5SDimitry Andric auto It = TrackedValues.find_as(V);
1530b57cec5SDimitry Andric if (It != TrackedValues.end())
1540b57cec5SDimitry Andric TrackedValues.erase(It);
1550b57cec5SDimitry Andric }
1560b57cec5SDimitry Andric
releaseMemory()1570b57cec5SDimitry Andric void PhiValues::releaseMemory() {
1580b57cec5SDimitry Andric DepthMap.clear();
1590b57cec5SDimitry Andric NonPhiReachableMap.clear();
1600b57cec5SDimitry Andric ReachableMap.clear();
1610b57cec5SDimitry Andric }
1620b57cec5SDimitry Andric
print(raw_ostream & OS) const1630b57cec5SDimitry Andric void PhiValues::print(raw_ostream &OS) const {
1640b57cec5SDimitry Andric // Iterate through the phi nodes of the function rather than iterating through
1650b57cec5SDimitry Andric // DepthMap in order to get predictable ordering.
1660b57cec5SDimitry Andric for (const BasicBlock &BB : F) {
1670b57cec5SDimitry Andric for (const PHINode &PN : BB.phis()) {
1680b57cec5SDimitry Andric OS << "PHI ";
1690b57cec5SDimitry Andric PN.printAsOperand(OS, false);
1700b57cec5SDimitry Andric OS << " has values:\n";
1710b57cec5SDimitry Andric unsigned int N = DepthMap.lookup(&PN);
1720b57cec5SDimitry Andric auto It = NonPhiReachableMap.find(N);
1730b57cec5SDimitry Andric if (It == NonPhiReachableMap.end())
1740b57cec5SDimitry Andric OS << " UNKNOWN\n";
1750b57cec5SDimitry Andric else if (It->second.empty())
1760b57cec5SDimitry Andric OS << " NONE\n";
1770b57cec5SDimitry Andric else
1780b57cec5SDimitry Andric for (Value *V : It->second)
1790b57cec5SDimitry Andric // Printing of an instruction prints two spaces at the start, so
1800b57cec5SDimitry Andric // handle instructions and everything else slightly differently in
1810b57cec5SDimitry Andric // order to get consistent indenting.
1820b57cec5SDimitry Andric if (Instruction *I = dyn_cast<Instruction>(V))
1830b57cec5SDimitry Andric OS << *I << "\n";
1840b57cec5SDimitry Andric else
1850b57cec5SDimitry Andric OS << " " << *V << "\n";
1860b57cec5SDimitry Andric }
1870b57cec5SDimitry Andric }
1880b57cec5SDimitry Andric }
1890b57cec5SDimitry Andric
1900b57cec5SDimitry Andric AnalysisKey PhiValuesAnalysis::Key;
run(Function & F,FunctionAnalysisManager &)1910b57cec5SDimitry Andric PhiValues PhiValuesAnalysis::run(Function &F, FunctionAnalysisManager &) {
1920b57cec5SDimitry Andric return PhiValues(F);
1930b57cec5SDimitry Andric }
1940b57cec5SDimitry Andric
run(Function & F,FunctionAnalysisManager & AM)1950b57cec5SDimitry Andric PreservedAnalyses PhiValuesPrinterPass::run(Function &F,
1960b57cec5SDimitry Andric FunctionAnalysisManager &AM) {
1970b57cec5SDimitry Andric OS << "PHI Values for function: " << F.getName() << "\n";
1980b57cec5SDimitry Andric PhiValues &PI = AM.getResult<PhiValuesAnalysis>(F);
1990b57cec5SDimitry Andric for (const BasicBlock &BB : F)
2000b57cec5SDimitry Andric for (const PHINode &PN : BB.phis())
2010b57cec5SDimitry Andric PI.getValuesForPhi(&PN);
2020b57cec5SDimitry Andric PI.print(OS);
2030b57cec5SDimitry Andric return PreservedAnalyses::all();
2040b57cec5SDimitry Andric }
2050b57cec5SDimitry Andric
PhiValuesWrapperPass()2060b57cec5SDimitry Andric PhiValuesWrapperPass::PhiValuesWrapperPass() : FunctionPass(ID) {
2070b57cec5SDimitry Andric initializePhiValuesWrapperPassPass(*PassRegistry::getPassRegistry());
2080b57cec5SDimitry Andric }
2090b57cec5SDimitry Andric
runOnFunction(Function & F)2100b57cec5SDimitry Andric bool PhiValuesWrapperPass::runOnFunction(Function &F) {
2110b57cec5SDimitry Andric Result.reset(new PhiValues(F));
2120b57cec5SDimitry Andric return false;
2130b57cec5SDimitry Andric }
2140b57cec5SDimitry Andric
releaseMemory()2150b57cec5SDimitry Andric void PhiValuesWrapperPass::releaseMemory() {
2160b57cec5SDimitry Andric Result->releaseMemory();
2170b57cec5SDimitry Andric }
2180b57cec5SDimitry Andric
getAnalysisUsage(AnalysisUsage & AU) const2190b57cec5SDimitry Andric void PhiValuesWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
2200b57cec5SDimitry Andric AU.setPreservesAll();
2210b57cec5SDimitry Andric }
2220b57cec5SDimitry Andric
2230b57cec5SDimitry Andric char PhiValuesWrapperPass::ID = 0;
2240b57cec5SDimitry Andric
2250b57cec5SDimitry Andric INITIALIZE_PASS(PhiValuesWrapperPass, "phi-values", "Phi Values Analysis", false,
2260b57cec5SDimitry Andric true)
227