ValueEnumerator.h revision 4ee451de366474b9c228b4e5fa573795a715216d
1//===-- Bitcode/Writer/ValueEnumerator.h - Number values --------*- 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 class gives values and types Unique ID's.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef VALUE_ENUMERATOR_H
15#define VALUE_ENUMERATOR_H
16
17#include "llvm/ADT/DenseMap.h"
18#include <vector>
19
20namespace llvm {
21
22class Type;
23class Value;
24class BasicBlock;
25class Function;
26class Module;
27class ParamAttrsList;
28class TypeSymbolTable;
29class ValueSymbolTable;
30
31class ValueEnumerator {
32public:
33  // For each type, we remember its Type* and occurrence frequency.
34  typedef std::vector<std::pair<const Type*, unsigned> > TypeList;
35
36  // For each value, we remember its Value* and occurrence frequency.
37  typedef std::vector<std::pair<const Value*, unsigned> > ValueList;
38private:
39  typedef DenseMap<const Type*, unsigned> TypeMapType;
40  TypeMapType TypeMap;
41  TypeList Types;
42
43  typedef DenseMap<const Value*, unsigned> ValueMapType;
44  ValueMapType ValueMap;
45  ValueList Values;
46
47  typedef DenseMap<const ParamAttrsList*, unsigned> ParamAttrMapType;
48  ParamAttrMapType ParamAttrMap;
49  std::vector<const ParamAttrsList*> ParamAttrs;
50
51  /// BasicBlocks - This contains all the basic blocks for the currently
52  /// incorporated function.  Their reverse mapping is stored in ValueMap.
53  std::vector<const BasicBlock*> BasicBlocks;
54
55  /// When a function is incorporated, this is the size of the Values list
56  /// before incorporation.
57  unsigned NumModuleValues;
58  unsigned FirstFuncConstantID;
59  unsigned FirstInstID;
60
61  ValueEnumerator(const ValueEnumerator &);  // DO NOT IMPLEMENT
62  void operator=(const ValueEnumerator &);   // DO NOT IMPLEMENT
63public:
64  ValueEnumerator(const Module *M);
65
66  unsigned getValueID(const Value *V) const {
67    ValueMapType::const_iterator I = ValueMap.find(V);
68    assert(I != ValueMap.end() && "Value not in slotcalculator!");
69    return I->second-1;
70  }
71
72  unsigned getTypeID(const Type *T) const {
73    TypeMapType::const_iterator I = TypeMap.find(T);
74    assert(I != TypeMap.end() && "Type not in ValueEnumerator!");
75    return I->second-1;
76  }
77
78  unsigned getParamAttrID(const ParamAttrsList *PAL) const {
79    if (PAL == 0) return 0;  // Null maps to zero.
80    ParamAttrMapType::const_iterator I = ParamAttrMap.find(PAL);
81    assert(I != ParamAttrMap.end() && "ParamAttr not in ValueEnumerator!");
82    return I->second;
83  }
84
85  /// getFunctionConstantRange - Return the range of values that corresponds to
86  /// function-local constants.
87  void getFunctionConstantRange(unsigned &Start, unsigned &End) const {
88    Start = FirstFuncConstantID;
89    End = FirstInstID;
90  }
91
92  const ValueList &getValues() const { return Values; }
93  const TypeList &getTypes() const { return Types; }
94  const std::vector<const BasicBlock*> &getBasicBlocks() const {
95    return BasicBlocks;
96  }
97  const std::vector<const ParamAttrsList*> &getParamAttrs() const {
98    return ParamAttrs;
99  }
100
101  /// PurgeAggregateValues - If there are any aggregate values at the end of the
102  /// value list, remove them and return the count of the remaining values.  If
103  /// there are none, return -1.
104  int PurgeAggregateValues();
105
106  /// incorporateFunction/purgeFunction - If you'd like to deal with a function,
107  /// use these two methods to get its data into the ValueEnumerator!
108  ///
109  void incorporateFunction(const Function &F);
110  void purgeFunction();
111
112private:
113  void OptimizeConstants(unsigned CstStart, unsigned CstEnd);
114
115  void EnumerateValue(const Value *V);
116  void EnumerateType(const Type *T);
117  void EnumerateOperandType(const Value *V);
118  void EnumerateParamAttrs(const ParamAttrsList *PAL);
119
120  void EnumerateTypeSymbolTable(const TypeSymbolTable &ST);
121  void EnumerateValueSymbolTable(const ValueSymbolTable &ST);
122};
123
124} // End llvm namespace
125
126#endif
127