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