LLVM API Documentation

StripDeadPrototypes.cpp

Go to the documentation of this file.
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 #define DEBUG_TYPE "strip-dead-prototypes"
00018 #include "llvm/Transforms/IPO.h"
00019 #include "llvm/Pass.h"
00020 #include "llvm/Module.h"
00021 #include "llvm/ADT/Statistic.h"
00022 #include "llvm/Support/Compiler.h"
00023 using namespace llvm;
00024 
00025 STATISTIC(NumDeadPrototypes, "Number of dead prototypes removed");
00026 
00027 namespace {
00028 
00029 /// @brief Pass to remove unused function declarations.
00030 class VISIBILITY_HIDDEN StripDeadPrototypesPass : public ModulePass {
00031 public:
00032   static char ID; // Pass identification, replacement for typeid
00033   StripDeadPrototypesPass() : ModulePass(&ID) { }
00034   virtual bool runOnModule(Module &M);
00035 };
00036 
00037 } // end anonymous namespace
00038 
00039 char StripDeadPrototypesPass::ID = 0;
00040 static RegisterPass<StripDeadPrototypesPass>
00041 X("strip-dead-prototypes", "Strip Unused Function Prototypes");
00042 
00043 bool StripDeadPrototypesPass::runOnModule(Module &M) {
00044   bool MadeChange = false;
00045   
00046   // Erase dead function prototypes.
00047   for (Module::iterator I = M.begin(), E = M.end(); I != E; ) {
00048     Function *F = I++;
00049     // Function must be a prototype and unused.
00050     if (F->isDeclaration() && F->use_empty()) {
00051       F->eraseFromParent();
00052       ++NumDeadPrototypes;
00053       MadeChange = true;
00054     }
00055   }
00056 
00057   // Erase dead global var prototypes.
00058   for (Module::global_iterator I = M.global_begin(), E = M.global_end();
00059        I != E; ) {
00060     GlobalVariable *GV = I++;
00061     // Global must be a prototype and unused.
00062     if (GV->isDeclaration() && GV->use_empty())
00063       GV->eraseFromParent();
00064   }
00065   
00066   // Return an indication of whether we changed anything or not.
00067   return MadeChange;
00068 }
00069 
00070 ModulePass *llvm::createStripDeadPrototypesPass() {
00071   return new StripDeadPrototypesPass();
00072 }