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