blob: 0ed76aba6280d2fc20620e8f11761c99e8f159d3 [file] [log] [blame]
Chris Lattnerdbb17352002-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 Lattner55e41ba2002-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 Lattnerdbb17352002-04-28 05:43:27 +00006//
7//===----------------------------------------------------------------------===//
8
Chris Lattner568ddab2002-07-24 17:12:05 +00009#include "llvm/Transforms/IPO.h"
Chris Lattnerdbb17352002-04-28 05:43:27 +000010#include "llvm/Pass.h"
11#include "llvm/Module.h"
Chris Lattnera92f6962002-10-01 22:38:41 +000012#include "Support/Statistic.h"
Chris Lattnerc7a2c7f2003-05-22 19:34:49 +000013#include "Support/CommandLine.h"
14#include <fstream>
15#include <set>
Chris Lattner3dec1f22002-05-10 15:38:35 +000016
Chris Lattnerf6293092002-07-23 18:06:35 +000017namespace {
Chris Lattnera92f6962002-10-01 22:38:41 +000018 Statistic<> NumFunctions("internalize", "Number of functions internalized");
19 Statistic<> NumGlobals ("internalize", "Number of global vars internalized");
Chris Lattnerdbb17352002-04-28 05:43:27 +000020
Chris Lattnerc7a2c7f2003-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 Lattner2345d712003-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 Lattner88c7c322003-05-22 20:27:13 +000030 cl::desc("A list of symbol names to preserve"),
31 cl::CommaSeparated);
Chris Lattner2345d712003-05-22 19:48:00 +000032
Chris Lattner55e41ba2002-07-30 19:48:44 +000033 class InternalizePass : public Pass {
Chris Lattnerc7a2c7f2003-05-22 19:34:49 +000034 std::set<std::string> ExternalNames;
35 public:
36 InternalizePass() {
Chris Lattner2345d712003-05-22 19:48:00 +000037 if (!APIFile.empty()) // If a filename is specified, use it
Chris Lattnerc7a2c7f2003-05-22 19:34:49 +000038 LoadFile(APIFile.c_str());
Chris Lattner2345d712003-05-22 19:48:00 +000039 else // Else, if a list is specified, use it.
40 ExternalNames.insert(APIList.begin(), APIList.end());
Chris Lattnerc7a2c7f2003-05-22 19:34:49 +000041 }
Chris Lattner93fbd732002-11-08 20:34:21 +000042
Chris Lattnerc7a2c7f2003-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 Lattner2345d712003-05-22 19:48:00 +000048 << "'!\n";
Chris Lattnerc7a2c7f2003-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 Lattner2345d712003-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 Lattner55e41ba2002-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 Lattnerc7a2c7f2003-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 Lattner4ad02e72003-04-16 20:28:45 +000080 I->setLinkage(GlobalValue::InternalLinkage);
Chris Lattner55e41ba2002-07-30 19:48:44 +000081 Changed = true;
82 ++NumFunctions;
83 DEBUG(std::cerr << "Internalizing func " << I->getName() << "\n");
84 }
Chris Lattnerdbb17352002-04-28 05:43:27 +000085
Chris Lattner55e41ba2002-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 Lattnerc7a2c7f2003-05-22 19:34:49 +000088 if (!I->isExternal() && !I->hasInternalLinkage() &&
89 !ExternalNames.count(I->getName())) {
Chris Lattner81d4e142003-06-26 05:30:40 +000090 // Special case handling of the global ctor and dtor list. When we
91 // internalize it, we mark it constant, which allows elimination of
92 // the list if it's empty.
93 //
94 if (I->hasAppendingLinkage() && (I->getName() == "llvm.global_ctors"||
95 I->getName() == "llvm.global_dtors"))
96 I->setConstant(true);
97
Chris Lattner4ad02e72003-04-16 20:28:45 +000098 I->setLinkage(GlobalValue::InternalLinkage);
Chris Lattner55e41ba2002-07-30 19:48:44 +000099 Changed = true;
100 ++NumGlobals;
101 DEBUG(std::cerr << "Internalizing gvar " << I->getName() << "\n");
102 }
103
104 return Changed;
105 }
106 };
Chris Lattnerdbb17352002-04-28 05:43:27 +0000107
Chris Lattnerc7a2c7f2003-05-22 19:34:49 +0000108 RegisterOpt<InternalizePass> X("internalize", "Internalize Global Symbols");
Chris Lattnerf6293092002-07-23 18:06:35 +0000109} // end anonymous namespace
110
Chris Lattnerdbb17352002-04-28 05:43:27 +0000111Pass *createInternalizePass() {
112 return new InternalizePass();
113}