2016-04-12 14:34:10 +08:00
|
|
|
//==-LTOInternalize.cpp - LLVM Link Time Optimizer Internalization Utility -==//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-04-12 14:34:10 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines a helper to run the internalization part of LTO.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-07-15 05:21:16 +08:00
|
|
|
#include "llvm/LTO/legacy/UpdateCompilerUsed.h"
|
2016-04-12 14:34:10 +08:00
|
|
|
#include "llvm/Analysis/TargetLibraryInfo.h"
|
2017-11-17 09:07:10 +08:00
|
|
|
#include "llvm/CodeGen/TargetLowering.h"
|
|
|
|
#include "llvm/CodeGen/TargetSubtargetInfo.h"
|
2016-04-12 14:34:10 +08:00
|
|
|
#include "llvm/IR/LegacyPassManager.h"
|
|
|
|
#include "llvm/IR/Mangler.h"
|
2016-10-26 07:53:31 +08:00
|
|
|
#include "llvm/Transforms/Utils/ModuleUtils.h"
|
2016-04-12 14:34:10 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
2016-04-13 14:32:46 +08:00
|
|
|
|
|
|
|
// Helper class that collects AsmUsed and user supplied libcalls.
|
|
|
|
class PreserveLibCallsAndAsmUsed {
|
2016-04-12 14:34:10 +08:00
|
|
|
public:
|
2016-04-13 14:32:46 +08:00
|
|
|
PreserveLibCallsAndAsmUsed(const StringSet<> &AsmUndefinedRefs,
|
|
|
|
const TargetMachine &TM,
|
2016-10-26 07:53:31 +08:00
|
|
|
std::vector<GlobalValue *> &LLVMUsed)
|
2016-04-13 14:32:46 +08:00
|
|
|
: AsmUndefinedRefs(AsmUndefinedRefs), TM(TM), LLVMUsed(LLVMUsed) {}
|
|
|
|
|
2016-10-26 07:53:31 +08:00
|
|
|
void findInModule(Module &TheModule) {
|
2016-04-13 14:32:46 +08:00
|
|
|
initializeLibCalls(TheModule);
|
2016-10-26 07:53:31 +08:00
|
|
|
for (Function &F : TheModule)
|
2016-04-13 14:32:46 +08:00
|
|
|
findLibCallsAndAsm(F);
|
2016-10-26 07:53:31 +08:00
|
|
|
for (GlobalVariable &GV : TheModule.globals())
|
2016-04-13 14:32:46 +08:00
|
|
|
findLibCallsAndAsm(GV);
|
2016-10-26 07:53:31 +08:00
|
|
|
for (GlobalAlias &GA : TheModule.aliases())
|
2016-04-13 14:32:46 +08:00
|
|
|
findLibCallsAndAsm(GA);
|
2016-04-12 14:34:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Inputs
|
2016-04-13 07:58:30 +08:00
|
|
|
const StringSet<> &AsmUndefinedRefs;
|
2016-04-12 14:34:10 +08:00
|
|
|
const TargetMachine &TM;
|
|
|
|
|
|
|
|
// Temps
|
2016-04-12 16:23:44 +08:00
|
|
|
llvm::Mangler Mangler;
|
2016-04-13 14:32:04 +08:00
|
|
|
StringSet<> Libcalls;
|
2016-04-12 14:34:10 +08:00
|
|
|
|
|
|
|
// Output
|
2016-10-26 07:53:31 +08:00
|
|
|
std::vector<GlobalValue *> &LLVMUsed;
|
2016-04-12 14:34:10 +08:00
|
|
|
|
|
|
|
// Collect names of runtime library functions. User-defined functions with the
|
|
|
|
// same names are added to llvm.compiler.used to prevent them from being
|
|
|
|
// deleted by optimizations.
|
2016-04-13 14:32:46 +08:00
|
|
|
void initializeLibCalls(const Module &TheModule) {
|
2016-04-12 14:34:10 +08:00
|
|
|
TargetLibraryInfoImpl TLII(Triple(TM.getTargetTriple()));
|
|
|
|
TargetLibraryInfo TLI(TLII);
|
|
|
|
|
|
|
|
// TargetLibraryInfo has info on C runtime library calls on the current
|
|
|
|
// target.
|
|
|
|
for (unsigned I = 0, E = static_cast<unsigned>(LibFunc::NumLibFuncs);
|
|
|
|
I != E; ++I) {
|
[Analysis] Add LibFunc_ prefix to enums in TargetLibraryInfo. (NFC)
Summary:
The LibFunc::Func enum holds enumerators named for libc functions.
Unfortunately, there are real situations, including libc implementations, where
function names are actually macros (musl uses "#define fopen64 fopen", for
example; any other transitively visible macro would have similar effects).
Strictly speaking, a conforming C++ Standard Library should provide any such
macros as functions instead (via <cstdio>). However, there are some "library"
functions which are not part of the standard, and thus not subject to this
rule (fopen64, for example). So, in order to be both portable and consistent,
the enum should not use the bare function names.
The old enum naming used a namespace LibFunc and an enum Func, with bare
enumerators. This patch changes LibFunc to be an enum with enumerators prefixed
with "LibFFunc_". (Unfortunately, a scoped enum is not sufficient to override
macros.)
There are additional changes required in clang.
Reviewers: rsmith
Subscribers: mehdi_amini, mzolotukhin, nemanjai, llvm-commits
Differential Revision: https://reviews.llvm.org/D28476
llvm-svn: 292848
2017-01-24 07:16:46 +08:00
|
|
|
LibFunc F = static_cast<LibFunc>(I);
|
2016-04-12 14:34:10 +08:00
|
|
|
if (TLI.has(F))
|
2016-04-13 14:32:04 +08:00
|
|
|
Libcalls.insert(TLI.getName(F));
|
2016-04-12 14:34:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SmallPtrSet<const TargetLowering *, 1> TLSet;
|
|
|
|
|
|
|
|
for (const Function &F : TheModule) {
|
|
|
|
const TargetLowering *Lowering =
|
|
|
|
TM.getSubtargetImpl(F)->getTargetLowering();
|
|
|
|
|
|
|
|
if (Lowering && TLSet.insert(Lowering).second)
|
|
|
|
// TargetLowering has info on library calls that CodeGen expects to be
|
|
|
|
// available, both from the C runtime and compiler-rt.
|
|
|
|
for (unsigned I = 0, E = static_cast<unsigned>(RTLIB::UNKNOWN_LIBCALL);
|
|
|
|
I != E; ++I)
|
|
|
|
if (const char *Name =
|
|
|
|
Lowering->getLibcallName(static_cast<RTLIB::Libcall>(I)))
|
2016-04-13 14:32:04 +08:00
|
|
|
Libcalls.insert(Name);
|
2016-04-12 14:34:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-26 07:53:31 +08:00
|
|
|
void findLibCallsAndAsm(GlobalValue &GV) {
|
2016-04-12 14:34:10 +08:00
|
|
|
// There are no restrictions to apply to declarations.
|
|
|
|
if (GV.isDeclaration())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// There is nothing more restrictive than private linkage.
|
|
|
|
if (GV.hasPrivateLinkage())
|
|
|
|
return;
|
|
|
|
|
2018-10-11 06:54:31 +08:00
|
|
|
// Conservatively append user-supplied runtime library functions (supplied
|
|
|
|
// either directly, or via a function alias) to llvm.compiler.used. These
|
|
|
|
// could be internalized and deleted by optimizations like -globalopt,
|
|
|
|
// causing problems when later optimizations add new library calls (e.g.,
|
|
|
|
// llvm.memset => memset and printf => puts).
|
2016-04-12 14:34:10 +08:00
|
|
|
// Leave it to the linker to remove any dead code (e.g. with -dead_strip).
|
2018-10-11 06:54:31 +08:00
|
|
|
GlobalValue *FuncAliasee = nullptr;
|
|
|
|
if (isa<GlobalAlias>(GV)) {
|
|
|
|
auto *A = cast<GlobalAlias>(&GV);
|
|
|
|
FuncAliasee = dyn_cast<Function>(A->getAliasee());
|
|
|
|
}
|
|
|
|
if ((isa<Function>(GV) || FuncAliasee) && Libcalls.count(GV.getName())) {
|
2016-10-26 07:53:31 +08:00
|
|
|
LLVMUsed.push_back(&GV);
|
|
|
|
return;
|
|
|
|
}
|
2016-04-13 14:32:46 +08:00
|
|
|
|
|
|
|
SmallString<64> Buffer;
|
|
|
|
TM.getNameWithPrefix(Buffer, &GV, Mangler);
|
|
|
|
if (AsmUndefinedRefs.count(Buffer))
|
2016-10-26 07:53:31 +08:00
|
|
|
LLVMUsed.push_back(&GV);
|
2016-04-12 14:34:10 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace anonymous
|
|
|
|
|
2016-06-23 03:50:42 +08:00
|
|
|
void llvm::updateCompilerUsed(Module &TheModule, const TargetMachine &TM,
|
2016-04-13 14:32:46 +08:00
|
|
|
const StringSet<> &AsmUndefinedRefs) {
|
2016-10-26 07:53:31 +08:00
|
|
|
std::vector<GlobalValue *> UsedValues;
|
2016-04-13 14:32:46 +08:00
|
|
|
PreserveLibCallsAndAsmUsed(AsmUndefinedRefs, TM, UsedValues)
|
|
|
|
.findInModule(TheModule);
|
2016-04-12 14:34:10 +08:00
|
|
|
|
2016-04-13 14:32:46 +08:00
|
|
|
if (UsedValues.empty())
|
|
|
|
return;
|
2016-04-12 14:34:10 +08:00
|
|
|
|
2016-10-26 07:53:31 +08:00
|
|
|
appendToCompilerUsed(TheModule, UsedValues);
|
2016-04-12 14:34:10 +08:00
|
|
|
}
|