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 #include "llvm/Transforms/IPO.h"
17 #include "llvm/Pass.h"
18 #include "llvm/Module.h"
19 #include "llvm/Support/CommandLine.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/ADT/Statistic.h"
28 Statistic<> NumFunctions("internalize", "Number of functions internalized");
29 Statistic<> NumGlobals ("internalize", "Number of global vars internalized");
31 // APIFile - A file which contains a list of symbols that should not be marked
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.
39 APIList("internalize-public-api-list", cl::value_desc("list"),
40 cl::desc("A list of symbol names to preserve"),
43 class InternalizePass : public ModulePass {
44 std::set<std::string> ExternalNames;
47 InternalizePass(bool InternalizeEverything = true);
48 InternalizePass(const std::vector <const char *>& exportList);
49 void LoadFile(const char *Filename);
50 virtual bool runOnModule(Module &M);
52 RegisterOpt<InternalizePass> X("internalize", "Internalize Global Symbols");
53 } // end anonymous namespace
55 InternalizePass::InternalizePass(bool InternalizeEverything)
56 : DontInternalize(false){
57 if (!APIFile.empty()) // If a filename is specified, use it
58 LoadFile(APIFile.c_str());
59 else if (!APIList.empty()) // Else, if a list is specified, use it.
60 ExternalNames.insert(APIList.begin(), APIList.end());
61 else if (!InternalizeEverything)
62 // Finally, if we're allowed to, internalize all but main.
63 DontInternalize = true;
66 InternalizePass::InternalizePass(const std::vector<const char *>&exportList) {
67 for(std::vector<const char *>::const_iterator itr = exportList.begin();
68 itr != exportList.end(); itr++) {
69 ExternalNames.insert(*itr);
73 void InternalizePass::LoadFile(const char *Filename) {
74 // Load the APIFile...
75 std::ifstream In(Filename);
77 std::cerr << "WARNING: Internalize couldn't load file '" << Filename
79 return; // Do not internalize anything...
85 ExternalNames.insert(Symbol);
89 bool InternalizePass::runOnModule(Module &M) {
90 if (DontInternalize) return false;
92 // If no list or file of symbols was specified, check to see if there is a
93 // "main" symbol defined in the module. If so, use it, otherwise do not
94 // internalize the module, it must be a library or something.
96 if (ExternalNames.empty()) {
97 Function *MainFunc = M.getMainFunction();
98 if (MainFunc == 0 || MainFunc->isExternal())
99 return false; // No main found, must be a library...
101 // Preserve main, internalize all else.
102 ExternalNames.insert(MainFunc->getName());
105 bool Changed = false;
107 // Found a main function, mark all functions not named main as internal.
108 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
109 if (!I->isExternal() && // Function must be defined here
110 !I->hasInternalLinkage() && // Can't already have internal linkage
111 !ExternalNames.count(I->getName())) {// Not marked to keep external?
112 I->setLinkage(GlobalValue::InternalLinkage);
115 DEBUG(std::cerr << "Internalizing func " << I->getName() << "\n");
118 // Never internalize the llvm.used symbol. It is used to implement
119 // attribute((used)).
120 ExternalNames.insert("llvm.used");
122 // Never internalize anchors used by the debugger, else the debugger won't
123 // find them. (see MachineDebugInfo.)
124 ExternalNames.insert("llvm.dbg.compile_units");
125 ExternalNames.insert("llvm.dbg.global_variables");
126 ExternalNames.insert("llvm.dbg.subprograms");
128 // Mark all global variables with initializers as internal as well.
129 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
131 if (!I->isExternal() && !I->hasInternalLinkage() &&
132 !ExternalNames.count(I->getName())) {
133 // Special case handling of the global ctor and dtor list. When we
134 // internalize it, we mark it constant, which allows elimination of
135 // the list if it's empty.
137 if (I->hasAppendingLinkage() && (I->getName() == "llvm.global_ctors" ||
138 I->getName() == "llvm.global_dtors")) {
139 // If the global ctors/dtors list has no uses, do not internalize it, as
140 // there is no __main in this program, so the asmprinter should handle
142 if (I->use_empty()) continue;
144 // Otherwise, also mark the list constant, as we know that it will not
145 // be mutated any longer, and the makes simple IPO xforms automatically
147 I->setConstant(true);
150 I->setLinkage(GlobalValue::InternalLinkage);
153 DEBUG(std::cerr << "Internalized gvar " << I->getName() << "\n");
159 ModulePass *llvm::createInternalizePass(bool InternalizeEverything) {
160 return new InternalizePass(InternalizeEverything);
163 ModulePass *llvm::createInternalizePass(const std::vector <const char *> &exportList) {
164 return new InternalizePass(exportList);