[Alignment][NFC] Convert StoreInst to MaybeAlign
[llvm-complete.git] / lib / Transforms / IPO / StripDeadPrototypes.cpp
blob106db3c8bd9d912466826accdade2c715b001718
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).
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"
22 using namespace llvm;
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; ) {
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;
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();
51 // Return an indication of whether we changed anything or not.
52 return MadeChange;
55 PreservedAnalyses StripDeadPrototypesPass::run(Module &M,
56 ModuleAnalysisManager &) {
57 if (stripDeadPrototypes(M))
58 return PreservedAnalyses::none();
59 return PreservedAnalyses::all();
62 namespace {
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());
71 bool runOnModule(Module &M) override {
72 if (skipModule(M))
73 return false;
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();