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