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/CommandLine.h"
13 #include "Support/Debug.h"
14 #include "Support/Statistic.h"
19 Statistic<> NumFunctions("internalize", "Number of functions internalized");
20 Statistic<> NumGlobals ("internalize", "Number of global vars internalized");
22 // APIFile - A file which contains a list of symbols that should not be marked
25 APIFile("internalize-public-api-file", cl::value_desc("filename"),
26 cl::desc("A file containing list of symbol names to preserve"));
28 // APIList - A list of symbols that should not be marked internal.
30 APIList("internalize-public-api-list", cl::value_desc("list"),
31 cl::desc("A list of symbol names to preserve"),
34 class InternalizePass : public Pass {
35 std::set<std::string> ExternalNames;
38 if (!APIFile.empty()) // If a filename is specified, use it
39 LoadFile(APIFile.c_str());
40 else // Else, if a list is specified, use it.
41 ExternalNames.insert(APIList.begin(), APIList.end());
44 void LoadFile(const char *Filename) {
45 // Load the APIFile...
46 std::ifstream In(Filename);
48 std::cerr << "WARNING: Internalize couldn't load file '" << Filename
50 return; // Do not internalize anything...
56 ExternalNames.insert(Symbol);
60 virtual bool run(Module &M) {
61 // If no list or file of symbols was specified, check to see if there is a
62 // "main" symbol defined in the module. If so, use it, otherwise do not
63 // internalize the module, it must be a library or something.
65 if (ExternalNames.empty()) {
66 Function *MainFunc = M.getMainFunction();
67 if (MainFunc == 0 || MainFunc->isExternal())
68 return false; // No main found, must be a library...
70 // Preserve main, internalize all else.
71 ExternalNames.insert(MainFunc->getName());
76 // Found a main function, mark all functions not named main as internal.
77 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
78 if (!I->isExternal() && // Function must be defined here
79 !I->hasInternalLinkage() && // Can't already have internal linkage
80 !ExternalNames.count(I->getName())) {// Not marked to keep external?
81 I->setLinkage(GlobalValue::InternalLinkage);
84 DEBUG(std::cerr << "Internalizing func " << I->getName() << "\n");
87 // Mark all global variables with initializers as internal as well...
88 for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
89 if (!I->isExternal() && !I->hasInternalLinkage() &&
90 !ExternalNames.count(I->getName())) {
91 // Special case handling of the global ctor and dtor list. When we
92 // internalize it, we mark it constant, which allows elimination of
93 // the list if it's empty.
95 if (I->hasAppendingLinkage() && (I->getName() == "llvm.global_ctors"||
96 I->getName() == "llvm.global_dtors"))
99 I->setLinkage(GlobalValue::InternalLinkage);
102 DEBUG(std::cerr << "Internalizing gvar " << I->getName() << "\n");
109 RegisterOpt<InternalizePass> X("internalize", "Internalize Global Symbols");
110 } // end anonymous namespace
112 Pass *createInternalizePass() {
113 return new InternalizePass();