LLVM 20.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 DeadFn->replaceAllUsesWith(PoisonValue::get(DeadFn->getType()));
33
34 if (LCG && !ReplacedFunctions.count(DeadFn)) {
35 // Taken mostly from the inliner:
36 LazyCallGraph::Node &N = LCG->get(*DeadFn);
37 auto *DeadSCC = LCG->lookupSCC(N);
38 assert(DeadSCC && DeadSCC->size() == 1 &&
39 &DeadSCC->begin()->getFunction() == DeadFn);
40
41 FAM->clear(*DeadFn, DeadFn->getName());
42 AM->clear(*DeadSCC, DeadSCC->getName());
43 LCG->markDeadFunction(*DeadFn);
44
45 // Mark the relevant parts of the call graph as invalid so we don't
46 // visit them.
47 UR->InvalidatedSCCs.insert(LCG->lookupSCC(N));
48 UR->DeadFunctions.push_back(DeadFn);
49 } else {
50 // The CGSCC infrastructure batch deletes functions at the end of the
51 // call graph walk, so only erase the function if we're not using that
52 // infrastructure.
53 // The function is now really dead and de-attached from everything.
54 DeadFn->eraseFromParent();
55 }
56 }
57
58 bool Changed = !DeadFunctions.empty();
59 DeadFunctionsInComdats.clear();
60 DeadFunctions.clear();
61 return Changed;
62}
63
65 if (LCG) {
66 LazyCallGraph::Node &N = LCG->get(Fn);
68 updateCGAndAnalysisManagerForCGSCCPass(*LCG, *C, N, *AM, *UR, *FAM);
69 }
70}
71
73 Function &NewFn) {
74 if (LCG)
75 LCG->addSplitFunction(OriginalFn, NewFn);
76}
77
79 DeadFn.deleteBody();
81 if (DeadFn.hasComdat())
82 DeadFunctionsInComdats.push_back(&DeadFn);
83 else
84 DeadFunctions.push_back(&DeadFn);
85
86 if (FAM)
87 FAM->clear(DeadFn, DeadFn.getName());
88}
89
92 ReplacedFunctions.insert(&OldFn);
93 if (LCG) {
94 // Directly substitute the functions in the call graph.
95 LazyCallGraph::Node &OldLCGN = LCG->get(OldFn);
96 SCC->getOuterRefSCC().replaceNodeFunction(OldLCGN, NewFn);
97 }
98 removeFunction(OldFn);
99}
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...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void clear(IRUnitT &IR, llvm::StringRef Name)
Clear any cached analysis results for a single unit of IR.
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...
void removeFunction(Function &Fn)
Remove Fn from the call graph.
void replaceFunctionWith(Function &OldFn, Function &NewFn)
Replace OldFn in the call graph (and SCC) with NewFn.
void reanalyzeFunction(Function &Fn)
After an CGSCC pass changes a function in ways that affect the call graph, this method can be called ...
void removeDeadConstantUsers() const
If there are any dead constant users dangling off of this constant, remove them.
Definition: Constants.cpp:723
void deleteBody()
deleteBody - This method deletes the body of the function, and converts the linkage to external.
Definition: Function.h:722
bool hasComdat() const
Definition: GlobalObject.h:128
void setLinkage(LinkageTypes LT)
Definition: GlobalValue.h:537
@ ExternalLinkage
Externally visible function.
Definition: GlobalValue.h:52
A node in the call graph.
void replaceNodeFunction(Node &N, Function &NewF)
Directly replace a node's function with a new function.
An SCC of the call graph.
RefSCC & getOuterRefSCC() const
void markDeadFunction(Function &F)
Mark a function as dead to be removed later by removeDeadFunctions().
void addSplitFunction(Function &OriginalFunction, Function &NewFunction)
Add a new function split/outlined from an existing function.
Node & get(Function &F)
Get a graph node for a given function, scanning it to populate the graph data as necessary.
SCC * lookupSCC(Node &N) const
Lookup a function's SCC in the graph.
static PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Definition: Constants.cpp:1852
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:309
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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.
void filterDeadComdatFunctions(SmallVectorImpl< Function * > &DeadComdatFunctions)
Filter out potentially dead comdat functions where other entries keep the entire comdat group alive.
#define N
SmallVector< Function *, 4 > & DeadFunctions
Functions that a pass has considered to be dead to be removed at the end of the call graph walk in ba...
SmallPtrSetImpl< LazyCallGraph::SCC * > & InvalidatedSCCs
The set of invalidated SCCs which should be skipped if they are found in CWorklist.