Write and read metadata attachments.
[oota-llvm.git] / lib / Bitcode / Writer / ValueEnumerator.h
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/Attributes.h"
19 #include <vector>
20
21 namespace llvm {
22
23 class Type;
24 class Value;
25 class Instruction;
26 class BasicBlock;
27 class Function;
28 class Module;
29 class MetadataBase;
30 class AttrListPtr;
31 class TypeSymbolTable;
32 class ValueSymbolTable;
33
34 class ValueEnumerator {
35 public:
36   // For each type, we remember its Type* and occurrence frequency.
37   typedef std::vector<std::pair<const Type*, unsigned> > TypeList;
38
39   // For each value, we remember its Value* and occurrence frequency.
40   typedef std::vector<std::pair<const Value*, unsigned> > ValueList;
41 private:
42   typedef DenseMap<const Type*, unsigned> TypeMapType;
43   TypeMapType TypeMap;
44   TypeList Types;
45
46   typedef DenseMap<const Value*, unsigned> ValueMapType;
47   ValueMapType ValueMap;
48   ValueList Values;
49   ValueList MDValues;
50   ValueMapType MDValueMap;
51   
52   typedef DenseMap<void*, unsigned> AttributeMapType;
53   AttributeMapType AttributeMap;
54   std::vector<AttrListPtr> Attributes;
55   
56   typedef DenseMap<const Instruction*, unsigned> InstructionMapType;
57   InstructionMapType InstructionMap;
58   unsigned InstructionCount;
59
60   /// BasicBlocks - This contains all the basic blocks for the currently
61   /// incorporated function.  Their reverse mapping is stored in ValueMap.
62   std::vector<const BasicBlock*> BasicBlocks;
63   
64   /// When a function is incorporated, this is the size of the Values list
65   /// before incorporation.
66   unsigned NumModuleValues;
67   unsigned FirstFuncConstantID;
68   unsigned FirstInstID;
69   
70   ValueEnumerator(const ValueEnumerator &);  // DO NOT IMPLEMENT
71   void operator=(const ValueEnumerator &);   // DO NOT IMPLEMENT
72 public:
73   ValueEnumerator(const Module *M);
74
75   unsigned getValueID(const Value *V) const;
76
77   unsigned getTypeID(const Type *T) const {
78     TypeMapType::const_iterator I = TypeMap.find(T);
79     assert(I != TypeMap.end() && "Type not in ValueEnumerator!");
80     return I->second-1;
81   }
82
83   unsigned getInstructionID(const Instruction *I) const;
84   void setInstructionID(const Instruction *I);
85
86   unsigned getAttributeID(const AttrListPtr &PAL) const {
87     if (PAL.isEmpty()) return 0;  // Null maps to zero.
88     AttributeMapType::const_iterator I = AttributeMap.find(PAL.getRawPointer());
89     assert(I != AttributeMap.end() && "Attribute not in ValueEnumerator!");
90     return I->second;
91   }
92
93   /// getFunctionConstantRange - Return the range of values that corresponds to
94   /// function-local constants.
95   void getFunctionConstantRange(unsigned &Start, unsigned &End) const {
96     Start = FirstFuncConstantID;
97     End = FirstInstID;
98   }
99   
100   const ValueList &getValues() const { return Values; }
101   const ValueList &getMDValues() const { return MDValues; }
102   const TypeList &getTypes() const { return Types; }
103   const std::vector<const BasicBlock*> &getBasicBlocks() const {
104     return BasicBlocks; 
105   }
106   const std::vector<AttrListPtr> &getAttributes() const {
107     return Attributes;
108   }
109
110   /// incorporateFunction/purgeFunction - If you'd like to deal with a function,
111   /// use these two methods to get its data into the ValueEnumerator!
112   ///
113   void incorporateFunction(const Function &F);
114   void purgeFunction();
115
116 private:
117   void OptimizeConstants(unsigned CstStart, unsigned CstEnd);
118     
119   void EnumerateMetadata(const MetadataBase *MD);
120   void EnumerateValue(const Value *V);
121   void EnumerateType(const Type *T);
122   void EnumerateOperandType(const Value *V);
123   void EnumerateAttributes(const AttrListPtr &PAL);
124   
125   void EnumerateTypeSymbolTable(const TypeSymbolTable &ST);
126   void EnumerateValueSymbolTable(const ValueSymbolTable &ST);
127 };
128
129 } // End llvm namespace
130
131 #endif