1 //===-- Internalize.cpp - Mark functions internal -------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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");
33 // APIFile - A file which contains a list of symbols that should not be marked
36 APIFile("internalize-public-api-file", cl::value_desc("filename"),
37 cl::desc("A file containing list of symbol names to preserve"));
39 // APIList - A list of symbols that should not be marked internal.
41 APIList("internalize-public-api-list", cl::value_desc("list"),
42 cl::desc("A list of symbol names to preserve"),
45 class VISIBILITY_HIDDEN InternalizePass : public ModulePass {
46 std::set<std::string> ExternalNames;
49 static char ID; // Pass identification, replacement for typeid
50 explicit InternalizePass(bool InternalizeEverything = true);
51 explicit InternalizePass(const std::vector <const char *>& exportList);
52 void LoadFile(const char *Filename);
53 virtual bool runOnModule(Module &M);
55 char InternalizePass::ID = 0;
56 RegisterPass<InternalizePass> X("internalize", "Internalize Global Symbols");
57 } // end anonymous namespace
59 InternalizePass::InternalizePass(bool InternalizeEverything)
60 : ModulePass((intptr_t)&ID), DontInternalize(false){
61 if (!APIFile.empty()) // If a filename is specified, use it
62 LoadFile(APIFile.c_str());
63 else if (!APIList.empty()) // Else, if a list is specified, use it.
64 ExternalNames.insert(APIList.begin(), APIList.end());
65 else if (!InternalizeEverything)
66 // Finally, if we're allowed to, internalize all but main.
67 DontInternalize = true;
70 InternalizePass::InternalizePass(const std::vector<const char *>&exportList)
71 : ModulePass((intptr_t)&ID), DontInternalize(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 << "'!\n";
83 return; // Do not internalize anything...
89 ExternalNames.insert(Symbol);
93 bool InternalizePass::runOnModule(Module &M) {
94 if (DontInternalize) return false;
96 // If no list or file of symbols was specified, check to see if there is a
97 // "main" symbol defined in the module. If so, use it, otherwise do not
98 // internalize the module, it must be a library or something.
100 if (ExternalNames.empty()) {
101 Function *MainFunc = M.getFunction("main");
102 if (MainFunc == 0 || MainFunc->isDeclaration())
103 return false; // No main found, must be a library...
105 // Preserve main, internalize all else.
106 ExternalNames.insert(MainFunc->getName());
109 bool Changed = false;
111 // Found a main function, mark all functions not named main as internal.
112 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
113 if (!I->isDeclaration() && // Function must be defined here
114 !I->hasInternalLinkage() && // Can't already have internal linkage
115 !ExternalNames.count(I->getName())) {// Not marked to keep external?
116 I->setLinkage(GlobalValue::InternalLinkage);
119 DOUT << "Internalizing func " << I->getName() << "\n";
122 // Never internalize the llvm.used symbol. It is used to implement
123 // attribute((used)).
124 ExternalNames.insert("llvm.used");
126 // Never internalize anchors used by the machine module info, else the info
127 // won't find them. (see MachineModuleInfo.)
128 ExternalNames.insert("llvm.dbg.compile_units");
129 ExternalNames.insert("llvm.dbg.global_variables");
130 ExternalNames.insert("llvm.dbg.subprograms");
131 ExternalNames.insert("llvm.global_ctors");
132 ExternalNames.insert("llvm.global_dtors");
133 ExternalNames.insert("llvm.noinline");
134 ExternalNames.insert("llvm.global.annotations");
136 // Mark all global variables with initializers as internal as well.
137 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
139 if (!I->isDeclaration() && !I->hasInternalLinkage() &&
140 !ExternalNames.count(I->getName())) {
141 I->setLinkage(GlobalValue::InternalLinkage);
144 DOUT << "Internalized gvar " << I->getName() << "\n";
150 ModulePass *llvm::createInternalizePass(bool InternalizeEverything) {
151 return new InternalizePass(InternalizeEverything);
154 ModulePass *llvm::createInternalizePass(const std::vector <const char *> &el) {
155 return new InternalizePass(el);