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