bitcode writer support for blockaddress.
[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   /// GlobalBasicBlockIDs - This map memoizes the basic block ID's referenced by
57   /// the "getGlobalBasicBlockID" method.
58   mutable DenseMap<const BasicBlock*, unsigned> GlobalBasicBlockIDs;
59   
60   typedef DenseMap<const Instruction*, unsigned> InstructionMapType;
61   InstructionMapType InstructionMap;
62   unsigned InstructionCount;
63
64   /// BasicBlocks - This contains all the basic blocks for the currently
65   /// incorporated function.  Their reverse mapping is stored in ValueMap.
66   std::vector<const BasicBlock*> BasicBlocks;
67   
68   /// When a function is incorporated, this is the size of the Values list
69   /// before incorporation.
70   unsigned NumModuleValues;
71   unsigned FirstFuncConstantID;
72   unsigned FirstInstID;
73   
74   ValueEnumerator(const ValueEnumerator &);  // DO NOT IMPLEMENT
75   void operator=(const ValueEnumerator &);   // DO NOT IMPLEMENT
76 public:
77   ValueEnumerator(const Module *M);
78
79   unsigned getValueID(const Value *V) const;
80
81   unsigned getTypeID(const Type *T) const {
82     TypeMapType::const_iterator I = TypeMap.find(T);
83     assert(I != TypeMap.end() && "Type not in ValueEnumerator!");
84     return I->second-1;
85   }
86
87   unsigned getInstructionID(const Instruction *I) const;
88   void setInstructionID(const Instruction *I);
89
90   unsigned getAttributeID(const AttrListPtr &PAL) const {
91     if (PAL.isEmpty()) return 0;  // Null maps to zero.
92     AttributeMapType::const_iterator I = AttributeMap.find(PAL.getRawPointer());
93     assert(I != AttributeMap.end() && "Attribute not in ValueEnumerator!");
94     return I->second;
95   }
96
97   /// getFunctionConstantRange - Return the range of values that corresponds to
98   /// function-local constants.
99   void getFunctionConstantRange(unsigned &Start, unsigned &End) const {
100     Start = FirstFuncConstantID;
101     End = FirstInstID;
102   }
103   
104   const ValueList &getValues() const { return Values; }
105   const ValueList &getMDValues() const { return MDValues; }
106   const TypeList &getTypes() const { return Types; }
107   const std::vector<const BasicBlock*> &getBasicBlocks() const {
108     return BasicBlocks; 
109   }
110   const std::vector<AttrListPtr> &getAttributes() const {
111     return Attributes;
112   }
113   
114   /// getGlobalBasicBlockID - This returns the function-specific ID for the
115   /// specified basic block.  This is relatively expensive information, so it
116   /// should only be used by rare constructs such as address-of-label.
117   unsigned getGlobalBasicBlockID(const BasicBlock *BB) const;
118
119   /// incorporateFunction/purgeFunction - If you'd like to deal with a function,
120   /// use these two methods to get its data into the ValueEnumerator!
121   ///
122   void incorporateFunction(const Function &F);
123   void purgeFunction();
124
125 private:
126   void OptimizeConstants(unsigned CstStart, unsigned CstEnd);
127     
128   void EnumerateMetadata(const MetadataBase *MD);
129   void EnumerateValue(const Value *V);
130   void EnumerateType(const Type *T);
131   void EnumerateOperandType(const Value *V);
132   void EnumerateAttributes(const AttrListPtr &PAL);
133   
134   void EnumerateTypeSymbolTable(const TypeSymbolTable &ST);
135   void EnumerateValueSymbolTable(const ValueSymbolTable &ST);
136 };
137
138 } // End llvm namespace
139
140 #endif