1 //===-- TypeFinder.cpp - Implement the TypeFinder class -------------------===//
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 file implements the TypeFinder class for the IR library.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/IR/TypeFinder.h"
15 #include "llvm/ADT/SmallVector.h"
16 #include "llvm/IR/BasicBlock.h"
17 #include "llvm/IR/DerivedTypes.h"
18 #include "llvm/IR/Function.h"
19 #include "llvm/IR/Metadata.h"
20 #include "llvm/IR/Module.h"
23 void TypeFinder::run(const Module &M, bool onlyNamed) {
24 OnlyNamed = onlyNamed;
26 // Get types from global variables.
27 for (Module::const_global_iterator I = M.global_begin(),
28 E = M.global_end(); I != E; ++I) {
29 incorporateType(I->getType());
30 if (I->hasInitializer())
31 incorporateValue(I->getInitializer());
34 // Get types from aliases.
35 for (Module::const_alias_iterator I = M.alias_begin(),
36 E = M.alias_end(); I != E; ++I) {
37 incorporateType(I->getType());
38 if (const Value *Aliasee = I->getAliasee())
39 incorporateValue(Aliasee);
42 // Get types from functions.
43 SmallVector<std::pair<unsigned, MDNode*>, 4> MDForInst;
44 for (Module::const_iterator FI = M.begin(), E = M.end(); FI != E; ++FI) {
45 incorporateType(FI->getType());
47 if (FI->hasPrefixData())
48 incorporateValue(FI->getPrefixData());
50 // First incorporate the arguments.
51 for (Function::const_arg_iterator AI = FI->arg_begin(),
52 AE = FI->arg_end(); AI != AE; ++AI)
55 for (Function::const_iterator BB = FI->begin(), E = FI->end();
57 for (BasicBlock::const_iterator II = BB->begin(),
58 E = BB->end(); II != E; ++II) {
59 const Instruction &I = *II;
61 // Incorporate the type of the instruction.
62 incorporateType(I.getType());
64 // Incorporate non-instruction operand types. (We are incorporating all
65 // instructions with this loop.)
66 for (User::const_op_iterator OI = I.op_begin(), OE = I.op_end();
68 if (!isa<Instruction>(OI))
69 incorporateValue(*OI);
71 // Incorporate types hiding in metadata.
72 I.getAllMetadataOtherThanDebugLoc(MDForInst);
73 for (unsigned i = 0, e = MDForInst.size(); i != e; ++i)
74 incorporateMDNode(MDForInst[i].second);
80 for (Module::const_named_metadata_iterator I = M.named_metadata_begin(),
81 E = M.named_metadata_end(); I != E; ++I) {
82 const NamedMDNode *NMD = I;
83 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
84 incorporateMDNode(NMD->getOperand(i));
88 void TypeFinder::clear() {
89 VisitedConstants.clear();
94 /// incorporateType - This method adds the type to the list of used structures
95 /// if it's not in there already.
96 void TypeFinder::incorporateType(Type *Ty) {
97 // Check to see if we're already visited this type.
98 if (!VisitedTypes.insert(Ty).second)
101 // If this is a structure or opaque type, add a name for the type.
102 if (StructType *STy = dyn_cast<StructType>(Ty))
103 if (!OnlyNamed || STy->hasName())
104 StructTypes.push_back(STy);
106 // Recursively walk all contained types.
107 for (Type::subtype_iterator I = Ty->subtype_begin(),
108 E = Ty->subtype_end(); I != E; ++I)
112 /// incorporateValue - This method is used to walk operand lists finding types
113 /// hiding in constant expressions and other operands that won't be walked in
114 /// other ways. GlobalValues, basic blocks, instructions, and inst operands are
115 /// all explicitly enumerated.
116 void TypeFinder::incorporateValue(const Value *V) {
117 if (const MDNode *M = dyn_cast<MDNode>(V))
118 return incorporateMDNode(M);
120 if (!isa<Constant>(V) || isa<GlobalValue>(V)) return;
123 if (!VisitedConstants.insert(V).second)
127 incorporateType(V->getType());
129 // If this is an instruction, we incorporate it separately.
130 if (isa<Instruction>(V))
133 // Look in operands for types.
134 const User *U = cast<User>(V);
135 for (Constant::const_op_iterator I = U->op_begin(),
136 E = U->op_end(); I != E;++I)
137 incorporateValue(*I);
140 /// incorporateMDNode - This method is used to walk the operands of an MDNode to
141 /// find types hiding within.
142 void TypeFinder::incorporateMDNode(const MDNode *V) {
144 if (!VisitedConstants.insert(V).second)
147 // Look in operands for types.
148 for (unsigned i = 0, e = V->getNumOperands(); i != e; ++i)
149 if (Value *Op = V->getOperand(i))
150 incorporateValue(Op);