LLVM  3.7.0
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 #include "llvm/Transforms/IPO.h"
18 #include "llvm/ADT/Statistic.h"
19 #include "llvm/IR/Module.h"
20 #include "llvm/Pass.h"
21 using namespace llvm;
22 
23 #define DEBUG_TYPE "strip-dead-prototypes"
24 
25 STATISTIC(NumDeadPrototypes, "Number of dead prototypes removed");
26 
27 namespace {
28 
29 /// @brief Pass to remove unused function declarations.
30 class StripDeadPrototypesPass : public ModulePass {
31 public:
32  static char ID; // Pass identification, replacement for typeid
33  StripDeadPrototypesPass() : ModulePass(ID) {
35  }
36  bool runOnModule(Module &M) override;
37 };
38 
39 } // end anonymous namespace
40 
42 INITIALIZE_PASS(StripDeadPrototypesPass, "strip-dead-prototypes",
43  "Strip Unused Function Prototypes", false, false)
44 
45 bool 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 
73  return new StripDeadPrototypesPass();
74 }
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
STATISTIC(NumFunctions,"Total number of functions")
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:114
F(f)
void eraseFromParent() override
eraseFromParent - This method unlinks 'this' from the containing module and deletes it...
Definition: Globals.cpp:194
INITIALIZE_PASS(StripDeadPrototypesPass,"strip-dead-prototypes","Strip Unused Function Prototypes", false, false) bool StripDeadPrototypesPass
Module.h This file contains the declarations for the Module class.
void eraseFromParent() override
eraseFromParent - This method unlinks 'this' from the containing module and deletes it...
Definition: Function.cpp:241
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:128
void initializeStripDeadPrototypesPassPass(PassRegistry &)
#define I(x, y, z)
Definition: MD5.cpp:54
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:236
bool use_empty() const
Definition: Value.h:275
ModulePass * createStripDeadPrototypesPass()
createStripDeadPrototypesPass - This pass removes any function declarations (prototypes) that are not...