ValueEnumerator.h revision 6a0c04dff288b7eaa0459cc7419159765bb5a0b9
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 "llvm/ADT/SmallVector.h"
19#include "llvm/Attributes.h"
20#include <vector>
21
22namespace llvm {
23
24class Type;
25class Value;
26class Instruction;
27class BasicBlock;
28class Function;
29class Module;
30class MDNode;
31class NamedMDNode;
32class AttrListPtr;
33class TypeSymbolTable;
34class ValueSymbolTable;
35class MDSymbolTable;
36
37class ValueEnumerator {
38public:
39  typedef std::vector<const Type*> TypeList;
40
41  // For each value, we remember its Value* and occurrence frequency.
42  typedef std::vector<std::pair<const Value*, unsigned> > ValueList;
43private:
44  typedef DenseMap<const Type*, unsigned> TypeMapType;
45  TypeMapType TypeMap;
46  TypeList Types;
47
48  typedef DenseMap<const Value*, unsigned> ValueMapType;
49  ValueMapType ValueMap;
50  ValueList Values;
51  ValueList MDValues;
52  SmallVector<const MDNode *, 8> FunctionLocalMDs;
53  ValueMapType MDValueMap;
54
55  typedef DenseMap<void*, unsigned> AttributeMapType;
56  AttributeMapType AttributeMap;
57  std::vector<AttrListPtr> Attributes;
58
59  /// GlobalBasicBlockIDs - This map memoizes the basic block ID's referenced by
60  /// the "getGlobalBasicBlockID" method.
61  mutable DenseMap<const BasicBlock*, unsigned> GlobalBasicBlockIDs;
62
63  typedef DenseMap<const Instruction*, unsigned> InstructionMapType;
64  InstructionMapType InstructionMap;
65  unsigned InstructionCount;
66
67  /// BasicBlocks - This contains all the basic blocks for the currently
68  /// incorporated function.  Their reverse mapping is stored in ValueMap.
69  std::vector<const BasicBlock*> BasicBlocks;
70
71  /// When a function is incorporated, this is the size of the Values list
72  /// before incorporation.
73  unsigned NumModuleValues;
74
75  /// When a function is incorporated, this is the size of the MDValues list
76  /// before incorporation.
77  unsigned NumModuleMDValues;
78
79  unsigned FirstFuncConstantID;
80  unsigned FirstInstID;
81
82  ValueEnumerator(const ValueEnumerator &);  // DO NOT IMPLEMENT
83  void operator=(const ValueEnumerator &);   // DO NOT IMPLEMENT
84public:
85  ValueEnumerator(const Module *M);
86
87  unsigned getValueID(const Value *V) const;
88
89  unsigned getTypeID(const Type *T) const {
90    TypeMapType::const_iterator I = TypeMap.find(T);
91    assert(I != TypeMap.end() && "Type not in ValueEnumerator!");
92    return I->second-1;
93  }
94
95  unsigned getInstructionID(const Instruction *I) const;
96  void setInstructionID(const Instruction *I);
97
98  unsigned getAttributeID(const AttrListPtr &PAL) const {
99    if (PAL.isEmpty()) return 0;  // Null maps to zero.
100    AttributeMapType::const_iterator I = AttributeMap.find(PAL.getRawPointer());
101    assert(I != AttributeMap.end() && "Attribute not in ValueEnumerator!");
102    return I->second;
103  }
104
105  /// getFunctionConstantRange - Return the range of values that corresponds to
106  /// function-local constants.
107  void getFunctionConstantRange(unsigned &Start, unsigned &End) const {
108    Start = FirstFuncConstantID;
109    End = FirstInstID;
110  }
111
112  const ValueList &getValues() const { return Values; }
113  const ValueList &getMDValues() const { return MDValues; }
114  const SmallVector<const MDNode *, 8> &getFunctionLocalMDValues() const {
115    return FunctionLocalMDs;
116  }
117  const TypeList &getTypes() const { return Types; }
118  const std::vector<const BasicBlock*> &getBasicBlocks() const {
119    return BasicBlocks;
120  }
121  const std::vector<AttrListPtr> &getAttributes() const {
122    return Attributes;
123  }
124
125  /// getGlobalBasicBlockID - This returns the function-specific ID for the
126  /// specified basic block.  This is relatively expensive information, so it
127  /// should only be used by rare constructs such as address-of-label.
128  unsigned getGlobalBasicBlockID(const BasicBlock *BB) const;
129
130  /// incorporateFunction/purgeFunction - If you'd like to deal with a function,
131  /// use these two methods to get its data into the ValueEnumerator!
132  ///
133  void incorporateFunction(const Function &F);
134  void purgeFunction();
135
136private:
137  void OptimizeConstants(unsigned CstStart, unsigned CstEnd);
138  void OptimizeTypes();
139
140  void EnumerateMDNodeOperands(const MDNode *N);
141  void EnumerateMetadata(const Value *MD);
142  void EnumerateFunctionLocalMetadata(const MDNode *N);
143  void EnumerateNamedMDNode(const NamedMDNode *NMD);
144  void EnumerateValue(const Value *V);
145  void EnumerateType(const Type *T);
146  void EnumerateOperandType(const Value *V);
147  void EnumerateAttributes(const AttrListPtr &PAL);
148
149  void EnumerateTypeSymbolTable(const TypeSymbolTable &ST);
150  void EnumerateValueSymbolTable(const ValueSymbolTable &ST);
151  void EnumerateNamedMetadata(const Module *M);
152};
153
154} // End llvm namespace
155
156#endif
157