LLVM  9.0.0svn
StripDeadPrototypes.cpp
Go to the documentation of this file.
1 //===-- StripDeadPrototypes.cpp - Remove unused function declarations ----===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This pass loops over all of the functions in the input module, looking for
10 // dead declarations and removes them. Dead declarations are declarations of
11 // functions for which no implementation is available (i.e., declarations for
12 // unused library functions).
13 //
14 //===----------------------------------------------------------------------===//
15 
17 #include "llvm/ADT/Statistic.h"
18 #include "llvm/IR/Module.h"
19 #include "llvm/Pass.h"
20 #include "llvm/Transforms/IPO.h"
21 
22 using namespace llvm;
23 
24 #define DEBUG_TYPE "strip-dead-prototypes"
25 
26 STATISTIC(NumDeadPrototypes, "Number of dead prototypes removed");
27 
28 static bool stripDeadPrototypes(Module &M) {
29  bool MadeChange = false;
30 
31  // Erase dead function prototypes.
32  for (Module::iterator I = M.begin(), E = M.end(); I != E; ) {
33  Function *F = &*I++;
34  // Function must be a prototype and unused.
35  if (F->isDeclaration() && F->use_empty()) {
36  F->eraseFromParent();
37  ++NumDeadPrototypes;
38  MadeChange = true;
39  }
40  }
41 
42  // Erase dead global var prototypes.
44  I != E; ) {
45  GlobalVariable *GV = &*I++;
46  // Global must be a prototype and unused.
47  if (GV->isDeclaration() && GV->use_empty())
48  GV->eraseFromParent();
49  }
50 
51  // Return an indication of whether we changed anything or not.
52  return MadeChange;
53 }
54 
57  if (stripDeadPrototypes(M))
58  return PreservedAnalyses::none();
59  return PreservedAnalyses::all();
60 }
61 
62 namespace {
63 
64 class StripDeadPrototypesLegacyPass : public ModulePass {
65 public:
66  static char ID; // Pass identification, replacement for typeid
67  StripDeadPrototypesLegacyPass() : ModulePass(ID) {
70  }
71  bool runOnModule(Module &M) override {
72  if (skipModule(M))
73  return false;
74 
75  return stripDeadPrototypes(M);
76  }
77 };
78 
79 } // end anonymous namespace
80 
82 INITIALIZE_PASS(StripDeadPrototypesLegacyPass, "strip-dead-prototypes",
83  "Strip Unused Function Prototypes", false, false)
84 
86  return new StripDeadPrototypesLegacyPass();
87 }
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:23
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:581
static bool stripDeadPrototypes(Module &M)
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: PassManager.h:156
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:153
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:380
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:159
global_iterator global_end()
Definition: Module.h:583
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:600
#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:224
iterator begin()
Definition: Module.h:598
void eraseFromParent()
eraseFromParent - This method unlinks &#39;this&#39; from the containing module and deletes it...
Definition: Function.cpp:226
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:227
A container for analyses that lazily runs them and caches their results.
bool use_empty() const
Definition: Value.h:322
void initializeStripDeadPrototypesLegacyPassPass(PassRegistry &)