2002-04-28 13:43:27 +08:00
|
|
|
//===-- Internalize.cpp - Mark functions internal -------------------------===//
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-04-28 13:43:27 +08:00
|
|
|
//
|
2012-10-27 02:47:48 +08:00
|
|
|
// This pass loops over all of the functions and variables in the input module.
|
|
|
|
// If the function or variable is not in the list of external names given to
|
|
|
|
// the pass it is marked as internal.
|
2002-04-28 13:43:27 +08:00
|
|
|
//
|
2013-11-01 04:51:58 +08:00
|
|
|
// This transformation would not be legal in a regular compilation, but it gets
|
|
|
|
// extra information from the linker about what is safe.
|
2013-10-22 01:14:55 +08:00
|
|
|
//
|
2013-11-01 04:51:58 +08:00
|
|
|
// For example: Internalizing a function with external linkage. Only if we are
|
|
|
|
// told it is only used from within this module, it is safe to do it.
|
2013-10-22 01:14:55 +08:00
|
|
|
//
|
2002-04-28 13:43:27 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-04-13 13:25:08 +08:00
|
|
|
#include "llvm/Transforms/IPO/Internalize.h"
|
2002-07-25 01:12:05 +08:00
|
|
|
#include "llvm/Transforms/IPO.h"
|
2013-07-25 11:23:25 +08:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2016-02-11 07:24:31 +08:00
|
|
|
#include "llvm/ADT/StringSet.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Analysis/CallGraph.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Pass.h"
|
2004-09-02 06:55:40 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
2009-07-25 08:23:56 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2013-10-22 01:14:55 +08:00
|
|
|
#include "llvm/Transforms/Utils/GlobalStatus.h"
|
2013-07-25 11:23:25 +08:00
|
|
|
#include "llvm/Transforms/Utils/ModuleUtils.h"
|
2003-05-23 03:34:49 +08:00
|
|
|
#include <fstream>
|
|
|
|
#include <set>
|
2003-11-22 05:54:22 +08:00
|
|
|
using namespace llvm;
|
2003-11-12 06:41:34 +08:00
|
|
|
|
2014-04-22 10:55:47 +08:00
|
|
|
#define DEBUG_TYPE "internalize"
|
|
|
|
|
2016-04-13 13:25:08 +08:00
|
|
|
STATISTIC(NumAliases, "Number of aliases internalized");
|
2006-12-20 06:09:18 +08:00
|
|
|
STATISTIC(NumFunctions, "Number of functions internalized");
|
2016-04-13 13:25:08 +08:00
|
|
|
STATISTIC(NumGlobals, "Number of global vars internalized");
|
2006-12-20 06:09:18 +08:00
|
|
|
|
2008-05-13 08:00:25 +08:00
|
|
|
// APIFile - A file which contains a list of symbols that should not be marked
|
|
|
|
// external.
|
|
|
|
static cl::opt<std::string>
|
2016-04-13 13:25:08 +08:00
|
|
|
APIFile("internalize-public-api-file", cl::value_desc("filename"),
|
|
|
|
cl::desc("A file containing list of symbol names to preserve"));
|
2003-05-23 03:48:00 +08:00
|
|
|
|
2008-05-13 08:00:25 +08:00
|
|
|
// APIList - A list of symbols that should not be marked internal.
|
|
|
|
static cl::list<std::string>
|
2016-04-13 13:25:08 +08:00
|
|
|
APIList("internalize-public-api-list", cl::value_desc("list"),
|
|
|
|
cl::desc("A list of symbol names to preserve"), cl::CommaSeparated);
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2008-05-13 08:00:25 +08:00
|
|
|
namespace {
|
2016-04-13 13:25:08 +08:00
|
|
|
|
2016-04-13 12:20:32 +08:00
|
|
|
// Helper to load an API list to preserve from file and expose it as a functor
|
2016-04-13 13:25:08 +08:00
|
|
|
// for internalization.
|
2016-04-13 12:20:32 +08:00
|
|
|
class PreserveAPIList {
|
|
|
|
public:
|
|
|
|
PreserveAPIList() {
|
|
|
|
if (!APIFile.empty())
|
|
|
|
LoadFile(APIFile);
|
|
|
|
ExternalNames.insert(APIList.begin(), APIList.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator()(const GlobalValue &GV) {
|
|
|
|
return ExternalNames.count(GV.getName());
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Contains the set of symbols loaded from file
|
2016-04-13 13:25:08 +08:00
|
|
|
StringSet<> ExternalNames;
|
|
|
|
|
|
|
|
void LoadFile(StringRef Filename) {
|
|
|
|
// Load the APIFile...
|
|
|
|
std::ifstream In(Filename.data());
|
|
|
|
if (!In.good()) {
|
|
|
|
errs() << "WARNING: Internalize couldn't load file '" << Filename
|
|
|
|
<< "'! Continuing as if it's empty.\n";
|
|
|
|
return; // Just continue as if the file were empty
|
|
|
|
}
|
|
|
|
while (In) {
|
|
|
|
std::string Symbol;
|
|
|
|
In >> Symbol;
|
|
|
|
if (!Symbol.empty())
|
|
|
|
ExternalNames.insert(Symbol);
|
2016-04-13 12:20:32 +08:00
|
|
|
}
|
2016-04-13 13:25:08 +08:00
|
|
|
}
|
2016-04-13 12:20:32 +08:00
|
|
|
};
|
|
|
|
|
2016-04-13 13:25:08 +08:00
|
|
|
// Internalization exposed as a pass
|
2016-04-13 12:20:32 +08:00
|
|
|
class InternalizePass : public ModulePass {
|
2016-04-13 13:25:08 +08:00
|
|
|
// Client supplied callback to control wheter a symbol must be preserved.
|
2016-04-13 12:20:32 +08:00
|
|
|
std::function<bool(const GlobalValue &)> MustPreserveGV;
|
|
|
|
|
2016-04-13 13:25:08 +08:00
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
|
|
|
|
|
|
|
InternalizePass() : ModulePass(ID), MustPreserveGV(PreserveAPIList()) {}
|
|
|
|
|
|
|
|
InternalizePass(std::function<bool(const GlobalValue &)> MustPreserveGV)
|
|
|
|
: ModulePass(ID), MustPreserveGV(std::move(MustPreserveGV)) {
|
|
|
|
initializeInternalizePassPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool runOnModule(Module &M) override {
|
|
|
|
CallGraphWrapperPass *CGPass =
|
|
|
|
getAnalysisIfAvailable<CallGraphWrapperPass>();
|
|
|
|
CallGraph *CG = CGPass ? &CGPass->getCallGraph() : nullptr;
|
|
|
|
return internalizeModule(M, MustPreserveGV, CG);
|
|
|
|
}
|
|
|
|
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
|
|
|
AU.setPreservesCFG();
|
|
|
|
AU.addPreserved<CallGraphWrapperPass>();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
char InternalizePass::ID = 0;
|
|
|
|
INITIALIZE_PASS(InternalizePass, "internalize", "Internalize Global Symbols",
|
|
|
|
false, false)
|
|
|
|
|
|
|
|
// Helper class to perform internalization.
|
|
|
|
class Internalizer {
|
|
|
|
// Client supplied callback to control wheter a symbol must be preserved.
|
|
|
|
const std::function<bool(const GlobalValue &)> &MustPreserveGV;
|
|
|
|
|
2016-04-13 12:20:32 +08:00
|
|
|
// Set of symbols private to the compiler that this pass should not touch.
|
|
|
|
StringSet<> AlwaysPreserved;
|
|
|
|
|
|
|
|
// Return false if we're allowed to internalize this GV.
|
|
|
|
bool ShouldPreserveGV(const GlobalValue &GV) {
|
|
|
|
// Function must be defined here
|
|
|
|
if (GV.isDeclaration())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Available externally is really just a "declaration with a body".
|
|
|
|
if (GV.hasAvailableExternallyLinkage())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Assume that dllexported symbols are referenced elsewhere
|
|
|
|
if (GV.hasDLLExportStorageClass())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Already local, has nothing to do.
|
|
|
|
if (GV.hasLocalLinkage())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Check some special cases
|
|
|
|
if (AlwaysPreserved.count(GV.getName()))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return MustPreserveGV(GV);
|
|
|
|
}
|
|
|
|
|
2016-04-13 13:25:08 +08:00
|
|
|
bool maybeInternalize(GlobalValue &GV,
|
|
|
|
const std::set<const Comdat *> &ExternalComdats);
|
|
|
|
void checkComdatVisibility(GlobalValue &GV,
|
|
|
|
std::set<const Comdat *> &ExternalComdats);
|
2003-05-23 03:34:49 +08:00
|
|
|
|
2016-04-13 13:25:08 +08:00
|
|
|
public:
|
|
|
|
Internalizer(const std::function<bool(const GlobalValue &)> &MustPreserveGV)
|
|
|
|
: MustPreserveGV(MustPreserveGV) {}
|
|
|
|
|
|
|
|
/// Run the internalizer on \p TheModule, returns true if any changes was
|
|
|
|
/// made.
|
|
|
|
///
|
|
|
|
/// If the CallGraph \p CG is supplied, it will be updated when
|
|
|
|
/// internalizing a function (by removing any edge from the "external node")
|
|
|
|
bool internalizeModule(Module &TheModule, CallGraph *CG = nullptr);
|
|
|
|
};
|
2013-09-05 02:37:36 +08:00
|
|
|
|
2015-07-17 01:42:21 +08:00
|
|
|
// Internalize GV if it is possible to do so, i.e. it is not externally visible
|
|
|
|
// and is not a member of an externally visible comdat.
|
2016-04-13 13:25:08 +08:00
|
|
|
bool Internalizer::maybeInternalize(
|
2015-07-17 01:42:21 +08:00
|
|
|
GlobalValue &GV, const std::set<const Comdat *> &ExternalComdats) {
|
|
|
|
if (Comdat *C = GV.getComdat()) {
|
|
|
|
if (ExternalComdats.count(C))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// If a comdat is not externally visible we can drop it.
|
|
|
|
if (auto GO = dyn_cast<GlobalObject>(&GV))
|
|
|
|
GO->setComdat(nullptr);
|
|
|
|
|
|
|
|
if (GV.hasLocalLinkage())
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
if (GV.hasLocalLinkage())
|
|
|
|
return false;
|
|
|
|
|
2016-04-13 12:20:32 +08:00
|
|
|
if (ShouldPreserveGV(GV))
|
2015-07-17 01:42:21 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
GV.setVisibility(GlobalValue::DefaultVisibility);
|
|
|
|
GV.setLinkage(GlobalValue::InternalLinkage);
|
2013-11-01 04:51:58 +08:00
|
|
|
return true;
|
2013-09-05 02:37:36 +08:00
|
|
|
}
|
|
|
|
|
2015-07-17 01:42:21 +08:00
|
|
|
// If GV is part of a comdat and is externally visible, keep track of its
|
|
|
|
// comdat so that we don't internalize any of its members.
|
2016-04-13 13:25:08 +08:00
|
|
|
void Internalizer::checkComdatVisibility(
|
2015-07-17 01:42:21 +08:00
|
|
|
GlobalValue &GV, std::set<const Comdat *> &ExternalComdats) {
|
|
|
|
Comdat *C = GV.getComdat();
|
|
|
|
if (!C)
|
|
|
|
return;
|
|
|
|
|
2016-04-13 12:20:32 +08:00
|
|
|
if (ShouldPreserveGV(GV))
|
2015-07-17 01:42:21 +08:00
|
|
|
ExternalComdats.insert(C);
|
|
|
|
}
|
|
|
|
|
2016-04-13 13:25:08 +08:00
|
|
|
bool Internalizer::internalizeModule(Module &M, CallGraph *CG) {
|
2016-04-13 13:25:16 +08:00
|
|
|
bool Changed = false;
|
2014-04-25 13:29:35 +08:00
|
|
|
CallGraphNode *ExternalNode = CG ? CG->getExternalCallingNode() : nullptr;
|
2009-01-06 04:38:27 +08:00
|
|
|
|
2013-07-25 11:23:25 +08:00
|
|
|
SmallPtrSet<GlobalValue *, 8> Used;
|
|
|
|
collectUsedGlobalVariables(M, Used, false);
|
|
|
|
|
2015-07-17 01:42:21 +08:00
|
|
|
// Collect comdat visiblity information for the module.
|
|
|
|
std::set<const Comdat *> ExternalComdats;
|
|
|
|
if (!M.getComdatSymbolTable().empty()) {
|
|
|
|
for (Function &F : M)
|
|
|
|
checkComdatVisibility(F, ExternalComdats);
|
|
|
|
for (GlobalVariable &GV : M.globals())
|
|
|
|
checkComdatVisibility(GV, ExternalComdats);
|
|
|
|
for (GlobalAlias &GA : M.aliases())
|
|
|
|
checkComdatVisibility(GA, ExternalComdats);
|
|
|
|
}
|
|
|
|
|
2013-07-25 11:23:25 +08:00
|
|
|
// We must assume that globals in llvm.used have a reference that not even
|
|
|
|
// the linker can see, so we don't internalize them.
|
|
|
|
// For llvm.compiler.used the situation is a bit fuzzy. The assembler and
|
|
|
|
// linker can drop those symbols. If this pass is running as part of LTO,
|
|
|
|
// one might think that it could just drop llvm.compiler.used. The problem
|
|
|
|
// is that even in LTO llvm doesn't see every reference. For example,
|
|
|
|
// we don't see references from function local inline assembly. To be
|
|
|
|
// conservative, we internalize symbols in llvm.compiler.used, but we
|
|
|
|
// keep llvm.compiler.used so that the symbol is not deleted by llvm.
|
2014-08-25 07:23:06 +08:00
|
|
|
for (GlobalValue *V : Used) {
|
2016-04-13 12:20:32 +08:00
|
|
|
AlwaysPreserved.insert(V->getName());
|
2013-07-25 11:23:25 +08:00
|
|
|
}
|
|
|
|
|
2008-05-15 04:01:01 +08:00
|
|
|
// Mark all functions not in the api as internal.
|
2015-10-14 01:51:03 +08:00
|
|
|
for (Function &I : M) {
|
|
|
|
if (!maybeInternalize(I, ExternalComdats))
|
2013-09-05 02:37:36 +08:00
|
|
|
continue;
|
2016-04-13 13:25:16 +08:00
|
|
|
Changed = true;
|
2013-09-05 02:37:36 +08:00
|
|
|
|
|
|
|
if (ExternalNode)
|
|
|
|
// Remove a callgraph edge from the external node to this function.
|
2015-10-14 01:51:03 +08:00
|
|
|
ExternalNode->removeOneAbstractEdgeTo((*CG)[&I]);
|
2013-09-05 02:37:36 +08:00
|
|
|
|
|
|
|
++NumFunctions;
|
2015-10-14 01:51:03 +08:00
|
|
|
DEBUG(dbgs() << "Internalizing func " << I.getName() << "\n");
|
2013-09-05 02:37:36 +08:00
|
|
|
}
|
2009-01-06 04:38:27 +08:00
|
|
|
|
2006-01-04 03:13:17 +08:00
|
|
|
// Never internalize the llvm.used symbol. It is used to implement
|
|
|
|
// attribute((used)).
|
2009-07-20 14:14:25 +08:00
|
|
|
// FIXME: Shouldn't this just filter on llvm.metadata section??
|
2016-04-13 12:20:32 +08:00
|
|
|
AlwaysPreserved.insert("llvm.used");
|
|
|
|
AlwaysPreserved.insert("llvm.compiler.used");
|
2009-01-06 04:38:27 +08:00
|
|
|
|
2007-01-27 05:22:28 +08:00
|
|
|
// Never internalize anchors used by the machine module info, else the info
|
|
|
|
// won't find them. (see MachineModuleInfo.)
|
2016-04-13 12:20:32 +08:00
|
|
|
AlwaysPreserved.insert("llvm.global_ctors");
|
|
|
|
AlwaysPreserved.insert("llvm.global_dtors");
|
|
|
|
AlwaysPreserved.insert("llvm.global.annotations");
|
2009-01-06 04:38:27 +08:00
|
|
|
|
2012-04-13 09:06:27 +08:00
|
|
|
// Never internalize symbols code-gen inserts.
|
2013-08-13 04:09:37 +08:00
|
|
|
// FIXME: We should probably add this (and the __stack_chk_guard) via some
|
|
|
|
// type of call-back in CodeGen.
|
2016-04-13 12:20:32 +08:00
|
|
|
AlwaysPreserved.insert("__stack_chk_fail");
|
|
|
|
AlwaysPreserved.insert("__stack_chk_guard");
|
2012-04-13 09:06:27 +08:00
|
|
|
|
2008-05-15 04:01:01 +08:00
|
|
|
// Mark all global variables with initializers that are not in the api as
|
|
|
|
// internal as well.
|
2016-04-13 13:25:12 +08:00
|
|
|
for (auto &GV : M.globals()) {
|
|
|
|
if (!maybeInternalize(GV, ExternalComdats))
|
2013-09-05 02:37:36 +08:00
|
|
|
continue;
|
2016-04-13 13:25:16 +08:00
|
|
|
Changed = true;
|
2013-09-05 02:37:36 +08:00
|
|
|
|
|
|
|
++NumGlobals;
|
2016-04-13 13:25:12 +08:00
|
|
|
DEBUG(dbgs() << "Internalized gvar " << GV.getName() << "\n");
|
2013-09-05 02:37:36 +08:00
|
|
|
}
|
2009-01-06 04:38:27 +08:00
|
|
|
|
2009-01-06 05:24:45 +08:00
|
|
|
// Mark all aliases that are not in the api as internal as well.
|
2016-04-13 13:25:12 +08:00
|
|
|
for (auto &GA : M.aliases()) {
|
|
|
|
if (!maybeInternalize(GA, ExternalComdats))
|
2013-09-05 02:37:36 +08:00
|
|
|
continue;
|
2016-04-13 13:25:16 +08:00
|
|
|
Changed = true;
|
2013-09-05 02:37:36 +08:00
|
|
|
|
|
|
|
++NumAliases;
|
2016-04-13 13:25:12 +08:00
|
|
|
DEBUG(dbgs() << "Internalized alias " << GA.getName() << "\n");
|
2013-09-05 02:37:36 +08:00
|
|
|
}
|
2009-01-06 05:24:45 +08:00
|
|
|
|
2016-04-13 13:25:16 +08:00
|
|
|
return Changed;
|
2006-01-04 03:13:17 +08:00
|
|
|
}
|
2002-07-24 02:06:35 +08:00
|
|
|
|
2016-04-13 13:25:08 +08:00
|
|
|
/// Public API below
|
|
|
|
|
|
|
|
bool llvm::internalizeModule(
|
|
|
|
Module &TheModule,
|
|
|
|
const std::function<bool(const GlobalValue &)> &MustPreserveGV,
|
|
|
|
CallGraph *CG) {
|
|
|
|
return Internalizer(MustPreserveGV).internalizeModule(TheModule, CG);
|
2006-07-21 01:48:05 +08:00
|
|
|
}
|
2016-02-11 07:24:31 +08:00
|
|
|
|
2016-04-13 13:25:08 +08:00
|
|
|
ModulePass *llvm::createInternalizePass() { return new InternalizePass(); }
|
|
|
|
|
2016-04-13 12:20:32 +08:00
|
|
|
ModulePass *llvm::createInternalizePass(
|
|
|
|
std::function<bool(const GlobalValue &)> MustPreserveGV) {
|
|
|
|
return new InternalizePass(std::move(MustPreserveGV));
|
2016-02-11 07:24:31 +08:00
|
|
|
}
|