1249259Sdim//===-- llvm/IR/TypeFinder.h - Class to find used struct types --*- C++ -*-===//
2249259Sdim//
3249259Sdim//                     The LLVM Compiler Infrastructure
4249259Sdim//
5249259Sdim// This file is distributed under the University of Illinois Open Source
6249259Sdim// License. See LICENSE.TXT for details.
7249259Sdim//
8249259Sdim//===----------------------------------------------------------------------===//
9249259Sdim//
10249259Sdim// This file contains the declaration of the TypeFinder class.
11249259Sdim//
12249259Sdim//===----------------------------------------------------------------------===//
13249259Sdim
14249259Sdim#ifndef LLVM_IR_TYPEFINDER_H
15249259Sdim#define LLVM_IR_TYPEFINDER_H
16249259Sdim
17249259Sdim#include "llvm/ADT/DenseSet.h"
18249259Sdim#include <vector>
19249259Sdim
20249259Sdimnamespace llvm {
21249259Sdim
22249259Sdimclass MDNode;
23249259Sdimclass Module;
24249259Sdimclass StructType;
25249259Sdimclass Type;
26249259Sdimclass Value;
27249259Sdim
28249259Sdim/// TypeFinder - Walk over a module, identifying all of the types that are
29249259Sdim/// used by the module.
30249259Sdimclass TypeFinder {
31249259Sdim  // To avoid walking constant expressions multiple times and other IR
32249259Sdim  // objects, we keep several helper maps.
33249259Sdim  DenseSet<const Value*> VisitedConstants;
34249259Sdim  DenseSet<Type*> VisitedTypes;
35249259Sdim
36249259Sdim  std::vector<StructType*> StructTypes;
37249259Sdim  bool OnlyNamed;
38249259Sdim
39249259Sdimpublic:
40249259Sdim  TypeFinder() : OnlyNamed(false) {}
41249259Sdim
42249259Sdim  void run(const Module &M, bool onlyNamed);
43249259Sdim  void clear();
44249259Sdim
45249259Sdim  typedef std::vector<StructType*>::iterator iterator;
46249259Sdim  typedef std::vector<StructType*>::const_iterator const_iterator;
47249259Sdim
48249259Sdim  iterator begin() { return StructTypes.begin(); }
49249259Sdim  iterator end() { return StructTypes.end(); }
50249259Sdim
51249259Sdim  const_iterator begin() const { return StructTypes.begin(); }
52249259Sdim  const_iterator end() const { return StructTypes.end(); }
53249259Sdim
54249259Sdim  bool empty() const { return StructTypes.empty(); }
55249259Sdim  size_t size() const { return StructTypes.size(); }
56249259Sdim  iterator erase(iterator I, iterator E) { return StructTypes.erase(I, E); }
57249259Sdim
58249259Sdim  StructType *&operator[](unsigned Idx) { return StructTypes[Idx]; }
59249259Sdim
60249259Sdimprivate:
61249259Sdim  /// incorporateType - This method adds the type to the list of used
62249259Sdim  /// structures if it's not in there already.
63249259Sdim  void incorporateType(Type *Ty);
64249259Sdim
65249259Sdim  /// incorporateValue - This method is used to walk operand lists finding types
66249259Sdim  /// hiding in constant expressions and other operands that won't be walked in
67249259Sdim  /// other ways.  GlobalValues, basic blocks, instructions, and inst operands
68249259Sdim  /// are all explicitly enumerated.
69249259Sdim  void incorporateValue(const Value *V);
70249259Sdim
71249259Sdim  /// incorporateMDNode - This method is used to walk the operands of an MDNode
72249259Sdim  /// to find types hiding within.
73249259Sdim  void incorporateMDNode(const MDNode *V);
74249259Sdim};
75249259Sdim
76249259Sdim} // end llvm namespace
77249259Sdim
78249259Sdim#endif
79