blob: 00482dee6e106f3e7d213625156b6d1e57b1f206 [file] [log] [blame]
Mehdi Amini31ce1f62016-04-13 06:32:46 +00001//==-LTOInternalize.cpp - LLVM Link Time Optimizer Internalization Utility -==//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a helper to run the internalization part of LTO.
11//
12//===----------------------------------------------------------------------===//
13
Peter Collingbourneef2acb52016-07-14 21:21:16 +000014#include "llvm/LTO/legacy/UpdateCompilerUsed.h"
Mehdi Amini31ce1f62016-04-13 06:32:46 +000015#include "llvm/Analysis/TargetLibraryInfo.h"
David Blaikiee3a9b4c2017-11-17 01:07:10 +000016#include "llvm/CodeGen/TargetLowering.h"
17#include "llvm/CodeGen/TargetSubtargetInfo.h"
Mehdi Amini31ce1f62016-04-13 06:32:46 +000018#include "llvm/IR/LegacyPassManager.h"
19#include "llvm/IR/Mangler.h"
Evgeniy Stepanov43122e22016-10-25 23:53:31 +000020#include "llvm/Transforms/Utils/ModuleUtils.h"
Mehdi Amini31ce1f62016-04-13 06:32:46 +000021
22using namespace llvm;
23
24namespace {
25
26// Helper class that collects AsmUsed and user supplied libcalls.
27class PreserveLibCallsAndAsmUsed {
28public:
29 PreserveLibCallsAndAsmUsed(const StringSet<> &AsmUndefinedRefs,
30 const TargetMachine &TM,
Evgeniy Stepanov43122e22016-10-25 23:53:31 +000031 std::vector<GlobalValue *> &LLVMUsed)
Mehdi Amini31ce1f62016-04-13 06:32:46 +000032 : AsmUndefinedRefs(AsmUndefinedRefs), TM(TM), LLVMUsed(LLVMUsed) {}
33
Evgeniy Stepanov43122e22016-10-25 23:53:31 +000034 void findInModule(Module &TheModule) {
Mehdi Amini31ce1f62016-04-13 06:32:46 +000035 initializeLibCalls(TheModule);
Evgeniy Stepanov43122e22016-10-25 23:53:31 +000036 for (Function &F : TheModule)
Mehdi Amini31ce1f62016-04-13 06:32:46 +000037 findLibCallsAndAsm(F);
Evgeniy Stepanov43122e22016-10-25 23:53:31 +000038 for (GlobalVariable &GV : TheModule.globals())
Mehdi Amini31ce1f62016-04-13 06:32:46 +000039 findLibCallsAndAsm(GV);
Evgeniy Stepanov43122e22016-10-25 23:53:31 +000040 for (GlobalAlias &GA : TheModule.aliases())
Mehdi Amini31ce1f62016-04-13 06:32:46 +000041 findLibCallsAndAsm(GA);
42 }
43
44private:
45 // Inputs
46 const StringSet<> &AsmUndefinedRefs;
47 const TargetMachine &TM;
48
49 // Temps
50 llvm::Mangler Mangler;
51 StringSet<> Libcalls;
52
53 // Output
Evgeniy Stepanov43122e22016-10-25 23:53:31 +000054 std::vector<GlobalValue *> &LLVMUsed;
Mehdi Amini31ce1f62016-04-13 06:32:46 +000055
56 // Collect names of runtime library functions. User-defined functions with the
57 // same names are added to llvm.compiler.used to prevent them from being
58 // deleted by optimizations.
59 void initializeLibCalls(const Module &TheModule) {
60 TargetLibraryInfoImpl TLII(Triple(TM.getTargetTriple()));
61 TargetLibraryInfo TLI(TLII);
62
63 // TargetLibraryInfo has info on C runtime library calls on the current
64 // target.
65 for (unsigned I = 0, E = static_cast<unsigned>(LibFunc::NumLibFuncs);
66 I != E; ++I) {
David L. Jones32028c82017-01-23 23:16:46 +000067 LibFunc F = static_cast<LibFunc>(I);
Mehdi Amini31ce1f62016-04-13 06:32:46 +000068 if (TLI.has(F))
69 Libcalls.insert(TLI.getName(F));
70 }
71
72 SmallPtrSet<const TargetLowering *, 1> TLSet;
73
74 for (const Function &F : TheModule) {
75 const TargetLowering *Lowering =
76 TM.getSubtargetImpl(F)->getTargetLowering();
77
78 if (Lowering && TLSet.insert(Lowering).second)
79 // TargetLowering has info on library calls that CodeGen expects to be
80 // available, both from the C runtime and compiler-rt.
81 for (unsigned I = 0, E = static_cast<unsigned>(RTLIB::UNKNOWN_LIBCALL);
82 I != E; ++I)
83 if (const char *Name =
84 Lowering->getLibcallName(static_cast<RTLIB::Libcall>(I)))
85 Libcalls.insert(Name);
86 }
87 }
88
Evgeniy Stepanov43122e22016-10-25 23:53:31 +000089 void findLibCallsAndAsm(GlobalValue &GV) {
Mehdi Amini31ce1f62016-04-13 06:32:46 +000090 // There are no restrictions to apply to declarations.
91 if (GV.isDeclaration())
92 return;
93
94 // There is nothing more restrictive than private linkage.
95 if (GV.hasPrivateLinkage())
96 return;
97
Warren Ristow7f7ab9f2018-10-10 22:54:31 +000098 // Conservatively append user-supplied runtime library functions (supplied
99 // either directly, or via a function alias) to llvm.compiler.used. These
100 // could be internalized and deleted by optimizations like -globalopt,
101 // causing problems when later optimizations add new library calls (e.g.,
102 // llvm.memset => memset and printf => puts).
Mehdi Amini31ce1f62016-04-13 06:32:46 +0000103 // Leave it to the linker to remove any dead code (e.g. with -dead_strip).
Warren Ristow7f7ab9f2018-10-10 22:54:31 +0000104 GlobalValue *FuncAliasee = nullptr;
105 if (isa<GlobalAlias>(GV)) {
106 auto *A = cast<GlobalAlias>(&GV);
107 FuncAliasee = dyn_cast<Function>(A->getAliasee());
108 }
109 if ((isa<Function>(GV) || FuncAliasee) && Libcalls.count(GV.getName())) {
Evgeniy Stepanov43122e22016-10-25 23:53:31 +0000110 LLVMUsed.push_back(&GV);
111 return;
112 }
Mehdi Amini31ce1f62016-04-13 06:32:46 +0000113
114 SmallString<64> Buffer;
115 TM.getNameWithPrefix(Buffer, &GV, Mangler);
116 if (AsmUndefinedRefs.count(Buffer))
Evgeniy Stepanov43122e22016-10-25 23:53:31 +0000117 LLVMUsed.push_back(&GV);
Mehdi Amini31ce1f62016-04-13 06:32:46 +0000118 }
119};
120
121} // namespace anonymous
122
Davide Italianoe93d6b12016-06-22 19:50:42 +0000123void llvm::updateCompilerUsed(Module &TheModule, const TargetMachine &TM,
Mehdi Amini31ce1f62016-04-13 06:32:46 +0000124 const StringSet<> &AsmUndefinedRefs) {
Evgeniy Stepanov43122e22016-10-25 23:53:31 +0000125 std::vector<GlobalValue *> UsedValues;
Mehdi Amini31ce1f62016-04-13 06:32:46 +0000126 PreserveLibCallsAndAsmUsed(AsmUndefinedRefs, TM, UsedValues)
127 .findInModule(TheModule);
128
129 if (UsedValues.empty())
130 return;
131
Evgeniy Stepanov43122e22016-10-25 23:53:31 +0000132 appendToCompilerUsed(TheModule, UsedValues);
Mehdi Amini31ce1f62016-04-13 06:32:46 +0000133}