ValueEnumerator.h 6.7 KB

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