blob: 34c126f5d45bc212626857f2742e31d18c13462b [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===- FindUsedTypes.cpp - Find all Types used by a module ----------------===//
Misha Brukman2b37d7c2005-04-21 21:13:18 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// 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.
Misha Brukman2b37d7c2005-04-21 21:13:18 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerb61107a2001-11-09 05:27:21 +00009//
Chris Lattnerba12c232003-11-02 01:28:41 +000010// This pass is used to seek out all of the types in use by the program. Note
11// that this analysis explicitly does not include types only used by the symbol
12// table.
Chris Lattnerb61107a2001-11-09 05:27:21 +000013//
14//===----------------------------------------------------------------------===//
15
16#include "llvm/Analysis/FindUsedTypes.h"
Chris Lattnerc2bcde42003-10-28 23:09:45 +000017#include "llvm/Constants.h"
Chris Lattnerb61107a2001-11-09 05:27:21 +000018#include "llvm/DerivedTypes.h"
Chris Lattner93193f82002-01-31 00:42:27 +000019#include "llvm/Module.h"
Chris Lattnere5388382006-12-06 06:35:25 +000020#include "llvm/Assembly/Writer.h"
Chris Lattner221d6882002-02-12 21:07:25 +000021#include "llvm/Support/InstIterator.h"
Chris Lattner0555ed82004-05-09 06:22:29 +000022using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000023
Devang Patel19974732007-05-03 01:11:54 +000024char FindUsedTypes::ID = 0;
Chris Lattner5d8925c2006-08-27 22:30:17 +000025static RegisterPass<FindUsedTypes>
Chris Lattnera6275cc2002-07-26 21:12:46 +000026X("printusedtypes", "Find Used Types");
Chris Lattnerb61107a2001-11-09 05:27:21 +000027
28// IncorporateType - Incorporate one type and all of its subtypes into the
29// collection of used types.
30//
31void FindUsedTypes::IncorporateType(const Type *Ty) {
Misha Brukman2b37d7c2005-04-21 21:13:18 +000032 // If ty doesn't already exist in the used types map, add it now, otherwise
Chris Lattnera5dcc4f2004-05-28 05:36:49 +000033 // return.
34 if (!UsedTypes.insert(Ty).second) return; // Already contain Ty.
Misha Brukman2b37d7c2005-04-21 21:13:18 +000035
Chris Lattnerb61107a2001-11-09 05:27:21 +000036 // Make sure to add any types this type references now.
37 //
38 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
39 I != E; ++I)
40 IncorporateType(*I);
41}
42
Chris Lattnerc2bcde42003-10-28 23:09:45 +000043void FindUsedTypes::IncorporateValue(const Value *V) {
44 IncorporateType(V->getType());
Misha Brukman2b37d7c2005-04-21 21:13:18 +000045
Chris Lattnerc2bcde42003-10-28 23:09:45 +000046 // If this is a constant, it could be using other types...
47 if (const Constant *C = dyn_cast<Constant>(V)) {
Reid Spencere8404342004-07-18 00:18:30 +000048 if (!isa<GlobalValue>(C))
49 for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end();
Misha Brukmandedf2bd2005-04-22 04:01:18 +000050 OI != OE; ++OI)
51 IncorporateValue(*OI);
Chris Lattnerc2bcde42003-10-28 23:09:45 +000052 }
53}
54
55
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000056// run - This incorporates all types used by the specified module
Chris Lattnerb61107a2001-11-09 05:27:21 +000057//
Chris Lattnerb12914b2004-09-20 04:48:05 +000058bool FindUsedTypes::runOnModule(Module &m) {
Chris Lattner93193f82002-01-31 00:42:27 +000059 UsedTypes.clear(); // reset if run multiple times...
60
Chris Lattner93193f82002-01-31 00:42:27 +000061 // Loop over global variables, incorporating their types
Chris Lattnere4d5c442005-03-15 04:54:21 +000062 for (Module::const_global_iterator I = m.global_begin(), E = m.global_end(); I != E; ++I) {
Chris Lattner7e708292002-06-25 16:13:24 +000063 IncorporateType(I->getType());
Chris Lattnerc2bcde42003-10-28 23:09:45 +000064 if (I->hasInitializer())
65 IncorporateValue(I->getInitializer());
66 }
Chris Lattner93193f82002-01-31 00:42:27 +000067
Chris Lattner7e708292002-06-25 16:13:24 +000068 for (Module::iterator MI = m.begin(), ME = m.end(); MI != ME; ++MI) {
Chris Lattnera92dc192003-05-31 23:30:52 +000069 IncorporateType(MI->getType());
Chris Lattner7e708292002-06-25 16:13:24 +000070 const Function &F = *MI;
Misha Brukman2b37d7c2005-04-21 21:13:18 +000071
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000072 // Loop over all of the instructions in the function, adding their return
73 // type as well as the types of their operands.
Chris Lattner93193f82002-01-31 00:42:27 +000074 //
Chris Lattner7e708292002-06-25 16:13:24 +000075 for (const_inst_iterator II = inst_begin(F), IE = inst_end(F);
Chris Lattner93193f82002-01-31 00:42:27 +000076 II != IE; ++II) {
Chris Lattner6ffe5512004-04-27 15:13:33 +000077 const Instruction &I = *II;
Misha Brukman2b37d7c2005-04-21 21:13:18 +000078
Chris Lattnera5dcc4f2004-05-28 05:36:49 +000079 IncorporateType(I.getType()); // Incorporate the type of the instruction
Chris Lattner6ffe5512004-04-27 15:13:33 +000080 for (User::const_op_iterator OI = I.op_begin(), OE = I.op_end();
Chris Lattner93193f82002-01-31 00:42:27 +000081 OI != OE; ++OI)
Chris Lattnerc2bcde42003-10-28 23:09:45 +000082 IncorporateValue(*OI); // Insert inst operand types as well
Chris Lattner93193f82002-01-31 00:42:27 +000083 }
Chris Lattnerb61107a2001-11-09 05:27:21 +000084 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +000085
Chris Lattnerb61107a2001-11-09 05:27:21 +000086 return false;
87}
88
89// Print the types found in the module. If the optional Module parameter is
90// passed in, then the types are printed symbolically if possible, using the
91// symbol table from the module.
92//
Chris Lattnera59cbb22002-07-27 01:12:17 +000093void FindUsedTypes::print(std::ostream &o, const Module *M) const {
Chris Lattnerb61107a2001-11-09 05:27:21 +000094 o << "Types in use by this module:\n";
Chris Lattnere5388382006-12-06 06:35:25 +000095 for (std::set<const Type *>::const_iterator I = UsedTypes.begin(),
96 E = UsedTypes.end(); I != E; ++I)
97 WriteTypeSymbolic(o << " ", *I, M) << "\n";
Chris Lattnerb61107a2001-11-09 05:27:21 +000098}
Reid Spencer4f1bd9e2006-06-07 22:00:26 +000099
100// Ensure that this file gets linked in when FindUsedTypes.h is used.
101DEFINING_FILE_FOR(FindUsedTypes)