blob: 9e21775cfd4625ee7a55e0a0271823564f56c318 [file] [log] [blame]
Stephen Hines4cc499d2011-08-24 19:06:17 -07001//===-- 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;
Stephen Hines01a90ee2012-07-13 00:37:48 -070035class raw_ostream;
Stephen Hines4cc499d2011-08-24 19:06:17 -070036
Stephen Hines01a90ee2012-07-13 00:37:48 -070037} // end llvm namespace
Stephen Hines331994b2012-07-12 20:13:42 -070038
39namespace llvm_2_9 {
40
Stephen Hines4cc499d2011-08-24 19:06:17 -070041class ValueEnumerator {
42public:
Stephen Hines331994b2012-07-12 20:13:42 -070043 typedef std::vector<llvm::Type*> TypeList;
Stephen Hines4cc499d2011-08-24 19:06:17 -070044
45 // For each value, we remember its Value* and occurrence frequency.
Stephen Hines331994b2012-07-12 20:13:42 -070046 typedef std::vector<std::pair<const llvm::Value*, unsigned> > ValueList;
Stephen Hines4cc499d2011-08-24 19:06:17 -070047private:
Stephen Hines331994b2012-07-12 20:13:42 -070048 typedef llvm::DenseMap<llvm::Type*, unsigned> TypeMapType;
Stephen Hines4cc499d2011-08-24 19:06:17 -070049 TypeMapType TypeMap;
50 TypeList Types;
51
Stephen Hines331994b2012-07-12 20:13:42 -070052 typedef llvm::DenseMap<const llvm::Value*, unsigned> ValueMapType;
Stephen Hines4cc499d2011-08-24 19:06:17 -070053 ValueMapType ValueMap;
54 ValueList Values;
55 ValueList MDValues;
Stephen Hines331994b2012-07-12 20:13:42 -070056 llvm::SmallVector<const llvm::MDNode *, 8> FunctionLocalMDs;
Stephen Hines4cc499d2011-08-24 19:06:17 -070057 ValueMapType MDValueMap;
58
Stephen Hines331994b2012-07-12 20:13:42 -070059 typedef llvm::DenseMap<void*, unsigned> AttributeMapType;
Stephen Hines4cc499d2011-08-24 19:06:17 -070060 AttributeMapType AttributeMap;
Stephen Hines331994b2012-07-12 20:13:42 -070061 std::vector<llvm::AttrListPtr> Attributes;
Stephen Hines4cc499d2011-08-24 19:06:17 -070062
63 /// GlobalBasicBlockIDs - This map memoizes the basic block ID's referenced by
64 /// the "getGlobalBasicBlockID" method.
Stephen Hines331994b2012-07-12 20:13:42 -070065 mutable llvm::DenseMap<const llvm::BasicBlock*, unsigned> GlobalBasicBlockIDs;
Stephen Hines4cc499d2011-08-24 19:06:17 -070066
Stephen Hines331994b2012-07-12 20:13:42 -070067 typedef llvm::DenseMap<const llvm::Instruction*, unsigned> InstructionMapType;
Stephen Hines4cc499d2011-08-24 19:06:17 -070068 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.
Stephen Hines331994b2012-07-12 20:13:42 -070073 std::vector<const llvm::BasicBlock*> BasicBlocks;
Stephen Hines4cc499d2011-08-24 19:06:17 -070074
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:
Stephen Hines331994b2012-07-12 20:13:42 -070089 ValueEnumerator(const llvm::Module *M);
Stephen Hines4cc499d2011-08-24 19:06:17 -070090
Stephen Hines01a90ee2012-07-13 00:37:48 -070091 void dump() const;
92 void print(llvm::raw_ostream &OS, const ValueMapType &Map, const char *Name) const;
93
Stephen Hines331994b2012-07-12 20:13:42 -070094 unsigned getValueID(const llvm::Value *V) const;
Stephen Hines4cc499d2011-08-24 19:06:17 -070095
Stephen Hines331994b2012-07-12 20:13:42 -070096 unsigned getTypeID(llvm::Type *T) const {
Stephen Hines4cc499d2011-08-24 19:06:17 -070097 TypeMapType::const_iterator I = TypeMap.find(T);
98 assert(I != TypeMap.end() && "Type not in ValueEnumerator!");
99 return I->second-1;
100 }
101
Stephen Hines331994b2012-07-12 20:13:42 -0700102 unsigned getInstructionID(const llvm::Instruction *I) const;
103 void setInstructionID(const llvm::Instruction *I);
Stephen Hines4cc499d2011-08-24 19:06:17 -0700104
Stephen Hines331994b2012-07-12 20:13:42 -0700105 unsigned getAttributeID(const llvm::AttrListPtr &PAL) const {
Stephen Hines4cc499d2011-08-24 19:06:17 -0700106 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; }
Stephen Hines331994b2012-07-12 20:13:42 -0700121 const llvm::SmallVector<const llvm::MDNode *, 8> &getFunctionLocalMDValues() const {
Stephen Hines4cc499d2011-08-24 19:06:17 -0700122 return FunctionLocalMDs;
123 }
124 const TypeList &getTypes() const { return Types; }
Stephen Hines331994b2012-07-12 20:13:42 -0700125 const std::vector<const llvm::BasicBlock*> &getBasicBlocks() const {
Stephen Hines4cc499d2011-08-24 19:06:17 -0700126 return BasicBlocks;
127 }
Stephen Hines331994b2012-07-12 20:13:42 -0700128 const std::vector<llvm::AttrListPtr> &getAttributes() const {
Stephen Hines4cc499d2011-08-24 19:06:17 -0700129 return Attributes;
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.
Stephen Hines331994b2012-07-12 20:13:42 -0700135 unsigned getGlobalBasicBlockID(const llvm::BasicBlock *BB) const;
Stephen Hines4cc499d2011-08-24 19:06:17 -0700136
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 ///
Stephen Hines331994b2012-07-12 20:13:42 -0700140 void incorporateFunction(const llvm::Function &F);
Stephen Hines4cc499d2011-08-24 19:06:17 -0700141 void purgeFunction();
142
143private:
144 void OptimizeConstants(unsigned CstStart, unsigned CstEnd);
145
Stephen Hines331994b2012-07-12 20:13:42 -0700146 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::AttrListPtr &PAL);
Stephen Hines4cc499d2011-08-24 19:06:17 -0700154
Stephen Hines331994b2012-07-12 20:13:42 -0700155 void EnumerateValueSymbolTable(const llvm::ValueSymbolTable &ST);
156 void EnumerateNamedMetadata(const llvm::Module *M);
Stephen Hines4cc499d2011-08-24 19:06:17 -0700157};
158
Stephen Hines331994b2012-07-12 20:13:42 -0700159} // end llvm_2_9 namespace
Stephen Hines4cc499d2011-08-24 19:06:17 -0700160
161#endif