Create a pass to strip dead function declarations (prototypes). This is
[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 <vector>
21 using namespace llvm;
22
23 STATISTIC(NumDeadPrototypes, "Number of dead prototypes removed");
24
25 namespace {
26
27 /// @brief Pass to remove unused function declarations.
28 class StripDeadPrototypesPass : public ModulePass {
29 public:
30   StripDeadPrototypesPass() { }
31   virtual bool runOnModule(Module &M);
32 };
33 RegisterPass<StripDeadPrototypesPass> X("strip-dead-prototypes", 
34                                         "Strip Unused Function Prototypes");
35
36 bool StripDeadPrototypesPass::runOnModule(Module &M) {
37   // Collect all the functions we want to erase
38   std::vector<Function*> FuncsToErase;
39   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
40     if (I->isDeclaration() &&         // Function must be only a prototype
41         I->use_empty()) {             // Function must not be used
42       FuncsToErase.push_back(&(*I));
43     }
44
45   // Erase the functions
46   for (std::vector<Function*>::iterator I = FuncsToErase.begin(), 
47        E = FuncsToErase.end(); I != E; ++I )
48     (*I)->eraseFromParent();
49   
50   // Increment the statistic
51   NumDeadPrototypes += FuncsToErase.size();
52
53   // Return an indication of whether we changed anything or not.
54   return !FuncsToErase.empty();
55 }
56
57 } // end anonymous namespace
58
59 ModulePass *llvm::createStripDeadPrototypesPass() {
60   return new StripDeadPrototypesPass();
61 }