1 //===-- StripDeadPrototypes.cpp - Removed unused function declarations ----===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This pass loops over all of the functions in the input module, looking for
11 // dead declarations and removes them.
13 //===----------------------------------------------------------------------===//
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"
23 STATISTIC(NumDeadPrototypes, "Number of dead prototypes removed");
27 /// @brief Pass to remove unused function declarations.
28 class VISIBILITY_HIDDEN StripDeadPrototypesPass : public ModulePass {
30 StripDeadPrototypesPass() { }
31 virtual bool runOnModule(Module &M);
33 RegisterPass<StripDeadPrototypesPass> X("strip-dead-prototypes",
34 "Strip Unused Function Prototypes");
36 } // end anonymous namespace
38 bool StripDeadPrototypesPass::runOnModule(Module &M) {
39 // Collect all the functions we want to erase
40 std::vector<Function*> FuncsToErase;
41 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
42 if (I->isDeclaration() && // Function must be only a prototype
43 I->use_empty()) { // Function must not be used
44 FuncsToErase.push_back(&(*I));
47 // Erase the functions
48 for (std::vector<Function*>::iterator I = FuncsToErase.begin(),
49 E = FuncsToErase.end(); I != E; ++I )
50 (*I)->eraseFromParent();
52 // Increment the statistic
53 NumDeadPrototypes += FuncsToErase.size();
55 // Return an indication of whether we changed anything or not.
56 return !FuncsToErase.empty();
59 ModulePass *llvm::createStripDeadPrototypesPass() {
60 return new StripDeadPrototypesPass();