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 #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"
23 STATISTIC(NumDeadPrototypes, "Number of dead prototypes removed");
27 /// @brief Pass to remove unused function declarations.
28 class VISIBILITY_HIDDEN StripDeadPrototypesPass : public ModulePass {
30 static char ID; // Pass identification, replacement for typeid
31 StripDeadPrototypesPass() : ModulePass((intptr_t)&ID) { }
32 virtual bool runOnModule(Module &M);
35 char StripDeadPrototypesPass::ID = 0;
36 RegisterPass<StripDeadPrototypesPass> X("strip-dead-prototypes",
37 "Strip Unused Function Prototypes");
39 } // end anonymous namespace
41 bool StripDeadPrototypesPass::runOnModule(Module &M) {
42 bool MadeChange = false;
44 // Erase dead function prototypes.
45 for (Module::iterator I = M.begin(), E = M.end(); I != E; ) {
47 // Function must be a prototype and unused.
48 if (F->isDeclaration() && F->use_empty()) {
55 // Erase dead global var prototypes.
56 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
58 GlobalVariable *GV = I++;
59 // Global must be a prototype and unused.
60 if (GV->isDeclaration() && GV->use_empty())
61 GV->eraseFromParent();
64 // Return an indication of whether we changed anything or not.
68 ModulePass *llvm::createStripDeadPrototypesPass() {
69 return new StripDeadPrototypesPass();