blob: 46d80e592ffb76f7b5e3c4c8afeac1a5d54275d2 [file] [log] [blame]
Chris Lattner1b94c002002-04-28 05:43:27 +00001//===-- Internalize.cpp - Mark functions internal -------------------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-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 Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner1b94c002002-04-28 05:43:27 +00009//
10// This pass loops over all of the functions in the input module, looking for a
Chris Lattner47cc3662002-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 Lattner1b94c002002-04-28 05:43:27 +000013//
14//===----------------------------------------------------------------------===//
15
Chris Lattner1631bcb2006-12-19 22:09:18 +000016#define DEBUG_TYPE "internalize"
Chris Lattner99a53f62002-07-24 17:12:05 +000017#include "llvm/Transforms/IPO.h"
Chris Lattner1b94c002002-04-28 05:43:27 +000018#include "llvm/Pass.h"
19#include "llvm/Module.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000020#include "llvm/Support/CommandLine.h"
Reid Spencer557ab152007-02-05 23:32:05 +000021#include "llvm/Support/Compiler.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000022#include "llvm/Support/Debug.h"
23#include "llvm/ADT/Statistic.h"
Chris Lattner44457bb2003-05-22 19:34:49 +000024#include <fstream>
25#include <set>
Chris Lattnerf52e03c2003-11-21 21:54:22 +000026using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000027
Chris Lattner1631bcb2006-12-19 22:09:18 +000028STATISTIC(NumFunctions, "Number of functions internalized");
29STATISTIC(NumGlobals , "Number of global vars internalized");
30
Chris Lattnerb28b6802002-07-23 18:06:35 +000031namespace {
Chris Lattner1b94c002002-04-28 05:43:27 +000032
Chris Lattner44457bb2003-05-22 19:34:49 +000033 // APIFile - A file which contains a list of symbols that should not be marked
34 // external.
35 cl::opt<std::string>
36 APIFile("internalize-public-api-file", cl::value_desc("filename"),
Chris Lattnerad44cd82003-05-22 19:48:00 +000037 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 cl::list<std::string>
41 APIList("internalize-public-api-list", cl::value_desc("list"),
Chris Lattner224ae022003-05-22 20:27:13 +000042 cl::desc("A list of symbol names to preserve"),
43 cl::CommaSeparated);
Misha Brukmanb1c93172005-04-21 23:48:37 +000044
Reid Spencer557ab152007-02-05 23:32:05 +000045 class VISIBILITY_HIDDEN InternalizePass : public ModulePass {
Chris Lattner44457bb2003-05-22 19:34:49 +000046 std::set<std::string> ExternalNames;
Chris Lattner45517ba2005-10-18 06:29:22 +000047 bool DontInternalize;
Chris Lattner44457bb2003-05-22 19:34:49 +000048 public:
Chris Lattner8cdc7732006-01-03 19:13:17 +000049 InternalizePass(bool InternalizeEverything = true);
Devang Patel839d9262006-07-20 17:48:05 +000050 InternalizePass(const std::vector <const char *>& exportList);
Chris Lattner8cdc7732006-01-03 19:13:17 +000051 void LoadFile(const char *Filename);
52 virtual bool runOnModule(Module &M);
Chris Lattner47cc3662002-07-30 19:48:44 +000053 };
Chris Lattnerc2d3d312006-08-27 22:42:52 +000054 RegisterPass<InternalizePass> X("internalize", "Internalize Global Symbols");
Chris Lattnerb28b6802002-07-23 18:06:35 +000055} // end anonymous namespace
56
Chris Lattner8cdc7732006-01-03 19:13:17 +000057InternalizePass::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;
66}
67
Devang Patelfab49722006-09-13 01:02:26 +000068InternalizePass::InternalizePass(const std::vector<const char *>&exportList)
69 : DontInternalize(false){
Devang Patel839d9262006-07-20 17:48:05 +000070 for(std::vector<const char *>::const_iterator itr = exportList.begin();
Anton Korobeynikovfb801512007-04-16 18:10:23 +000071 itr != exportList.end(); itr++) {
Devang Patel839d9262006-07-20 17:48:05 +000072 ExternalNames.insert(*itr);
73 }
74}
75
Chris Lattner8cdc7732006-01-03 19:13:17 +000076void InternalizePass::LoadFile(const char *Filename) {
77 // Load the APIFile...
78 std::ifstream In(Filename);
79 if (!In.good()) {
Bill Wendlingf3baad32006-12-07 01:30:32 +000080 cerr << "WARNING: Internalize couldn't load file '" << Filename << "'!\n";
Chris Lattner8cdc7732006-01-03 19:13:17 +000081 return; // Do not internalize anything...
82 }
83 while (In) {
84 std::string Symbol;
85 In >> Symbol;
86 if (!Symbol.empty())
87 ExternalNames.insert(Symbol);
88 }
89}
90
91bool InternalizePass::runOnModule(Module &M) {
92 if (DontInternalize) return false;
93
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.
97 //
98 if (ExternalNames.empty()) {
Reid Spencer1241d6d2007-02-05 21:19:13 +000099 Function *MainFunc = M.getFunction("main");
Reid Spencer5301e7c2007-01-30 20:08:39 +0000100 if (MainFunc == 0 || MainFunc->isDeclaration())
Chris Lattner8cdc7732006-01-03 19:13:17 +0000101 return false; // No main found, must be a library...
102
103 // Preserve main, internalize all else.
104 ExternalNames.insert(MainFunc->getName());
105 }
106
107 bool Changed = false;
108
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)
Reid Spencer5301e7c2007-01-30 20:08:39 +0000111 if (!I->isDeclaration() && // Function must be defined here
Chris Lattner8cdc7732006-01-03 19:13:17 +0000112 !I->hasInternalLinkage() && // Can't already have internal linkage
113 !ExternalNames.count(I->getName())) {// Not marked to keep external?
114 I->setLinkage(GlobalValue::InternalLinkage);
115 Changed = true;
116 ++NumFunctions;
Bill Wendling8f13b5c2006-11-26 10:02:32 +0000117 DOUT << "Internalizing func " << I->getName() << "\n";
Chris Lattner8cdc7732006-01-03 19:13:17 +0000118 }
119
120 // Never internalize the llvm.used symbol. It is used to implement
121 // attribute((used)).
122 ExternalNames.insert("llvm.used");
Chris Lattnerd693b792006-01-19 00:40:39 +0000123
Jim Laskeyc56315c2007-01-26 21:22:28 +0000124 // Never internalize anchors used by the machine module info, else the info
125 // won't find them. (see MachineModuleInfo.)
Jim Laskey69effa22006-03-07 20:53:47 +0000126 ExternalNames.insert("llvm.dbg.compile_units");
127 ExternalNames.insert("llvm.dbg.global_variables");
128 ExternalNames.insert("llvm.dbg.subprograms");
Chris Lattner8cdc7732006-01-03 19:13:17 +0000129
130 // Mark all global variables with initializers as internal as well.
131 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
132 I != E; ++I)
Reid Spencer5301e7c2007-01-30 20:08:39 +0000133 if (!I->isDeclaration() && !I->hasInternalLinkage() &&
Chris Lattner8cdc7732006-01-03 19:13:17 +0000134 !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.
138 //
139 if (I->hasAppendingLinkage() && (I->getName() == "llvm.global_ctors" ||
Chris Lattnerd693b792006-01-19 00:40:39 +0000140 I->getName() == "llvm.global_dtors")) {
Chris Lattnerd693b792006-01-19 00:40:39 +0000141 // 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
143 // it.
144 if (I->use_empty()) continue;
Chris Lattner7be22032006-01-19 00:46:54 +0000145
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
148 // better.
149 I->setConstant(true);
Chris Lattnerd693b792006-01-19 00:40:39 +0000150 }
Chris Lattner8cdc7732006-01-03 19:13:17 +0000151
152 I->setLinkage(GlobalValue::InternalLinkage);
153 Changed = true;
154 ++NumGlobals;
Bill Wendling8f13b5c2006-11-26 10:02:32 +0000155 DOUT << "Internalized gvar " << I->getName() << "\n";
Chris Lattner8cdc7732006-01-03 19:13:17 +0000156 }
157
158 return Changed;
159}
160
Chris Lattner45517ba2005-10-18 06:29:22 +0000161ModulePass *llvm::createInternalizePass(bool InternalizeEverything) {
162 return new InternalizePass(InternalizeEverything);
Chris Lattner1b94c002002-04-28 05:43:27 +0000163}
Devang Patel839d9262006-07-20 17:48:05 +0000164
Devang Pateledd2f992006-07-20 18:03:39 +0000165ModulePass *llvm::createInternalizePass(const std::vector <const char *> &el) {
166 return new InternalizePass(el);
Devang Patel839d9262006-07-20 17:48:05 +0000167}