1 //===- llvm/IR/TypeFinder.h - Class to find used struct types ---*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains the declaration of the TypeFinder class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_IR_TYPEFINDER_H
14 #define LLVM_IR_TYPEFINDER_H
15 
16 #include "llvm/ADT/DenseSet.h"
17 #include "llvm/IR/Attributes.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<AttributeList> VisitedAttributes;
37   DenseSet<Type*> VisitedTypes;
38 
39   std::vector<StructType*> StructTypes;
40   bool OnlyNamed = false;
41 
42 public:
43   TypeFinder() = default;
44 
45   void run(const Module &M, bool onlyNamed);
46   void clear();
47 
48   using iterator = std::vector<StructType*>::iterator;
49   using const_iterator = std::vector<StructType*>::const_iterator;
50 
51   iterator begin() { return StructTypes.begin(); }
52   iterator end() { return StructTypes.end(); }
53 
54   const_iterator begin() const { return StructTypes.begin(); }
55   const_iterator end() const { return StructTypes.end(); }
56 
57   bool empty() const { return StructTypes.empty(); }
58   size_t size() const { return StructTypes.size(); }
59   iterator erase(iterator I, iterator E) { return StructTypes.erase(I, E); }
60 
61   StructType *&operator[](unsigned Idx) { return StructTypes[Idx]; }
62 
63   DenseSet<const MDNode *> &getVisitedMetadata() { return VisitedMetadata; }
64 
65 private:
66   /// incorporateType - This method adds the type to the list of used
67   /// structures if it's not in there already.
68   void incorporateType(Type *Ty);
69 
70   /// incorporateValue - This method is used to walk operand lists finding types
71   /// hiding in constant expressions and other operands that won't be walked in
72   /// other ways.  GlobalValues, basic blocks, instructions, and inst operands
73   /// are all explicitly enumerated.
74   void incorporateValue(const Value *V);
75 
76   /// incorporateMDNode - This method is used to walk the operands of an MDNode
77   /// to find types hiding within.
78   void incorporateMDNode(const MDNode *V);
79 
80   /// Incorporate types referenced by attributes.
81   void incorporateAttributes(AttributeList AL);
82 };
83 
84 } // end namespace llvm
85 
86 #endif // LLVM_IR_TYPEFINDER_H
87