blob: 6434f902088d1fd45240e201658398e778b85086 [file] [log] [blame]
Mehdi Aminice23e972016-04-13 06:32:46 +00001//==-LTOInternalize.cpp - LLVM Link Time Optimizer Internalization Utility -==//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Mehdi Aminice23e972016-04-13 06:32:46 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file defines a helper to run the internalization part of LTO.
10//
11//===----------------------------------------------------------------------===//
12
Peter Collingbourne5c732202016-07-14 21:21:16 +000013#include "llvm/LTO/legacy/UpdateCompilerUsed.h"
Mehdi Aminice23e972016-04-13 06:32:46 +000014#include "llvm/Analysis/TargetLibraryInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000015#include "llvm/CodeGen/TargetLowering.h"
16#include "llvm/CodeGen/TargetSubtargetInfo.h"
Mehdi Aminice23e972016-04-13 06:32:46 +000017#include "llvm/IR/LegacyPassManager.h"
18#include "llvm/IR/Mangler.h"
Evgeniy Stepanovea6d49d2016-10-25 23:53:31 +000019#include "llvm/Transforms/Utils/ModuleUtils.h"
Mehdi Aminice23e972016-04-13 06:32:46 +000020
21using namespace llvm;
22
23namespace {
24
25// Helper class that collects AsmUsed and user supplied libcalls.
26class PreserveLibCallsAndAsmUsed {
27public:
28 PreserveLibCallsAndAsmUsed(const StringSet<> &AsmUndefinedRefs,
29 const TargetMachine &TM,
Evgeniy Stepanovea6d49d2016-10-25 23:53:31 +000030 std::vector<GlobalValue *> &LLVMUsed)
Mehdi Aminice23e972016-04-13 06:32:46 +000031 : AsmUndefinedRefs(AsmUndefinedRefs), TM(TM), LLVMUsed(LLVMUsed) {}
32
Evgeniy Stepanovea6d49d2016-10-25 23:53:31 +000033 void findInModule(Module &TheModule) {
Mehdi Aminice23e972016-04-13 06:32:46 +000034 initializeLibCalls(TheModule);
Evgeniy Stepanovea6d49d2016-10-25 23:53:31 +000035 for (Function &F : TheModule)
Mehdi Aminice23e972016-04-13 06:32:46 +000036 findLibCallsAndAsm(F);
Evgeniy Stepanovea6d49d2016-10-25 23:53:31 +000037 for (GlobalVariable &GV : TheModule.globals())
Mehdi Aminice23e972016-04-13 06:32:46 +000038 findLibCallsAndAsm(GV);
Evgeniy Stepanovea6d49d2016-10-25 23:53:31 +000039 for (GlobalAlias &GA : TheModule.aliases())
Mehdi Aminice23e972016-04-13 06:32:46 +000040 findLibCallsAndAsm(GA);
41 }
42
43private:
44 // Inputs
45 const StringSet<> &AsmUndefinedRefs;
46 const TargetMachine &TM;
47
48 // Temps
49 llvm::Mangler Mangler;
50 StringSet<> Libcalls;
51
52 // Output
Evgeniy Stepanovea6d49d2016-10-25 23:53:31 +000053 std::vector<GlobalValue *> &LLVMUsed;
Mehdi Aminice23e972016-04-13 06:32:46 +000054
55 // Collect names of runtime library functions. User-defined functions with the
56 // same names are added to llvm.compiler.used to prevent them from being
57 // deleted by optimizations.
58 void initializeLibCalls(const Module &TheModule) {
59 TargetLibraryInfoImpl TLII(Triple(TM.getTargetTriple()));
60 TargetLibraryInfo TLI(TLII);
61
62 // TargetLibraryInfo has info on C runtime library calls on the current
63 // target.
64 for (unsigned I = 0, E = static_cast<unsigned>(LibFunc::NumLibFuncs);
65 I != E; ++I) {
David L. Jonesd21529f2017-01-23 23:16:46 +000066 LibFunc F = static_cast<LibFunc>(I);
Mehdi Aminice23e972016-04-13 06:32:46 +000067 if (TLI.has(F))
68 Libcalls.insert(TLI.getName(F));
69 }
70
71 SmallPtrSet<const TargetLowering *, 1> TLSet;
72
73 for (const Function &F : TheModule) {
74 const TargetLowering *Lowering =
75 TM.getSubtargetImpl(F)->getTargetLowering();
76
77 if (Lowering && TLSet.insert(Lowering).second)
78 // TargetLowering has info on library calls that CodeGen expects to be
79 // available, both from the C runtime and compiler-rt.
80 for (unsigned I = 0, E = static_cast<unsigned>(RTLIB::UNKNOWN_LIBCALL);
81 I != E; ++I)
82 if (const char *Name =
83 Lowering->getLibcallName(static_cast<RTLIB::Libcall>(I)))
84 Libcalls.insert(Name);
85 }
86 }
87
Evgeniy Stepanovea6d49d2016-10-25 23:53:31 +000088 void findLibCallsAndAsm(GlobalValue &GV) {
Mehdi Aminice23e972016-04-13 06:32:46 +000089 // There are no restrictions to apply to declarations.
90 if (GV.isDeclaration())
91 return;
92
93 // There is nothing more restrictive than private linkage.
94 if (GV.hasPrivateLinkage())
95 return;
96
Warren Ristowfebfc4e2018-10-10 22:54:31 +000097 // Conservatively append user-supplied runtime library functions (supplied
98 // either directly, or via a function alias) to llvm.compiler.used. These
99 // could be internalized and deleted by optimizations like -globalopt,
100 // causing problems when later optimizations add new library calls (e.g.,
101 // llvm.memset => memset and printf => puts).
Mehdi Aminice23e972016-04-13 06:32:46 +0000102 // Leave it to the linker to remove any dead code (e.g. with -dead_strip).
Warren Ristowfebfc4e2018-10-10 22:54:31 +0000103 GlobalValue *FuncAliasee = nullptr;
104 if (isa<GlobalAlias>(GV)) {
105 auto *A = cast<GlobalAlias>(&GV);
106 FuncAliasee = dyn_cast<Function>(A->getAliasee());
107 }
108 if ((isa<Function>(GV) || FuncAliasee) && Libcalls.count(GV.getName())) {
Evgeniy Stepanovea6d49d2016-10-25 23:53:31 +0000109 LLVMUsed.push_back(&GV);
110 return;
111 }
Mehdi Aminice23e972016-04-13 06:32:46 +0000112
113 SmallString<64> Buffer;
114 TM.getNameWithPrefix(Buffer, &GV, Mangler);
115 if (AsmUndefinedRefs.count(Buffer))
Evgeniy Stepanovea6d49d2016-10-25 23:53:31 +0000116 LLVMUsed.push_back(&GV);
Mehdi Aminice23e972016-04-13 06:32:46 +0000117 }
118};
119
120} // namespace anonymous
121
Davide Italiano53d457c2016-06-22 19:50:42 +0000122void llvm::updateCompilerUsed(Module &TheModule, const TargetMachine &TM,
Mehdi Aminice23e972016-04-13 06:32:46 +0000123 const StringSet<> &AsmUndefinedRefs) {
Evgeniy Stepanovea6d49d2016-10-25 23:53:31 +0000124 std::vector<GlobalValue *> UsedValues;
Mehdi Aminice23e972016-04-13 06:32:46 +0000125 PreserveLibCallsAndAsmUsed(AsmUndefinedRefs, TM, UsedValues)
126 .findInModule(TheModule);
127
128 if (UsedValues.empty())
129 return;
130
Evgeniy Stepanovea6d49d2016-10-25 23:53:31 +0000131 appendToCompilerUsed(TheModule, UsedValues);
Mehdi Aminice23e972016-04-13 06:32:46 +0000132}