2018-06-28 16:13:06 +02:00
|
|
|
//===- PhiValues.cpp - Phi Value Analysis ---------------------------------===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-06-28 16:13:06 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Analysis/PhiValues.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
Sink all InitializePasses.h includes
This file lists every pass in LLVM, and is included by Pass.h, which is
very popular. Every time we add, remove, or rename a pass in LLVM, it
caused lots of recompilation.
I found this fact by looking at this table, which is sorted by the
number of times a file was changed over the last 100,000 git commits
multiplied by the number of object files that depend on it in the
current checkout:
recompiles touches affected_files header
342380 95 3604 llvm/include/llvm/ADT/STLExtras.h
314730 234 1345 llvm/include/llvm/InitializePasses.h
307036 118 2602 llvm/include/llvm/ADT/APInt.h
213049 59 3611 llvm/include/llvm/Support/MathExtras.h
170422 47 3626 llvm/include/llvm/Support/Compiler.h
162225 45 3605 llvm/include/llvm/ADT/Optional.h
158319 63 2513 llvm/include/llvm/ADT/Triple.h
140322 39 3598 llvm/include/llvm/ADT/StringRef.h
137647 59 2333 llvm/include/llvm/Support/Error.h
131619 73 1803 llvm/include/llvm/Support/FileSystem.h
Before this change, touching InitializePasses.h would cause 1345 files
to recompile. After this change, touching it only causes 550 compiles in
an incremental rebuild.
Reviewers: bkramer, asbirlea, bollu, jdoerfert
Differential Revision: https://reviews.llvm.org/D70211
2019-11-13 22:15:01 +01:00
|
|
|
#include "llvm/InitializePasses.h"
|
2018-06-28 16:13:06 +02:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2018-08-24 17:48:30 +02:00
|
|
|
void PhiValues::PhiValuesCallbackVH::deleted() {
|
|
|
|
PV->invalidateValue(getValPtr());
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhiValues::PhiValuesCallbackVH::allUsesReplacedWith(Value *) {
|
|
|
|
// We could potentially update the cached values we have with the new value,
|
|
|
|
// but it's simpler to just treat the old value as invalidated.
|
|
|
|
PV->invalidateValue(getValPtr());
|
|
|
|
}
|
|
|
|
|
2018-06-28 16:13:06 +02:00
|
|
|
bool PhiValues::invalidate(Function &, const PreservedAnalyses &PA,
|
|
|
|
FunctionAnalysisManager::Invalidator &) {
|
|
|
|
// PhiValues is invalidated if it isn't preserved.
|
|
|
|
auto PAC = PA.getChecker<PhiValuesAnalysis>();
|
|
|
|
return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>());
|
|
|
|
}
|
|
|
|
|
|
|
|
// The goal here is to find all of the non-phi values reachable from this phi,
|
|
|
|
// and to do the same for all of the phis reachable from this phi, as doing so
|
|
|
|
// is necessary anyway in order to get the values for this phi. We do this using
|
|
|
|
// Tarjan's algorithm with Nuutila's improvements to find the strongly connected
|
|
|
|
// components of the phi graph rooted in this phi:
|
|
|
|
// * All phis in a strongly connected component will have the same reachable
|
|
|
|
// non-phi values. The SCC may not be the maximal subgraph for that set of
|
|
|
|
// reachable values, but finding out that isn't really necessary (it would
|
|
|
|
// only reduce the amount of memory needed to store the values).
|
|
|
|
// * Tarjan's algorithm completes components in a bottom-up manner, i.e. it
|
|
|
|
// never completes a component before the components reachable from it have
|
|
|
|
// been completed. This means that when we complete a component we have
|
|
|
|
// everything we need to collect the values reachable from that component.
|
|
|
|
// * We collect both the non-phi values reachable from each SCC, as that's what
|
|
|
|
// we're ultimately interested in, and all of the reachable values, i.e.
|
|
|
|
// including phis, as that makes invalidateValue easier.
|
|
|
|
void PhiValues::processPhi(const PHINode *Phi,
|
2019-11-05 08:42:23 +01:00
|
|
|
SmallVectorImpl<const PHINode *> &Stack) {
|
2018-06-28 16:13:06 +02:00
|
|
|
// Initialize the phi with the next depth number.
|
|
|
|
assert(DepthMap.lookup(Phi) == 0);
|
|
|
|
assert(NextDepthNumber != UINT_MAX);
|
2019-11-05 08:42:23 +01:00
|
|
|
unsigned int RootDepthNumber = ++NextDepthNumber;
|
|
|
|
DepthMap[Phi] = RootDepthNumber;
|
2018-06-28 16:13:06 +02:00
|
|
|
|
|
|
|
// Recursively process the incoming phis of this phi.
|
2018-08-24 17:48:30 +02:00
|
|
|
TrackedValues.insert(PhiValuesCallbackVH(const_cast<PHINode *>(Phi), this));
|
2018-06-28 16:13:06 +02:00
|
|
|
for (Value *PhiOp : Phi->incoming_values()) {
|
|
|
|
if (PHINode *PhiPhiOp = dyn_cast<PHINode>(PhiOp)) {
|
|
|
|
// Recurse if the phi has not yet been visited.
|
2019-11-05 08:42:23 +01:00
|
|
|
unsigned int OpDepthNumber = DepthMap.lookup(PhiPhiOp);
|
|
|
|
if (OpDepthNumber == 0) {
|
2018-06-28 16:13:06 +02:00
|
|
|
processPhi(PhiPhiOp, Stack);
|
2019-11-05 08:42:23 +01:00
|
|
|
OpDepthNumber = DepthMap.lookup(PhiPhiOp);
|
|
|
|
assert(OpDepthNumber != 0);
|
|
|
|
}
|
2018-06-28 16:13:06 +02:00
|
|
|
// If the phi did not become part of a component then this phi and that
|
|
|
|
// phi are part of the same component, so adjust the depth number.
|
2019-11-05 08:42:23 +01:00
|
|
|
if (!ReachableMap.count(OpDepthNumber))
|
|
|
|
DepthMap[Phi] = std::min(DepthMap[Phi], OpDepthNumber);
|
2018-08-24 17:48:30 +02:00
|
|
|
} else {
|
|
|
|
TrackedValues.insert(PhiValuesCallbackVH(PhiOp, this));
|
2018-06-28 16:13:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that incoming phis have been handled, push this phi to the stack.
|
|
|
|
Stack.push_back(Phi);
|
|
|
|
|
|
|
|
// If the depth number has not changed then we've finished collecting the phis
|
|
|
|
// of a strongly connected component.
|
2019-11-05 08:42:23 +01:00
|
|
|
if (DepthMap[Phi] == RootDepthNumber) {
|
2018-06-28 16:13:06 +02:00
|
|
|
// Collect the reachable values for this component. The phis of this
|
2019-11-05 08:42:23 +01:00
|
|
|
// component will be those on top of the depth stack with the same or
|
2018-06-28 16:13:06 +02:00
|
|
|
// greater depth number.
|
2019-11-05 08:42:23 +01:00
|
|
|
ConstValueSet &Reachable = ReachableMap[RootDepthNumber];
|
|
|
|
while (true) {
|
2018-06-28 16:13:06 +02:00
|
|
|
const PHINode *ComponentPhi = Stack.pop_back_val();
|
|
|
|
Reachable.insert(ComponentPhi);
|
2019-11-05 08:42:23 +01:00
|
|
|
|
2018-06-28 16:13:06 +02:00
|
|
|
for (Value *Op : ComponentPhi->incoming_values()) {
|
|
|
|
if (PHINode *PhiOp = dyn_cast<PHINode>(Op)) {
|
|
|
|
// If this phi is not part of the same component then that component
|
|
|
|
// is guaranteed to have been completed before this one. Therefore we
|
|
|
|
// can just add its reachable values to the reachable values of this
|
|
|
|
// component.
|
2019-11-05 08:42:23 +01:00
|
|
|
unsigned int OpDepthNumber = DepthMap[PhiOp];
|
|
|
|
if (OpDepthNumber != RootDepthNumber) {
|
|
|
|
auto It = ReachableMap.find(OpDepthNumber);
|
|
|
|
if (It != ReachableMap.end())
|
|
|
|
Reachable.insert(It->second.begin(), It->second.end());
|
|
|
|
}
|
|
|
|
} else
|
2018-06-28 16:13:06 +02:00
|
|
|
Reachable.insert(Op);
|
|
|
|
}
|
2019-11-05 08:42:23 +01:00
|
|
|
|
|
|
|
if (Stack.empty())
|
|
|
|
break;
|
|
|
|
|
|
|
|
unsigned int &ComponentDepthNumber = DepthMap[Stack.back()];
|
|
|
|
if (ComponentDepthNumber < RootDepthNumber)
|
|
|
|
break;
|
|
|
|
|
|
|
|
ComponentDepthNumber = RootDepthNumber;
|
2018-06-28 16:13:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Filter out phis to get the non-phi reachable values.
|
2019-11-05 08:42:23 +01:00
|
|
|
ValueSet &NonPhi = NonPhiReachableMap[RootDepthNumber];
|
2018-06-28 16:13:06 +02:00
|
|
|
for (const Value *V : Reachable)
|
|
|
|
if (!isa<PHINode>(V))
|
2019-11-05 08:42:23 +01:00
|
|
|
NonPhi.insert(const_cast<Value *>(V));
|
2018-06-28 16:13:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const PhiValues::ValueSet &PhiValues::getValuesForPhi(const PHINode *PN) {
|
2019-11-05 08:42:23 +01:00
|
|
|
unsigned int DepthNumber = DepthMap.lookup(PN);
|
|
|
|
if (DepthNumber == 0) {
|
2018-06-28 16:13:06 +02:00
|
|
|
SmallVector<const PHINode *, 8> Stack;
|
|
|
|
processPhi(PN, Stack);
|
2019-11-05 08:42:23 +01:00
|
|
|
DepthNumber = DepthMap.lookup(PN);
|
2018-06-28 16:13:06 +02:00
|
|
|
assert(Stack.empty());
|
2019-11-05 08:42:23 +01:00
|
|
|
assert(DepthNumber != 0);
|
2018-06-28 16:13:06 +02:00
|
|
|
}
|
2019-11-05 08:42:23 +01:00
|
|
|
return NonPhiReachableMap[DepthNumber];
|
2018-06-28 16:13:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhiValues::invalidateValue(const Value *V) {
|
|
|
|
// Components that can reach V are invalid.
|
|
|
|
SmallVector<unsigned int, 8> InvalidComponents;
|
|
|
|
for (auto &Pair : ReachableMap)
|
|
|
|
if (Pair.second.count(V))
|
|
|
|
InvalidComponents.push_back(Pair.first);
|
|
|
|
|
|
|
|
for (unsigned int N : InvalidComponents) {
|
|
|
|
for (const Value *V : ReachableMap[N])
|
|
|
|
if (const PHINode *PN = dyn_cast<PHINode>(V))
|
|
|
|
DepthMap.erase(PN);
|
|
|
|
NonPhiReachableMap.erase(N);
|
|
|
|
ReachableMap.erase(N);
|
|
|
|
}
|
2018-08-24 17:48:30 +02:00
|
|
|
// This value is no longer tracked
|
|
|
|
auto It = TrackedValues.find_as(V);
|
|
|
|
if (It != TrackedValues.end())
|
|
|
|
TrackedValues.erase(It);
|
2018-06-28 16:13:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhiValues::releaseMemory() {
|
|
|
|
DepthMap.clear();
|
|
|
|
NonPhiReachableMap.clear();
|
|
|
|
ReachableMap.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhiValues::print(raw_ostream &OS) const {
|
|
|
|
// Iterate through the phi nodes of the function rather than iterating through
|
|
|
|
// DepthMap in order to get predictable ordering.
|
|
|
|
for (const BasicBlock &BB : F) {
|
|
|
|
for (const PHINode &PN : BB.phis()) {
|
|
|
|
OS << "PHI ";
|
|
|
|
PN.printAsOperand(OS, false);
|
|
|
|
OS << " has values:\n";
|
|
|
|
unsigned int N = DepthMap.lookup(&PN);
|
|
|
|
auto It = NonPhiReachableMap.find(N);
|
|
|
|
if (It == NonPhiReachableMap.end())
|
|
|
|
OS << " UNKNOWN\n";
|
|
|
|
else if (It->second.empty())
|
|
|
|
OS << " NONE\n";
|
|
|
|
else
|
|
|
|
for (Value *V : It->second)
|
|
|
|
// Printing of an instruction prints two spaces at the start, so
|
|
|
|
// handle instructions and everything else slightly differently in
|
|
|
|
// order to get consistent indenting.
|
|
|
|
if (Instruction *I = dyn_cast<Instruction>(V))
|
|
|
|
OS << *I << "\n";
|
|
|
|
else
|
|
|
|
OS << " " << *V << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AnalysisKey PhiValuesAnalysis::Key;
|
|
|
|
PhiValues PhiValuesAnalysis::run(Function &F, FunctionAnalysisManager &) {
|
|
|
|
return PhiValues(F);
|
|
|
|
}
|
|
|
|
|
|
|
|
PreservedAnalyses PhiValuesPrinterPass::run(Function &F,
|
|
|
|
FunctionAnalysisManager &AM) {
|
|
|
|
OS << "PHI Values for function: " << F.getName() << "\n";
|
|
|
|
PhiValues &PI = AM.getResult<PhiValuesAnalysis>(F);
|
|
|
|
for (const BasicBlock &BB : F)
|
|
|
|
for (const PHINode &PN : BB.phis())
|
|
|
|
PI.getValuesForPhi(&PN);
|
|
|
|
PI.print(OS);
|
|
|
|
return PreservedAnalyses::all();
|
|
|
|
}
|
|
|
|
|
|
|
|
PhiValuesWrapperPass::PhiValuesWrapperPass() : FunctionPass(ID) {
|
|
|
|
initializePhiValuesWrapperPassPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PhiValuesWrapperPass::runOnFunction(Function &F) {
|
|
|
|
Result.reset(new PhiValues(F));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhiValuesWrapperPass::releaseMemory() {
|
|
|
|
Result->releaseMemory();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhiValuesWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
char PhiValuesWrapperPass::ID = 0;
|
|
|
|
|
|
|
|
INITIALIZE_PASS(PhiValuesWrapperPass, "phi-values", "Phi Values Analysis", false,
|
|
|
|
true)
|