LLVM  4.0.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 (const Function &FI : M) {
45  incorporateType(FI.getType());
46 
47  for (const Use &U : FI.operands())
48  incorporateValue(U.get());
49 
50  // First incorporate the arguments.
51  for (Function::const_arg_iterator AI = FI.arg_begin(), AE = FI.arg_end();
52  AI != AE; ++AI)
53  incorporateValue(&*AI);
54 
55  for (const BasicBlock &BB : FI)
56  for (const Instruction &I : BB) {
57  // Incorporate the type of the instruction.
58  incorporateType(I.getType());
59 
60  // Incorporate non-instruction operand types. (We are incorporating all
61  // instructions with this loop.)
62  for (User::const_op_iterator OI = I.op_begin(), OE = I.op_end();
63  OI != OE; ++OI)
64  if (*OI && !isa<Instruction>(OI))
65  incorporateValue(*OI);
66 
67  // Incorporate types hiding in metadata.
68  I.getAllMetadataOtherThanDebugLoc(MDForInst);
69  for (unsigned i = 0, e = MDForInst.size(); i != e; ++i)
70  incorporateMDNode(MDForInst[i].second);
71 
72  MDForInst.clear();
73  }
74  }
75 
76  for (Module::const_named_metadata_iterator I = M.named_metadata_begin(),
77  E = M.named_metadata_end(); I != E; ++I) {
78  const NamedMDNode *NMD = &*I;
79  for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
80  incorporateMDNode(NMD->getOperand(i));
81  }
82 }
83 
85  VisitedConstants.clear();
86  VisitedTypes.clear();
87  StructTypes.clear();
88 }
89 
90 /// incorporateType - This method adds the type to the list of used structures
91 /// if it's not in there already.
92 void TypeFinder::incorporateType(Type *Ty) {
93  // Check to see if we've already visited this type.
94  if (!VisitedTypes.insert(Ty).second)
95  return;
96 
97  SmallVector<Type *, 4> TypeWorklist;
98  TypeWorklist.push_back(Ty);
99  do {
100  Ty = TypeWorklist.pop_back_val();
101 
102  // If this is a structure or opaque type, add a name for the type.
103  if (StructType *STy = dyn_cast<StructType>(Ty))
104  if (!OnlyNamed || STy->hasName())
105  StructTypes.push_back(STy);
106 
107  // Add all unvisited subtypes to worklist for processing
109  E = Ty->subtype_rend();
110  I != E; ++I)
111  if (VisitedTypes.insert(*I).second)
112  TypeWorklist.push_back(*I);
113  } while (!TypeWorklist.empty());
114 }
115 
116 /// incorporateValue - This method is used to walk operand lists finding types
117 /// hiding in constant expressions and other operands that won't be walked in
118 /// other ways. GlobalValues, basic blocks, instructions, and inst operands are
119 /// all explicitly enumerated.
120 void TypeFinder::incorporateValue(const Value *V) {
121  if (const auto *M = dyn_cast<MetadataAsValue>(V)) {
122  if (const auto *N = dyn_cast<MDNode>(M->getMetadata()))
123  return incorporateMDNode(N);
124  if (const auto *MDV = dyn_cast<ValueAsMetadata>(M->getMetadata()))
125  return incorporateValue(MDV->getValue());
126  return;
127  }
128 
129  if (!isa<Constant>(V) || isa<GlobalValue>(V)) return;
130 
131  // Already visited?
132  if (!VisitedConstants.insert(V).second)
133  return;
134 
135  // Check this type.
136  incorporateType(V->getType());
137 
138  // If this is an instruction, we incorporate it separately.
139  if (isa<Instruction>(V))
140  return;
141 
142  // Look in operands for types.
143  const User *U = cast<User>(V);
145  E = U->op_end(); I != E;++I)
146  incorporateValue(*I);
147 }
148 
149 /// incorporateMDNode - This method is used to walk the operands of an MDNode to
150 /// find types hiding within.
151 void TypeFinder::incorporateMDNode(const MDNode *V) {
152  // Already visited?
153  if (!VisitedMetadata.insert(V).second)
154  return;
155 
156  // Look in operands for types.
157  for (unsigned i = 0, e = V->getNumOperands(); i != e; ++i) {
158  Metadata *Op = V->getOperand(i);
159  if (!Op)
160  continue;
161  if (auto *N = dyn_cast<MDNode>(Op)) {
162  incorporateMDNode(N);
163  continue;
164  }
165  if (auto *C = dyn_cast<ConstantAsMetadata>(Op)) {
166  incorporateValue(C->getValue());
167  continue;
168  }
169  }
170 }
size_t i
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:52
unsigned getNumOperands() const
Return number of MDNode operands.
Definition: Metadata.h:1040
This file contains the declarations for metadata subclasses.
Metadata node.
Definition: Metadata.h:830
subtype_reverse_iterator subtype_rend() const
Definition: Type.h:309
op_iterator op_begin()
Definition: User.h:205
A tuple of MDNodes.
Definition: Metadata.h:1282
Class to represent struct types.
Definition: DerivedTypes.h:199
A Use represents the edge between a Value definition and its users.
Definition: Use.h:56
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:60
global_iterator global_begin()
Definition: Module.h:518
static GCRegistry::Add< CoreCLRGC > E("coreclr","CoreCLR-compatible GC")
alias_iterator alias_end()
Definition: Module.h:559
LLVM Basic Block Representation.
Definition: BasicBlock.h:51
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
op_iterator op_end()
Definition: User.h:207
MDNode * getOperand(unsigned i) const
Definition: Metadata.cpp:1042
global_iterator global_end()
Definition: Module.h:520
const MDOperand & getOperand(unsigned I) const
Definition: Metadata.h:1034
Iterator for intrusive lists based on ilist_node.
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:843
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:230
LLVM_NODISCARD T pop_back_val()
Definition: SmallVector.h:382
alias_iterator alias_begin()
Definition: Module.h:557
static GCRegistry::Add< ShadowStackGC > C("shadow-stack","Very portable GC for uncooperative code generators")
#define I(x, y, z)
Definition: MD5.cpp:54
#define N
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
Definition: SmallVector.h:135
LLVM Value Representation.
Definition: Value.h:71
unsigned getNumOperands() const
Definition: Metadata.cpp:1038
std::reverse_iterator< subtype_iterator > subtype_reverse_iterator
Definition: Type.h:305
Root of the metadata hierarchy.
Definition: Metadata.h:55
subtype_reverse_iterator subtype_rbegin() const
Definition: Type.h:306
const Use * const_op_iterator
Definition: User.h:201