[Alignment][NFC] Convert StoreInst to MaybeAlign
[llvm-complete.git] / include / llvm / IR / TypeFinder.h
bloba83f85ea84c303e1a47bb194845a6fe1b09cde00
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.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_IR_TYPEFINDER_H
14 #define LLVM_IR_TYPEFINDER_H
16 #include "llvm/ADT/DenseSet.h"
17 #include <cstddef>
18 #include <vector>
20 namespace llvm {
22 class MDNode;
23 class Module;
24 class StructType;
25 class Type;
26 class Value;
28 /// TypeFinder - Walk over a module, identifying all of the types that are
29 /// used by the module.
30 class TypeFinder {
31 // To avoid walking constant expressions multiple times and other IR
32 // objects, we keep several helper maps.
33 DenseSet<const Value*> VisitedConstants;
34 DenseSet<const MDNode *> VisitedMetadata;
35 DenseSet<Type*> VisitedTypes;
37 std::vector<StructType*> StructTypes;
38 bool OnlyNamed = false;
40 public:
41 TypeFinder() = default;
43 void run(const Module &M, bool onlyNamed);
44 void clear();
46 using iterator = std::vector<StructType*>::iterator;
47 using const_iterator = std::vector<StructType*>::const_iterator;
49 iterator begin() { return StructTypes.begin(); }
50 iterator end() { return StructTypes.end(); }
52 const_iterator begin() const { return StructTypes.begin(); }
53 const_iterator end() const { return StructTypes.end(); }
55 bool empty() const { return StructTypes.empty(); }
56 size_t size() const { return StructTypes.size(); }
57 iterator erase(iterator I, iterator E) { return StructTypes.erase(I, E); }
59 StructType *&operator[](unsigned Idx) { return StructTypes[Idx]; }
61 DenseSet<const MDNode *> &getVisitedMetadata() { return VisitedMetadata; }
63 private:
64 /// incorporateType - This method adds the type to the list of used
65 /// structures if it's not in there already.
66 void incorporateType(Type *Ty);
68 /// incorporateValue - This method is used to walk operand lists finding types
69 /// hiding in constant expressions and other operands that won't be walked in
70 /// other ways. GlobalValues, basic blocks, instructions, and inst operands
71 /// are all explicitly enumerated.
72 void incorporateValue(const Value *V);
74 /// incorporateMDNode - This method is used to walk the operands of an MDNode
75 /// to find types hiding within.
76 void incorporateMDNode(const MDNode *V);
79 } // end namespace llvm
81 #endif // LLVM_IR_TYPEFINDER_H