1 //===- FindUsedTypes.cpp - Find all Types used by a module ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // 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
14 //===----------------------------------------------------------------------===//
16 #include "llvm/Analysis/FindUsedTypes.h"
17 #include "llvm/IR/Constants.h"
18 #include "llvm/IR/DerivedTypes.h"
19 #include "llvm/IR/Module.h"
20 #include "llvm/Support/InstIterator.h"
21 #include "llvm/Support/raw_ostream.h"
24 char FindUsedTypes::ID = 0;
25 INITIALIZE_PASS(FindUsedTypes, "print-used-types",
26 "Find Used Types", false, true)
28 // IncorporateType - Incorporate one type and all of its subtypes into the
29 // collection of used types.
31 void FindUsedTypes::IncorporateType(Type *Ty) {
32 // If ty doesn't already exist in the used types map, add it now, otherwise
34 if (!UsedTypes.insert(Ty)) return; // Already contain Ty.
36 // Make sure to add any types this type references now.
38 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
43 void FindUsedTypes::IncorporateValue(const Value *V) {
44 IncorporateType(V->getType());
46 // If this is a constant, it could be using other types...
47 if (const Constant *C = dyn_cast<Constant>(V)) {
48 if (!isa<GlobalValue>(C))
49 for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end();
51 IncorporateValue(*OI);
56 // run - This incorporates all types used by the specified module
58 bool FindUsedTypes::runOnModule(Module &m) {
59 UsedTypes.clear(); // reset if run multiple times...
61 // Loop over global variables, incorporating their types
62 for (Module::const_global_iterator I = m.global_begin(), E = m.global_end();
64 IncorporateType(I->getType());
65 if (I->hasInitializer())
66 IncorporateValue(I->getInitializer());
69 for (Module::iterator MI = m.begin(), ME = m.end(); MI != ME; ++MI) {
70 IncorporateType(MI->getType());
71 const Function &F = *MI;
73 // Loop over all of the instructions in the function, adding their return
74 // type as well as the types of their operands.
76 for (const_inst_iterator II = inst_begin(F), IE = inst_end(F);
78 const Instruction &I = *II;
80 IncorporateType(I.getType()); // Incorporate the type of the instruction
81 for (User::const_op_iterator OI = I.op_begin(), OE = I.op_end();
83 IncorporateValue(*OI); // Insert inst operand types as well
90 // Print the types found in the module. If the optional Module parameter is
91 // passed in, then the types are printed symbolically if possible, using the
92 // symbol table from the module.
94 void FindUsedTypes::print(raw_ostream &OS, const Module *M) const {
95 OS << "Types in use by this module:\n";
96 for (SetVector<Type *>::const_iterator I = UsedTypes.begin(),
97 E = UsedTypes.end(); I != E; ++I) {
98 OS << " " << **I << '\n';