blob: e5bacf611afb5d1e1cf947239d11cdf1da5c0798 [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"
21#include "llvm/Support/Debug.h"
22#include "llvm/ADT/Statistic.h"
Chris Lattner44457bb2003-05-22 19:34:49 +000023#include <fstream>
24#include <set>
Chris Lattnerf52e03c2003-11-21 21:54:22 +000025using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000026
Chris Lattner1631bcb2006-12-19 22:09:18 +000027STATISTIC(NumFunctions, "Number of functions internalized");
28STATISTIC(NumGlobals , "Number of global vars internalized");
29
Chris Lattnerb28b6802002-07-23 18:06:35 +000030namespace {
Chris Lattner1b94c002002-04-28 05:43:27 +000031
Chris Lattner44457bb2003-05-22 19:34:49 +000032 // APIFile - A file which contains a list of symbols that should not be marked
33 // external.
34 cl::opt<std::string>
35 APIFile("internalize-public-api-file", cl::value_desc("filename"),
Chris Lattnerad44cd82003-05-22 19:48:00 +000036 cl::desc("A file containing list of symbol names to preserve"));
37
38 // APIList - A list of symbols that should not be marked internal.
39 cl::list<std::string>
40 APIList("internalize-public-api-list", cl::value_desc("list"),
Chris Lattner224ae022003-05-22 20:27:13 +000041 cl::desc("A list of symbol names to preserve"),
42 cl::CommaSeparated);
Misha Brukmanb1c93172005-04-21 23:48:37 +000043
Chris Lattner4f2cf032004-09-20 04:48:05 +000044 class InternalizePass : public ModulePass {
Chris Lattner44457bb2003-05-22 19:34:49 +000045 std::set<std::string> ExternalNames;
Chris Lattner45517ba2005-10-18 06:29:22 +000046 bool DontInternalize;
Chris Lattner44457bb2003-05-22 19:34:49 +000047 public:
Chris Lattner8cdc7732006-01-03 19:13:17 +000048 InternalizePass(bool InternalizeEverything = true);
Devang Patel839d9262006-07-20 17:48:05 +000049 InternalizePass(const std::vector <const char *>& exportList);
Chris Lattner8cdc7732006-01-03 19:13:17 +000050 void LoadFile(const char *Filename);
51 virtual bool runOnModule(Module &M);
Chris Lattner47cc3662002-07-30 19:48:44 +000052 };
Chris Lattnerc2d3d312006-08-27 22:42:52 +000053 RegisterPass<InternalizePass> X("internalize", "Internalize Global Symbols");
Chris Lattnerb28b6802002-07-23 18:06:35 +000054} // end anonymous namespace
55
Chris Lattner8cdc7732006-01-03 19:13:17 +000056InternalizePass::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;
65}
66
Devang Patelfab49722006-09-13 01:02:26 +000067InternalizePass::InternalizePass(const std::vector<const char *>&exportList)
68 : DontInternalize(false){
Devang Patel839d9262006-07-20 17:48:05 +000069 for(std::vector<const char *>::const_iterator itr = exportList.begin();
70 itr != exportList.end(); itr++) {
71 ExternalNames.insert(*itr);
72 }
73}
74
Chris Lattner8cdc7732006-01-03 19:13:17 +000075void InternalizePass::LoadFile(const char *Filename) {
76 // Load the APIFile...
77 std::ifstream In(Filename);
78 if (!In.good()) {
Bill Wendlingf3baad32006-12-07 01:30:32 +000079 cerr << "WARNING: Internalize couldn't load file '" << Filename << "'!\n";
Chris Lattner8cdc7732006-01-03 19:13:17 +000080 return; // Do not internalize anything...
81 }
82 while (In) {
83 std::string Symbol;
84 In >> Symbol;
85 if (!Symbol.empty())
86 ExternalNames.insert(Symbol);
87 }
88}
89
90bool InternalizePass::runOnModule(Module &M) {
91 if (DontInternalize) return false;
92
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.
96 //
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...
101
102 // Preserve main, internalize all else.
103 ExternalNames.insert(MainFunc->getName());
104 }
105
106 bool Changed = false;
107
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);
114 Changed = true;
115 ++NumFunctions;
Bill Wendling8f13b5c2006-11-26 10:02:32 +0000116 DOUT << "Internalizing func " << I->getName() << "\n";
Chris Lattner8cdc7732006-01-03 19:13:17 +0000117 }
118
119 // Never internalize the llvm.used symbol. It is used to implement
120 // attribute((used)).
121 ExternalNames.insert("llvm.used");
Chris Lattnerd693b792006-01-19 00:40:39 +0000122
Jim Laskeyc56315c2007-01-26 21:22:28 +0000123 // Never internalize anchors used by the machine module info, else the info
124 // won't find them. (see MachineModuleInfo.)
Jim Laskey69effa22006-03-07 20:53:47 +0000125 ExternalNames.insert("llvm.dbg.compile_units");
126 ExternalNames.insert("llvm.dbg.global_variables");
127 ExternalNames.insert("llvm.dbg.subprograms");
Chris Lattner8cdc7732006-01-03 19:13:17 +0000128
129 // Mark all global variables with initializers as internal as well.
130 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
131 I != E; ++I)
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.
137 //
138 if (I->hasAppendingLinkage() && (I->getName() == "llvm.global_ctors" ||
Chris Lattnerd693b792006-01-19 00:40:39 +0000139 I->getName() == "llvm.global_dtors")) {
Chris Lattnerd693b792006-01-19 00:40:39 +0000140 // 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
142 // it.
143 if (I->use_empty()) continue;
Chris Lattner7be22032006-01-19 00:46:54 +0000144
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
147 // better.
148 I->setConstant(true);
Chris Lattnerd693b792006-01-19 00:40:39 +0000149 }
Chris Lattner8cdc7732006-01-03 19:13:17 +0000150
151 I->setLinkage(GlobalValue::InternalLinkage);
152 Changed = true;
153 ++NumGlobals;
Bill Wendling8f13b5c2006-11-26 10:02:32 +0000154 DOUT << "Internalized gvar " << I->getName() << "\n";
Chris Lattner8cdc7732006-01-03 19:13:17 +0000155 }
156
157 return Changed;
158}
159
Chris Lattner45517ba2005-10-18 06:29:22 +0000160ModulePass *llvm::createInternalizePass(bool InternalizeEverything) {
161 return new InternalizePass(InternalizeEverything);
Chris Lattner1b94c002002-04-28 05:43:27 +0000162}
Devang Patel839d9262006-07-20 17:48:05 +0000163
Devang Pateledd2f992006-07-20 18:03:39 +0000164ModulePass *llvm::createInternalizePass(const std::vector <const char *> &el) {
165 return new InternalizePass(el);
Devang Patel839d9262006-07-20 17:48:05 +0000166}