LLVM 20.0.0git
TypeFinder.h
Go to the documentation of this file.
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
21namespace llvm {
22
23class MDNode;
24class Module;
25class StructType;
26class Type;
27class Value;
28
29/// TypeFinder - Walk over a module, identifying all of the types that are
30/// used by the module.
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
42public:
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
65private:
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
This file contains the simple types necessary to represent the attributes associated with functions a...
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
RelocType Type
Definition: COFFYAML.cpp:391
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
This file defines the DenseSet and SmallDenseSet classes.
#define I(x, y, z)
Definition: MD5.cpp:58
Machine Check Debug Module
Implements a dense probed hash-table based set.
Definition: DenseSet.h:271
Metadata node.
Definition: Metadata.h:1069
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
Class to represent struct types.
Definition: DerivedTypes.h:216
TypeFinder - Walk over a module, identifying all of the types that are used by the module.
Definition: TypeFinder.h:31
iterator end()
Definition: TypeFinder.h:52
DenseSet< const MDNode * > & getVisitedMetadata()
Definition: TypeFinder.h:63
void run(const Module &M, bool onlyNamed)
Definition: TypeFinder.cpp:34
iterator erase(iterator I, iterator E)
Definition: TypeFinder.h:59
const_iterator end() const
Definition: TypeFinder.h:55
iterator begin()
Definition: TypeFinder.h:51
size_t size() const
Definition: TypeFinder.h:58
const_iterator begin() const
Definition: TypeFinder.h:54
std::vector< StructType * >::const_iterator const_iterator
Definition: TypeFinder.h:49
StructType *& operator[](unsigned Idx)
Definition: TypeFinder.h:61
TypeFinder()=default
bool empty() const
Definition: TypeFinder.h:57
std::vector< StructType * >::iterator iterator
Definition: TypeFinder.h:48
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
LLVM Value Representation.
Definition: Value.h:74
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18