LLVM  3.7.0
ValueEnumerator.h
Go to the documentation of this file.
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/UseListOrder.h"
22 #include <vector>
23 
24 namespace llvm {
25 
26 class Type;
27 class Value;
28 class Instruction;
29 class BasicBlock;
30 class Comdat;
31 class Function;
32 class Module;
33 class Metadata;
34 class LocalAsMetadata;
35 class MDNode;
36 class NamedMDNode;
37 class AttributeSet;
38 class ValueSymbolTable;
39 class MDSymbolTable;
40 class raw_ostream;
41 
43 public:
44  typedef std::vector<Type*> TypeList;
45 
46  // For each value, we remember its Value* and occurrence frequency.
47  typedef std::vector<std::pair<const Value*, unsigned> > ValueList;
48 
50 
51 private:
53  TypeMapType TypeMap;
54  TypeList Types;
55 
58  ValueList Values;
59 
61  ComdatSetType Comdats;
62 
63  std::vector<const Metadata *> MDs;
66  MetadataMapType MDValueMap;
67  bool HasMDString;
68  bool HasDILocation;
69  bool HasGenericDINode;
70  bool ShouldPreserveUseListOrder;
71 
73  AttributeGroupMapType AttributeGroupMap;
74  std::vector<AttributeSet> AttributeGroups;
75 
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 
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 MDValues 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 MDValueMap.lookup(MD);
121  }
122 
123  bool hasMDString() const { return HasMDString; }
124  bool hasDILocation() const { return HasDILocation; }
125  bool hasGenericDINode() const { return HasGenericDINode; }
126 
127  bool shouldPreserveUseListOrder() const { return ShouldPreserveUseListOrder; }
128 
129  unsigned getTypeID(Type *T) const {
130  TypeMapType::const_iterator I = TypeMap.find(T);
131  assert(I != TypeMap.end() && "Type not in ValueEnumerator!");
132  return I->second-1;
133  }
134 
135  unsigned getInstructionID(const Instruction *I) const;
136  void setInstructionID(const Instruction *I);
137 
138  unsigned getAttributeID(AttributeSet PAL) const {
139  if (PAL.isEmpty()) return 0; // Null maps to zero.
140  AttributeMapType::const_iterator I = AttributeMap.find(PAL);
141  assert(I != AttributeMap.end() && "Attribute not in ValueEnumerator!");
142  return I->second;
143  }
144 
145  unsigned getAttributeGroupID(AttributeSet PAL) const {
146  if (PAL.isEmpty()) return 0; // Null maps to zero.
147  AttributeGroupMapType::const_iterator I = AttributeGroupMap.find(PAL);
148  assert(I != AttributeGroupMap.end() && "Attribute not in ValueEnumerator!");
149  return I->second;
150  }
151 
152  /// getFunctionConstantRange - Return the range of values that corresponds to
153  /// function-local constants.
154  void getFunctionConstantRange(unsigned &Start, unsigned &End) const {
155  Start = FirstFuncConstantID;
156  End = FirstInstID;
157  }
158 
159  const ValueList &getValues() const { return Values; }
160  const std::vector<const Metadata *> &getMDs() const { return MDs; }
162  return FunctionLocalMDs;
163  }
164  const TypeList &getTypes() const { return Types; }
165  const std::vector<const BasicBlock*> &getBasicBlocks() const {
166  return BasicBlocks;
167  }
168  const std::vector<AttributeSet> &getAttributes() const {
169  return Attribute;
170  }
171  const std::vector<AttributeSet> &getAttributeGroups() const {
172  return AttributeGroups;
173  }
174 
175  const ComdatSetType &getComdats() const { return Comdats; }
176  unsigned getComdatID(const Comdat *C) const;
177 
178  /// getGlobalBasicBlockID - This returns the function-specific ID for the
179  /// specified basic block. This is relatively expensive information, so it
180  /// should only be used by rare constructs such as address-of-label.
181  unsigned getGlobalBasicBlockID(const BasicBlock *BB) const;
182 
183  /// incorporateFunction/purgeFunction - If you'd like to deal with a function,
184  /// use these two methods to get its data into the ValueEnumerator!
185  ///
186  void incorporateFunction(const Function &F);
187  void purgeFunction();
188  uint64_t computeBitsRequiredForTypeIndicies() const;
189 
190 private:
191  void OptimizeConstants(unsigned CstStart, unsigned CstEnd);
192 
193  void EnumerateMDNodeOperands(const MDNode *N);
194  void EnumerateMetadata(const Metadata *MD);
195  void EnumerateFunctionLocalMetadata(const LocalAsMetadata *Local);
196  void EnumerateNamedMDNode(const NamedMDNode *NMD);
197  void EnumerateValue(const Value *V);
198  void EnumerateType(Type *T);
199  void EnumerateOperandType(const Value *V);
200  void EnumerateAttributes(AttributeSet PAL);
201 
202  void EnumerateValueSymbolTable(const ValueSymbolTable &ST);
203  void EnumerateNamedMetadata(const Module &M);
204 };
205 
206 } // End llvm namespace
207 
208 #endif
This class provides a symbol table of name/value pairs.
const ComdatSetType & getComdats() const
ValueT lookup(const KeyT &Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition: DenseMap.h:159
Various leaf nodes.
Definition: ISDOpcodes.h:60
bool hasMDString() const
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:114
void setInstructionID(const Instruction *I)
Metadata node.
Definition: Metadata.h:740
F(f)
std::vector< std::pair< const Value *, unsigned > > ValueList
void incorporateFunction(const Function &F)
incorporateFunction/purgeFunction - If you'd like to deal with a function, use these two methods to g...
A tuple of MDNodes.
Definition: Metadata.h:1127
unsigned getComdatID(const Comdat *C) const
bool hasGenericDINode() const
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APInt.h:33
std::vector< UseListOrder > UseListOrderStack
Definition: UseListOrder.h:52
This file contains the simple types necessary to represent the attributes associated with functions a...
uint64_t computeBitsRequiredForTypeIndicies() const
unsigned getAttributeID(AttributeSet PAL) const
bool hasDILocation() const
const TypeList & getTypes() const
unsigned getInstructionID(const Instruction *I) const
unsigned getValueID(const Value *V) const
void getFunctionConstantRange(unsigned &Start, unsigned &End) const
getFunctionConstantRange - Return the range of values that corresponds to function-local constants...
const ValueList & getValues() const
bool shouldPreserveUseListOrder() const
const std::vector< AttributeSet > & getAttributes() const
unsigned getTypeID(Type *T) const
LLVM Basic Block Representation.
Definition: BasicBlock.h:65
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
unsigned getAttributeGroupID(AttributeSet PAL) const
const std::vector< const Metadata * > & getMDs() const
See the file comment.
Definition: ValueMap.h:80
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
const SmallVectorImpl< const LocalAsMetadata * > & getFunctionLocalMDs() const
const std::vector< AttributeSet > & getAttributeGroups() const
const std::vector< const BasicBlock * > & getBasicBlocks() const
unsigned getMetadataID(const Metadata *MD) const
UseListOrderStack UseListOrders
unsigned getMetadataOrNullID(const Metadata *MD) const
void print(raw_ostream &OS, const ValueMapType &Map, const char *Name) const
#define I(x, y, z)
Definition: MD5.cpp:54
#define N
iterator end()
Definition: DenseMap.h:68
iterator find(const KeyT &Val)
Definition: DenseMap.h:124
unsigned getGlobalBasicBlockID(const BasicBlock *BB) const
getGlobalBasicBlockID - This returns the function-specific ID for the specified basic block...
LLVM Value Representation.
Definition: Value.h:69
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:38
std::vector< Type * > TypeList
Root of the metadata hierarchy.
Definition: Metadata.h:45
bool isEmpty() const
Return true if there are no attributes.
Definition: Attributes.h:390