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/Debug.h"
22 #include "llvm/ADT/Statistic.h"
27 STATISTIC(NumFunctions, "Number of functions internalized");
28 STATISTIC(NumGlobals , "Number of global vars internalized");
32 // APIFile - A file which contains a list of symbols that should not be marked
35 APIFile("internalize-public-api-file", cl::value_desc("filename"),
36 cl::desc("A file containing list of symbol names to preserve"));
38 // APIList - A list of symbols that should not be marked internal.
40 APIList("internalize-public-api-list", cl::value_desc("list"),
41 cl::desc("A list of symbol names to preserve"),
44 class InternalizePass : public ModulePass {
45 std::set<std::string> ExternalNames;
48 InternalizePass(bool InternalizeEverything = true);
49 InternalizePass(const std::vector <const char *>& exportList);
50 void LoadFile(const char *Filename);
51 virtual bool runOnModule(Module &M);
53 RegisterPass<InternalizePass> X("internalize", "Internalize Global Symbols");
54 } // end anonymous namespace
56 InternalizePass::InternalizePass(bool InternalizeEverything)
57 : DontInternalize(false){
58 if (!APIFile.empty()) // If a filename is specified, use it
59 LoadFile(APIFile.c_str());
60 else if (!APIList.empty()) // Else, if a list is specified, use it.
61 ExternalNames.insert(APIList.begin(), APIList.end());
62 else if (!InternalizeEverything)
63 // Finally, if we're allowed to, internalize all but main.
64 DontInternalize = true;
67 InternalizePass::InternalizePass(const std::vector<const char *>&exportList)
68 : DontInternalize(false){
69 for(std::vector<const char *>::const_iterator itr = exportList.begin();
70 itr != exportList.end(); itr++) {
71 ExternalNames.insert(*itr);
75 void InternalizePass::LoadFile(const char *Filename) {
76 // Load the APIFile...
77 std::ifstream In(Filename);
79 cerr << "WARNING: Internalize couldn't load file '" << Filename << "'!\n";
80 return; // Do not internalize anything...
86 ExternalNames.insert(Symbol);
90 bool InternalizePass::runOnModule(Module &M) {
91 if (DontInternalize) return false;
93 // If no list or file of symbols was specified, check to see if there is a
94 // "main" symbol defined in the module. If so, use it, otherwise do not
95 // internalize the module, it must be a library or something.
97 if (ExternalNames.empty()) {
98 Function *MainFunc = M.getMainFunction();
99 if (MainFunc == 0 || MainFunc->isExternal())
100 return false; // No main found, must be a library...
102 // Preserve main, internalize all else.
103 ExternalNames.insert(MainFunc->getName());
106 bool Changed = false;
108 // Found a main function, mark all functions not named main as internal.
109 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
110 if (!I->isExternal() && // Function must be defined here
111 !I->hasInternalLinkage() && // Can't already have internal linkage
112 !ExternalNames.count(I->getName())) {// Not marked to keep external?
113 I->setLinkage(GlobalValue::InternalLinkage);
116 DOUT << "Internalizing func " << I->getName() << "\n";
119 // Never internalize the llvm.used symbol. It is used to implement
120 // attribute((used)).
121 ExternalNames.insert("llvm.used");
123 // Never internalize anchors used by the machine module info, else the info
124 // won't find them. (see MachineModuleInfo.)
125 ExternalNames.insert("llvm.dbg.compile_units");
126 ExternalNames.insert("llvm.dbg.global_variables");
127 ExternalNames.insert("llvm.dbg.subprograms");
129 // Mark all global variables with initializers as internal as well.
130 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
132 if (!I->isExternal() && !I->hasInternalLinkage() &&
133 !ExternalNames.count(I->getName())) {
134 // Special case handling of the global ctor and dtor list. When we
135 // internalize it, we mark it constant, which allows elimination of
136 // the list if it's empty.
138 if (I->hasAppendingLinkage() && (I->getName() == "llvm.global_ctors" ||
139 I->getName() == "llvm.global_dtors")) {
140 // If the global ctors/dtors list has no uses, do not internalize it, as
141 // there is no __main in this program, so the asmprinter should handle
143 if (I->use_empty()) continue;
145 // Otherwise, also mark the list constant, as we know that it will not
146 // be mutated any longer, and the makes simple IPO xforms automatically
148 I->setConstant(true);
151 I->setLinkage(GlobalValue::InternalLinkage);
154 DOUT << "Internalized gvar " << I->getName() << "\n";
160 ModulePass *llvm::createInternalizePass(bool InternalizeEverything) {
161 return new InternalizePass(InternalizeEverything);
164 ModulePass *llvm::createInternalizePass(const std::vector <const char *> &el) {
165 return new InternalizePass(el);