Do not use typeinfo to identify pass in pass manager.
[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 #define DEBUG_TYPE "strip-dead-prototypes"
16 #include "llvm/Transforms/IPO.h"
17 #include "llvm/Pass.h"
18 #include "llvm/Module.h"
19 #include "llvm/ADT/Statistic.h"
20 #include "llvm/Support/Compiler.h"
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 VISIBILITY_HIDDEN StripDeadPrototypesPass : public ModulePass {
29 public:
30   static const int ID; // Pass identifcation, replacement for typeid
31   StripDeadPrototypesPass() : ModulePass((intptr_t)&ID) { }
32   virtual bool runOnModule(Module &M);
33 };
34
35 const int StripDeadPrototypesPass::ID = 0;
36 RegisterPass<StripDeadPrototypesPass> X("strip-dead-prototypes", 
37                                         "Strip Unused Function Prototypes");
38
39 } // end anonymous namespace
40
41 bool StripDeadPrototypesPass::runOnModule(Module &M) {
42   bool MadeChange = false;
43   
44   // Erase dead function prototypes.
45   for (Module::iterator I = M.begin(), E = M.end(); I != E; ) {
46     Function *F = I++;
47     // Function must be a prototype and unused.
48     if (F->isDeclaration() && F->use_empty()) {
49       F->eraseFromParent();
50       ++NumDeadPrototypes;
51       MadeChange = true;
52     }
53   }
54
55   // Erase dead global var prototypes.
56   for (Module::global_iterator I = M.global_begin(), E = M.global_end();
57        I != E; ) {
58     GlobalVariable *GV = I++;
59     // Global must be a prototype and unused.
60     if (GV->isDeclaration() && GV->use_empty())
61       GV->eraseFromParent();
62   }
63   
64   // Return an indication of whether we changed anything or not.
65   return MadeChange;
66 }
67
68 ModulePass *llvm::createStripDeadPrototypesPass() {
69   return new StripDeadPrototypesPass();
70 }