blob: c19e2f2b99cfe136c2136cd1eb75cfcf63549942 [file] [log] [blame]
Chris Lattnerdbb17352002-04-28 05:43:27 +00001//===-- Internalize.cpp - Mark functions internal -------------------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
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.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerdbb17352002-04-28 05:43:27 +00009//
10// This pass loops over all of the functions in the input module, looking for a
Chris Lattner55e41ba2002-07-30 19:48:44 +000011// main function. If a main function is found, all other functions and all
12// global variables with initializers are marked as internal.
Chris Lattnerdbb17352002-04-28 05:43:27 +000013//
14//===----------------------------------------------------------------------===//
15
Chris Lattner568ddab2002-07-24 17:12:05 +000016#include "llvm/Transforms/IPO.h"
Chris Lattnerdbb17352002-04-28 05:43:27 +000017#include "llvm/Pass.h"
18#include "llvm/Module.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000019#include "llvm/Support/CommandLine.h"
20#include "llvm/Support/Debug.h"
21#include "llvm/ADT/Statistic.h"
Chris Lattnerc7a2c7f2003-05-22 19:34:49 +000022#include <fstream>
23#include <set>
Chris Lattner1e2385b2003-11-21 21:54:22 +000024using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000025
Chris Lattnerf6293092002-07-23 18:06:35 +000026namespace {
Chris Lattnera92f6962002-10-01 22:38:41 +000027 Statistic<> NumFunctions("internalize", "Number of functions internalized");
28 Statistic<> NumGlobals ("internalize", "Number of global vars internalized");
Chris Lattnerdbb17352002-04-28 05:43:27 +000029
Chris Lattnerc7a2c7f2003-05-22 19:34:49 +000030 // APIFile - A file which contains a list of symbols that should not be marked
31 // external.
32 cl::opt<std::string>
33 APIFile("internalize-public-api-file", cl::value_desc("filename"),
Chris Lattner2345d712003-05-22 19:48:00 +000034 cl::desc("A file containing list of symbol names to preserve"));
35
36 // APIList - A list of symbols that should not be marked internal.
37 cl::list<std::string>
38 APIList("internalize-public-api-list", cl::value_desc("list"),
Chris Lattner88c7c322003-05-22 20:27:13 +000039 cl::desc("A list of symbol names to preserve"),
40 cl::CommaSeparated);
Misha Brukmanfd939082005-04-21 23:48:37 +000041
Chris Lattnerb12914b2004-09-20 04:48:05 +000042 class InternalizePass : public ModulePass {
Chris Lattnerc7a2c7f2003-05-22 19:34:49 +000043 std::set<std::string> ExternalNames;
Chris Lattnera27ea762005-10-18 06:29:22 +000044 bool DontInternalize;
Chris Lattnerc7a2c7f2003-05-22 19:34:49 +000045 public:
Chris Lattner4eb40df2006-01-03 19:13:17 +000046 InternalizePass(bool InternalizeEverything = true);
Devang Patel753d94a2006-07-20 17:48:05 +000047 InternalizePass(const std::vector <const char *>& exportList);
Chris Lattner4eb40df2006-01-03 19:13:17 +000048 void LoadFile(const char *Filename);
49 virtual bool runOnModule(Module &M);
Chris Lattner55e41ba2002-07-30 19:48:44 +000050 };
Chris Lattner7f8897f2006-08-27 22:42:52 +000051 RegisterPass<InternalizePass> X("internalize", "Internalize Global Symbols");
Chris Lattnerf6293092002-07-23 18:06:35 +000052} // end anonymous namespace
53
Chris Lattner4eb40df2006-01-03 19:13:17 +000054InternalizePass::InternalizePass(bool InternalizeEverything)
55 : DontInternalize(false){
56 if (!APIFile.empty()) // If a filename is specified, use it
57 LoadFile(APIFile.c_str());
58 else if (!APIList.empty()) // Else, if a list is specified, use it.
59 ExternalNames.insert(APIList.begin(), APIList.end());
60 else if (!InternalizeEverything)
61 // Finally, if we're allowed to, internalize all but main.
62 DontInternalize = true;
63}
64
Devang Patel56eac5f2006-09-13 01:02:26 +000065InternalizePass::InternalizePass(const std::vector<const char *>&exportList)
66 : DontInternalize(false){
Devang Patel753d94a2006-07-20 17:48:05 +000067 for(std::vector<const char *>::const_iterator itr = exportList.begin();
68 itr != exportList.end(); itr++) {
69 ExternalNames.insert(*itr);
70 }
71}
72
Chris Lattner4eb40df2006-01-03 19:13:17 +000073void InternalizePass::LoadFile(const char *Filename) {
74 // Load the APIFile...
75 std::ifstream In(Filename);
76 if (!In.good()) {
Bill Wendling0a81aac2006-11-26 10:02:32 +000077 llvm_cerr << "WARNING: Internalize couldn't load file '" << Filename
78 << "'!\n";
Chris Lattner4eb40df2006-01-03 19:13:17 +000079 return; // Do not internalize anything...
80 }
81 while (In) {
82 std::string Symbol;
83 In >> Symbol;
84 if (!Symbol.empty())
85 ExternalNames.insert(Symbol);
86 }
87}
88
89bool InternalizePass::runOnModule(Module &M) {
90 if (DontInternalize) return false;
91
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.
95 //
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...
100
101 // Preserve main, internalize all else.
102 ExternalNames.insert(MainFunc->getName());
103 }
104
105 bool Changed = false;
106
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);
113 Changed = true;
114 ++NumFunctions;
Bill Wendling0a81aac2006-11-26 10:02:32 +0000115 DOUT << "Internalizing func " << I->getName() << "\n";
Chris Lattner4eb40df2006-01-03 19:13:17 +0000116 }
117
118 // Never internalize the llvm.used symbol. It is used to implement
119 // attribute((used)).
120 ExternalNames.insert("llvm.used");
Chris Lattneree9e14c2006-01-19 00:40:39 +0000121
Chris Lattner4eb40df2006-01-03 19:13:17 +0000122 // Never internalize anchors used by the debugger, else the debugger won't
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000123 // find them. (see MachineDebugInfo.)
124 ExternalNames.insert("llvm.dbg.compile_units");
125 ExternalNames.insert("llvm.dbg.global_variables");
126 ExternalNames.insert("llvm.dbg.subprograms");
Chris Lattner4eb40df2006-01-03 19:13:17 +0000127
128 // Mark all global variables with initializers as internal as well.
129 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
130 I != E; ++I)
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.
136 //
137 if (I->hasAppendingLinkage() && (I->getName() == "llvm.global_ctors" ||
Chris Lattneree9e14c2006-01-19 00:40:39 +0000138 I->getName() == "llvm.global_dtors")) {
Chris Lattneree9e14c2006-01-19 00:40:39 +0000139 // 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
141 // it.
142 if (I->use_empty()) continue;
Chris Lattner727552b2006-01-19 00:46:54 +0000143
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
146 // better.
147 I->setConstant(true);
Chris Lattneree9e14c2006-01-19 00:40:39 +0000148 }
Chris Lattner4eb40df2006-01-03 19:13:17 +0000149
150 I->setLinkage(GlobalValue::InternalLinkage);
151 Changed = true;
152 ++NumGlobals;
Bill Wendling0a81aac2006-11-26 10:02:32 +0000153 DOUT << "Internalized gvar " << I->getName() << "\n";
Chris Lattner4eb40df2006-01-03 19:13:17 +0000154 }
155
156 return Changed;
157}
158
Chris Lattnera27ea762005-10-18 06:29:22 +0000159ModulePass *llvm::createInternalizePass(bool InternalizeEverything) {
160 return new InternalizePass(InternalizeEverything);
Chris Lattnerdbb17352002-04-28 05:43:27 +0000161}
Devang Patel753d94a2006-07-20 17:48:05 +0000162
Devang Pateld828a4b2006-07-20 18:03:39 +0000163ModulePass *llvm::createInternalizePass(const std::vector <const char *> &el) {
164 return new InternalizePass(el);
Devang Patel753d94a2006-07-20 17:48:05 +0000165}