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 
16 #include "llvm/Transforms/IPO/StripDeadPrototypes.h"
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.
43   for (Module::global_iterator I = M.global_begin(), E = M.global_end();
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 
55 PreservedAnalyses StripDeadPrototypesPass::run(Module &M,
56                                                ModuleAnalysisManager &) {
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) {
68     initializeStripDeadPrototypesLegacyPassPass(
69         *PassRegistry::getPassRegistry());
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 
81 char StripDeadPrototypesLegacyPass::ID = 0;
82 INITIALIZE_PASS(StripDeadPrototypesLegacyPass, "strip-dead-prototypes",
83                 "Strip Unused Function Prototypes", false, false)
84 
85 ModulePass *llvm::createStripDeadPrototypesPass() {
86   return new StripDeadPrototypesLegacyPass();
87 }
88