blob: cd1eda71dc69ce437746516238c93b91de13bc19 [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 Lattner99a53f62002-07-24 17:12:05 +000016#include "llvm/Transforms/IPO.h"
Chris Lattner1b94c002002-04-28 05:43:27 +000017#include "llvm/Pass.h"
18#include "llvm/Module.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000019#include "llvm/Support/CommandLine.h"
20#include "llvm/Support/Debug.h"
21#include "llvm/ADT/Statistic.h"
Chris Lattner44457bb2003-05-22 19:34:49 +000022#include <fstream>
23#include <set>
Chris Lattnerf52e03c2003-11-21 21:54:22 +000024using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000025
Chris Lattnerb28b6802002-07-23 18:06:35 +000026namespace {
Chris Lattnerbf3a0992002-10-01 22:38:41 +000027 Statistic<> NumFunctions("internalize", "Number of functions internalized");
28 Statistic<> NumGlobals ("internalize", "Number of global vars internalized");
Chris Lattner1b94c002002-04-28 05:43:27 +000029
Chris Lattner44457bb2003-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 Lattnerad44cd82003-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 Lattner224ae022003-05-22 20:27:13 +000039 cl::desc("A list of symbol names to preserve"),
40 cl::CommaSeparated);
Misha Brukmanb1c93172005-04-21 23:48:37 +000041
Chris Lattner4f2cf032004-09-20 04:48:05 +000042 class InternalizePass : public ModulePass {
Chris Lattner44457bb2003-05-22 19:34:49 +000043 std::set<std::string> ExternalNames;
Chris Lattner45517ba2005-10-18 06:29:22 +000044 bool DontInternalize;
Chris Lattner44457bb2003-05-22 19:34:49 +000045 public:
Chris Lattner45517ba2005-10-18 06:29:22 +000046 InternalizePass(bool InternalizeEverything = true) : DontInternalize(false){
Chris Lattnerad44cd82003-05-22 19:48:00 +000047 if (!APIFile.empty()) // If a filename is specified, use it
Chris Lattner44457bb2003-05-22 19:34:49 +000048 LoadFile(APIFile.c_str());
Chris Lattner45517ba2005-10-18 06:29:22 +000049 else if (!APIList.empty()) // Else, if a list is specified, use it.
Chris Lattnerad44cd82003-05-22 19:48:00 +000050 ExternalNames.insert(APIList.begin(), APIList.end());
Chris Lattner45517ba2005-10-18 06:29:22 +000051 else if (!InternalizeEverything)
52 // Finally, if we're allowed to, internalize all but main.
53 DontInternalize = true;
Chris Lattner44457bb2003-05-22 19:34:49 +000054 }
Chris Lattner17069b32002-11-08 20:34:21 +000055
Chris Lattner44457bb2003-05-22 19:34:49 +000056 void LoadFile(const char *Filename) {
57 // Load the APIFile...
58 std::ifstream In(Filename);
59 if (!In.good()) {
60 std::cerr << "WARNING: Internalize couldn't load file '" << Filename
Chris Lattnerad44cd82003-05-22 19:48:00 +000061 << "'!\n";
Chris Lattner44457bb2003-05-22 19:34:49 +000062 return; // Do not internalize anything...
63 }
64 while (In) {
65 std::string Symbol;
66 In >> Symbol;
67 if (!Symbol.empty())
68 ExternalNames.insert(Symbol);
69 }
70 }
71
Chris Lattner4f2cf032004-09-20 04:48:05 +000072 virtual bool runOnModule(Module &M) {
Chris Lattner45517ba2005-10-18 06:29:22 +000073 if (DontInternalize) return false;
74
Chris Lattnerad44cd82003-05-22 19:48:00 +000075 // If no list or file of symbols was specified, check to see if there is a
76 // "main" symbol defined in the module. If so, use it, otherwise do not
77 // internalize the module, it must be a library or something.
78 //
79 if (ExternalNames.empty()) {
80 Function *MainFunc = M.getMainFunction();
81 if (MainFunc == 0 || MainFunc->isExternal())
82 return false; // No main found, must be a library...
83
84 // Preserve main, internalize all else.
85 ExternalNames.insert(MainFunc->getName());
86 }
87
Chris Lattner47cc3662002-07-30 19:48:44 +000088 bool Changed = false;
Misha Brukmanb1c93172005-04-21 23:48:37 +000089
Chris Lattner47cc3662002-07-30 19:48:44 +000090 // Found a main function, mark all functions not named main as internal.
91 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattner44457bb2003-05-22 19:34:49 +000092 if (!I->isExternal() && // Function must be defined here
93 !I->hasInternalLinkage() && // Can't already have internal linkage
94 !ExternalNames.count(I->getName())) {// Not marked to keep external?
Chris Lattner379a8d22003-04-16 20:28:45 +000095 I->setLinkage(GlobalValue::InternalLinkage);
Chris Lattner47cc3662002-07-30 19:48:44 +000096 Changed = true;
97 ++NumFunctions;
98 DEBUG(std::cerr << "Internalizing func " << I->getName() << "\n");
99 }
Chris Lattner1b94c002002-04-28 05:43:27 +0000100
Chris Lattner47cc3662002-07-30 19:48:44 +0000101 // Mark all global variables with initializers as internal as well...
Chris Lattner16599822005-12-05 05:07:38 +0000102 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
103 I != E; ++I)
Chris Lattner44457bb2003-05-22 19:34:49 +0000104 if (!I->isExternal() && !I->hasInternalLinkage() &&
Chris Lattner16599822005-12-05 05:07:38 +0000105 !ExternalNames.count(I->getName()) &&
106 // *never* internalize the llvm.used symbol, used to implement
107 // attribute((used)).
108 I->getName() != "llvm.used") {
Chris Lattnerfd5d3232003-06-26 05:30:40 +0000109 // Special case handling of the global ctor and dtor list. When we
110 // internalize it, we mark it constant, which allows elimination of
111 // the list if it's empty.
112 //
113 if (I->hasAppendingLinkage() && (I->getName() == "llvm.global_ctors"||
114 I->getName() == "llvm.global_dtors"))
115 I->setConstant(true);
116
Chris Lattner379a8d22003-04-16 20:28:45 +0000117 I->setLinkage(GlobalValue::InternalLinkage);
Chris Lattner47cc3662002-07-30 19:48:44 +0000118 Changed = true;
119 ++NumGlobals;
120 DEBUG(std::cerr << "Internalizing gvar " << I->getName() << "\n");
121 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000122
Chris Lattner47cc3662002-07-30 19:48:44 +0000123 return Changed;
124 }
125 };
Chris Lattner1b94c002002-04-28 05:43:27 +0000126
Chris Lattner44457bb2003-05-22 19:34:49 +0000127 RegisterOpt<InternalizePass> X("internalize", "Internalize Global Symbols");
Chris Lattnerb28b6802002-07-23 18:06:35 +0000128} // end anonymous namespace
129
Chris Lattner45517ba2005-10-18 06:29:22 +0000130ModulePass *llvm::createInternalizePass(bool InternalizeEverything) {
131 return new InternalizePass(InternalizeEverything);
Chris Lattner1b94c002002-04-28 05:43:27 +0000132}