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