blob: 7bca45970a96e3133aab6fe5767c1fff59c4bfbc [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 Lattnerc7a2c7f2003-05-22 19:34:49 +000012#include "Support/CommandLine.h"
Chris Lattner6806f562003-08-01 22:15:03 +000013#include "Support/Debug.h"
14#include "Support/Statistic.h"
Chris Lattnerc7a2c7f2003-05-22 19:34:49 +000015#include <fstream>
16#include <set>
Chris Lattner3dec1f22002-05-10 15:38:35 +000017
Chris Lattnerf6293092002-07-23 18:06:35 +000018namespace {
Chris Lattnera92f6962002-10-01 22:38:41 +000019 Statistic<> NumFunctions("internalize", "Number of functions internalized");
20 Statistic<> NumGlobals ("internalize", "Number of global vars internalized");
Chris Lattnerdbb17352002-04-28 05:43:27 +000021
Chris Lattnerc7a2c7f2003-05-22 19:34:49 +000022 // APIFile - A file which contains a list of symbols that should not be marked
23 // external.
24 cl::opt<std::string>
25 APIFile("internalize-public-api-file", cl::value_desc("filename"),
Chris Lattner2345d712003-05-22 19:48:00 +000026 cl::desc("A file containing list of symbol names to preserve"));
27
28 // APIList - A list of symbols that should not be marked internal.
29 cl::list<std::string>
30 APIList("internalize-public-api-list", cl::value_desc("list"),
Chris Lattner88c7c322003-05-22 20:27:13 +000031 cl::desc("A list of symbol names to preserve"),
32 cl::CommaSeparated);
Chris Lattner2345d712003-05-22 19:48:00 +000033
Chris Lattner55e41ba2002-07-30 19:48:44 +000034 class InternalizePass : public Pass {
Chris Lattnerc7a2c7f2003-05-22 19:34:49 +000035 std::set<std::string> ExternalNames;
36 public:
37 InternalizePass() {
Chris Lattner2345d712003-05-22 19:48:00 +000038 if (!APIFile.empty()) // If a filename is specified, use it
Chris Lattnerc7a2c7f2003-05-22 19:34:49 +000039 LoadFile(APIFile.c_str());
Chris Lattner2345d712003-05-22 19:48:00 +000040 else // Else, if a list is specified, use it.
41 ExternalNames.insert(APIList.begin(), APIList.end());
Chris Lattnerc7a2c7f2003-05-22 19:34:49 +000042 }
Chris Lattner93fbd732002-11-08 20:34:21 +000043
Chris Lattnerc7a2c7f2003-05-22 19:34:49 +000044 void LoadFile(const char *Filename) {
45 // Load the APIFile...
46 std::ifstream In(Filename);
47 if (!In.good()) {
48 std::cerr << "WARNING: Internalize couldn't load file '" << Filename
Chris Lattner2345d712003-05-22 19:48:00 +000049 << "'!\n";
Chris Lattnerc7a2c7f2003-05-22 19:34:49 +000050 return; // Do not internalize anything...
51 }
52 while (In) {
53 std::string Symbol;
54 In >> Symbol;
55 if (!Symbol.empty())
56 ExternalNames.insert(Symbol);
57 }
58 }
59
60 virtual bool run(Module &M) {
Chris Lattner2345d712003-05-22 19:48:00 +000061 // If no list or file of symbols was specified, check to see if there is a
62 // "main" symbol defined in the module. If so, use it, otherwise do not
63 // internalize the module, it must be a library or something.
64 //
65 if (ExternalNames.empty()) {
66 Function *MainFunc = M.getMainFunction();
67 if (MainFunc == 0 || MainFunc->isExternal())
68 return false; // No main found, must be a library...
69
70 // Preserve main, internalize all else.
71 ExternalNames.insert(MainFunc->getName());
72 }
73
Chris Lattner55e41ba2002-07-30 19:48:44 +000074 bool Changed = false;
75
76 // Found a main function, mark all functions not named main as internal.
77 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattnerc7a2c7f2003-05-22 19:34:49 +000078 if (!I->isExternal() && // Function must be defined here
79 !I->hasInternalLinkage() && // Can't already have internal linkage
80 !ExternalNames.count(I->getName())) {// Not marked to keep external?
Chris Lattner4ad02e72003-04-16 20:28:45 +000081 I->setLinkage(GlobalValue::InternalLinkage);
Chris Lattner55e41ba2002-07-30 19:48:44 +000082 Changed = true;
83 ++NumFunctions;
84 DEBUG(std::cerr << "Internalizing func " << I->getName() << "\n");
85 }
Chris Lattnerdbb17352002-04-28 05:43:27 +000086
Chris Lattner55e41ba2002-07-30 19:48:44 +000087 // Mark all global variables with initializers as internal as well...
88 for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
Chris Lattnerc7a2c7f2003-05-22 19:34:49 +000089 if (!I->isExternal() && !I->hasInternalLinkage() &&
90 !ExternalNames.count(I->getName())) {
Chris Lattner81d4e142003-06-26 05:30:40 +000091 // Special case handling of the global ctor and dtor list. When we
92 // internalize it, we mark it constant, which allows elimination of
93 // the list if it's empty.
94 //
95 if (I->hasAppendingLinkage() && (I->getName() == "llvm.global_ctors"||
96 I->getName() == "llvm.global_dtors"))
97 I->setConstant(true);
98
Chris Lattner4ad02e72003-04-16 20:28:45 +000099 I->setLinkage(GlobalValue::InternalLinkage);
Chris Lattner55e41ba2002-07-30 19:48:44 +0000100 Changed = true;
101 ++NumGlobals;
102 DEBUG(std::cerr << "Internalizing gvar " << I->getName() << "\n");
103 }
104
105 return Changed;
106 }
107 };
Chris Lattnerdbb17352002-04-28 05:43:27 +0000108
Chris Lattnerc7a2c7f2003-05-22 19:34:49 +0000109 RegisterOpt<InternalizePass> X("internalize", "Internalize Global Symbols");
Chris Lattnerf6293092002-07-23 18:06:35 +0000110} // end anonymous namespace
111
Chris Lattnerdbb17352002-04-28 05:43:27 +0000112Pass *createInternalizePass() {
113 return new InternalizePass();
114}