TypeFinder.h 2.4 KB

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