blob: 5165cc96503876f9d955f94cfeeb2140c2f363c5 [file] [log] [blame]
Mehdi Aminice23e972016-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 Collingbourne5c732202016-07-14 21:21:16 +000014#include "llvm/LTO/legacy/UpdateCompilerUsed.h"
Mehdi Aminice23e972016-04-13 06:32:46 +000015#include "llvm/Analysis/TargetLibraryInfo.h"
16#include "llvm/IR/LegacyPassManager.h"
17#include "llvm/IR/Mangler.h"
Mehdi Aminice23e972016-04-13 06:32:46 +000018#include "llvm/Target/TargetLowering.h"
19#include "llvm/Target/TargetSubtargetInfo.h"
20#include "llvm/Transforms/IPO/Internalize.h"
Evgeniy Stepanovea6d49d2016-10-25 23:53:31 +000021#include "llvm/Transforms/Utils/ModuleUtils.h"
Mehdi Aminice23e972016-04-13 06:32:46 +000022
23using namespace llvm;
24
25namespace {
26
27// Helper class that collects AsmUsed and user supplied libcalls.
28class PreserveLibCallsAndAsmUsed {
29public:
30 PreserveLibCallsAndAsmUsed(const StringSet<> &AsmUndefinedRefs,
31 const TargetMachine &TM,
Evgeniy Stepanovea6d49d2016-10-25 23:53:31 +000032 std::vector<GlobalValue *> &LLVMUsed)
Mehdi Aminice23e972016-04-13 06:32:46 +000033 : AsmUndefinedRefs(AsmUndefinedRefs), TM(TM), LLVMUsed(LLVMUsed) {}
34
Evgeniy Stepanovea6d49d2016-10-25 23:53:31 +000035 void findInModule(Module &TheModule) {
Mehdi Aminice23e972016-04-13 06:32:46 +000036 initializeLibCalls(TheModule);
Evgeniy Stepanovea6d49d2016-10-25 23:53:31 +000037 for (Function &F : TheModule)
Mehdi Aminice23e972016-04-13 06:32:46 +000038 findLibCallsAndAsm(F);
Evgeniy Stepanovea6d49d2016-10-25 23:53:31 +000039 for (GlobalVariable &GV : TheModule.globals())
Mehdi Aminice23e972016-04-13 06:32:46 +000040 findLibCallsAndAsm(GV);
Evgeniy Stepanovea6d49d2016-10-25 23:53:31 +000041 for (GlobalAlias &GA : TheModule.aliases())
Mehdi Aminice23e972016-04-13 06:32:46 +000042 findLibCallsAndAsm(GA);
43 }
44
45private:
46 // Inputs
47 const StringSet<> &AsmUndefinedRefs;
48 const TargetMachine &TM;
49
50 // Temps
51 llvm::Mangler Mangler;
52 StringSet<> Libcalls;
53
54 // Output
Evgeniy Stepanovea6d49d2016-10-25 23:53:31 +000055 std::vector<GlobalValue *> &LLVMUsed;
Mehdi Aminice23e972016-04-13 06:32:46 +000056
57 // Collect names of runtime library functions. User-defined functions with the
58 // same names are added to llvm.compiler.used to prevent them from being
59 // deleted by optimizations.
60 void initializeLibCalls(const Module &TheModule) {
61 TargetLibraryInfoImpl TLII(Triple(TM.getTargetTriple()));
62 TargetLibraryInfo TLI(TLII);
63
64 // TargetLibraryInfo has info on C runtime library calls on the current
65 // target.
66 for (unsigned I = 0, E = static_cast<unsigned>(LibFunc::NumLibFuncs);
67 I != E; ++I) {
David L. Jonesd21529f2017-01-23 23:16:46 +000068 LibFunc F = static_cast<LibFunc>(I);
Mehdi Aminice23e972016-04-13 06:32:46 +000069 if (TLI.has(F))
70 Libcalls.insert(TLI.getName(F));
71 }
72
73 SmallPtrSet<const TargetLowering *, 1> TLSet;
74
75 for (const Function &F : TheModule) {
76 const TargetLowering *Lowering =
77 TM.getSubtargetImpl(F)->getTargetLowering();
78
79 if (Lowering && TLSet.insert(Lowering).second)
80 // TargetLowering has info on library calls that CodeGen expects to be
81 // available, both from the C runtime and compiler-rt.
82 for (unsigned I = 0, E = static_cast<unsigned>(RTLIB::UNKNOWN_LIBCALL);
83 I != E; ++I)
84 if (const char *Name =
85 Lowering->getLibcallName(static_cast<RTLIB::Libcall>(I)))
86 Libcalls.insert(Name);
87 }
88 }
89
Evgeniy Stepanovea6d49d2016-10-25 23:53:31 +000090 void findLibCallsAndAsm(GlobalValue &GV) {
Mehdi Aminice23e972016-04-13 06:32:46 +000091 // There are no restrictions to apply to declarations.
92 if (GV.isDeclaration())
93 return;
94
95 // There is nothing more restrictive than private linkage.
96 if (GV.hasPrivateLinkage())
97 return;
98
99 // Conservatively append user-supplied runtime library functions to
100 // llvm.compiler.used. These could be internalized and deleted by
101 // optimizations like -globalopt, causing problems when later optimizations
102 // add new library calls (e.g., llvm.memset => memset and printf => puts).
103 // Leave it to the linker to remove any dead code (e.g. with -dead_strip).
Evgeniy Stepanovea6d49d2016-10-25 23:53:31 +0000104 if (isa<Function>(GV) && Libcalls.count(GV.getName())) {
105 LLVMUsed.push_back(&GV);
106 return;
107 }
Mehdi Aminice23e972016-04-13 06:32:46 +0000108
109 SmallString<64> Buffer;
110 TM.getNameWithPrefix(Buffer, &GV, Mangler);
111 if (AsmUndefinedRefs.count(Buffer))
Evgeniy Stepanovea6d49d2016-10-25 23:53:31 +0000112 LLVMUsed.push_back(&GV);
Mehdi Aminice23e972016-04-13 06:32:46 +0000113 }
114};
115
116} // namespace anonymous
117
Davide Italiano53d457c2016-06-22 19:50:42 +0000118void llvm::updateCompilerUsed(Module &TheModule, const TargetMachine &TM,
Mehdi Aminice23e972016-04-13 06:32:46 +0000119 const StringSet<> &AsmUndefinedRefs) {
Evgeniy Stepanovea6d49d2016-10-25 23:53:31 +0000120 std::vector<GlobalValue *> UsedValues;
Mehdi Aminice23e972016-04-13 06:32:46 +0000121 PreserveLibCallsAndAsmUsed(AsmUndefinedRefs, TM, UsedValues)
122 .findInModule(TheModule);
123
124 if (UsedValues.empty())
125 return;
126
Evgeniy Stepanovea6d49d2016-10-25 23:53:31 +0000127 appendToCompilerUsed(TheModule, UsedValues);
Mehdi Aminice23e972016-04-13 06:32:46 +0000128}