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