blob: 5a254fa687f308a18fc2341dcd5c1b8d99ed3e50 [file] [log] [blame]
Chris Lattnerdbb17352002-04-28 05:43:27 +00001//===-- Internalize.cpp - Mark functions internal -------------------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// 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.
7//
8//===----------------------------------------------------------------------===//
Chris Lattnerdbb17352002-04-28 05:43:27 +00009//
10// This pass loops over all of the functions in the input module, looking for a
Chris Lattner55e41ba2002-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 Lattnerdbb17352002-04-28 05:43:27 +000013//
14//===----------------------------------------------------------------------===//
15
Chris Lattner568ddab2002-07-24 17:12:05 +000016#include "llvm/Transforms/IPO.h"
Chris Lattnerdbb17352002-04-28 05:43:27 +000017#include "llvm/Pass.h"
18#include "llvm/Module.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000019#include "llvm/Support/CommandLine.h"
20#include "llvm/Support/Debug.h"
21#include "llvm/ADT/Statistic.h"
Chris Lattnerc7a2c7f2003-05-22 19:34:49 +000022#include <fstream>
23#include <set>
Chris Lattner1e2385b2003-11-21 21:54:22 +000024using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000025
Chris Lattnerf6293092002-07-23 18:06:35 +000026namespace {
Chris Lattnera92f6962002-10-01 22:38:41 +000027 Statistic<> NumFunctions("internalize", "Number of functions internalized");
28 Statistic<> NumGlobals ("internalize", "Number of global vars internalized");
Chris Lattnerdbb17352002-04-28 05:43:27 +000029
Chris Lattnerc7a2c7f2003-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 Lattner2345d712003-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 Lattner88c7c322003-05-22 20:27:13 +000039 cl::desc("A list of symbol names to preserve"),
40 cl::CommaSeparated);
Chris Lattner2345d712003-05-22 19:48:00 +000041
Chris Lattnerb12914b2004-09-20 04:48:05 +000042 class InternalizePass : public ModulePass {
Chris Lattnerc7a2c7f2003-05-22 19:34:49 +000043 std::set<std::string> ExternalNames;
44 public:
45 InternalizePass() {
Chris Lattner2345d712003-05-22 19:48:00 +000046 if (!APIFile.empty()) // If a filename is specified, use it
Chris Lattnerc7a2c7f2003-05-22 19:34:49 +000047 LoadFile(APIFile.c_str());
Chris Lattner2345d712003-05-22 19:48:00 +000048 else // Else, if a list is specified, use it.
49 ExternalNames.insert(APIList.begin(), APIList.end());
Chris Lattnerc7a2c7f2003-05-22 19:34:49 +000050 }
Chris Lattner93fbd732002-11-08 20:34:21 +000051
Chris Lattnerc7a2c7f2003-05-22 19:34:49 +000052 void LoadFile(const char *Filename) {
53 // Load the APIFile...
54 std::ifstream In(Filename);
55 if (!In.good()) {
56 std::cerr << "WARNING: Internalize couldn't load file '" << Filename
Chris Lattner2345d712003-05-22 19:48:00 +000057 << "'!\n";
Chris Lattnerc7a2c7f2003-05-22 19:34:49 +000058 return; // Do not internalize anything...
59 }
60 while (In) {
61 std::string Symbol;
62 In >> Symbol;
63 if (!Symbol.empty())
64 ExternalNames.insert(Symbol);
65 }
66 }
67
Chris Lattnerb12914b2004-09-20 04:48:05 +000068 virtual bool runOnModule(Module &M) {
Chris Lattner2345d712003-05-22 19:48:00 +000069 // If no list or file of symbols was specified, check to see if there is a
70 // "main" symbol defined in the module. If so, use it, otherwise do not
71 // internalize the module, it must be a library or something.
72 //
73 if (ExternalNames.empty()) {
74 Function *MainFunc = M.getMainFunction();
75 if (MainFunc == 0 || MainFunc->isExternal())
76 return false; // No main found, must be a library...
77
78 // Preserve main, internalize all else.
79 ExternalNames.insert(MainFunc->getName());
80 }
81
Chris Lattner55e41ba2002-07-30 19:48:44 +000082 bool Changed = false;
83
84 // Found a main function, mark all functions not named main as internal.
85 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattnerc7a2c7f2003-05-22 19:34:49 +000086 if (!I->isExternal() && // Function must be defined here
87 !I->hasInternalLinkage() && // Can't already have internal linkage
88 !ExternalNames.count(I->getName())) {// Not marked to keep external?
Chris Lattner4ad02e72003-04-16 20:28:45 +000089 I->setLinkage(GlobalValue::InternalLinkage);
Chris Lattner55e41ba2002-07-30 19:48:44 +000090 Changed = true;
91 ++NumFunctions;
92 DEBUG(std::cerr << "Internalizing func " << I->getName() << "\n");
93 }
Chris Lattnerdbb17352002-04-28 05:43:27 +000094
Chris Lattner55e41ba2002-07-30 19:48:44 +000095 // Mark all global variables with initializers as internal as well...
Chris Lattnere4d5c442005-03-15 04:54:21 +000096 for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
Chris Lattnerc7a2c7f2003-05-22 19:34:49 +000097 if (!I->isExternal() && !I->hasInternalLinkage() &&
98 !ExternalNames.count(I->getName())) {
Chris Lattner81d4e142003-06-26 05:30:40 +000099 // Special case handling of the global ctor and dtor list. When we
100 // internalize it, we mark it constant, which allows elimination of
101 // the list if it's empty.
102 //
103 if (I->hasAppendingLinkage() && (I->getName() == "llvm.global_ctors"||
104 I->getName() == "llvm.global_dtors"))
105 I->setConstant(true);
106
Chris Lattner4ad02e72003-04-16 20:28:45 +0000107 I->setLinkage(GlobalValue::InternalLinkage);
Chris Lattner55e41ba2002-07-30 19:48:44 +0000108 Changed = true;
109 ++NumGlobals;
110 DEBUG(std::cerr << "Internalizing gvar " << I->getName() << "\n");
111 }
112
113 return Changed;
114 }
115 };
Chris Lattnerdbb17352002-04-28 05:43:27 +0000116
Chris Lattnerc7a2c7f2003-05-22 19:34:49 +0000117 RegisterOpt<InternalizePass> X("internalize", "Internalize Global Symbols");
Chris Lattnerf6293092002-07-23 18:06:35 +0000118} // end anonymous namespace
119
Chris Lattnerb12914b2004-09-20 04:48:05 +0000120ModulePass *llvm::createInternalizePass() {
Chris Lattnerdbb17352002-04-28 05:43:27 +0000121 return new InternalizePass();
122}