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