31#define DEBUG_TYPE "elim-avail-extern"
35 cl::desc(
"Convert available_externally into locals, renaming them "
36 "to avoid link-time clashes."));
43 "avail-extern-gv-in-addrspace-to-local",
cl::Hidden,
45 "Convert available_externally global variables into locals if they are "
46 "in specificed addrspace, renaming them to avoid link-time clashes."));
48STATISTIC(NumRemovals,
"Number of functions removed");
49STATISTIC(NumFunctionsConverted,
"Number of functions converted");
50STATISTIC(NumGlobalVariablesConverted,
"Number of global variables converted");
51STATISTIC(NumVariables,
"Number of global variables removed");
72 assert(
F.hasAvailableExternallyLinkage());
76 return isa<CallBase>(U.getUser());
80 auto OrigName =
F.getName().str();
88 if (
auto *SP =
F.getSubprogram())
89 SP->replaceLinkageName(
MDString::get(
F.getParent()->getContext(), NewName));
100 F.getAddressSpace(), OrigName,
F.getParent());
101 F.replaceUsesWithIf(Decl,
102 [&](
Use &U) {
return !isa<CallBase>(U.getUser()); });
103 ++NumFunctionsConverted;
112 ++NumGlobalVariablesConverted;
116 bool Changed =
false;
121 if (!GV.hasAvailableExternallyLinkage())
130 if (GV.hasInitializer()) {
132 GV.setInitializer(
nullptr);
134 Init->destroyConstant();
136 GV.removeDeadConstantUsers();
144 if (
F.isDeclaration() || !
F.hasAvailableExternallyLinkage())
152 F.removeDeadConstantUsers();
169 M, (CtxProf && CtxProf->isInSpecializedModule())))
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static std::string getNewName(Module &M, const GlobalValue &GV)
static void convertToLocalCopy(Module &M, Function &F)
Create a copy of the thinlto import, mark it local, and redirect direct calls to the copy.
static cl::opt< bool > ConvertToLocal("avail-extern-to-local", cl::Hidden, cl::desc("Convert available_externally into locals, renaming them " "to avoid link-time clashes."))
void deleteFunction(Function &F)
static cl::opt< unsigned > ConvertGlobalVariableInAddrSpace("avail-extern-gv-in-addrspace-to-local", cl::Hidden, cl::desc("Convert available_externally global variables into locals if they are " "in specificed addrspace, renaming them to avoid link-time clashes."))
static bool eliminateAvailableExternally(Module &M, bool Convert)
Module.h This file contains the declarations for the Module class.
ModuleAnalysisManager MAM
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
A container for analyses that lazily runs them and caches their results.
PassT::Result * getCachedResult(IRUnitT &IR) const
Get the cached result of an analysis pass for a given IR unit.
This is an important base class in LLVM.
PreservedAnalyses run(Module &M, ModuleAnalysisManager &)
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
void setLinkage(LinkageTypes LT)
bool hasAvailableExternallyLinkage() const
@ InternalLinkage
Rename collisions when linking (static functions).
@ ExternalLinkage
Externally visible function.
static LLVM_ABI MDString * get(LLVMContext &Context, StringRef Str)
A Module instance is used to store all the information related to an LLVM module.
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
std::string str() const
str - Get the contents as an std::string.
A Use represents the edge between a Value definition and its users.
LLVM_ABI void setName(const Twine &Name)
Change the name of the value.
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
This is an optimization pass for GlobalISel generic memory operations.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
bool isSafeToDestroyConstant(const Constant *C)
It is safe to destroy a constant iff it is only used by constants itself.
LLVM_ABI std::string getUniqueModuleId(Module *M)
Produce a unique identifier for this module by taking the MD5 sum of the names of the module's strong...
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.