LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
StripDeadPrototypes.cpp
Go to the documentation of this file.
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 #define DEBUG_TYPE "strip-dead-prototypes"
18 #include "llvm/Transforms/IPO.h"
19 #include "llvm/ADT/Statistic.h"
20 #include "llvm/IR/Module.h"
21 #include "llvm/Pass.h"
22 using namespace llvm;
23 
24 STATISTIC(NumDeadPrototypes, "Number of dead prototypes removed");
25 
26 namespace {
27 
28 /// @brief Pass to remove unused function declarations.
29 class StripDeadPrototypesPass : public ModulePass {
30 public:
31  static char ID; // Pass identification, replacement for typeid
32  StripDeadPrototypesPass() : ModulePass(ID) {
34  }
35  virtual bool runOnModule(Module &M);
36 };
37 
38 } // end anonymous namespace
39 
41 INITIALIZE_PASS(StripDeadPrototypesPass, "strip-dead-prototypes",
42  "Strip Unused Function Prototypes", false, false)
43 
44 bool StripDeadPrototypesPass::runOnModule(Module &M) {
45  bool MadeChange = false;
46 
47  // Erase dead function prototypes.
48  for (Module::iterator I = M.begin(), E = M.end(); I != E; ) {
49  Function *F = I++;
50  // Function must be a prototype and unused.
51  if (F->isDeclaration() && F->use_empty()) {
52  F->eraseFromParent();
53  ++NumDeadPrototypes;
54  MadeChange = true;
55  }
56  }
57 
58  // Erase dead global var prototypes.
59  for (Module::global_iterator I = M.global_begin(), E = M.global_end();
60  I != E; ) {
61  GlobalVariable *GV = I++;
62  // Global must be a prototype and unused.
63  if (GV->isDeclaration() && GV->use_empty())
64  GV->eraseFromParent();
65  }
66 
67  // Return an indication of whether we changed anything or not.
68  return MadeChange;
69 }
70 
72  return new StripDeadPrototypesPass();
73 }
static PassRegistry * getPassRegistry()
The main container class for the LLVM Intermediate Representation.
Definition: Module.h:112
F(f)
ID
LLVM Calling Convention Representation.
Definition: CallingConv.h:26
STATISTIC(NumDeadPrototypes,"Number of dead prototypes removed")
INITIALIZE_PASS(StripDeadPrototypesPass,"strip-dead-prototypes","Strip Unused Function Prototypes", false, false) bool StripDeadPrototypesPass
virtual void eraseFromParent()
Definition: Globals.cpp:142
bool isDeclaration() const
Definition: Globals.cpp:66
void initializeStripDeadPrototypesPassPass(PassRegistry &)
#define I(x, y, z)
Definition: MD5.cpp:54
virtual void eraseFromParent()
Definition: Function.cpp:187
bool use_empty() const
Definition: Value.h:149
ModulePass * createStripDeadPrototypesPass()