blob: ec28ecf81d0607ddd0f99af46d35e85ad4ea2dcb [file] [log] [blame]
Chris Lattner1b94c002002-04-28 05:43:27 +00001//===-- Internalize.cpp - Mark functions internal -------------------------===//
2//
3// This pass loops over all of the functions in the input module, looking for a
Chris Lattner47cc3662002-07-30 19:48:44 +00004// main function. If a main function is found, all other functions and all
5// global variables with initializers are marked as internal.
Chris Lattner1b94c002002-04-28 05:43:27 +00006//
7//===----------------------------------------------------------------------===//
8
Chris Lattner99a53f62002-07-24 17:12:05 +00009#include "llvm/Transforms/IPO.h"
Chris Lattner1b94c002002-04-28 05:43:27 +000010#include "llvm/Pass.h"
11#include "llvm/Module.h"
Chris Lattnerbf3a0992002-10-01 22:38:41 +000012#include "Support/Statistic.h"
Chris Lattner44457bb2003-05-22 19:34:49 +000013#include "Support/CommandLine.h"
14#include <fstream>
15#include <set>
Chris Lattner0b18c1d2002-05-10 15:38:35 +000016
Chris Lattnerb28b6802002-07-23 18:06:35 +000017namespace {
Chris Lattnerbf3a0992002-10-01 22:38:41 +000018 Statistic<> NumFunctions("internalize", "Number of functions internalized");
19 Statistic<> NumGlobals ("internalize", "Number of global vars internalized");
Chris Lattner1b94c002002-04-28 05:43:27 +000020
Chris Lattner44457bb2003-05-22 19:34:49 +000021 // APIFile - A file which contains a list of symbols that should not be marked
22 // external.
23 cl::opt<std::string>
24 APIFile("internalize-public-api-file", cl::value_desc("filename"),
Chris Lattnerad44cd82003-05-22 19:48:00 +000025 cl::desc("A file containing list of symbol names to preserve"));
26
27 // APIList - A list of symbols that should not be marked internal.
28 cl::list<std::string>
29 APIList("internalize-public-api-list", cl::value_desc("list"),
Chris Lattner224ae022003-05-22 20:27:13 +000030 cl::desc("A list of symbol names to preserve"),
31 cl::CommaSeparated);
Chris Lattnerad44cd82003-05-22 19:48:00 +000032
Chris Lattner47cc3662002-07-30 19:48:44 +000033 class InternalizePass : public Pass {
Chris Lattner44457bb2003-05-22 19:34:49 +000034 std::set<std::string> ExternalNames;
35 public:
36 InternalizePass() {
Chris Lattnerad44cd82003-05-22 19:48:00 +000037 if (!APIFile.empty()) // If a filename is specified, use it
Chris Lattner44457bb2003-05-22 19:34:49 +000038 LoadFile(APIFile.c_str());
Chris Lattnerad44cd82003-05-22 19:48:00 +000039 else // Else, if a list is specified, use it.
40 ExternalNames.insert(APIList.begin(), APIList.end());
Chris Lattner44457bb2003-05-22 19:34:49 +000041 }
Chris Lattner17069b32002-11-08 20:34:21 +000042
Chris Lattner44457bb2003-05-22 19:34:49 +000043 void LoadFile(const char *Filename) {
44 // Load the APIFile...
45 std::ifstream In(Filename);
46 if (!In.good()) {
47 std::cerr << "WARNING: Internalize couldn't load file '" << Filename
Chris Lattnerad44cd82003-05-22 19:48:00 +000048 << "'!\n";
Chris Lattner44457bb2003-05-22 19:34:49 +000049 return; // Do not internalize anything...
50 }
51 while (In) {
52 std::string Symbol;
53 In >> Symbol;
54 if (!Symbol.empty())
55 ExternalNames.insert(Symbol);
56 }
57 }
58
59 virtual bool run(Module &M) {
Chris Lattnerad44cd82003-05-22 19:48:00 +000060 // If no list or file of symbols was specified, check to see if there is a
61 // "main" symbol defined in the module. If so, use it, otherwise do not
62 // internalize the module, it must be a library or something.
63 //
64 if (ExternalNames.empty()) {
65 Function *MainFunc = M.getMainFunction();
66 if (MainFunc == 0 || MainFunc->isExternal())
67 return false; // No main found, must be a library...
68
69 // Preserve main, internalize all else.
70 ExternalNames.insert(MainFunc->getName());
71 }
72
Chris Lattner47cc3662002-07-30 19:48:44 +000073 bool Changed = false;
74
75 // Found a main function, mark all functions not named main as internal.
76 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattner44457bb2003-05-22 19:34:49 +000077 if (!I->isExternal() && // Function must be defined here
78 !I->hasInternalLinkage() && // Can't already have internal linkage
79 !ExternalNames.count(I->getName())) {// Not marked to keep external?
Chris Lattner379a8d22003-04-16 20:28:45 +000080 I->setLinkage(GlobalValue::InternalLinkage);
Chris Lattner47cc3662002-07-30 19:48:44 +000081 Changed = true;
82 ++NumFunctions;
83 DEBUG(std::cerr << "Internalizing func " << I->getName() << "\n");
84 }
Chris Lattner1b94c002002-04-28 05:43:27 +000085
Chris Lattner47cc3662002-07-30 19:48:44 +000086 // Mark all global variables with initializers as internal as well...
87 for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
Chris Lattner44457bb2003-05-22 19:34:49 +000088 if (!I->isExternal() && !I->hasInternalLinkage() &&
89 !ExternalNames.count(I->getName())) {
Chris Lattner379a8d22003-04-16 20:28:45 +000090 I->setLinkage(GlobalValue::InternalLinkage);
Chris Lattner47cc3662002-07-30 19:48:44 +000091 Changed = true;
92 ++NumGlobals;
93 DEBUG(std::cerr << "Internalizing gvar " << I->getName() << "\n");
94 }
95
96 return Changed;
97 }
98 };
Chris Lattner1b94c002002-04-28 05:43:27 +000099
Chris Lattner44457bb2003-05-22 19:34:49 +0000100 RegisterOpt<InternalizePass> X("internalize", "Internalize Global Symbols");
Chris Lattnerb28b6802002-07-23 18:06:35 +0000101} // end anonymous namespace
102
Chris Lattner1b94c002002-04-28 05:43:27 +0000103Pass *createInternalizePass() {
104 return new InternalizePass();
105}