StripDeadPrototypes.cpp revision ae73dc1448d25b02cabc7c64c86c64371453dda8
1//===-- StripDeadPrototypes.cpp - Remove unused function declarations ----===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This pass loops over all of the functions in the input module, looking for 11// dead declarations and removes them. Dead declarations are declarations of 12// functions for which no implementation is available (i.e., declarations for 13// unused library functions). 14// 15//===----------------------------------------------------------------------===// 16 17#define DEBUG_TYPE "strip-dead-prototypes" 18#include "llvm/Transforms/IPO.h" 19#include "llvm/Pass.h" 20#include "llvm/Module.h" 21#include "llvm/ADT/Statistic.h" 22#include "llvm/Support/Compiler.h" 23using namespace llvm; 24 25STATISTIC(NumDeadPrototypes, "Number of dead prototypes removed"); 26 27namespace { 28 29/// @brief Pass to remove unused function declarations. 30class VISIBILITY_HIDDEN StripDeadPrototypesPass : public ModulePass { 31public: 32 static char ID; // Pass identification, replacement for typeid 33 StripDeadPrototypesPass() : ModulePass(&ID) { } 34 virtual bool runOnModule(Module &M); 35}; 36 37} // end anonymous namespace 38 39char StripDeadPrototypesPass::ID = 0; 40static RegisterPass<StripDeadPrototypesPass> 41X("strip-dead-prototypes", "Strip Unused Function Prototypes"); 42 43bool StripDeadPrototypesPass::runOnModule(Module &M) { 44 bool MadeChange = false; 45 46 // Erase dead function prototypes. 47 for (Module::iterator I = M.begin(), E = M.end(); I != E; ) { 48 Function *F = I++; 49 // Function must be a prototype and unused. 50 if (F->isDeclaration() && F->use_empty()) { 51 F->eraseFromParent(); 52 ++NumDeadPrototypes; 53 MadeChange = true; 54 } 55 } 56 57 // Erase dead global var prototypes. 58 for (Module::global_iterator I = M.global_begin(), E = M.global_end(); 59 I != E; ) { 60 GlobalVariable *GV = I++; 61 // Global must be a prototype and unused. 62 if (GV->isDeclaration() && GV->use_empty()) 63 GV->eraseFromParent(); 64 } 65 66 // Return an indication of whether we changed anything or not. 67 return MadeChange; 68} 69 70ModulePass *llvm::createStripDeadPrototypesPass() { 71 return new StripDeadPrototypesPass(); 72} 73