Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:10

0001 //===- llvm/IR/TypeFinder.h - Class to find used struct types ---*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file contains the declaration of the TypeFinder class.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_IR_TYPEFINDER_H
0014 #define LLVM_IR_TYPEFINDER_H
0015 
0016 #include "llvm/ADT/DenseSet.h"
0017 #include "llvm/IR/Attributes.h"
0018 #include <cstddef>
0019 #include <vector>
0020 
0021 namespace llvm {
0022 
0023 class MDNode;
0024 class Module;
0025 class StructType;
0026 class Type;
0027 class Value;
0028 
0029 /// TypeFinder - Walk over a module, identifying all of the types that are
0030 /// used by the module.
0031 class TypeFinder {
0032   // To avoid walking constant expressions multiple times and other IR
0033   // objects, we keep several helper maps.
0034   DenseSet<const Value*> VisitedConstants;
0035   DenseSet<const MDNode *> VisitedMetadata;
0036   DenseSet<AttributeList> VisitedAttributes;
0037   DenseSet<Type*> VisitedTypes;
0038 
0039   std::vector<StructType*> StructTypes;
0040   bool OnlyNamed = false;
0041 
0042 public:
0043   TypeFinder() = default;
0044 
0045   void run(const Module &M, bool onlyNamed);
0046   void clear();
0047 
0048   using iterator = std::vector<StructType*>::iterator;
0049   using const_iterator = std::vector<StructType*>::const_iterator;
0050 
0051   iterator begin() { return StructTypes.begin(); }
0052   iterator end() { return StructTypes.end(); }
0053 
0054   const_iterator begin() const { return StructTypes.begin(); }
0055   const_iterator end() const { return StructTypes.end(); }
0056 
0057   bool empty() const { return StructTypes.empty(); }
0058   size_t size() const { return StructTypes.size(); }
0059   iterator erase(iterator I, iterator E) { return StructTypes.erase(I, E); }
0060 
0061   StructType *&operator[](unsigned Idx) { return StructTypes[Idx]; }
0062 
0063   DenseSet<const MDNode *> &getVisitedMetadata() { return VisitedMetadata; }
0064 
0065 private:
0066   /// incorporateType - This method adds the type to the list of used
0067   /// structures if it's not in there already.
0068   void incorporateType(Type *Ty);
0069 
0070   /// incorporateValue - This method is used to walk operand lists finding types
0071   /// hiding in constant expressions and other operands that won't be walked in
0072   /// other ways.  GlobalValues, basic blocks, instructions, and inst operands
0073   /// are all explicitly enumerated.
0074   void incorporateValue(const Value *V);
0075 
0076   /// incorporateMDNode - This method is used to walk the operands of an MDNode
0077   /// to find types hiding within.
0078   void incorporateMDNode(const MDNode *V);
0079 
0080   /// Incorporate types referenced by attributes.
0081   void incorporateAttributes(AttributeList AL);
0082 };
0083 
0084 } // end namespace llvm
0085 
0086 #endif // LLVM_IR_TYPEFINDER_H