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 StripDeadPrototypesPass : public ModulePass {
30 StripDeadPrototypesPass() { }
31 virtual bool runOnModule(Module &M);
33 RegisterPass<StripDeadPrototypesPass> X("strip-dead-prototypes",
34 "Strip Unused Function Prototypes");
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));
45 // Erase the functions
46 for (std::vector<Function*>::iterator I = FuncsToErase.begin(),
47 E = FuncsToErase.end(); I != E; ++I )
48 (*I)->eraseFromParent();
50 // Increment the statistic
51 NumDeadPrototypes += FuncsToErase.size();
53 // Return an indication of whether we changed anything or not.
54 return !FuncsToErase.empty();
57 } // end anonymous namespace
59 ModulePass *llvm::createStripDeadPrototypesPass() {
60 return new StripDeadPrototypesPass();