LLVM  3.7.0
TypeFinder.cpp
Go to the documentation of this file.
1 //===-- TypeFinder.cpp - Implement the TypeFinder class -------------------===//
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 file implements the TypeFinder class for the IR library.
11 //
12 //===----------------------------------------------------------------------===//
13 
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"
21 using namespace llvm;
22 
23 void TypeFinder::run(const Module &M, bool onlyNamed) {
24  OnlyNamed = onlyNamed;
25 
26  // Get types from global variables.
28  E = M.global_end(); I != E; ++I) {
29  incorporateType(I->getType());
30  if (I->hasInitializer())
31  incorporateValue(I->getInitializer());
32  }
33 
34  // Get types from aliases.
36  E = M.alias_end(); I != E; ++I) {
37  incorporateType(I->getType());
38  if (const Value *Aliasee = I->getAliasee())
39  incorporateValue(Aliasee);
40  }
41 
42  // Get types from functions.
44  for (Module::const_iterator FI = M.begin(), E = M.end(); FI != E; ++FI) {
45  incorporateType(FI->getType());
46 
47  if (FI->hasPrefixData())
48  incorporateValue(FI->getPrefixData());
49 
50  if (FI->hasPrologueData())
51  incorporateValue(FI->getPrologueData());
52 
53  if (FI->hasPersonalityFn())
54  incorporateValue(FI->getPersonalityFn());
55 
56  // First incorporate the arguments.
57  for (Function::const_arg_iterator AI = FI->arg_begin(),
58  AE = FI->arg_end(); AI != AE; ++AI)
59  incorporateValue(AI);
60 
61  for (Function::const_iterator BB = FI->begin(), E = FI->end();
62  BB != E;++BB)
63  for (BasicBlock::const_iterator II = BB->begin(),
64  E = BB->end(); II != E; ++II) {
65  const Instruction &I = *II;
66 
67  // Incorporate the type of the instruction.
68  incorporateType(I.getType());
69 
70  // Incorporate non-instruction operand types. (We are incorporating all
71  // instructions with this loop.)
72  for (User::const_op_iterator OI = I.op_begin(), OE = I.op_end();
73  OI != OE; ++OI)
74  if (*OI && !isa<Instruction>(OI))
75  incorporateValue(*OI);
76 
77  // Incorporate types hiding in metadata.
79  for (unsigned i = 0, e = MDForInst.size(); i != e; ++i)
80  incorporateMDNode(MDForInst[i].second);
81 
82  MDForInst.clear();
83  }
84  }
85 
87  E = M.named_metadata_end(); I != E; ++I) {
88  const NamedMDNode *NMD = I;
89  for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
90  incorporateMDNode(NMD->getOperand(i));
91  }
92 }
93 
95  VisitedConstants.clear();
96  VisitedTypes.clear();
97  StructTypes.clear();
98 }
99 
100 /// incorporateType - This method adds the type to the list of used structures
101 /// if it's not in there already.
102 void TypeFinder::incorporateType(Type *Ty) {
103  // Check to see if we've already visited this type.
104  if (!VisitedTypes.insert(Ty).second)
105  return;
106 
107  SmallVector<Type *, 4> TypeWorklist;
108  TypeWorklist.push_back(Ty);
109  do {
110  Ty = TypeWorklist.pop_back_val();
111 
112  // If this is a structure or opaque type, add a name for the type.
113  if (StructType *STy = dyn_cast<StructType>(Ty))
114  if (!OnlyNamed || STy->hasName())
115  StructTypes.push_back(STy);
116 
117  // Add all unvisited subtypes to worklist for processing
119  E = Ty->subtype_rend();
120  I != E; ++I)
121  if (VisitedTypes.insert(*I).second)
122  TypeWorklist.push_back(*I);
123  } while (!TypeWorklist.empty());
124 }
125 
126 /// incorporateValue - This method is used to walk operand lists finding types
127 /// hiding in constant expressions and other operands that won't be walked in
128 /// other ways. GlobalValues, basic blocks, instructions, and inst operands are
129 /// all explicitly enumerated.
130 void TypeFinder::incorporateValue(const Value *V) {
131  if (const auto *M = dyn_cast<MetadataAsValue>(V)) {
132  if (const auto *N = dyn_cast<MDNode>(M->getMetadata()))
133  return incorporateMDNode(N);
134  if (const auto *MDV = dyn_cast<ValueAsMetadata>(M->getMetadata()))
135  return incorporateValue(MDV->getValue());
136  return;
137  }
138 
139  if (!isa<Constant>(V) || isa<GlobalValue>(V)) return;
140 
141  // Already visited?
142  if (!VisitedConstants.insert(V).second)
143  return;
144 
145  // Check this type.
146  incorporateType(V->getType());
147 
148  // If this is an instruction, we incorporate it separately.
149  if (isa<Instruction>(V))
150  return;
151 
152  // Look in operands for types.
153  const User *U = cast<User>(V);
155  E = U->op_end(); I != E;++I)
156  incorporateValue(*I);
157 }
158 
159 /// incorporateMDNode - This method is used to walk the operands of an MDNode to
160 /// find types hiding within.
161 void TypeFinder::incorporateMDNode(const MDNode *V) {
162  // Already visited?
163  if (!VisitedMetadata.insert(V).second)
164  return;
165 
166  // Look in operands for types.
167  for (unsigned i = 0, e = V->getNumOperands(); i != e; ++i) {
168  Metadata *Op = V->getOperand(i);
169  if (!Op)
170  continue;
171  if (auto *N = dyn_cast<MDNode>(Op)) {
172  incorporateMDNode(N);
173  continue;
174  }
175  if (auto *C = dyn_cast<ConstantAsMetadata>(Op)) {
176  incorporateValue(C->getValue());
177  continue;
178  }
179  }
180 }
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:114
unsigned getNumOperands() const
Return number of MDNode operands.
Definition: Metadata.h:942
named_metadata_iterator named_metadata_end()
Definition: Module.h:614
This file contains the declarations for metadata subclasses.
Metadata node.
Definition: Metadata.h:740
subtype_reverse_iterator subtype_rend() const
Definition: Type.h:324
op_iterator op_begin()
Definition: User.h:183
A tuple of MDNodes.
Definition: Metadata.h:1127
T LLVM_ATTRIBUTE_UNUSED_RESULT pop_back_val()
Definition: SmallVector.h:406
StructType - Class to represent struct types.
Definition: DerivedTypes.h:191
A Use represents the edge between a Value definition and its users.
Definition: Use.h:69
global_iterator global_begin()
Definition: Module.h:552
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
Definition: SmallVector.h:57
alias_iterator alias_end()
Definition: Module.h:593
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
void getAllMetadataOtherThanDebugLoc(SmallVectorImpl< std::pair< unsigned, MDNode * >> &MDs) const
getAllMetadataOtherThanDebugLoc - This does the same thing as getAllMetadata, except that it filters ...
Definition: Instruction.h:190
op_iterator op_end()
Definition: User.h:185
MDNode * getOperand(unsigned i) const
Definition: Metadata.cpp:965
global_iterator global_end()
Definition: Module.h:554
const MDOperand & getOperand(unsigned I) const
Definition: Metadata.h:936
std::pair< iterator, bool > insert(const ValueT &V)
Definition: DenseSet.h:147
void run(const Module &M, bool onlyNamed)
Definition: TypeFinder.cpp:23
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
Module.h This file contains the declarations for the Module class.
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:222
alias_iterator alias_begin()
Definition: Module.h:591
iterator end()
Definition: Module.h:571
#define I(x, y, z)
Definition: MD5.cpp:54
#define N
iterator begin()
Definition: Module.h:569
void clear()
Definition: DenseSet.h:60
LLVM Value Representation.
Definition: Value.h:69
unsigned getNumOperands() const
Definition: Metadata.cpp:961
std::reverse_iterator< subtype_iterator > subtype_reverse_iterator
Definition: Type.h:320
Root of the metadata hierarchy.
Definition: Metadata.h:45
subtype_reverse_iterator subtype_rbegin() const
Definition: Type.h:321
named_metadata_iterator named_metadata_begin()
Definition: Module.h:609
const Use * const_op_iterator
Definition: User.h:179