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