LLVM API Documentation
00001 //===-- StripDeadPrototypes.cpp - Remove unused function declarations ----===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This pass loops over all of the functions in the input module, looking for 00011 // dead declarations and removes them. Dead declarations are declarations of 00012 // functions for which no implementation is available (i.e., declarations for 00013 // unused library functions). 00014 // 00015 //===----------------------------------------------------------------------===// 00016 00017 #include "llvm/Transforms/IPO.h" 00018 #include "llvm/ADT/Statistic.h" 00019 #include "llvm/IR/Module.h" 00020 #include "llvm/Pass.h" 00021 using namespace llvm; 00022 00023 #define DEBUG_TYPE "strip-dead-prototypes" 00024 00025 STATISTIC(NumDeadPrototypes, "Number of dead prototypes removed"); 00026 00027 namespace { 00028 00029 /// @brief Pass to remove unused function declarations. 00030 class StripDeadPrototypesPass : public ModulePass { 00031 public: 00032 static char ID; // Pass identification, replacement for typeid 00033 StripDeadPrototypesPass() : ModulePass(ID) { 00034 initializeStripDeadPrototypesPassPass(*PassRegistry::getPassRegistry()); 00035 } 00036 bool runOnModule(Module &M) override; 00037 }; 00038 00039 } // end anonymous namespace 00040 00041 char StripDeadPrototypesPass::ID = 0; 00042 INITIALIZE_PASS(StripDeadPrototypesPass, "strip-dead-prototypes", 00043 "Strip Unused Function Prototypes", false, false) 00044 00045 bool StripDeadPrototypesPass::runOnModule(Module &M) { 00046 bool MadeChange = false; 00047 00048 // Erase dead function prototypes. 00049 for (Module::iterator I = M.begin(), E = M.end(); I != E; ) { 00050 Function *F = I++; 00051 // Function must be a prototype and unused. 00052 if (F->isDeclaration() && F->use_empty()) { 00053 F->eraseFromParent(); 00054 ++NumDeadPrototypes; 00055 MadeChange = true; 00056 } 00057 } 00058 00059 // Erase dead global var prototypes. 00060 for (Module::global_iterator I = M.global_begin(), E = M.global_end(); 00061 I != E; ) { 00062 GlobalVariable *GV = I++; 00063 // Global must be a prototype and unused. 00064 if (GV->isDeclaration() && GV->use_empty()) 00065 GV->eraseFromParent(); 00066 } 00067 00068 // Return an indication of whether we changed anything or not. 00069 return MadeChange; 00070 } 00071 00072 ModulePass *llvm::createStripDeadPrototypesPass() { 00073 return new StripDeadPrototypesPass(); 00074 }