LLVM  8.0.1
TypeFinder.h
Go to the documentation of this file.
1 //===- llvm/IR/TypeFinder.h - Class to find used struct types ---*- C++ -*-===//
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 contains the declaration of the TypeFinder class.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_IR_TYPEFINDER_H
15 #define LLVM_IR_TYPEFINDER_H
16 
17 #include "llvm/ADT/DenseSet.h"
18 #include <cstddef>
19 #include <vector>
20 
21 namespace llvm {
22 
23 class MDNode;
24 class Module;
25 class StructType;
26 class Type;
27 class Value;
28 
29 /// TypeFinder - Walk over a module, identifying all of the types that are
30 /// used by the module.
31 class TypeFinder {
32  // To avoid walking constant expressions multiple times and other IR
33  // objects, we keep several helper maps.
34  DenseSet<const Value*> VisitedConstants;
35  DenseSet<const MDNode *> VisitedMetadata;
36  DenseSet<Type*> VisitedTypes;
37 
38  std::vector<StructType*> StructTypes;
39  bool OnlyNamed = false;
40 
41 public:
42  TypeFinder() = default;
43 
44  void run(const Module &M, bool onlyNamed);
45  void clear();
46 
47  using iterator = std::vector<StructType*>::iterator;
48  using const_iterator = std::vector<StructType*>::const_iterator;
49 
50  iterator begin() { return StructTypes.begin(); }
51  iterator end() { return StructTypes.end(); }
52 
53  const_iterator begin() const { return StructTypes.begin(); }
54  const_iterator end() const { return StructTypes.end(); }
55 
56  bool empty() const { return StructTypes.empty(); }
57  size_t size() const { return StructTypes.size(); }
58  iterator erase(iterator I, iterator E) { return StructTypes.erase(I, E); }
59 
60  StructType *&operator[](unsigned Idx) { return StructTypes[Idx]; }
61 
62  DenseSet<const MDNode *> &getVisitedMetadata() { return VisitedMetadata; }
63 
64 private:
65  /// incorporateType - This method adds the type to the list of used
66  /// structures if it's not in there already.
67  void incorporateType(Type *Ty);
68 
69  /// incorporateValue - This method is used to walk operand lists finding types
70  /// hiding in constant expressions and other operands that won't be walked in
71  /// other ways. GlobalValues, basic blocks, instructions, and inst operands
72  /// are all explicitly enumerated.
73  void incorporateValue(const Value *V);
74 
75  /// incorporateMDNode - This method is used to walk the operands of an MDNode
76  /// to find types hiding within.
77  void incorporateMDNode(const MDNode *V);
78 };
79 
80 } // end namespace llvm
81 
82 #endif // LLVM_IR_TYPEFINDER_H
Type
MessagePack types as defined in the standard, with the exception of Integer being divided into a sign...
Definition: MsgPackReader.h:49
This class represents lattice values for constants.
Definition: AllocatorList.h:24
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
std::vector< StructType * >::const_iterator const_iterator
Definition: TypeFinder.h:48
Metadata node.
Definition: Metadata.h:864
Class to represent struct types.
Definition: DerivedTypes.h:201
DenseSet< const MDNode * > & getVisitedMetadata()
Definition: TypeFinder.h:62
const_iterator end() const
Definition: TypeFinder.h:54
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
StructType *& operator[](unsigned Idx)
Definition: TypeFinder.h:60
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
bool empty() const
Definition: TypeFinder.h:56
std::vector< StructType * >::iterator iterator
Definition: TypeFinder.h:47
void run(const Module &M, bool onlyNamed)
Definition: TypeFinder.cpp:32
iterator end()
Definition: TypeFinder.h:51
TypeFinder()=default
size_t size() const
Definition: TypeFinder.h:57
#define I(x, y, z)
Definition: MD5.cpp:58
iterator erase(iterator I, iterator E)
Definition: TypeFinder.h:58
LLVM Value Representation.
Definition: Value.h:73
iterator begin()
Definition: TypeFinder.h:50
const_iterator begin() const
Definition: TypeFinder.h:53
TypeFinder - Walk over a module, identifying all of the types that are used by the module...
Definition: TypeFinder.h:31