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 "llvm/IR/Metadata.h" 19 #include "llvm/IR/Type.h" 20 #include <vector> 21 22 namespace llvm { 23 24 class MDNode; 25 class Module; 26 class StructType; 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; 40 41 public: TypeFinder()42 TypeFinder() : OnlyNamed(false) {} 43 44 void run(const Module &M, bool onlyNamed); 45 void clear(); 46 47 typedef std::vector<StructType*>::iterator iterator; 48 typedef std::vector<StructType*>::const_iterator const_iterator; 49 begin()50 iterator begin() { return StructTypes.begin(); } end()51 iterator end() { return StructTypes.end(); } 52 begin()53 const_iterator begin() const { return StructTypes.begin(); } end()54 const_iterator end() const { return StructTypes.end(); } 55 empty()56 bool empty() const { return StructTypes.empty(); } size()57 size_t size() const { return StructTypes.size(); } erase(iterator I,iterator E)58 iterator erase(iterator I, iterator E) { return StructTypes.erase(I, E); } 59 60 StructType *&operator[](unsigned Idx) { return StructTypes[Idx]; } 61 62 private: 63 /// incorporateType - This method adds the type to the list of used 64 /// structures if it's not in there already. 65 void incorporateType(Type *Ty); 66 67 /// incorporateValue - This method is used to walk operand lists finding types 68 /// hiding in constant expressions and other operands that won't be walked in 69 /// other ways. GlobalValues, basic blocks, instructions, and inst operands 70 /// are all explicitly enumerated. 71 void incorporateValue(const Value *V); 72 73 /// incorporateMDNode - This method is used to walk the operands of an MDNode 74 /// to find types hiding within. 75 void incorporateMDNode(const MDNode *V); 76 }; 77 78 } // end llvm namespace 79 80 #endif 81