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 "Support/CommandLine.h"
20 #include "Support/Debug.h"
21 #include "Support/Statistic.h"
26 Statistic<> NumFunctions("internalize", "Number of functions internalized");
27 Statistic<> NumGlobals ("internalize", "Number of global vars internalized");
29 // APIFile - A file which contains a list of symbols that should not be marked
32 APIFile("internalize-public-api-file", cl::value_desc("filename"),
33 cl::desc("A file containing list of symbol names to preserve"));
35 // APIList - A list of symbols that should not be marked internal.
37 APIList("internalize-public-api-list", cl::value_desc("list"),
38 cl::desc("A list of symbol names to preserve"),
41 class InternalizePass : public Pass {
42 std::set<std::string> ExternalNames;
45 if (!APIFile.empty()) // If a filename is specified, use it
46 LoadFile(APIFile.c_str());
47 else // Else, if a list is specified, use it.
48 ExternalNames.insert(APIList.begin(), APIList.end());
51 void LoadFile(const char *Filename) {
52 // Load the APIFile...
53 std::ifstream In(Filename);
55 std::cerr << "WARNING: Internalize couldn't load file '" << Filename
57 return; // Do not internalize anything...
63 ExternalNames.insert(Symbol);
67 virtual bool run(Module &M) {
68 // If no list or file of symbols was specified, check to see if there is a
69 // "main" symbol defined in the module. If so, use it, otherwise do not
70 // internalize the module, it must be a library or something.
72 if (ExternalNames.empty()) {
73 Function *MainFunc = M.getMainFunction();
74 if (MainFunc == 0 || MainFunc->isExternal())
75 return false; // No main found, must be a library...
77 // Preserve main, internalize all else.
78 ExternalNames.insert(MainFunc->getName());
83 // Found a main function, mark all functions not named main as internal.
84 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
85 if (!I->isExternal() && // Function must be defined here
86 !I->hasInternalLinkage() && // Can't already have internal linkage
87 !ExternalNames.count(I->getName())) {// Not marked to keep external?
88 I->setLinkage(GlobalValue::InternalLinkage);
91 DEBUG(std::cerr << "Internalizing func " << I->getName() << "\n");
94 // Mark all global variables with initializers as internal as well...
95 for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
96 if (!I->isExternal() && !I->hasInternalLinkage() &&
97 !ExternalNames.count(I->getName())) {
98 // Special case handling of the global ctor and dtor list. When we
99 // internalize it, we mark it constant, which allows elimination of
100 // the list if it's empty.
102 if (I->hasAppendingLinkage() && (I->getName() == "llvm.global_ctors"||
103 I->getName() == "llvm.global_dtors"))
104 I->setConstant(true);
106 I->setLinkage(GlobalValue::InternalLinkage);
109 DEBUG(std::cerr << "Internalizing gvar " << I->getName() << "\n");
116 RegisterOpt<InternalizePass> X("internalize", "Internalize Global Symbols");
117 } // end anonymous namespace
119 Pass *createInternalizePass() {
120 return new InternalizePass();