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;
48 static char ID; // Pass identification, replacement for typeid
49 explicit InternalizePass(bool InternalizeEverything = true);
50 explicit InternalizePass(const std::vector <const char *>& exportList);
51 void LoadFile(const char *Filename);
52 virtual bool runOnModule(Module &M);
54 } // end anonymous namespace
56 char InternalizePass::ID = 0;
57 static RegisterPass<InternalizePass>
58 X("internalize", "Internalize Global Symbols");
60 InternalizePass::InternalizePass(bool InternalizeEverything)
61 : ModulePass((intptr_t)&ID), DontInternalize(false){
62 if (!APIFile.empty()) // If a filename is specified, use it
63 LoadFile(APIFile.c_str());
64 else if (!APIList.empty()) // Else, if a list is specified, use it.
65 ExternalNames.insert(APIList.begin(), APIList.end());
66 else if (!InternalizeEverything)
67 // Finally, if we're allowed to, internalize all but main.
68 DontInternalize = true;
71 InternalizePass::InternalizePass(const std::vector<const char *>&exportList)
72 : ModulePass((intptr_t)&ID), DontInternalize(false){
73 for(std::vector<const char *>::const_iterator itr = exportList.begin();
74 itr != exportList.end(); itr++) {
75 ExternalNames.insert(*itr);
79 void InternalizePass::LoadFile(const char *Filename) {
80 // Load the APIFile...
81 std::ifstream In(Filename);
83 cerr << "WARNING: Internalize couldn't load file '" << Filename << "'!\n";
84 return; // Do not internalize anything...
90 ExternalNames.insert(Symbol);
94 bool InternalizePass::runOnModule(Module &M) {
95 if (DontInternalize) return false;
97 // If no list or file of symbols was specified, check to see if there is a
98 // "main" symbol defined in the module. If so, use it, otherwise do not
99 // internalize the module, it must be a library or something.
101 if (ExternalNames.empty()) {
102 Function *MainFunc = M.getFunction("main");
103 if (MainFunc == 0 || MainFunc->isDeclaration())
104 return false; // No main found, must be a library...
106 // Preserve main, internalize all else.
107 ExternalNames.insert(MainFunc->getName());
110 bool Changed = false;
112 // Found a main function, mark all functions not named main as internal.
113 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
114 if (!I->isDeclaration() && // Function must be defined here
115 !I->hasInternalLinkage() && // Can't already have internal linkage
116 !ExternalNames.count(I->getName())) {// Not marked to keep external?
117 I->setLinkage(GlobalValue::InternalLinkage);
120 DOUT << "Internalizing func " << I->getName() << "\n";
123 // Never internalize the llvm.used symbol. It is used to implement
124 // attribute((used)).
125 ExternalNames.insert("llvm.used");
127 // Never internalize anchors used by the machine module info, else the info
128 // won't find them. (see MachineModuleInfo.)
129 ExternalNames.insert("llvm.dbg.compile_units");
130 ExternalNames.insert("llvm.dbg.global_variables");
131 ExternalNames.insert("llvm.dbg.subprograms");
132 ExternalNames.insert("llvm.global_ctors");
133 ExternalNames.insert("llvm.global_dtors");
134 ExternalNames.insert("llvm.noinline");
135 ExternalNames.insert("llvm.global.annotations");
137 // Mark all global variables with initializers as internal as well.
138 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
140 if (!I->isDeclaration() && !I->hasInternalLinkage() &&
141 !ExternalNames.count(I->getName())) {
142 I->setLinkage(GlobalValue::InternalLinkage);
145 DOUT << "Internalized gvar " << I->getName() << "\n";
151 ModulePass *llvm::createInternalizePass(bool InternalizeEverything) {
152 return new InternalizePass(InternalizeEverything);
155 ModulePass *llvm::createInternalizePass(const std::vector <const char *> &el) {
156 return new InternalizePass(el);