Internalize.cpp revision f629309f74cf1a64aa7fd1cd5784fd7db9a8f59e
1dbb1735673ed177a85f04698b9cd89f2dc1b4e91Chris Lattner//===-- Internalize.cpp - Mark functions internal -------------------------===//
2dbb1735673ed177a85f04698b9cd89f2dc1b4e91Chris Lattner//
3dbb1735673ed177a85f04698b9cd89f2dc1b4e91Chris Lattner// This pass loops over all of the functions in the input module, looking for a
4dbb1735673ed177a85f04698b9cd89f2dc1b4e91Chris Lattner// main function.  If a main function is found, all other functions are marked
5dbb1735673ed177a85f04698b9cd89f2dc1b4e91Chris Lattner// as internal.
6dbb1735673ed177a85f04698b9cd89f2dc1b4e91Chris Lattner//
7dbb1735673ed177a85f04698b9cd89f2dc1b4e91Chris Lattner//===----------------------------------------------------------------------===//
8dbb1735673ed177a85f04698b9cd89f2dc1b4e91Chris Lattner
9dbb1735673ed177a85f04698b9cd89f2dc1b4e91Chris Lattner#include "llvm/Transforms/IPO/Internalize.h"
10dbb1735673ed177a85f04698b9cd89f2dc1b4e91Chris Lattner#include "llvm/Pass.h"
11dbb1735673ed177a85f04698b9cd89f2dc1b4e91Chris Lattner#include "llvm/Module.h"
12dbb1735673ed177a85f04698b9cd89f2dc1b4e91Chris Lattner#include "llvm/Function.h"
133dec1f272219ee1f8e1499929cdf53f5bc3c2272Chris Lattner#include "Support/StatisticReporter.h"
143dec1f272219ee1f8e1499929cdf53f5bc3c2272Chris Lattner
153dec1f272219ee1f8e1499929cdf53f5bc3c2272Chris Lattnerstatic Statistic<> NumChanged("internalize\t- Number of functions internal'd");
16dbb1735673ed177a85f04698b9cd89f2dc1b4e91Chris Lattner
17f629309f74cf1a64aa7fd1cd5784fd7db9a8f59eChris Lattnernamespace {
18dbb1735673ed177a85f04698b9cd89f2dc1b4e91Chris Lattnerclass InternalizePass : public Pass {
190b12b5f50ec77a8bd01b92d287c52d748619bb4bChris Lattner  virtual bool run(Module &M) {
20dbb1735673ed177a85f04698b9cd89f2dc1b4e91Chris Lattner    bool FoundMain = false;   // Look for a function named main...
210b12b5f50ec77a8bd01b92d287c52d748619bb4bChris Lattner    for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
220b12b5f50ec77a8bd01b92d287c52d748619bb4bChris Lattner      if (I->getName() == "main" && !I->isExternal()) {
23dbb1735673ed177a85f04698b9cd89f2dc1b4e91Chris Lattner        FoundMain = true;
24dbb1735673ed177a85f04698b9cd89f2dc1b4e91Chris Lattner        break;
25dbb1735673ed177a85f04698b9cd89f2dc1b4e91Chris Lattner      }
26dbb1735673ed177a85f04698b9cd89f2dc1b4e91Chris Lattner
27dbb1735673ed177a85f04698b9cd89f2dc1b4e91Chris Lattner    if (!FoundMain) return false;  // No main found, must be a library...
28dbb1735673ed177a85f04698b9cd89f2dc1b4e91Chris Lattner
29dbb1735673ed177a85f04698b9cd89f2dc1b4e91Chris Lattner    bool Changed = false;
30dbb1735673ed177a85f04698b9cd89f2dc1b4e91Chris Lattner
31dbb1735673ed177a85f04698b9cd89f2dc1b4e91Chris Lattner    // Found a main function, mark all functions not named main as internal.
320b12b5f50ec77a8bd01b92d287c52d748619bb4bChris Lattner    for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
330b12b5f50ec77a8bd01b92d287c52d748619bb4bChris Lattner      if (I->getName() != "main" &&   // Leave the main function external
340b12b5f50ec77a8bd01b92d287c52d748619bb4bChris Lattner          !I->isExternal()) {         // Function must be defined here
350b12b5f50ec77a8bd01b92d287c52d748619bb4bChris Lattner        I->setInternalLinkage(true);
363dec1f272219ee1f8e1499929cdf53f5bc3c2272Chris Lattner        Changed = true;
373dec1f272219ee1f8e1499929cdf53f5bc3c2272Chris Lattner        ++NumChanged;
383dec1f272219ee1f8e1499929cdf53f5bc3c2272Chris Lattner      }
39dbb1735673ed177a85f04698b9cd89f2dc1b4e91Chris Lattner
40dbb1735673ed177a85f04698b9cd89f2dc1b4e91Chris Lattner    return Changed;
41dbb1735673ed177a85f04698b9cd89f2dc1b4e91Chris Lattner  }
42dbb1735673ed177a85f04698b9cd89f2dc1b4e91Chris Lattner};
43dbb1735673ed177a85f04698b9cd89f2dc1b4e91Chris Lattner
44f629309f74cf1a64aa7fd1cd5784fd7db9a8f59eChris LattnerRegisterPass<InternalizePass> X("internalize", "Internalize Functions");
45f629309f74cf1a64aa7fd1cd5784fd7db9a8f59eChris Lattner} // end anonymous namespace
46f629309f74cf1a64aa7fd1cd5784fd7db9a8f59eChris Lattner
47dbb1735673ed177a85f04698b9cd89f2dc1b4e91Chris LattnerPass *createInternalizePass() {
48dbb1735673ed177a85f04698b9cd89f2dc1b4e91Chris Lattner  return new InternalizePass();
49dbb1735673ed177a85f04698b9cd89f2dc1b4e91Chris Lattner}
50