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/Constant.h"
18 #include "llvm/IR/DerivedTypes.h"
19 #include "llvm/IR/Function.h"
20 #include "llvm/IR/Instruction.h"
21 #include "llvm/IR/Metadata.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/IR/Type.h"
24 #include "llvm/IR/Use.h"
25 #include "llvm/IR/User.h"
26 #include "llvm/IR/Value.h"
27 #include "llvm/Support/Casting.h"
32 void TypeFinder::run(const Module &M, bool onlyNamed) {
33 OnlyNamed = onlyNamed;
35 // Get types from global variables.
36 for (Module::const_global_iterator I = M.global_begin(),
37 E = M.global_end(); I != E; ++I) {
38 incorporateType(I->getType());
39 if (I->hasInitializer())
40 incorporateValue(I->getInitializer());
43 // Get types from aliases.
44 for (Module::const_alias_iterator I = M.alias_begin(),
45 E = M.alias_end(); I != E; ++I) {
46 incorporateType(I->getType());
47 if (const Value *Aliasee = I->getAliasee())
48 incorporateValue(Aliasee);
51 // Get types from functions.
52 SmallVector<std::pair<unsigned, MDNode *>, 4> MDForInst;
53 for (const Function &FI : M) {
54 incorporateType(FI.getType());
56 for (const Use &U : FI.operands())
57 incorporateValue(U.get());
59 // First incorporate the arguments.
60 for (Function::const_arg_iterator AI = FI.arg_begin(), AE = FI.arg_end();
62 incorporateValue(&*AI);
64 for (const BasicBlock &BB : FI)
65 for (const Instruction &I : BB) {
66 // Incorporate the type of the instruction.
67 incorporateType(I.getType());
69 // Incorporate non-instruction operand types. (We are incorporating all
70 // instructions with this loop.)
71 for (User::const_op_iterator OI = I.op_begin(), OE = I.op_end();
73 if (*OI && !isa<Instruction>(OI))
74 incorporateValue(*OI);
76 // Incorporate types hiding in metadata.
77 I.getAllMetadataOtherThanDebugLoc(MDForInst);
78 for (unsigned i = 0, e = MDForInst.size(); i != e; ++i)
79 incorporateMDNode(MDForInst[i].second);
85 for (Module::const_named_metadata_iterator I = M.named_metadata_begin(),
86 E = M.named_metadata_end(); I != E; ++I) {
87 const NamedMDNode *NMD = &*I;
88 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
89 incorporateMDNode(NMD->getOperand(i));
93 void TypeFinder::clear() {
94 VisitedConstants.clear();
99 /// incorporateType - This method adds the type to the list of used structures
100 /// if it's not in there already.
101 void TypeFinder::incorporateType(Type *Ty) {
102 // Check to see if we've already visited this type.
103 if (!VisitedTypes.insert(Ty).second)
106 SmallVector<Type *, 4> TypeWorklist;
107 TypeWorklist.push_back(Ty);
109 Ty = TypeWorklist.pop_back_val();
111 // If this is a structure or opaque type, add a name for the type.
112 if (StructType *STy = dyn_cast<StructType>(Ty))
113 if (!OnlyNamed || STy->hasName())
114 StructTypes.push_back(STy);
116 // Add all unvisited subtypes to worklist for processing
117 for (Type::subtype_reverse_iterator I = Ty->subtype_rbegin(),
118 E = Ty->subtype_rend();
120 if (VisitedTypes.insert(*I).second)
121 TypeWorklist.push_back(*I);
122 } while (!TypeWorklist.empty());
125 /// incorporateValue - This method is used to walk operand lists finding types
126 /// hiding in constant expressions and other operands that won't be walked in
127 /// other ways. GlobalValues, basic blocks, instructions, and inst operands are
128 /// all explicitly enumerated.
129 void TypeFinder::incorporateValue(const Value *V) {
130 if (const auto *M = dyn_cast<MetadataAsValue>(V)) {
131 if (const auto *N = dyn_cast<MDNode>(M->getMetadata()))
132 return incorporateMDNode(N);
133 if (const auto *MDV = dyn_cast<ValueAsMetadata>(M->getMetadata()))
134 return incorporateValue(MDV->getValue());
138 if (!isa<Constant>(V) || isa<GlobalValue>(V)) return;
141 if (!VisitedConstants.insert(V).second)
145 incorporateType(V->getType());
147 // If this is an instruction, we incorporate it separately.
148 if (isa<Instruction>(V))
151 // Look in operands for types.
152 const User *U = cast<User>(V);
153 for (Constant::const_op_iterator I = U->op_begin(),
154 E = U->op_end(); I != E;++I)
155 incorporateValue(*I);
158 /// incorporateMDNode - This method is used to walk the operands of an MDNode to
159 /// find types hiding within.
160 void TypeFinder::incorporateMDNode(const MDNode *V) {
162 if (!VisitedMetadata.insert(V).second)
165 // Look in operands for types.
166 for (unsigned i = 0, e = V->getNumOperands(); i != e; ++i) {
167 Metadata *Op = V->getOperand(i);
170 if (auto *N = dyn_cast<MDNode>(Op)) {
171 incorporateMDNode(N);
174 if (auto *C = dyn_cast<ConstantAsMetadata>(Op)) {
175 incorporateValue(C->getValue());