blob: a2ee18e325151ce91c63e7a0082ae5c3e899d995 [file] [log] [blame]
Chris Lattner7311e382002-07-23 22:03:41 +00001//===- DeadTypeElimination.cpp - Eliminate unused types for symbol table --===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnere51e03b2001-10-31 04:33:19 +00009//
Chris Lattner7311e382002-07-23 22:03:41 +000010// This pass is used to cleanup the output of GCC. It eliminate names for types
11// that are unused in the entire translation unit, using the FindUsedTypes pass.
Chris Lattnere51e03b2001-10-31 04:33:19 +000012//
13//===----------------------------------------------------------------------===//
14
Chris Lattner86453c52006-12-19 22:09:18 +000015#define DEBUG_TYPE "deadtypeelim"
Chris Lattner7311e382002-07-23 22:03:41 +000016#include "llvm/Transforms/IPO.h"
Chris Lattner793c6b82002-01-31 00:45:11 +000017#include "llvm/Analysis/FindUsedTypes.h"
Chris Lattner793c6b82002-01-31 00:45:11 +000018#include "llvm/Module.h"
Reid Spencer78d033e2007-01-06 07:24:44 +000019#include "llvm/TypeSymbolTable.h"
Chris Lattnerbfe11102001-10-31 06:35:59 +000020#include "llvm/DerivedTypes.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000021#include "llvm/ADT/Statistic.h"
Reid Spencer9133fe22007-02-05 23:32:05 +000022#include "llvm/Support/Compiler.h"
Chris Lattner1e2385b2003-11-21 21:54:22 +000023using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000024
Chris Lattner86453c52006-12-19 22:09:18 +000025STATISTIC(NumKilled, "Number of unused typenames removed from symtab");
26
Chris Lattnerbd0ef772002-02-26 21:46:54 +000027namespace {
Reid Spencer9133fe22007-02-05 23:32:05 +000028 struct VISIBILITY_HIDDEN DTE : public ModulePass {
Devang Patel19974732007-05-03 01:11:54 +000029 static char ID; // Pass identifcation, replacement for typeid
Devang Patel794fd752007-05-01 21:15:47 +000030 DTE() : ModulePass((intptr_t)&ID) {}
31
Chris Lattnerbd0ef772002-02-26 21:46:54 +000032 // doPassInitialization - For this pass, it removes global symbol table
33 // entries for primitive types. These are never used for linking in GCC and
34 // they make the output uglier to look at, so we nuke them.
35 //
36 // Also, initialize instance variables.
37 //
Chris Lattnerb12914b2004-09-20 04:48:05 +000038 bool runOnModule(Module &M);
Chris Lattnerce0141e2002-06-25 15:55:03 +000039
Chris Lattnerf57b8452002-04-27 06:56:12 +000040 // getAnalysisUsage - This function needs FindUsedTypes to do its job...
Chris Lattnerbd0ef772002-02-26 21:46:54 +000041 //
Chris Lattnerf57b8452002-04-27 06:56:12 +000042 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner5f0eb8d2002-08-08 19:01:30 +000043 AU.addRequired<FindUsedTypes>();
Chris Lattnerbd0ef772002-02-26 21:46:54 +000044 }
45 };
Devang Patel19974732007-05-03 01:11:54 +000046 char DTE::ID = 0;
Chris Lattner7f8897f2006-08-27 22:42:52 +000047 RegisterPass<DTE> X("deadtypeelim", "Dead Type Elimination");
Chris Lattnerbd0ef772002-02-26 21:46:54 +000048}
49
Chris Lattnerb12914b2004-09-20 04:48:05 +000050ModulePass *llvm::createDeadTypeEliminationPass() {
Chris Lattner7311e382002-07-23 22:03:41 +000051 return new DTE();
Chris Lattnerb6b26922001-11-03 09:19:00 +000052}
53
54
Chris Lattnerb045e142003-11-09 05:04:25 +000055// ShouldNukeSymtabEntry - Return true if this module level symbol table entry
Chris Lattnerb6b26922001-11-03 09:19:00 +000056// should be eliminated.
57//
Chris Lattner4f77caa2004-02-26 20:02:23 +000058static inline bool ShouldNukeSymtabEntry(const Type *Ty){
Chris Lattnere51e03b2001-10-31 04:33:19 +000059 // Nuke all names for primitive types!
Chris Lattner42a75512007-01-15 02:27:26 +000060 if (Ty->isPrimitiveType() || Ty->isInteger())
Reid Spencera54b7cb2007-01-12 07:05:14 +000061 return true;
Chris Lattnere51e03b2001-10-31 04:33:19 +000062
Chris Lattner430aa9e2001-11-15 04:34:46 +000063 // Nuke all pointers to primitive types as well...
Chris Lattner4f77caa2004-02-26 20:02:23 +000064 if (const PointerType *PT = dyn_cast<PointerType>(Ty))
Reid Spencera54b7cb2007-01-12 07:05:14 +000065 if (PT->getElementType()->isPrimitiveType() ||
Chris Lattner42a75512007-01-15 02:27:26 +000066 PT->getElementType()->isInteger())
Reid Spencera54b7cb2007-01-12 07:05:14 +000067 return true;
Chris Lattner430aa9e2001-11-15 04:34:46 +000068
Chris Lattnere51e03b2001-10-31 04:33:19 +000069 return false;
70}
71
Chris Lattner7311e382002-07-23 22:03:41 +000072// run - For this pass, it removes global symbol table entries for primitive
73// types. These are never used for linking in GCC and they make the output
74// uglier to look at, so we nuke them. Also eliminate types that are never used
75// in the entire program as indicated by FindUsedTypes.
Chris Lattnere51e03b2001-10-31 04:33:19 +000076//
Chris Lattnerb12914b2004-09-20 04:48:05 +000077bool DTE::runOnModule(Module &M) {
Chris Lattnere51e03b2001-10-31 04:33:19 +000078 bool Changed = false;
79
Reid Spencer78d033e2007-01-06 07:24:44 +000080 TypeSymbolTable &ST = M.getTypeSymbolTable();
Chris Lattner4f77caa2004-02-26 20:02:23 +000081 std::set<const Type *> UsedTypes = getAnalysis<FindUsedTypes>().getTypes();
Chris Lattner7311e382002-07-23 22:03:41 +000082
Chris Lattner6e6026b2002-11-20 18:36:02 +000083 // Check the symbol table for superfluous type entries...
84 //
85 // Grab the 'type' plane of the module symbol...
Reid Spencer78d033e2007-01-06 07:24:44 +000086 TypeSymbolTable::iterator TI = ST.begin();
87 TypeSymbolTable::iterator TE = ST.end();
88 while ( TI != TE ) {
Reid Spencer9231ac82004-05-25 08:53:40 +000089 // If this entry should be unconditionally removed, or if we detect that
90 // the type is not used, remove it.
91 const Type *RHS = TI->second;
92 if (ShouldNukeSymtabEntry(RHS) || !UsedTypes.count(RHS)) {
Chris Lattner82aa5662005-03-06 05:42:36 +000093 ST.remove(TI++);
Reid Spencer9231ac82004-05-25 08:53:40 +000094 ++NumKilled;
95 Changed = true;
96 } else {
97 ++TI;
98 // We only need to leave one name for each type.
99 UsedTypes.erase(RHS);
Chris Lattner4f77caa2004-02-26 20:02:23 +0000100 }
Chris Lattner430aa9e2001-11-15 04:34:46 +0000101 }
Chris Lattner7311e382002-07-23 22:03:41 +0000102
Chris Lattnere51e03b2001-10-31 04:33:19 +0000103 return Changed;
104}
Reid Spencer9231ac82004-05-25 08:53:40 +0000105
106// vim: sw=2