• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
22 namespace llvm {
23 
24 class Type;
25 class Value;
26 class Instruction;
27 class BasicBlock;
28 class Function;
29 class Module;
30 class Metadata;
31 class LocalAsMetadata;
32 class MDNode;
33 class NamedMDNode;
34 class AttributeSet;
35 class ValueSymbolTable;
36 class MDSymbolTable;
37 class raw_ostream;
38 
39 }  // end llvm namespace
40 
41 namespace llvm_2_9_func {
42 
43 class ValueEnumerator {
44 public:
45   typedef std::vector<llvm::Type*> TypeList;
46 
47   // For each value, we remember its Value* and occurrence frequency.
48   typedef std::vector<std::pair<const llvm::Value*, unsigned> > ValueList;
49 private:
50   typedef llvm::DenseMap<llvm::Type*, unsigned> TypeMapType;
51   TypeMapType TypeMap;
52   TypeList Types;
53 
54   typedef llvm::DenseMap<const llvm::Value*, unsigned> ValueMapType;
55   ValueMapType ValueMap;
56   ValueList Values;
57 
58 
59   std::vector<const llvm::Metadata *> MDs;
60   llvm::SmallVector<const llvm::LocalAsMetadata *, 8> FunctionLocalMDs;
61   typedef llvm::DenseMap<const llvm::Metadata *, unsigned> MetadataMapType;
62   MetadataMapType MDValueMap;
63   bool HasMDString;
64   bool HasDILocation;
65 
66   typedef llvm::DenseMap<llvm::AttributeSet, unsigned> AttributeGroupMapType;
67   AttributeGroupMapType AttributeGroupMap;
68   std::vector<llvm::AttributeSet> AttributeGroups;
69 
70   typedef llvm::DenseMap<llvm::AttributeSet, unsigned> AttributeMapType;
71   AttributeMapType AttributeMap;
72   std::vector<llvm::AttributeSet> Attribute;
73 
74   /// GlobalBasicBlockIDs - This map memoizes the basic block ID's referenced by
75   /// the "getGlobalBasicBlockID" method.
76   mutable llvm::DenseMap<const llvm::BasicBlock*, unsigned> GlobalBasicBlockIDs;
77 
78   typedef llvm::DenseMap<const llvm::Instruction*, unsigned> InstructionMapType;
79   InstructionMapType InstructionMap;
80   unsigned InstructionCount;
81 
82   /// BasicBlocks - This contains all the basic blocks for the currently
83   /// incorporated function.  Their reverse mapping is stored in ValueMap.
84   std::vector<const llvm::BasicBlock*> BasicBlocks;
85 
86   /// When a function is incorporated, this is the size of the Values list
87   /// before incorporation.
88   unsigned NumModuleValues;
89 
90   /// When a function is incorporated, this is the size of the MDValues list
91   /// before incorporation.
92   unsigned NumModuleMDs;
93 
94   unsigned FirstFuncConstantID;
95   unsigned FirstInstID;
96 
97   ValueEnumerator(const ValueEnumerator &) = delete;
98   void operator=(const ValueEnumerator &) = delete;
99 public:
100   explicit ValueEnumerator(const llvm::Module &M);
101 
102   void dump() const;
103   void print(llvm::raw_ostream &OS, const ValueMapType &Map, const char *Name) const;
104   void print(llvm::raw_ostream &OS, const MetadataMapType &Map,
105              const char *Name) const;
106 
107   unsigned getValueID(const llvm::Value *V) const;
getMetadataID(const llvm::Metadata * MD)108   unsigned getMetadataID(const llvm::Metadata *MD) const {
109     auto ID = getMetadataOrNullID(MD);
110     assert(ID != 0 && "Metadata not in slotcalculator!");
111     return ID - 1;
112   }
getMetadataOrNullID(const llvm::Metadata * MD)113   unsigned getMetadataOrNullID(const llvm::Metadata *MD) const {
114     return MDValueMap.lookup(MD);
115   }
116 
hasMDString()117   bool hasMDString() const { return HasMDString; }
hasDILocation()118   bool hasDILocation() const { return HasDILocation; }
119 
getTypeID(llvm::Type * T)120   unsigned getTypeID(llvm::Type *T) const {
121     TypeMapType::const_iterator I = TypeMap.find(T);
122     assert(I != TypeMap.end() && "Type not in ValueEnumerator!");
123     return I->second-1;
124   }
125 
126   unsigned getInstructionID(const llvm::Instruction *I) const;
127   void setInstructionID(const llvm::Instruction *I);
128 
getAttributeID(llvm::AttributeSet PAL)129   unsigned getAttributeID(llvm::AttributeSet PAL) const {
130     if (PAL.isEmpty()) return 0;  // Null maps to zero.
131     AttributeMapType::const_iterator I = AttributeMap.find(PAL);
132     assert(I != AttributeMap.end() && "Attribute not in ValueEnumerator!");
133     return I->second;
134   }
135 
getAttributeGroupID(llvm::AttributeSet PAL)136   unsigned getAttributeGroupID(llvm::AttributeSet PAL) const {
137     if (PAL.isEmpty()) return 0;  // Null maps to zero.
138     AttributeGroupMapType::const_iterator I = AttributeGroupMap.find(PAL);
139     assert(I != AttributeGroupMap.end() && "Attribute not in ValueEnumerator!");
140     return I->second;
141   }
142 
143   /// getFunctionConstantRange - Return the range of values that corresponds to
144   /// function-local constants.
getFunctionConstantRange(unsigned & Start,unsigned & End)145   void getFunctionConstantRange(unsigned &Start, unsigned &End) const {
146     Start = FirstFuncConstantID;
147     End = FirstInstID;
148   }
149 
getValues()150   const ValueList &getValues() const { return Values; }
getMDs()151   const std::vector<const llvm::Metadata *> &getMDs() const { return MDs; }
getFunctionLocalMDs()152   const llvm::SmallVectorImpl<const llvm::LocalAsMetadata *> &getFunctionLocalMDs() const {
153     return FunctionLocalMDs;
154   }
getTypes()155   const TypeList &getTypes() const { return Types; }
getBasicBlocks()156   const std::vector<const llvm::BasicBlock*> &getBasicBlocks() const {
157     return BasicBlocks;
158   }
getAttributes()159   const std::vector<llvm::AttributeSet> &getAttributes() const {
160     return Attribute;
161   }
getAttributeGroups()162   const std::vector<llvm::AttributeSet> &getAttributeGroups() const {
163     return AttributeGroups;
164   }
165 
166   /// getGlobalBasicBlockID - This returns the function-specific ID for the
167   /// specified basic block.  This is relatively expensive information, so it
168   /// should only be used by rare constructs such as address-of-label.
169   unsigned getGlobalBasicBlockID(const llvm::BasicBlock *BB) const;
170 
171   /// incorporateFunction/purgeFunction - If you'd like to deal with a function,
172   /// use these two methods to get its data into the ValueEnumerator!
173   ///
174   void incorporateFunction(const llvm::Function &F);
175   void purgeFunction();
176 
177 private:
178   void OptimizeConstants(unsigned CstStart, unsigned CstEnd);
179 
180   void EnumerateMDNodeOperands(const llvm::MDNode *N);
181   void EnumerateMetadata(const llvm::Metadata *MD);
182   void EnumerateFunctionLocalMetadata(const llvm::LocalAsMetadata *Local);
183   void EnumerateNamedMDNode(const llvm::NamedMDNode *NMD);
184   void EnumerateValue(const llvm::Value *V);
185   void EnumerateType(llvm::Type *T);
186   void EnumerateOperandType(const llvm::Value *V);
187   void EnumerateAttributes(llvm::AttributeSet PAL);
188 
189   void EnumerateValueSymbolTable(const llvm::ValueSymbolTable &ST);
190   void EnumerateNamedMetadata(const llvm::Module &M);
191 };
192 
193 }  // End llvm_2_9_func namespace
194 
195 #endif
196