blob: fd22fdb37f803d4b8e00a074f40fd82f21ce3a37 [file] [log] [blame]
Reid Spencerc19fbd62007-02-05 20:24:25 +00001//===-- StripDeadPrototypes.cpp - Removed unused function declarations ----===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencerc19fbd62007-02-05 20:24:25 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This pass loops over all of the functions in the input module, looking for
11// dead declarations and removes them.
12//
13//===----------------------------------------------------------------------===//
14
Chris Lattnereb473912007-02-18 22:10:34 +000015#define DEBUG_TYPE "strip-dead-prototypes"
Reid Spencerc19fbd62007-02-05 20:24:25 +000016#include "llvm/Transforms/IPO.h"
17#include "llvm/Pass.h"
18#include "llvm/Module.h"
19#include "llvm/ADT/Statistic.h"
Reid Spencer7ea07652007-02-05 21:47:39 +000020#include "llvm/Support/Compiler.h"
Reid Spencerc19fbd62007-02-05 20:24:25 +000021using namespace llvm;
22
23STATISTIC(NumDeadPrototypes, "Number of dead prototypes removed");
24
25namespace {
26
27/// @brief Pass to remove unused function declarations.
Reid Spencer9153a672007-02-05 21:45:12 +000028class VISIBILITY_HIDDEN StripDeadPrototypesPass : public ModulePass {
Reid Spencerc19fbd62007-02-05 20:24:25 +000029public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +000030 static char ID; // Pass identification, replacement for typeid
Devang Patel794fd752007-05-01 21:15:47 +000031 StripDeadPrototypesPass() : ModulePass((intptr_t)&ID) { }
Reid Spencerc19fbd62007-02-05 20:24:25 +000032 virtual bool runOnModule(Module &M);
33};
Devang Patel794fd752007-05-01 21:15:47 +000034
Devang Patel19974732007-05-03 01:11:54 +000035char StripDeadPrototypesPass::ID = 0;
Reid Spencerc19fbd62007-02-05 20:24:25 +000036RegisterPass<StripDeadPrototypesPass> X("strip-dead-prototypes",
37 "Strip Unused Function Prototypes");
38
Reid Spencer9153a672007-02-05 21:45:12 +000039} // end anonymous namespace
40
Reid Spencerc19fbd62007-02-05 20:24:25 +000041bool StripDeadPrototypesPass::runOnModule(Module &M) {
Chris Lattnereb473912007-02-18 22:10:34 +000042 bool MadeChange = false;
Reid Spencerc19fbd62007-02-05 20:24:25 +000043
Chris Lattnereb473912007-02-18 22:10:34 +000044 // Erase dead function prototypes.
45 for (Module::iterator I = M.begin(), E = M.end(); I != E; ) {
46 Function *F = I++;
47 // Function must be a prototype and unused.
48 if (F->isDeclaration() && F->use_empty()) {
49 F->eraseFromParent();
50 ++NumDeadPrototypes;
51 MadeChange = true;
52 }
53 }
Reid Spencerc19fbd62007-02-05 20:24:25 +000054
Chris Lattner4a6f3632007-02-18 22:10:58 +000055 // Erase dead global var prototypes.
Chris Lattnereb473912007-02-18 22:10:34 +000056 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
57 I != E; ) {
58 GlobalVariable *GV = I++;
59 // Global must be a prototype and unused.
60 if (GV->isDeclaration() && GV->use_empty())
61 GV->eraseFromParent();
62 }
63
Reid Spencerc19fbd62007-02-05 20:24:25 +000064 // Return an indication of whether we changed anything or not.
Chris Lattnereb473912007-02-18 22:10:34 +000065 return MadeChange;
Reid Spencerc19fbd62007-02-05 20:24:25 +000066}
67
Reid Spencerc19fbd62007-02-05 20:24:25 +000068ModulePass *llvm::createStripDeadPrototypesPass() {
69 return new StripDeadPrototypesPass();
70}