xref: /llvm-project/llvm/lib/Transforms/IPO/StripDeadPrototypes.cpp (revision 98ea1a81a28a6dd36941456c8ab4ce46f665f57a)
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 
20 using namespace llvm;
21 
22 #define DEBUG_TYPE "strip-dead-prototypes"
23 
24 STATISTIC(NumDeadPrototypes, "Number of dead prototypes removed");
25 
26 static bool stripDeadPrototypes(Module &M) {
27   bool MadeChange = false;
28 
29   // Erase dead function prototypes.
30   for (Function &F : llvm::make_early_inc_range(M)) {
31     // Function must be a prototype and unused.
32     if (F.isDeclaration() && F.use_empty()) {
33       F.eraseFromParent();
34       ++NumDeadPrototypes;
35       MadeChange = true;
36     }
37   }
38 
39   // Erase dead global var prototypes.
40   for (GlobalVariable &GV : llvm::make_early_inc_range(M.globals())) {
41     // Global must be a prototype and unused.
42     if (GV.isDeclaration() && GV.use_empty())
43       GV.eraseFromParent();
44   }
45 
46   // Return an indication of whether we changed anything or not.
47   return MadeChange;
48 }
49 
50 PreservedAnalyses StripDeadPrototypesPass::run(Module &M,
51                                                ModuleAnalysisManager &) {
52   if (stripDeadPrototypes(M))
53     return PreservedAnalyses::none();
54   return PreservedAnalyses::all();
55 }
56