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 InternalizePass(bool InternalizeEverything = true);
50 InternalizePass(const std::vector <const char *>& exportList);
51 void LoadFile(const char *Filename);
52 virtual bool runOnModule(Module &M);
54 RegisterPass<InternalizePass> X("internalize", "Internalize Global Symbols");
55 } // end anonymous namespace
57 InternalizePass::InternalizePass(bool InternalizeEverything)
58 : DontInternalize(false){
59 if (!APIFile.empty()) // If a filename is specified, use it
60 LoadFile(APIFile.c_str());
61 else if (!APIList.empty()) // Else, if a list is specified, use it.
62 ExternalNames.insert(APIList.begin(), APIList.end());
63 else if (!InternalizeEverything)
64 // Finally, if we're allowed to, internalize all but main.
65 DontInternalize = true;
68 InternalizePass::InternalizePass(const std::vector<const char *>&exportList)
69 : DontInternalize(false){
70 for(std::vector<const char *>::const_iterator itr = exportList.begin();
71 itr != exportList.end(); itr++) {
72 ExternalNames.insert(*itr);
76 void InternalizePass::LoadFile(const char *Filename) {
77 // Load the APIFile...
78 std::ifstream In(Filename);
80 cerr << "WARNING: Internalize couldn't load file '" << Filename << "'!\n";
81 return; // Do not internalize anything...
87 ExternalNames.insert(Symbol);
91 bool InternalizePass::runOnModule(Module &M) {
92 if (DontInternalize) return false;
94 // If no list or file of symbols was specified, check to see if there is a
95 // "main" symbol defined in the module. If so, use it, otherwise do not
96 // internalize the module, it must be a library or something.
98 if (ExternalNames.empty()) {
99 Function *MainFunc = M.getFunction("main");
100 if (MainFunc == 0 || MainFunc->isDeclaration())
101 return false; // No main found, must be a library...
103 // Preserve main, internalize all else.
104 ExternalNames.insert(MainFunc->getName());
107 bool Changed = false;
109 // Found a main function, mark all functions not named main as internal.
110 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
111 if (!I->isDeclaration() && // Function must be defined here
112 !I->hasInternalLinkage() && // Can't already have internal linkage
113 !ExternalNames.count(I->getName())) {// Not marked to keep external?
114 I->setLinkage(GlobalValue::InternalLinkage);
117 DOUT << "Internalizing func " << I->getName() << "\n";
120 // Never internalize the llvm.used symbol. It is used to implement
121 // attribute((used)).
122 ExternalNames.insert("llvm.used");
124 // Never internalize anchors used by the machine module info, else the info
125 // won't find them. (see MachineModuleInfo.)
126 ExternalNames.insert("llvm.dbg.compile_units");
127 ExternalNames.insert("llvm.dbg.global_variables");
128 ExternalNames.insert("llvm.dbg.subprograms");
130 // Mark all global variables with initializers as internal as well.
131 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
133 if (!I->isDeclaration() && !I->hasInternalLinkage() &&
134 !ExternalNames.count(I->getName())) {
135 // Special case handling of the global ctor and dtor list. When we
136 // internalize it, we mark it constant, which allows elimination of
137 // the list if it's empty.
139 if (I->hasAppendingLinkage() && (I->getName() == "llvm.global_ctors" ||
140 I->getName() == "llvm.global_dtors")) {
141 // If the global ctors/dtors list has no uses, do not internalize it, as
142 // there is no __main in this program, so the asmprinter should handle
144 if (I->use_empty()) continue;
146 // Otherwise, also mark the list constant, as we know that it will not
147 // be mutated any longer, and the makes simple IPO xforms automatically
149 I->setConstant(true);
152 I->setLinkage(GlobalValue::InternalLinkage);
155 DOUT << "Internalized gvar " << I->getName() << "\n";
161 ModulePass *llvm::createInternalizePass(bool InternalizeEverything) {
162 return new InternalizePass(InternalizeEverything);
165 ModulePass *llvm::createInternalizePass(const std::vector <const char *> &el) {
166 return new InternalizePass(el);