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