1//===-- StripDeadPrototypes.cpp - Remove unused function declarations ----===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
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).
14//
15//===----------------------------------------------------------------------===//
16
17#include "llvm/Transforms/IPO.h"
18#include "llvm/ADT/Statistic.h"
19#include "llvm/IR/Module.h"
20#include "llvm/Pass.h"
21using namespace llvm;
22
23#define DEBUG_TYPE "strip-dead-prototypes"
24
25STATISTIC(NumDeadPrototypes, "Number of dead prototypes removed");
26
27namespace {
28
29/// @brief Pass to remove unused function declarations.
30class StripDeadPrototypesPass : public ModulePass {
31public:
32  static char ID; // Pass identification, replacement for typeid
33  StripDeadPrototypesPass() : ModulePass(ID) {
34    initializeStripDeadPrototypesPassPass(*PassRegistry::getPassRegistry());
35  }
36  bool runOnModule(Module &M) override;
37};
38
39} // end anonymous namespace
40
41char StripDeadPrototypesPass::ID = 0;
42INITIALIZE_PASS(StripDeadPrototypesPass, "strip-dead-prototypes",
43                "Strip Unused Function Prototypes", false, false)
44
45bool StripDeadPrototypesPass::runOnModule(Module &M) {
46  bool MadeChange = false;
47
48  // Erase dead function prototypes.
49  for (Module::iterator I = M.begin(), E = M.end(); I != E; ) {
50    Function *F = I++;
51    // Function must be a prototype and unused.
52    if (F->isDeclaration() && F->use_empty()) {
53      F->eraseFromParent();
54      ++NumDeadPrototypes;
55      MadeChange = true;
56    }
57  }
58
59  // Erase dead global var prototypes.
60  for (Module::global_iterator I = M.global_begin(), E = M.global_end();
61       I != E; ) {
62    GlobalVariable *GV = I++;
63    // Global must be a prototype and unused.
64    if (GV->isDeclaration() && GV->use_empty())
65      GV->eraseFromParent();
66  }
67
68  // Return an indication of whether we changed anything or not.
69  return MadeChange;
70}
71
72ModulePass *llvm::createStripDeadPrototypesPass() {
73  return new StripDeadPrototypesPass();
74}
75