Internalize.cpp revision 44c3b9fdd416c79f4b67cde1aecfced5921efd81
1//===-- Internalize.cpp - Mark functions internal -------------------------===//
2//
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//===----------------------------------------------------------------------===//
9//
10// This pass loops over all of the functions in the input module, looking for a
11// main function.  If a main function is found, all other functions and all
12// global variables with initializers are marked as internal.
13//
14//===----------------------------------------------------------------------===//
15
16#define DEBUG_TYPE "internalize"
17#include "llvm/Transforms/IPO.h"
18#include "llvm/Pass.h"
19#include "llvm/Module.h"
20#include "llvm/Support/CommandLine.h"
21#include "llvm/Support/Debug.h"
22#include "llvm/ADT/Statistic.h"
23#include <fstream>
24#include <set>
25using namespace llvm;
26
27STATISTIC(NumFunctions, "Number of functions internalized");
28STATISTIC(NumGlobals  , "Number of global vars internalized");
29
30namespace {
31
32  // APIFile - A file which contains a list of symbols that should not be marked
33  // external.
34  cl::opt<std::string>
35  APIFile("internalize-public-api-file", cl::value_desc("filename"),
36          cl::desc("A file containing list of symbol names to preserve"));
37
38  // APIList - A list of symbols that should not be marked internal.
39  cl::list<std::string>
40  APIList("internalize-public-api-list", cl::value_desc("list"),
41          cl::desc("A list of symbol names to preserve"),
42          cl::CommaSeparated);
43
44  class InternalizePass : public ModulePass {
45    std::set<std::string> ExternalNames;
46    bool DontInternalize;
47  public:
48    InternalizePass(bool InternalizeEverything = true);
49    InternalizePass(const std::vector <const char *>& exportList);
50    void LoadFile(const char *Filename);
51    virtual bool runOnModule(Module &M);
52  };
53  RegisterPass<InternalizePass> X("internalize", "Internalize Global Symbols");
54} // end anonymous namespace
55
56InternalizePass::InternalizePass(bool InternalizeEverything)
57  : DontInternalize(false){
58  if (!APIFile.empty())           // If a filename is specified, use it
59    LoadFile(APIFile.c_str());
60  else if (!APIList.empty())      // Else, if a list is specified, use it.
61    ExternalNames.insert(APIList.begin(), APIList.end());
62  else if (!InternalizeEverything)
63    // Finally, if we're allowed to, internalize all but main.
64    DontInternalize = true;
65}
66
67InternalizePass::InternalizePass(const std::vector<const char *>&exportList)
68  : DontInternalize(false){
69  for(std::vector<const char *>::const_iterator itr = exportList.begin();
70	itr != exportList.end(); itr++) {
71    ExternalNames.insert(*itr);
72  }
73}
74
75void InternalizePass::LoadFile(const char *Filename) {
76  // Load the APIFile...
77  std::ifstream In(Filename);
78  if (!In.good()) {
79    cerr << "WARNING: Internalize couldn't load file '" << Filename << "'!\n";
80    return;   // Do not internalize anything...
81  }
82  while (In) {
83    std::string Symbol;
84    In >> Symbol;
85    if (!Symbol.empty())
86      ExternalNames.insert(Symbol);
87  }
88}
89
90bool InternalizePass::runOnModule(Module &M) {
91  if (DontInternalize) return false;
92
93  // If no list or file of symbols was specified, check to see if there is a
94  // "main" symbol defined in the module.  If so, use it, otherwise do not
95  // internalize the module, it must be a library or something.
96  //
97  if (ExternalNames.empty()) {
98    Function *MainFunc = M.getMainFunction();
99    if (MainFunc == 0 || MainFunc->isExternal())
100      return false;  // No main found, must be a library...
101
102    // Preserve main, internalize all else.
103    ExternalNames.insert(MainFunc->getName());
104  }
105
106  bool Changed = false;
107
108  // Found a main function, mark all functions not named main as internal.
109  for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
110    if (!I->isExternal() &&         // Function must be defined here
111        !I->hasInternalLinkage() &&  // Can't already have internal linkage
112        !ExternalNames.count(I->getName())) {// Not marked to keep external?
113      I->setLinkage(GlobalValue::InternalLinkage);
114      Changed = true;
115      ++NumFunctions;
116      DOUT << "Internalizing func " << I->getName() << "\n";
117    }
118
119  // Never internalize the llvm.used symbol.  It is used to implement
120  // attribute((used)).
121  ExternalNames.insert("llvm.used");
122
123  // Never internalize anchors used by the machine module info, else the info
124  // won't find them.  (see MachineModuleInfo.)
125  ExternalNames.insert("llvm.dbg.compile_units");
126  ExternalNames.insert("llvm.dbg.global_variables");
127  ExternalNames.insert("llvm.dbg.subprograms");
128
129  // Mark all global variables with initializers as internal as well.
130  for (Module::global_iterator I = M.global_begin(), E = M.global_end();
131       I != E; ++I)
132    if (!I->isExternal() && !I->hasInternalLinkage() &&
133        !ExternalNames.count(I->getName())) {
134      // Special case handling of the global ctor and dtor list.  When we
135      // internalize it, we mark it constant, which allows elimination of
136      // the list if it's empty.
137      //
138      if (I->hasAppendingLinkage() && (I->getName() == "llvm.global_ctors" ||
139                                       I->getName() == "llvm.global_dtors")) {
140        // If the global ctors/dtors list has no uses, do not internalize it, as
141        // there is no __main in this program, so the asmprinter should handle
142        // it.
143        if (I->use_empty()) continue;
144
145        // Otherwise, also mark the list constant, as we know that it will not
146        // be mutated any longer, and the makes simple IPO xforms automatically
147        // better.
148        I->setConstant(true);
149      }
150
151      I->setLinkage(GlobalValue::InternalLinkage);
152      Changed = true;
153      ++NumGlobals;
154      DOUT << "Internalized gvar " << I->getName() << "\n";
155    }
156
157  return Changed;
158}
159
160ModulePass *llvm::createInternalizePass(bool InternalizeEverything) {
161  return new InternalizePass(InternalizeEverything);
162}
163
164ModulePass *llvm::createInternalizePass(const std::vector <const char *> &el) {
165  return new InternalizePass(el);
166}
167