LLVM  8.0.1
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 
18 #include "llvm/ADT/Statistic.h"
19 #include "llvm/IR/Module.h"
20 #include "llvm/Pass.h"
21 #include "llvm/Transforms/IPO.h"
22 
23 using namespace llvm;
24 
25 #define DEBUG_TYPE "strip-dead-prototypes"
26 
27 STATISTIC(NumDeadPrototypes, "Number of dead prototypes removed");
28 
29 static bool stripDeadPrototypes(Module &M) {
30  bool MadeChange = false;
31 
32  // Erase dead function prototypes.
33  for (Module::iterator I = M.begin(), E = M.end(); I != E; ) {
34  Function *F = &*I++;
35  // Function must be a prototype and unused.
36  if (F->isDeclaration() && F->use_empty()) {
37  F->eraseFromParent();
38  ++NumDeadPrototypes;
39  MadeChange = true;
40  }
41  }
42 
43  // Erase dead global var prototypes.
45  I != E; ) {
46  GlobalVariable *GV = &*I++;
47  // Global must be a prototype and unused.
48  if (GV->isDeclaration() && GV->use_empty())
49  GV->eraseFromParent();
50  }
51 
52  // Return an indication of whether we changed anything or not.
53  return MadeChange;
54 }
55 
58  if (stripDeadPrototypes(M))
59  return PreservedAnalyses::none();
60  return PreservedAnalyses::all();
61 }
62 
63 namespace {
64 
65 class StripDeadPrototypesLegacyPass : public ModulePass {
66 public:
67  static char ID; // Pass identification, replacement for typeid
68  StripDeadPrototypesLegacyPass() : ModulePass(ID) {
71  }
72  bool runOnModule(Module &M) override {
73  if (skipModule(M))
74  return false;
75 
76  return stripDeadPrototypes(M);
77  }
78 };
79 
80 } // end anonymous namespace
81 
83 INITIALIZE_PASS(StripDeadPrototypesLegacyPass, "strip-dead-prototypes",
84  "Strip Unused Function Prototypes", false, false)
85 
87  return new StripDeadPrototypesLegacyPass();
88 }
ModulePass * createStripDeadPrototypesPass()
createStripDeadPrototypesPass - This pass removes any function declarations (prototypes) that are not...
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
This class represents lattice values for constants.
Definition: AllocatorList.h:24
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
STATISTIC(NumFunctions, "Total number of functions")
F(f)
INITIALIZE_PASS(StripDeadPrototypesLegacyPass, "strip-dead-prototypes", "Strip Unused Function Prototypes", false, false) ModulePass *llvm
global_iterator global_begin()
Definition: Module.h:578
static bool stripDeadPrototypes(Module &M)
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: PassManager.h:157
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:154
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
void eraseFromParent()
eraseFromParent - This method unlinks &#39;this&#39; from the containing module and deletes it...
Definition: Globals.cpp:359
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:160
global_iterator global_end()
Definition: Module.h:580
Iterator for intrusive lists based on ilist_node.
Module.h This file contains the declarations for the Module class.
PreservedAnalyses run(Module &M, ModuleAnalysisManager &)
iterator end()
Definition: Module.h:597
#define I(x, y, z)
Definition: MD5.cpp:58
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:225
iterator begin()
Definition: Module.h:595
void eraseFromParent()
eraseFromParent - This method unlinks &#39;this&#39; from the containing module and deletes it...
Definition: Function.cpp:214
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:206
A container for analyses that lazily runs them and caches their results.
bool use_empty() const
Definition: Value.h:323
void initializeStripDeadPrototypesLegacyPassPass(PassRegistry &)