1 //===-- StripDeadPrototypes.cpp - Remove unused function declarations ----===//
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
7 //===----------------------------------------------------------------------===//
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).
14 //===----------------------------------------------------------------------===//
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"
24 #define DEBUG_TYPE "strip-dead-prototypes"
26 STATISTIC(NumDeadPrototypes
, "Number of dead prototypes removed");
28 static bool stripDeadPrototypes(Module
&M
) {
29 bool MadeChange
= false;
31 // Erase dead function prototypes.
32 for (Module::iterator I
= M
.begin(), E
= M
.end(); I
!= E
; ) {
34 // Function must be a prototype and unused.
35 if (F
->isDeclaration() && F
->use_empty()) {
42 // Erase dead global var prototypes.
43 for (Module::global_iterator I
= M
.global_begin(), E
= M
.global_end();
45 GlobalVariable
*GV
= &*I
++;
46 // Global must be a prototype and unused.
47 if (GV
->isDeclaration() && GV
->use_empty())
48 GV
->eraseFromParent();
51 // Return an indication of whether we changed anything or not.
55 PreservedAnalyses
StripDeadPrototypesPass::run(Module
&M
,
56 ModuleAnalysisManager
&) {
57 if (stripDeadPrototypes(M
))
58 return PreservedAnalyses::none();
59 return PreservedAnalyses::all();
64 class StripDeadPrototypesLegacyPass
: public ModulePass
{
66 static char ID
; // Pass identification, replacement for typeid
67 StripDeadPrototypesLegacyPass() : ModulePass(ID
) {
68 initializeStripDeadPrototypesLegacyPassPass(
69 *PassRegistry::getPassRegistry());
71 bool runOnModule(Module
&M
) override
{
75 return stripDeadPrototypes(M
);
79 } // end anonymous namespace
81 char StripDeadPrototypesLegacyPass::ID
= 0;
82 INITIALIZE_PASS(StripDeadPrototypesLegacyPass
, "strip-dead-prototypes",
83 "Strip Unused Function Prototypes", false, false)
85 ModulePass
*llvm::createStripDeadPrototypesPass() {
86 return new StripDeadPrototypesLegacyPass();