1 //===-- StripDeadPrototypes.cpp - Remove unused function declarations ----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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).
15 //===----------------------------------------------------------------------===//
17 #include "llvm/Transforms/IPO/StripDeadPrototypes.h"
18 #include "llvm/ADT/Statistic.h"
19 #include "llvm/IR/Module.h"
20 #include "llvm/Pass.h"
21 #include "llvm/Transforms/IPO.h"
25 #define DEBUG_TYPE "strip-dead-prototypes"
27 STATISTIC(NumDeadPrototypes, "Number of dead prototypes removed");
29 static bool stripDeadPrototypes(Module &M) {
30 bool MadeChange = false;
32 // Erase dead function prototypes.
33 for (Module::iterator I = M.begin(), E = M.end(); I != E; ) {
35 // Function must be a prototype and unused.
36 if (F->isDeclaration() && F->use_empty()) {
43 // Erase dead global var prototypes.
44 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
46 GlobalVariable *GV = &*I++;
47 // Global must be a prototype and unused.
48 if (GV->isDeclaration() && GV->use_empty())
49 GV->eraseFromParent();
52 // Return an indication of whether we changed anything or not.
56 PreservedAnalyses StripDeadPrototypesPass::run(Module &M) {
57 if (stripDeadPrototypes(M))
58 return PreservedAnalyses::none();
59 return PreservedAnalyses::all();
64 class StripDeadPrototypesLegacyPass : public ModulePass {
66 static char ID; // Pass identification, replacement for typeid
67 StripDeadPrototypesLegacyPass() : ModulePass(ID) {
68 initializeStripDeadPrototypesLegacyPassPass(
69 *PassRegistry::getPassRegistry());
71 bool runOnModule(Module &M) override {
72 return stripDeadPrototypes(M);
76 } // end anonymous namespace
78 char StripDeadPrototypesLegacyPass::ID = 0;
79 INITIALIZE_PASS(StripDeadPrototypesLegacyPass, "strip-dead-prototypes",
80 "Strip Unused Function Prototypes", false, false)
82 ModulePass *llvm::createStripDeadPrototypesPass() {
83 return new StripDeadPrototypesLegacyPass();