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