Reid Spencer | c19fbd6 | 2007-02-05 20:24:25 +0000 | [diff] [blame] | 1 | //===-- StripDeadPrototypes.cpp - Removed unused function declarations ----===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame^] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | c19fbd6 | 2007-02-05 20:24:25 +0000 | [diff] [blame] | 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 | |
Chris Lattner | eb47391 | 2007-02-18 22:10:34 +0000 | [diff] [blame] | 15 | #define DEBUG_TYPE "strip-dead-prototypes" |
Reid Spencer | c19fbd6 | 2007-02-05 20:24:25 +0000 | [diff] [blame] | 16 | #include "llvm/Transforms/IPO.h" |
| 17 | #include "llvm/Pass.h" |
| 18 | #include "llvm/Module.h" |
| 19 | #include "llvm/ADT/Statistic.h" |
Reid Spencer | 7ea0765 | 2007-02-05 21:47:39 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Compiler.h" |
Reid Spencer | c19fbd6 | 2007-02-05 20:24:25 +0000 | [diff] [blame] | 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. |
Reid Spencer | 9153a67 | 2007-02-05 21:45:12 +0000 | [diff] [blame] | 28 | class VISIBILITY_HIDDEN StripDeadPrototypesPass : public ModulePass { |
Reid Spencer | c19fbd6 | 2007-02-05 20:24:25 +0000 | [diff] [blame] | 29 | public: |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 30 | static char ID; // Pass identification, replacement for typeid |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 31 | StripDeadPrototypesPass() : ModulePass((intptr_t)&ID) { } |
Reid Spencer | c19fbd6 | 2007-02-05 20:24:25 +0000 | [diff] [blame] | 32 | virtual bool runOnModule(Module &M); |
| 33 | }; |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 34 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 35 | char StripDeadPrototypesPass::ID = 0; |
Reid Spencer | c19fbd6 | 2007-02-05 20:24:25 +0000 | [diff] [blame] | 36 | RegisterPass<StripDeadPrototypesPass> X("strip-dead-prototypes", |
| 37 | "Strip Unused Function Prototypes"); |
| 38 | |
Reid Spencer | 9153a67 | 2007-02-05 21:45:12 +0000 | [diff] [blame] | 39 | } // end anonymous namespace |
| 40 | |
Reid Spencer | c19fbd6 | 2007-02-05 20:24:25 +0000 | [diff] [blame] | 41 | bool StripDeadPrototypesPass::runOnModule(Module &M) { |
Chris Lattner | eb47391 | 2007-02-18 22:10:34 +0000 | [diff] [blame] | 42 | bool MadeChange = false; |
Reid Spencer | c19fbd6 | 2007-02-05 20:24:25 +0000 | [diff] [blame] | 43 | |
Chris Lattner | eb47391 | 2007-02-18 22:10:34 +0000 | [diff] [blame] | 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 | } |
Reid Spencer | c19fbd6 | 2007-02-05 20:24:25 +0000 | [diff] [blame] | 54 | |
Chris Lattner | 4a6f363 | 2007-02-18 22:10:58 +0000 | [diff] [blame] | 55 | // Erase dead global var prototypes. |
Chris Lattner | eb47391 | 2007-02-18 22:10:34 +0000 | [diff] [blame] | 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 | |
Reid Spencer | c19fbd6 | 2007-02-05 20:24:25 +0000 | [diff] [blame] | 64 | // Return an indication of whether we changed anything or not. |
Chris Lattner | eb47391 | 2007-02-18 22:10:34 +0000 | [diff] [blame] | 65 | return MadeChange; |
Reid Spencer | c19fbd6 | 2007-02-05 20:24:25 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Reid Spencer | c19fbd6 | 2007-02-05 20:24:25 +0000 | [diff] [blame] | 68 | ModulePass *llvm::createStripDeadPrototypesPass() { |
| 69 | return new StripDeadPrototypesPass(); |
| 70 | } |