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