LLVM  13.0.0git
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/InitializePasses.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.
44  for (Module::global_iterator I = M.global_begin(), E = M.global_end();
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 
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 }
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:155
llvm::GlobalVariable::eraseFromParent
void eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing module and deletes it.
Definition: Globals.cpp:385
llvm
Definition: AllocatorList.h:23
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::ModulePass
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:238
llvm::Module::iterator
FunctionListType::iterator iterator
The Function iterators.
Definition: Module.h:92
llvm::Function
Definition: Function.h:61
Pass.h
Statistic.h
llvm::GlobalVariable
Definition: GlobalVariable.h:40
llvm::PreservedAnalyses::none
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: PassManager.h:158
Module.h
llvm::StripDeadPrototypesPass::run
PreservedAnalyses run(Module &M, ModuleAnalysisManager &)
Definition: StripDeadPrototypes.cpp:56
StripDeadPrototypes.h
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::createStripDeadPrototypesPass
ModulePass * createStripDeadPrototypesPass()
createStripDeadPrototypesPass - This pass removes any function declarations (prototypes) that are not...
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition: PassRegistry.cpp:31
llvm::GlobalValue::isDeclaration
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:228
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::Module::global_iterator
GlobalListType::iterator global_iterator
The Global Variable iterator.
Definition: Module.h:87
llvm::STATISTIC
STATISTIC(NumFunctions, "Total number of functions")
INITIALIZE_PASS
INITIALIZE_PASS(StripDeadPrototypesLegacyPass, "strip-dead-prototypes", "Strip Unused Function Prototypes", false, false) ModulePass *llvm
Definition: StripDeadPrototypes.cpp:83
llvm::Value::use_empty
bool use_empty() const
Definition: Value.h:345
IPO.h
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
llvm::initializeStripDeadPrototypesLegacyPassPass
void initializeStripDeadPrototypesLegacyPassPass(PassRegistry &)
stripDeadPrototypes
static bool stripDeadPrototypes(Module &M)
Definition: StripDeadPrototypes.cpp:29
llvm::PreservedAnalyses::all
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:161
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:44
InitializePasses.h
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:38