1 //===-- Internalize.cpp - Mark functions internal -------------------------===//
3 // This pass loops over all of the functions in the input module, looking for a
4 // main function. If a main function is found, all other functions and all
5 // global variables with initializers are marked as internal.
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Transforms/IPO.h"
10 #include "llvm/Pass.h"
11 #include "llvm/Module.h"
12 #include "Support/Statistic.h"
13 #include "Support/CommandLine.h"
18 Statistic<> NumFunctions("internalize", "Number of functions internalized");
19 Statistic<> NumGlobals ("internalize", "Number of global vars internalized");
21 // APIFile - A file which contains a list of symbols that should not be marked
24 APIFile("internalize-public-api-file", cl::value_desc("filename"),
25 cl::desc("A file containing list of symbol names to preserve"));
27 // APIList - A list of symbols that should not be marked internal.
29 APIList("internalize-public-api-list", cl::value_desc("list"),
30 cl::desc("A list of symbol names to preserve"),
33 class InternalizePass : public Pass {
34 std::set<std::string> ExternalNames;
37 if (!APIFile.empty()) // If a filename is specified, use it
38 LoadFile(APIFile.c_str());
39 else // Else, if a list is specified, use it.
40 ExternalNames.insert(APIList.begin(), APIList.end());
43 void LoadFile(const char *Filename) {
44 // Load the APIFile...
45 std::ifstream In(Filename);
47 std::cerr << "WARNING: Internalize couldn't load file '" << Filename
49 return; // Do not internalize anything...
55 ExternalNames.insert(Symbol);
59 virtual bool run(Module &M) {
60 // If no list or file of symbols was specified, check to see if there is a
61 // "main" symbol defined in the module. If so, use it, otherwise do not
62 // internalize the module, it must be a library or something.
64 if (ExternalNames.empty()) {
65 Function *MainFunc = M.getMainFunction();
66 if (MainFunc == 0 || MainFunc->isExternal())
67 return false; // No main found, must be a library...
69 // Preserve main, internalize all else.
70 ExternalNames.insert(MainFunc->getName());
75 // Found a main function, mark all functions not named main as internal.
76 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
77 if (!I->isExternal() && // Function must be defined here
78 !I->hasInternalLinkage() && // Can't already have internal linkage
79 !ExternalNames.count(I->getName())) {// Not marked to keep external?
80 I->setLinkage(GlobalValue::InternalLinkage);
83 DEBUG(std::cerr << "Internalizing func " << I->getName() << "\n");
86 // Mark all global variables with initializers as internal as well...
87 for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
88 if (!I->isExternal() && !I->hasInternalLinkage() &&
89 !ExternalNames.count(I->getName())) {
90 // Special case handling of the global ctor and dtor list. When we
91 // internalize it, we mark it constant, which allows elimination of
92 // the list if it's empty.
94 if (I->hasAppendingLinkage() && (I->getName() == "llvm.global_ctors"||
95 I->getName() == "llvm.global_dtors"))
98 I->setLinkage(GlobalValue::InternalLinkage);
101 DEBUG(std::cerr << "Internalizing gvar " << I->getName() << "\n");
108 RegisterOpt<InternalizePass> X("internalize", "Internalize Global Symbols");
109 } // end anonymous namespace
111 Pass *createInternalizePass() {
112 return new InternalizePass();