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