Mehdi Amini | ce23e97 | 2016-04-13 06:32:46 +0000 | [diff] [blame] | 1 | //==-LTOInternalize.cpp - LLVM Link Time Optimizer Internalization Utility -==// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame^] | 3 | // 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 Amini | ce23e97 | 2016-04-13 06:32:46 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file defines a helper to run the internalization part of LTO. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Peter Collingbourne | 5c73220 | 2016-07-14 21:21:16 +0000 | [diff] [blame] | 13 | #include "llvm/LTO/legacy/UpdateCompilerUsed.h" |
Mehdi Amini | ce23e97 | 2016-04-13 06:32:46 +0000 | [diff] [blame] | 14 | #include "llvm/Analysis/TargetLibraryInfo.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/TargetLowering.h" |
| 16 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
Mehdi Amini | ce23e97 | 2016-04-13 06:32:46 +0000 | [diff] [blame] | 17 | #include "llvm/IR/LegacyPassManager.h" |
| 18 | #include "llvm/IR/Mangler.h" |
Evgeniy Stepanov | ea6d49d | 2016-10-25 23:53:31 +0000 | [diff] [blame] | 19 | #include "llvm/Transforms/Utils/ModuleUtils.h" |
Mehdi Amini | ce23e97 | 2016-04-13 06:32:46 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace llvm; |
| 22 | |
| 23 | namespace { |
| 24 | |
| 25 | // Helper class that collects AsmUsed and user supplied libcalls. |
| 26 | class PreserveLibCallsAndAsmUsed { |
| 27 | public: |
| 28 | PreserveLibCallsAndAsmUsed(const StringSet<> &AsmUndefinedRefs, |
| 29 | const TargetMachine &TM, |
Evgeniy Stepanov | ea6d49d | 2016-10-25 23:53:31 +0000 | [diff] [blame] | 30 | std::vector<GlobalValue *> &LLVMUsed) |
Mehdi Amini | ce23e97 | 2016-04-13 06:32:46 +0000 | [diff] [blame] | 31 | : AsmUndefinedRefs(AsmUndefinedRefs), TM(TM), LLVMUsed(LLVMUsed) {} |
| 32 | |
Evgeniy Stepanov | ea6d49d | 2016-10-25 23:53:31 +0000 | [diff] [blame] | 33 | void findInModule(Module &TheModule) { |
Mehdi Amini | ce23e97 | 2016-04-13 06:32:46 +0000 | [diff] [blame] | 34 | initializeLibCalls(TheModule); |
Evgeniy Stepanov | ea6d49d | 2016-10-25 23:53:31 +0000 | [diff] [blame] | 35 | for (Function &F : TheModule) |
Mehdi Amini | ce23e97 | 2016-04-13 06:32:46 +0000 | [diff] [blame] | 36 | findLibCallsAndAsm(F); |
Evgeniy Stepanov | ea6d49d | 2016-10-25 23:53:31 +0000 | [diff] [blame] | 37 | for (GlobalVariable &GV : TheModule.globals()) |
Mehdi Amini | ce23e97 | 2016-04-13 06:32:46 +0000 | [diff] [blame] | 38 | findLibCallsAndAsm(GV); |
Evgeniy Stepanov | ea6d49d | 2016-10-25 23:53:31 +0000 | [diff] [blame] | 39 | for (GlobalAlias &GA : TheModule.aliases()) |
Mehdi Amini | ce23e97 | 2016-04-13 06:32:46 +0000 | [diff] [blame] | 40 | findLibCallsAndAsm(GA); |
| 41 | } |
| 42 | |
| 43 | private: |
| 44 | // Inputs |
| 45 | const StringSet<> &AsmUndefinedRefs; |
| 46 | const TargetMachine &TM; |
| 47 | |
| 48 | // Temps |
| 49 | llvm::Mangler Mangler; |
| 50 | StringSet<> Libcalls; |
| 51 | |
| 52 | // Output |
Evgeniy Stepanov | ea6d49d | 2016-10-25 23:53:31 +0000 | [diff] [blame] | 53 | std::vector<GlobalValue *> &LLVMUsed; |
Mehdi Amini | ce23e97 | 2016-04-13 06:32:46 +0000 | [diff] [blame] | 54 | |
| 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. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 66 | LibFunc F = static_cast<LibFunc>(I); |
Mehdi Amini | ce23e97 | 2016-04-13 06:32:46 +0000 | [diff] [blame] | 67 | 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 Stepanov | ea6d49d | 2016-10-25 23:53:31 +0000 | [diff] [blame] | 88 | void findLibCallsAndAsm(GlobalValue &GV) { |
Mehdi Amini | ce23e97 | 2016-04-13 06:32:46 +0000 | [diff] [blame] | 89 | // 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 Ristow | febfc4e | 2018-10-10 22:54:31 +0000 | [diff] [blame] | 97 | // 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 Amini | ce23e97 | 2016-04-13 06:32:46 +0000 | [diff] [blame] | 102 | // Leave it to the linker to remove any dead code (e.g. with -dead_strip). |
Warren Ristow | febfc4e | 2018-10-10 22:54:31 +0000 | [diff] [blame] | 103 | 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 Stepanov | ea6d49d | 2016-10-25 23:53:31 +0000 | [diff] [blame] | 109 | LLVMUsed.push_back(&GV); |
| 110 | return; |
| 111 | } |
Mehdi Amini | ce23e97 | 2016-04-13 06:32:46 +0000 | [diff] [blame] | 112 | |
| 113 | SmallString<64> Buffer; |
| 114 | TM.getNameWithPrefix(Buffer, &GV, Mangler); |
| 115 | if (AsmUndefinedRefs.count(Buffer)) |
Evgeniy Stepanov | ea6d49d | 2016-10-25 23:53:31 +0000 | [diff] [blame] | 116 | LLVMUsed.push_back(&GV); |
Mehdi Amini | ce23e97 | 2016-04-13 06:32:46 +0000 | [diff] [blame] | 117 | } |
| 118 | }; |
| 119 | |
| 120 | } // namespace anonymous |
| 121 | |
Davide Italiano | 53d457c | 2016-06-22 19:50:42 +0000 | [diff] [blame] | 122 | void llvm::updateCompilerUsed(Module &TheModule, const TargetMachine &TM, |
Mehdi Amini | ce23e97 | 2016-04-13 06:32:46 +0000 | [diff] [blame] | 123 | const StringSet<> &AsmUndefinedRefs) { |
Evgeniy Stepanov | ea6d49d | 2016-10-25 23:53:31 +0000 | [diff] [blame] | 124 | std::vector<GlobalValue *> UsedValues; |
Mehdi Amini | ce23e97 | 2016-04-13 06:32:46 +0000 | [diff] [blame] | 125 | PreserveLibCallsAndAsmUsed(AsmUndefinedRefs, TM, UsedValues) |
| 126 | .findInModule(TheModule); |
| 127 | |
| 128 | if (UsedValues.empty()) |
| 129 | return; |
| 130 | |
Evgeniy Stepanov | ea6d49d | 2016-10-25 23:53:31 +0000 | [diff] [blame] | 131 | appendToCompilerUsed(TheModule, UsedValues); |
Mehdi Amini | ce23e97 | 2016-04-13 06:32:46 +0000 | [diff] [blame] | 132 | } |