LLVM 24.0.0git
CallGraphUpdater.cpp
Go to the documentation of this file.
1//===- CallGraphUpdater.cpp - A (lazy) call graph update helper -----------===//
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/// \file
9///
10/// This file provides interfaces used to manipulate a call graph, regardless
11/// if it is a "old style" CallGraph or an "new style" LazyCallGraph.
12///
13//===----------------------------------------------------------------------===//
14
16#include "llvm/IR/Constants.h"
18
19using namespace llvm;
20
22 if (!DeadFunctionsInComdats.empty()) {
23 filterDeadComdatFunctions(DeadFunctionsInComdats);
24 for (Function *DeadFn : DeadFunctionsInComdats)
26 DeadFunctions.append(DeadFunctionsInComdats.begin(),
27 DeadFunctionsInComdats.end());
28 }
29
30 // This is the code path for the new lazy call graph and for the case were
31 // no call graph was provided.
32 for (Function *DeadFn : DeadFunctions) {
33 DeadFn->removeDeadConstantUsers();
34 // If the function is used by metadata, we don't want it to be replaced with
35 // poison in the metadata, so we replace it with nullptr in the metadata
36 // before RAUW'ing the non-metadata uses below.
37 if (DeadFn->isUsedByMetadata())
39 DeadFn->replaceNonMetadataUsesWith(PoisonValue::get(DeadFn->getType()));
40
41 if (LCG && !ReplacedFunctions.count(DeadFn)) {
42 // Taken mostly from the inliner:
43 LazyCallGraph::Node &N = LCG->get(*DeadFn);
44 auto *DeadSCC = LCG->lookupSCC(N);
45 assert(DeadSCC && DeadSCC->size() == 1 &&
46 &DeadSCC->begin()->getFunction() == DeadFn);
47
48 FAM->clear(*DeadFn, DeadFn->getName());
49 AM->clear(*DeadSCC, DeadSCC->getName());
50 LCG->markDeadFunction(*DeadFn);
51
52 // Mark the relevant parts of the call graph as invalid so we don't
53 // visit them.
54 UR->InvalidatedSCCs.insert(LCG->lookupSCC(N));
55 UR->DeadFunctions.push_back(DeadFn);
56 } else {
57 // The CGSCC infrastructure batch deletes functions at the end of the
58 // call graph walk, so only erase the function if we're not using that
59 // infrastructure.
60 // The function is now really dead and de-attached from everything.
61 DeadFn->eraseFromParent();
62 }
63 }
64
65 bool Changed = !DeadFunctions.empty();
66 DeadFunctionsInComdats.clear();
67 DeadFunctions.clear();
68 return Changed;
69}
70
72 if (LCG) {
73 LazyCallGraph::Node &N = LCG->get(Fn);
74 LazyCallGraph::SCC *C = LCG->lookupSCC(N);
75 updateCGAndAnalysisManagerForCGSCCPass(*LCG, *C, N, *AM, *UR, *FAM);
76 }
77}
78
80 if (!LCG)
81 return;
82
83 LazyCallGraph::Node &N = LCG->get(Fn);
84 SmallVector<Constant *, 4> Worklist = {&C};
85 SmallPtrSet<Constant *, 4> Visited = {&C};
86 LazyCallGraph::visitReferences(Worklist, Visited, [&](Function &Referee) {
87 LazyCallGraph::Node &RefereeN = LCG->get(Referee);
88 if (!N->lookup(RefereeN))
89 LCG->lookupRefSCC(N)->insertTrivialRefEdge(N, RefereeN);
90 });
91}
92
94 Function &NewFn) {
95 if (LCG)
96 LCG->addSplitFunction(OriginalFn, NewFn);
97}
98
100 // With a LazyCallGraph, the function is erased only at the end of the CGSCC
101 // walk, and its node must stay a definition until then. Functions in a comdat
102 // are only cleared in finalize(), once we know the whole comdat is dead.
103 if (DeadFn.hasComdat()) {
104 DeadFunctionsInComdats.push_back(&DeadFn);
105 } else {
107 DeadFunctions.push_back(&DeadFn);
108 }
109
110 if (FAM)
111 FAM->clear(DeadFn, DeadFn.getName());
112}
113
116 ReplacedFunctions.insert(&OldFn);
117 if (LCG) {
118 // Directly substitute the functions in the call graph.
119 LazyCallGraph::Node &OldLCGN = LCG->get(OldFn);
120 SCC->getOuterRefSCC().replaceNodeFunction(OldLCGN, NewFn);
121 }
122 removeFunction(OldFn);
123}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
This file provides interfaces used to manipulate a call graph, regardless if it is a "old style" Call...
This file contains the declarations for the subclasses of Constant, which represent the different fla...
LLVM_ABI void registerOutlinedFunction(Function &OriginalFn, Function &NewFn)
If a new function was created by outlining, this method can be called to update the call graph for th...
LLVM_ABI void addReferences(Function &Fn, Constant &C)
Add reference edges from Fn to the functions referenced by C, which is newly used in Fn.
LLVM_ABI void removeFunction(Function &Fn)
Remove Fn from the call graph.
LLVM_ABI void replaceFunctionWith(Function &OldFn, Function &NewFn)
Replace OldFn in the call graph (and SCC) with NewFn.
LLVM_ABI void reanalyzeFunction(Function &Fn)
After an CGSCC pass changes a function in ways that affect the call graph, this method can be called ...
This is an important base class in LLVM.
Definition Constant.h:44
LLVM_ABI void removeDeadConstantUsers() const
If there are any dead constant users dangling off of this constant, remove them.
bool hasComdat() const
A node in the call graph.
An SCC of the call graph.
static LLVM_ABI void visitReferences(SmallVectorImpl< Constant * > &Worklist, SmallPtrSetImpl< Constant * > &Visited, function_ref< void(Function &)> Callback)
Recursively visits the defined functions whose address is reachable from every constant in the Workli...
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
static LLVM_ABI void handleDeletion(Value *V)
Definition Metadata.cpp:536
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:314
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI void makeFunctionBodyUnreachable(Function &F)
Replace the body of the dead function F with a single unreachable block.
LLVM_ABI LazyCallGraph::SCC & updateCGAndAnalysisManagerForCGSCCPass(LazyCallGraph &G, LazyCallGraph::SCC &C, LazyCallGraph::Node &N, CGSCCAnalysisManager &AM, CGSCCUpdateResult &UR, FunctionAnalysisManager &FAM)
Helper to update the call graph after running a CGSCC pass.
LLVM_ABI void filterDeadComdatFunctions(SmallVectorImpl< Function * > &DeadComdatFunctions)
Filter out potentially dead comdat functions where other entries keep the entire comdat group alive.
#define N