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
22namespace llvm {
23
24class Type;
25class Value;
26class Instruction;
27class BasicBlock;
28class Function;
29class Module;
30class Metadata;
31class LocalAsMetadata;
32class MDNode;
33class NamedMDNode;
34class AttributeSet;
35class ValueSymbolTable;
36class MDSymbolTable;
37class raw_ostream;
38
39}  // end llvm namespace
40
41namespace llvm_2_9_func {
42
43class ValueEnumerator {
44public:
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;
49private:
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;
99public:
100  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;
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  }
113  unsigned getMetadataOrNullID(const llvm::Metadata *MD) const {
114    return MDValueMap.lookup(MD);
115  }
116
117  bool hasMDString() const { return HasMDString; }
118  bool hasDILocation() const { return HasDILocation; }
119
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
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
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.
145  void getFunctionConstantRange(unsigned &Start, unsigned &End) const {
146    Start = FirstFuncConstantID;
147    End = FirstInstID;
148  }
149
150  const ValueList &getValues() const { return Values; }
151  const std::vector<const llvm::Metadata *> &getMDs() const { return MDs; }
152  const llvm::SmallVectorImpl<const llvm::LocalAsMetadata *> &getFunctionLocalMDs() const {
153    return FunctionLocalMDs;
154  }
155  const TypeList &getTypes() const { return Types; }
156  const std::vector<const llvm::BasicBlock*> &getBasicBlocks() const {
157    return BasicBlocks;
158  }
159  const std::vector<llvm::AttributeSet> &getAttributes() const {
160    return Attribute;
161  }
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
177private:
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