Use the new script to sort the includes of every file under lib.
[oota-llvm.git] / lib / Transforms / IPO / Internalize.cpp
1 //===-- Internalize.cpp - Mark functions internal -------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This pass loops over all of the functions and variables in the input module.
11 // If the function or variable is not in the list of external names given to
12 // the pass it is marked as internal.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #define DEBUG_TYPE "internalize"
17 #include "llvm/Transforms/IPO.h"
18 #include "llvm/ADT/Statistic.h"
19 #include "llvm/Analysis/CallGraph.h"
20 #include "llvm/Module.h"
21 #include "llvm/Pass.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/raw_ostream.h"
25 #include <fstream>
26 #include <set>
27 using namespace llvm;
28
29 STATISTIC(NumAliases  , "Number of aliases internalized");
30 STATISTIC(NumFunctions, "Number of functions internalized");
31 STATISTIC(NumGlobals  , "Number of global vars internalized");
32
33 // APIFile - A file which contains a list of symbols that should not be marked
34 // external.
35 static cl::opt<std::string>
36 APIFile("internalize-public-api-file", cl::value_desc("filename"),
37         cl::desc("A file containing list of symbol names to preserve"));
38
39 // APIList - A list of symbols that should not be marked internal.
40 static cl::list<std::string>
41 APIList("internalize-public-api-list", cl::value_desc("list"),
42         cl::desc("A list of symbol names to preserve"),
43         cl::CommaSeparated);
44
45 namespace {
46   class InternalizePass : public ModulePass {
47     std::set<std::string> ExternalNames;
48   public:
49     static char ID; // Pass identification, replacement for typeid
50     explicit InternalizePass();
51     explicit InternalizePass(const std::vector <const char *>& exportList);
52     void LoadFile(const char *Filename);
53     virtual bool runOnModule(Module &M);
54
55     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
56       AU.setPreservesCFG();
57       AU.addPreserved<CallGraph>();
58     }
59   };
60 } // end anonymous namespace
61
62 char InternalizePass::ID = 0;
63 INITIALIZE_PASS(InternalizePass, "internalize",
64                 "Internalize Global Symbols", false, false)
65
66 InternalizePass::InternalizePass()
67   : ModulePass(ID) {
68   initializeInternalizePassPass(*PassRegistry::getPassRegistry());
69   if (!APIFile.empty())           // If a filename is specified, use it.
70     LoadFile(APIFile.c_str());
71   if (!APIList.empty())           // If a list is specified, use it as well.
72     ExternalNames.insert(APIList.begin(), APIList.end());
73 }
74
75 InternalizePass::InternalizePass(const std::vector<const char *>&exportList)
76   : ModulePass(ID){
77   initializeInternalizePassPass(*PassRegistry::getPassRegistry());
78   for(std::vector<const char *>::const_iterator itr = exportList.begin();
79         itr != exportList.end(); itr++) {
80     ExternalNames.insert(*itr);
81   }
82 }
83
84 void InternalizePass::LoadFile(const char *Filename) {
85   // Load the APIFile...
86   std::ifstream In(Filename);
87   if (!In.good()) {
88     errs() << "WARNING: Internalize couldn't load file '" << Filename
89          << "'! Continuing as if it's empty.\n";
90     return; // Just continue as if the file were empty
91   }
92   while (In) {
93     std::string Symbol;
94     In >> Symbol;
95     if (!Symbol.empty())
96       ExternalNames.insert(Symbol);
97   }
98 }
99
100 bool InternalizePass::runOnModule(Module &M) {
101   CallGraph *CG = getAnalysisIfAvailable<CallGraph>();
102   CallGraphNode *ExternalNode = CG ? CG->getExternalCallingNode() : 0;
103   bool Changed = false;
104
105   // Never internalize functions which code-gen might insert.
106   // FIXME: We should probably add this (and the __stack_chk_guard) via some
107   // type of call-back in CodeGen.
108   ExternalNames.insert("__stack_chk_fail");
109
110   // Mark all functions not in the api as internal.
111   // FIXME: maybe use private linkage?
112   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
113     if (!I->isDeclaration() &&         // Function must be defined here
114         // Available externally is really just a "declaration with a body".
115         !I->hasAvailableExternallyLinkage() &&
116         !I->hasLocalLinkage() &&  // Can't already have internal linkage
117         !ExternalNames.count(I->getName())) {// Not marked to keep external?
118       I->setLinkage(GlobalValue::InternalLinkage);
119       // Remove a callgraph edge from the external node to this function.
120       if (ExternalNode) ExternalNode->removeOneAbstractEdgeTo((*CG)[I]);
121       Changed = true;
122       ++NumFunctions;
123       DEBUG(dbgs() << "Internalizing func " << I->getName() << "\n");
124     }
125
126   // Never internalize the llvm.used symbol.  It is used to implement
127   // attribute((used)).
128   // FIXME: Shouldn't this just filter on llvm.metadata section??
129   ExternalNames.insert("llvm.used");
130   ExternalNames.insert("llvm.compiler.used");
131
132   // Never internalize anchors used by the machine module info, else the info
133   // won't find them.  (see MachineModuleInfo.)
134   ExternalNames.insert("llvm.global_ctors");
135   ExternalNames.insert("llvm.global_dtors");
136   ExternalNames.insert("llvm.global.annotations");
137
138   // Never internalize symbols code-gen inserts.
139   ExternalNames.insert("__stack_chk_guard");
140
141   // Mark all global variables with initializers that are not in the api as
142   // internal as well.
143   // FIXME: maybe use private linkage?
144   for (Module::global_iterator I = M.global_begin(), E = M.global_end();
145        I != E; ++I)
146     if (!I->isDeclaration() && !I->hasLocalLinkage() &&
147         // Available externally is really just a "declaration with a body".
148         !I->hasAvailableExternallyLinkage() &&
149         !ExternalNames.count(I->getName())) {
150       I->setLinkage(GlobalValue::InternalLinkage);
151       Changed = true;
152       ++NumGlobals;
153       DEBUG(dbgs() << "Internalized gvar " << I->getName() << "\n");
154     }
155
156   // Mark all aliases that are not in the api as internal as well.
157   for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
158        I != E; ++I)
159     if (!I->isDeclaration() && !I->hasInternalLinkage() &&
160         // Available externally is really just a "declaration with a body".
161         !I->hasAvailableExternallyLinkage() &&
162         !ExternalNames.count(I->getName())) {
163       I->setLinkage(GlobalValue::InternalLinkage);
164       Changed = true;
165       ++NumAliases;
166       DEBUG(dbgs() << "Internalized alias " << I->getName() << "\n");
167     }
168
169   return Changed;
170 }
171
172 ModulePass *llvm::createInternalizePass() {
173   return new InternalizePass();
174 }
175
176 ModulePass *llvm::createInternalizePass(const std::vector <const char *> &el) {
177   return new InternalizePass(el);
178 }