xref: /aosp_15_r20/external/llvm/lib/LTO/UpdateCompilerUsed.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //==-LTOInternalize.cpp - LLVM Link Time Optimizer Internalization Utility -==//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file defines a helper to run the internalization part of LTO.
11*9880d681SAndroid Build Coastguard Worker //
12*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
13*9880d681SAndroid Build Coastguard Worker 
14*9880d681SAndroid Build Coastguard Worker #include "llvm/LTO/legacy/UpdateCompilerUsed.h"
15*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/TargetLibraryInfo.h"
16*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/LegacyPassManager.h"
17*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Mangler.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetLowering.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetSubtargetInfo.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/Transforms/IPO/Internalize.h"
21*9880d681SAndroid Build Coastguard Worker 
22*9880d681SAndroid Build Coastguard Worker using namespace llvm;
23*9880d681SAndroid Build Coastguard Worker 
24*9880d681SAndroid Build Coastguard Worker namespace {
25*9880d681SAndroid Build Coastguard Worker 
26*9880d681SAndroid Build Coastguard Worker // Helper class that collects AsmUsed and user supplied libcalls.
27*9880d681SAndroid Build Coastguard Worker class PreserveLibCallsAndAsmUsed {
28*9880d681SAndroid Build Coastguard Worker public:
PreserveLibCallsAndAsmUsed(const StringSet<> & AsmUndefinedRefs,const TargetMachine & TM,SmallPtrSetImpl<const GlobalValue * > & LLVMUsed)29*9880d681SAndroid Build Coastguard Worker   PreserveLibCallsAndAsmUsed(const StringSet<> &AsmUndefinedRefs,
30*9880d681SAndroid Build Coastguard Worker                              const TargetMachine &TM,
31*9880d681SAndroid Build Coastguard Worker                              SmallPtrSetImpl<const GlobalValue *> &LLVMUsed)
32*9880d681SAndroid Build Coastguard Worker       : AsmUndefinedRefs(AsmUndefinedRefs), TM(TM), LLVMUsed(LLVMUsed) {}
33*9880d681SAndroid Build Coastguard Worker 
findInModule(const Module & TheModule)34*9880d681SAndroid Build Coastguard Worker   void findInModule(const Module &TheModule) {
35*9880d681SAndroid Build Coastguard Worker     initializeLibCalls(TheModule);
36*9880d681SAndroid Build Coastguard Worker     for (const Function &F : TheModule)
37*9880d681SAndroid Build Coastguard Worker       findLibCallsAndAsm(F);
38*9880d681SAndroid Build Coastguard Worker     for (const GlobalVariable &GV : TheModule.globals())
39*9880d681SAndroid Build Coastguard Worker       findLibCallsAndAsm(GV);
40*9880d681SAndroid Build Coastguard Worker     for (const GlobalAlias &GA : TheModule.aliases())
41*9880d681SAndroid Build Coastguard Worker       findLibCallsAndAsm(GA);
42*9880d681SAndroid Build Coastguard Worker   }
43*9880d681SAndroid Build Coastguard Worker 
44*9880d681SAndroid Build Coastguard Worker private:
45*9880d681SAndroid Build Coastguard Worker   // Inputs
46*9880d681SAndroid Build Coastguard Worker   const StringSet<> &AsmUndefinedRefs;
47*9880d681SAndroid Build Coastguard Worker   const TargetMachine &TM;
48*9880d681SAndroid Build Coastguard Worker 
49*9880d681SAndroid Build Coastguard Worker   // Temps
50*9880d681SAndroid Build Coastguard Worker   llvm::Mangler Mangler;
51*9880d681SAndroid Build Coastguard Worker   StringSet<> Libcalls;
52*9880d681SAndroid Build Coastguard Worker 
53*9880d681SAndroid Build Coastguard Worker   // Output
54*9880d681SAndroid Build Coastguard Worker   SmallPtrSetImpl<const GlobalValue *> &LLVMUsed;
55*9880d681SAndroid Build Coastguard Worker 
56*9880d681SAndroid Build Coastguard Worker   // Collect names of runtime library functions. User-defined functions with the
57*9880d681SAndroid Build Coastguard Worker   // same names are added to llvm.compiler.used to prevent them from being
58*9880d681SAndroid Build Coastguard Worker   // deleted by optimizations.
initializeLibCalls(const Module & TheModule)59*9880d681SAndroid Build Coastguard Worker   void initializeLibCalls(const Module &TheModule) {
60*9880d681SAndroid Build Coastguard Worker     TargetLibraryInfoImpl TLII(Triple(TM.getTargetTriple()));
61*9880d681SAndroid Build Coastguard Worker     TargetLibraryInfo TLI(TLII);
62*9880d681SAndroid Build Coastguard Worker 
63*9880d681SAndroid Build Coastguard Worker     // TargetLibraryInfo has info on C runtime library calls on the current
64*9880d681SAndroid Build Coastguard Worker     // target.
65*9880d681SAndroid Build Coastguard Worker     for (unsigned I = 0, E = static_cast<unsigned>(LibFunc::NumLibFuncs);
66*9880d681SAndroid Build Coastguard Worker          I != E; ++I) {
67*9880d681SAndroid Build Coastguard Worker       LibFunc::Func F = static_cast<LibFunc::Func>(I);
68*9880d681SAndroid Build Coastguard Worker       if (TLI.has(F))
69*9880d681SAndroid Build Coastguard Worker         Libcalls.insert(TLI.getName(F));
70*9880d681SAndroid Build Coastguard Worker     }
71*9880d681SAndroid Build Coastguard Worker 
72*9880d681SAndroid Build Coastguard Worker     SmallPtrSet<const TargetLowering *, 1> TLSet;
73*9880d681SAndroid Build Coastguard Worker 
74*9880d681SAndroid Build Coastguard Worker     for (const Function &F : TheModule) {
75*9880d681SAndroid Build Coastguard Worker       const TargetLowering *Lowering =
76*9880d681SAndroid Build Coastguard Worker           TM.getSubtargetImpl(F)->getTargetLowering();
77*9880d681SAndroid Build Coastguard Worker 
78*9880d681SAndroid Build Coastguard Worker       if (Lowering && TLSet.insert(Lowering).second)
79*9880d681SAndroid Build Coastguard Worker         // TargetLowering has info on library calls that CodeGen expects to be
80*9880d681SAndroid Build Coastguard Worker         // available, both from the C runtime and compiler-rt.
81*9880d681SAndroid Build Coastguard Worker         for (unsigned I = 0, E = static_cast<unsigned>(RTLIB::UNKNOWN_LIBCALL);
82*9880d681SAndroid Build Coastguard Worker              I != E; ++I)
83*9880d681SAndroid Build Coastguard Worker           if (const char *Name =
84*9880d681SAndroid Build Coastguard Worker                   Lowering->getLibcallName(static_cast<RTLIB::Libcall>(I)))
85*9880d681SAndroid Build Coastguard Worker             Libcalls.insert(Name);
86*9880d681SAndroid Build Coastguard Worker     }
87*9880d681SAndroid Build Coastguard Worker   }
88*9880d681SAndroid Build Coastguard Worker 
findLibCallsAndAsm(const GlobalValue & GV)89*9880d681SAndroid Build Coastguard Worker   void findLibCallsAndAsm(const GlobalValue &GV) {
90*9880d681SAndroid Build Coastguard Worker     // There are no restrictions to apply to declarations.
91*9880d681SAndroid Build Coastguard Worker     if (GV.isDeclaration())
92*9880d681SAndroid Build Coastguard Worker       return;
93*9880d681SAndroid Build Coastguard Worker 
94*9880d681SAndroid Build Coastguard Worker     // There is nothing more restrictive than private linkage.
95*9880d681SAndroid Build Coastguard Worker     if (GV.hasPrivateLinkage())
96*9880d681SAndroid Build Coastguard Worker       return;
97*9880d681SAndroid Build Coastguard Worker 
98*9880d681SAndroid Build Coastguard Worker     // Conservatively append user-supplied runtime library functions to
99*9880d681SAndroid Build Coastguard Worker     // llvm.compiler.used.  These could be internalized and deleted by
100*9880d681SAndroid Build Coastguard Worker     // optimizations like -globalopt, causing problems when later optimizations
101*9880d681SAndroid Build Coastguard Worker     // add new library calls (e.g., llvm.memset => memset and printf => puts).
102*9880d681SAndroid Build Coastguard Worker     // Leave it to the linker to remove any dead code (e.g. with -dead_strip).
103*9880d681SAndroid Build Coastguard Worker     if (isa<Function>(GV) && Libcalls.count(GV.getName()))
104*9880d681SAndroid Build Coastguard Worker       LLVMUsed.insert(&GV);
105*9880d681SAndroid Build Coastguard Worker 
106*9880d681SAndroid Build Coastguard Worker     SmallString<64> Buffer;
107*9880d681SAndroid Build Coastguard Worker     TM.getNameWithPrefix(Buffer, &GV, Mangler);
108*9880d681SAndroid Build Coastguard Worker     if (AsmUndefinedRefs.count(Buffer))
109*9880d681SAndroid Build Coastguard Worker       LLVMUsed.insert(&GV);
110*9880d681SAndroid Build Coastguard Worker   }
111*9880d681SAndroid Build Coastguard Worker };
112*9880d681SAndroid Build Coastguard Worker 
113*9880d681SAndroid Build Coastguard Worker } // namespace anonymous
114*9880d681SAndroid Build Coastguard Worker 
updateCompilerUsed(Module & TheModule,const TargetMachine & TM,const StringSet<> & AsmUndefinedRefs)115*9880d681SAndroid Build Coastguard Worker void llvm::updateCompilerUsed(Module &TheModule, const TargetMachine &TM,
116*9880d681SAndroid Build Coastguard Worker                               const StringSet<> &AsmUndefinedRefs) {
117*9880d681SAndroid Build Coastguard Worker   SmallPtrSet<const GlobalValue *, 8> UsedValues;
118*9880d681SAndroid Build Coastguard Worker   PreserveLibCallsAndAsmUsed(AsmUndefinedRefs, TM, UsedValues)
119*9880d681SAndroid Build Coastguard Worker       .findInModule(TheModule);
120*9880d681SAndroid Build Coastguard Worker 
121*9880d681SAndroid Build Coastguard Worker   if (UsedValues.empty())
122*9880d681SAndroid Build Coastguard Worker     return;
123*9880d681SAndroid Build Coastguard Worker 
124*9880d681SAndroid Build Coastguard Worker   llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(TheModule.getContext());
125*9880d681SAndroid Build Coastguard Worker   std::vector<Constant *> UsedValuesList;
126*9880d681SAndroid Build Coastguard Worker   for (const auto *GV : UsedValues) {
127*9880d681SAndroid Build Coastguard Worker     Constant *c =
128*9880d681SAndroid Build Coastguard Worker         ConstantExpr::getBitCast(const_cast<GlobalValue *>(GV), i8PTy);
129*9880d681SAndroid Build Coastguard Worker     UsedValuesList.push_back(c);
130*9880d681SAndroid Build Coastguard Worker   }
131*9880d681SAndroid Build Coastguard Worker 
132*9880d681SAndroid Build Coastguard Worker   GlobalVariable *LLVMUsed = TheModule.getGlobalVariable("llvm.compiler.used");
133*9880d681SAndroid Build Coastguard Worker   if (LLVMUsed) {
134*9880d681SAndroid Build Coastguard Worker     ConstantArray *Inits = cast<ConstantArray>(LLVMUsed->getInitializer());
135*9880d681SAndroid Build Coastguard Worker     for (auto &V : Inits->operands())
136*9880d681SAndroid Build Coastguard Worker       UsedValuesList.push_back(cast<Constant>(&V));
137*9880d681SAndroid Build Coastguard Worker     LLVMUsed->eraseFromParent();
138*9880d681SAndroid Build Coastguard Worker   }
139*9880d681SAndroid Build Coastguard Worker 
140*9880d681SAndroid Build Coastguard Worker   llvm::ArrayType *ATy = llvm::ArrayType::get(i8PTy, UsedValuesList.size());
141*9880d681SAndroid Build Coastguard Worker   LLVMUsed = new llvm::GlobalVariable(
142*9880d681SAndroid Build Coastguard Worker       TheModule, ATy, false, llvm::GlobalValue::AppendingLinkage,
143*9880d681SAndroid Build Coastguard Worker       llvm::ConstantArray::get(ATy, UsedValuesList), "llvm.compiler.used");
144*9880d681SAndroid Build Coastguard Worker 
145*9880d681SAndroid Build Coastguard Worker   LLVMUsed->setSection("llvm.metadata");
146*9880d681SAndroid Build Coastguard Worker }
147