Mehdi Amini | 31ce1f6 | 2016-04-13 06:32:46 +0000 | [diff] [blame] | 1 | //==-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 Collingbourne | ef2acb5 | 2016-07-14 21:21:16 +0000 | [diff] [blame] | 14 | #include "llvm/LTO/legacy/UpdateCompilerUsed.h" |
Mehdi Amini | 31ce1f6 | 2016-04-13 06:32:46 +0000 | [diff] [blame] | 15 | #include "llvm/Analysis/TargetLibraryInfo.h" |
David Blaikie | e3a9b4c | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/TargetLowering.h" |
| 17 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
Mehdi Amini | 31ce1f6 | 2016-04-13 06:32:46 +0000 | [diff] [blame] | 18 | #include "llvm/IR/LegacyPassManager.h" |
| 19 | #include "llvm/IR/Mangler.h" |
Evgeniy Stepanov | 43122e2 | 2016-10-25 23:53:31 +0000 | [diff] [blame] | 20 | #include "llvm/Transforms/Utils/ModuleUtils.h" |
Mehdi Amini | 31ce1f6 | 2016-04-13 06:32:46 +0000 | [diff] [blame] | 21 | |
| 22 | using namespace llvm; |
| 23 | |
| 24 | namespace { |
| 25 | |
| 26 | // Helper class that collects AsmUsed and user supplied libcalls. |
| 27 | class PreserveLibCallsAndAsmUsed { |
| 28 | public: |
| 29 | PreserveLibCallsAndAsmUsed(const StringSet<> &AsmUndefinedRefs, |
| 30 | const TargetMachine &TM, |
Evgeniy Stepanov | 43122e2 | 2016-10-25 23:53:31 +0000 | [diff] [blame] | 31 | std::vector<GlobalValue *> &LLVMUsed) |
Mehdi Amini | 31ce1f6 | 2016-04-13 06:32:46 +0000 | [diff] [blame] | 32 | : AsmUndefinedRefs(AsmUndefinedRefs), TM(TM), LLVMUsed(LLVMUsed) {} |
| 33 | |
Evgeniy Stepanov | 43122e2 | 2016-10-25 23:53:31 +0000 | [diff] [blame] | 34 | void findInModule(Module &TheModule) { |
Mehdi Amini | 31ce1f6 | 2016-04-13 06:32:46 +0000 | [diff] [blame] | 35 | initializeLibCalls(TheModule); |
Evgeniy Stepanov | 43122e2 | 2016-10-25 23:53:31 +0000 | [diff] [blame] | 36 | for (Function &F : TheModule) |
Mehdi Amini | 31ce1f6 | 2016-04-13 06:32:46 +0000 | [diff] [blame] | 37 | findLibCallsAndAsm(F); |
Evgeniy Stepanov | 43122e2 | 2016-10-25 23:53:31 +0000 | [diff] [blame] | 38 | for (GlobalVariable &GV : TheModule.globals()) |
Mehdi Amini | 31ce1f6 | 2016-04-13 06:32:46 +0000 | [diff] [blame] | 39 | findLibCallsAndAsm(GV); |
Evgeniy Stepanov | 43122e2 | 2016-10-25 23:53:31 +0000 | [diff] [blame] | 40 | for (GlobalAlias &GA : TheModule.aliases()) |
Mehdi Amini | 31ce1f6 | 2016-04-13 06:32:46 +0000 | [diff] [blame] | 41 | findLibCallsAndAsm(GA); |
| 42 | } |
| 43 | |
| 44 | private: |
| 45 | // Inputs |
| 46 | const StringSet<> &AsmUndefinedRefs; |
| 47 | const TargetMachine &TM; |
| 48 | |
| 49 | // Temps |
| 50 | llvm::Mangler Mangler; |
| 51 | StringSet<> Libcalls; |
| 52 | |
| 53 | // Output |
Evgeniy Stepanov | 43122e2 | 2016-10-25 23:53:31 +0000 | [diff] [blame] | 54 | std::vector<GlobalValue *> &LLVMUsed; |
Mehdi Amini | 31ce1f6 | 2016-04-13 06:32:46 +0000 | [diff] [blame] | 55 | |
| 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. Jones | 32028c8 | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 67 | LibFunc F = static_cast<LibFunc>(I); |
Mehdi Amini | 31ce1f6 | 2016-04-13 06:32:46 +0000 | [diff] [blame] | 68 | 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 Stepanov | 43122e2 | 2016-10-25 23:53:31 +0000 | [diff] [blame] | 89 | void findLibCallsAndAsm(GlobalValue &GV) { |
Mehdi Amini | 31ce1f6 | 2016-04-13 06:32:46 +0000 | [diff] [blame] | 90 | // 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 Ristow | 7f7ab9f | 2018-10-10 22:54:31 +0000 | [diff] [blame] | 98 | // 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 Amini | 31ce1f6 | 2016-04-13 06:32:46 +0000 | [diff] [blame] | 103 | // Leave it to the linker to remove any dead code (e.g. with -dead_strip). |
Warren Ristow | 7f7ab9f | 2018-10-10 22:54:31 +0000 | [diff] [blame] | 104 | 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 Stepanov | 43122e2 | 2016-10-25 23:53:31 +0000 | [diff] [blame] | 110 | LLVMUsed.push_back(&GV); |
| 111 | return; |
| 112 | } |
Mehdi Amini | 31ce1f6 | 2016-04-13 06:32:46 +0000 | [diff] [blame] | 113 | |
| 114 | SmallString<64> Buffer; |
| 115 | TM.getNameWithPrefix(Buffer, &GV, Mangler); |
| 116 | if (AsmUndefinedRefs.count(Buffer)) |
Evgeniy Stepanov | 43122e2 | 2016-10-25 23:53:31 +0000 | [diff] [blame] | 117 | LLVMUsed.push_back(&GV); |
Mehdi Amini | 31ce1f6 | 2016-04-13 06:32:46 +0000 | [diff] [blame] | 118 | } |
| 119 | }; |
| 120 | |
| 121 | } // namespace anonymous |
| 122 | |
Davide Italiano | e93d6b1 | 2016-06-22 19:50:42 +0000 | [diff] [blame] | 123 | void llvm::updateCompilerUsed(Module &TheModule, const TargetMachine &TM, |
Mehdi Amini | 31ce1f6 | 2016-04-13 06:32:46 +0000 | [diff] [blame] | 124 | const StringSet<> &AsmUndefinedRefs) { |
Evgeniy Stepanov | 43122e2 | 2016-10-25 23:53:31 +0000 | [diff] [blame] | 125 | std::vector<GlobalValue *> UsedValues; |
Mehdi Amini | 31ce1f6 | 2016-04-13 06:32:46 +0000 | [diff] [blame] | 126 | PreserveLibCallsAndAsmUsed(AsmUndefinedRefs, TM, UsedValues) |
| 127 | .findInModule(TheModule); |
| 128 | |
| 129 | if (UsedValues.empty()) |
| 130 | return; |
| 131 | |
Evgeniy Stepanov | 43122e2 | 2016-10-25 23:53:31 +0000 | [diff] [blame] | 132 | appendToCompilerUsed(TheModule, UsedValues); |
Mehdi Amini | 31ce1f6 | 2016-04-13 06:32:46 +0000 | [diff] [blame] | 133 | } |