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 DeadFunctions.append(DeadFunctionsInComdats.begin(),
25 DeadFunctionsInComdats.end());
26 }
27
28 // This is the code path for the new lazy call graph and for the case were
29 // no call graph was provided.
30 for (Function *DeadFn : DeadFunctions) {
31 DeadFn->removeDeadConstantUsers();
32 // If the function is used by metadata, we don't want it to be replaced with
33 // poison in the metadata, so we replace it with nullptr in the metadata
34 // before RAUW'ing the non-metadata uses below.
35 if (DeadFn->isUsedByMetadata())
37 DeadFn->replaceNonMetadataUsesWith(PoisonValue::get(DeadFn->getType()));
38
39 if (LCG && !ReplacedFunctions.count(DeadFn)) {
40 // Taken mostly from the inliner:
41 LazyCallGraph::Node &N = LCG->get(*DeadFn);
42 auto *DeadSCC = LCG->lookupSCC(N);
43 assert(DeadSCC && DeadSCC->size() == 1 &&
44 &DeadSCC->begin()->getFunction() == DeadFn);
45
46 FAM->clear(*DeadFn, DeadFn->getName());
47 AM->clear(*DeadSCC, DeadSCC->getName());
48 LCG->markDeadFunction(*DeadFn);
49
50 // Mark the relevant parts of the call graph as invalid so we don't
51 // visit them.
52 UR->InvalidatedSCCs.insert(LCG->lookupSCC(N));
53 UR->DeadFunctions.push_back(DeadFn);
54 } else {
55 // The CGSCC infrastructure batch deletes functions at the end of the
56 // call graph walk, so only erase the function if we're not using that
57 // infrastructure.
58 // The function is now really dead and de-attached from everything.
59 DeadFn->eraseFromParent();
60 }
61 }
62
63 bool Changed = !DeadFunctions.empty();
64 DeadFunctionsInComdats.clear();
65 DeadFunctions.clear();
66 return Changed;
67}
68
70 if (LCG) {
71 LazyCallGraph::Node &N = LCG->get(Fn);
72 LazyCallGraph::SCC *C = LCG->lookupSCC(N);
73 updateCGAndAnalysisManagerForCGSCCPass(*LCG, *C, N, *AM, *UR, *FAM);
74 }
75}
76
78 if (!LCG)
79 return;
80
81 LazyCallGraph::Node &N = LCG->get(Fn);
82 SmallVector<Constant *, 4> Worklist = {&C};
83 SmallPtrSet<Constant *, 4> Visited = {&C};
84 LazyCallGraph::visitReferences(Worklist, Visited, [&](Function &Referee) {
85 LazyCallGraph::Node &RefereeN = LCG->get(Referee);
86 if (!N->lookup(RefereeN))
87 LCG->lookupRefSCC(N)->insertTrivialRefEdge(N, RefereeN);
88 });
89}
90
92 Function &NewFn) {
93 if (LCG)
94 LCG->addSplitFunction(OriginalFn, NewFn);
95}
96
98 DeadFn.deleteBody();
100 if (DeadFn.hasComdat())
101 DeadFunctionsInComdats.push_back(&DeadFn);
102 else
103 DeadFunctions.push_back(&DeadFn);
104
105 if (FAM)
106 FAM->clear(DeadFn, DeadFn.getName());
107}
108
111 ReplacedFunctions.insert(&OldFn);
112 if (LCG) {
113 // Directly substitute the functions in the call graph.
114 LazyCallGraph::Node &OldLCGN = LCG->get(OldFn);
115 SCC->getOuterRefSCC().replaceNodeFunction(OldLCGN, NewFn);
116 }
117 removeFunction(OldFn);
118}
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.
void deleteBody()
deleteBody - This method deletes the body of the function, and converts the linkage to external.
Definition Function.h:717
bool hasComdat() const
void setLinkage(LinkageTypes LT)
@ ExternalLinkage
Externally visible function.
Definition GlobalValue.h:53
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:538
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:315
This is an optimization pass for GlobalISel generic memory operations.
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