Add and use Type::subtypes. NFC.
[oota-llvm.git] / lib / Analysis / IPA / FindUsedTypes.cpp
1 //===- FindUsedTypes.cpp - Find all Types used by a module ----------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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
12 // table.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "llvm/Analysis/FindUsedTypes.h"
17 #include "llvm/IR/Constants.h"
18 #include "llvm/IR/DerivedTypes.h"
19 #include "llvm/IR/InstIterator.h"
20 #include "llvm/IR/Module.h"
21 #include "llvm/Support/raw_ostream.h"
22 using namespace llvm;
23
24 char FindUsedTypes::ID = 0;
25 INITIALIZE_PASS(FindUsedTypes, "print-used-types",
26                 "Find Used Types", false, true)
27
28 // IncorporateType - Incorporate one type and all of its subtypes into the
29 // collection of used types.
30 //
31 void FindUsedTypes::IncorporateType(Type *Ty) {
32   // If ty doesn't already exist in the used types map, add it now, otherwise
33   // return.
34   if (!UsedTypes.insert(Ty)) return;  // Already contain Ty.
35
36   // Make sure to add any types this type references now.
37   //
38   for (Type *SubTy : Ty->subtypes())
39     IncorporateType(SubTy);
40 }
41
42 void FindUsedTypes::IncorporateValue(const Value *V) {
43   IncorporateType(V->getType());
44
45   // If this is a constant, it could be using other types...
46   if (const Constant *C = dyn_cast<Constant>(V)) {
47     if (!isa<GlobalValue>(C))
48       for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end();
49            OI != OE; ++OI)
50         IncorporateValue(*OI);
51   }
52 }
53
54
55 // run - This incorporates all types used by the specified module
56 //
57 bool FindUsedTypes::runOnModule(Module &m) {
58   UsedTypes.clear();  // reset if run multiple times...
59
60   // Loop over global variables, incorporating their types
61   for (Module::const_global_iterator I = m.global_begin(), E = m.global_end();
62        I != E; ++I) {
63     IncorporateType(I->getType());
64     if (I->hasInitializer())
65       IncorporateValue(I->getInitializer());
66   }
67
68   for (Module::iterator MI = m.begin(), ME = m.end(); MI != ME; ++MI) {
69     IncorporateType(MI->getType());
70     const Function &F = *MI;
71
72     // Loop over all of the instructions in the function, adding their return
73     // type as well as the types of their operands.
74     //
75     for (const_inst_iterator II = inst_begin(F), IE = inst_end(F);
76          II != IE; ++II) {
77       const Instruction &I = *II;
78
79       IncorporateType(I.getType());  // Incorporate the type of the instruction
80       for (User::const_op_iterator OI = I.op_begin(), OE = I.op_end();
81            OI != OE; ++OI)
82         IncorporateValue(*OI);  // Insert inst operand types as well
83     }
84   }
85
86   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 //
93 void FindUsedTypes::print(raw_ostream &OS, const Module *M) const {
94   OS << "Types in use by this module:\n";
95   for (SetVector<Type *>::const_iterator I = UsedTypes.begin(),
96        E = UsedTypes.end(); I != E; ++I) {
97     OS << "   " << **I << '\n';
98   }
99 }