1 //===-- Internalize.cpp - Mark functions internal -------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This pass loops over all of the functions in the input module, looking for a
11 // main function. If a main function is found, all other functions and all
12 // global variables with initializers are marked as internal.
14 //===----------------------------------------------------------------------===//
16 #define DEBUG_TYPE "internalize"
17 #include "llvm/Transforms/IPO.h"
18 #include "llvm/Pass.h"
19 #include "llvm/Module.h"
20 #include "llvm/Support/CommandLine.h"
21 #include "llvm/Support/Compiler.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/ADT/Statistic.h"
28 STATISTIC(NumFunctions, "Number of functions internalized");
29 STATISTIC(NumGlobals , "Number of global vars internalized");
31 // APIFile - A file which contains a list of symbols that should not be marked
33 static cl::opt<std::string>
34 APIFile("internalize-public-api-file", cl::value_desc("filename"),
35 cl::desc("A file containing list of symbol names to preserve"));
37 // APIList - A list of symbols that should not be marked internal.
38 static cl::list<std::string>
39 APIList("internalize-public-api-list", cl::value_desc("list"),
40 cl::desc("A list of symbol names to preserve"),
44 class VISIBILITY_HIDDEN InternalizePass : public ModulePass {
45 std::set<std::string> ExternalNames;
46 /// If no api symbols were specified and a main function is defined,
47 /// assume the main function is the only API
50 static char ID; // Pass identification, replacement for typeid
51 explicit InternalizePass(bool AllButMain = true);
52 explicit InternalizePass(const std::vector <const char *>& exportList);
53 void LoadFile(const char *Filename);
54 virtual bool runOnModule(Module &M);
56 } // end anonymous namespace
58 char InternalizePass::ID = 0;
59 static RegisterPass<InternalizePass>
60 X("internalize", "Internalize Global Symbols");
62 InternalizePass::InternalizePass(bool AllButMain)
63 : ModulePass((intptr_t)&ID), AllButMain(AllButMain){
64 if (!APIFile.empty()) // If a filename is specified, use it.
65 LoadFile(APIFile.c_str());
66 if (!APIList.empty()) // If a list is specified, use it as well.
67 ExternalNames.insert(APIList.begin(), APIList.end());
70 InternalizePass::InternalizePass(const std::vector<const char *>&exportList)
71 : ModulePass((intptr_t)&ID), AllButMain(false){
72 for(std::vector<const char *>::const_iterator itr = exportList.begin();
73 itr != exportList.end(); itr++) {
74 ExternalNames.insert(*itr);
78 void InternalizePass::LoadFile(const char *Filename) {
79 // Load the APIFile...
80 std::ifstream In(Filename);
82 cerr << "WARNING: Internalize couldn't load file '" << Filename
83 << "'! Continuing as if it's empty.\n";
84 return; // Just continue as if the file were empty
90 ExternalNames.insert(Symbol);
94 bool InternalizePass::runOnModule(Module &M) {
95 if (ExternalNames.empty()) {
96 // Return if we're not in 'all but main' mode and have no external api
99 // If no list or file of symbols was specified, check to see if there is a
100 // "main" symbol defined in the module. If so, use it, otherwise do not
101 // internalize the module, it must be a library or something.
103 Function *MainFunc = M.getFunction("main");
104 if (MainFunc == 0 || MainFunc->isDeclaration())
105 return false; // No main found, must be a library...
107 // Preserve main, internalize all else.
108 ExternalNames.insert(MainFunc->getName());
111 bool Changed = false;
113 // Mark all functions not in the api as internal.
114 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
115 if (!I->isDeclaration() && // Function must be defined here
116 !I->hasInternalLinkage() && // Can't already have internal linkage
117 !ExternalNames.count(I->getName())) {// Not marked to keep external?
118 I->setLinkage(GlobalValue::InternalLinkage);
121 DOUT << "Internalizing func " << I->getName() << "\n";
124 // Never internalize the llvm.used symbol. It is used to implement
125 // attribute((used)).
126 ExternalNames.insert("llvm.used");
128 // Never internalize anchors used by the machine module info, else the info
129 // won't find them. (see MachineModuleInfo.)
130 ExternalNames.insert("llvm.dbg.compile_units");
131 ExternalNames.insert("llvm.dbg.global_variables");
132 ExternalNames.insert("llvm.dbg.subprograms");
133 ExternalNames.insert("llvm.global_ctors");
134 ExternalNames.insert("llvm.global_dtors");
135 ExternalNames.insert("llvm.noinline");
136 ExternalNames.insert("llvm.global.annotations");
138 // Mark all global variables with initializers that are not in the api as
140 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
142 if (!I->isDeclaration() && !I->hasInternalLinkage() &&
143 !ExternalNames.count(I->getName())) {
144 I->setLinkage(GlobalValue::InternalLinkage);
147 DOUT << "Internalized gvar " << I->getName() << "\n";
153 ModulePass *llvm::createInternalizePass(bool AllButMain) {
154 return new InternalizePass(AllButMain);
157 ModulePass *llvm::createInternalizePass(const std::vector <const char *> &el) {
158 return new InternalizePass(el);