Add missing and needed #include.
[oota-llvm.git] / lib / Transforms / IPO / StripDeadPrototypes.cpp
1 //===-- StripDeadPrototypes.cpp - Removed unused function declarations ----===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Reid Spencer and is distributed under the 
6 // 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 
11 // dead declarations and removes them.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/Transforms/IPO.h"
16 #include "llvm/Pass.h"
17 #include "llvm/Module.h"
18 #include "llvm/ADT/Statistic.h"
19 #include "llvm/Support/Debug.h"
20 #include "llvm/Support/Compiler.h"
21 #include <vector>
22 using namespace llvm;
23
24 STATISTIC(NumDeadPrototypes, "Number of dead prototypes removed");
25
26 namespace {
27
28 /// @brief Pass to remove unused function declarations.
29 class VISIBILITY_HIDDEN StripDeadPrototypesPass : public ModulePass {
30 public:
31   StripDeadPrototypesPass() { }
32   virtual bool runOnModule(Module &M);
33 };
34 RegisterPass<StripDeadPrototypesPass> X("strip-dead-prototypes", 
35                                         "Strip Unused Function Prototypes");
36
37 } // end anonymous namespace
38
39 bool StripDeadPrototypesPass::runOnModule(Module &M) {
40   // Collect all the functions we want to erase
41   std::vector<Function*> FuncsToErase;
42   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
43     if (I->isDeclaration() &&         // Function must be only a prototype
44         I->use_empty()) {             // Function must not be used
45       FuncsToErase.push_back(&(*I));
46     }
47
48   // Erase the functions
49   for (std::vector<Function*>::iterator I = FuncsToErase.begin(), 
50        E = FuncsToErase.end(); I != E; ++I )
51     (*I)->eraseFromParent();
52   
53   // Increment the statistic
54   NumDeadPrototypes += FuncsToErase.size();
55
56   // Return an indication of whether we changed anything or not.
57   return !FuncsToErase.empty();
58 }
59
60 ModulePass *llvm::createStripDeadPrototypesPass() {
61   return new StripDeadPrototypesPass();
62 }