blob: 2b9b15fa5a77a788b7c40383b343ab9055b65271 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- Bitcode/Writer/ValueEnumerator.h - Number values --------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
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"
Devang Patel63b11ba2010-06-02 23:05:04 +000018#include "llvm/ADT/SmallVector.h"
Devang Patele480dfa2008-09-23 23:03:40 +000019#include "llvm/Attributes.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000020#include <vector>
21
22namespace llvm {
23
24class Type;
25class Value;
Devang Pateladc37612009-09-18 19:26:43 +000026class Instruction;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027class BasicBlock;
28class Function;
29class Module;
Devang Patel63b11ba2010-06-02 23:05:04 +000030class MDNode;
Devang Patel3c5c9ee2010-01-09 00:30:14 +000031class NamedMDNode;
Devang Pateld222f862008-09-25 21:00:45 +000032class AttrListPtr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033class TypeSymbolTable;
34class ValueSymbolTable;
Devang Patel833d4422010-01-07 19:39:36 +000035class MDSymbolTable;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000036
37class ValueEnumerator {
38public:
39 // For each type, we remember its Type* and occurrence frequency.
40 typedef std::vector<std::pair<const Type*, unsigned> > TypeList;
41
42 // For each value, we remember its Value* and occurrence frequency.
43 typedef std::vector<std::pair<const Value*, unsigned> > ValueList;
44private:
45 typedef DenseMap<const Type*, unsigned> TypeMapType;
46 TypeMapType TypeMap;
47 TypeList Types;
48
49 typedef DenseMap<const Value*, unsigned> ValueMapType;
50 ValueMapType ValueMap;
51 ValueList Values;
Devang Patelea27c232009-08-04 06:00:18 +000052 ValueList MDValues;
Devang Patel63b11ba2010-06-02 23:05:04 +000053 SmallVector<const MDNode *, 8> FunctionLocalMDs;
Devang Patelea27c232009-08-04 06:00:18 +000054 ValueMapType MDValueMap;
Devang Pateladc37612009-09-18 19:26:43 +000055
Devang Pateld222f862008-09-25 21:00:45 +000056 typedef DenseMap<void*, unsigned> AttributeMapType;
57 AttributeMapType AttributeMap;
58 std::vector<AttrListPtr> Attributes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059
Chris Lattnerd5693a22009-10-28 05:24:40 +000060 /// GlobalBasicBlockIDs - This map memoizes the basic block ID's referenced by
61 /// the "getGlobalBasicBlockID" method.
62 mutable DenseMap<const BasicBlock*, unsigned> GlobalBasicBlockIDs;
63
Devang Pateladc37612009-09-18 19:26:43 +000064 typedef DenseMap<const Instruction*, unsigned> InstructionMapType;
65 InstructionMapType InstructionMap;
66 unsigned InstructionCount;
67
Dan Gohmanf17a25c2007-07-18 16:29:46 +000068 /// BasicBlocks - This contains all the basic blocks for the currently
69 /// incorporated function. Their reverse mapping is stored in ValueMap.
70 std::vector<const BasicBlock*> BasicBlocks;
71
72 /// When a function is incorporated, this is the size of the Values list
73 /// before incorporation.
74 unsigned NumModuleValues;
75 unsigned FirstFuncConstantID;
76 unsigned FirstInstID;
77
78 ValueEnumerator(const ValueEnumerator &); // DO NOT IMPLEMENT
79 void operator=(const ValueEnumerator &); // DO NOT IMPLEMENT
80public:
81 ValueEnumerator(const Module *M);
82
Devang Patelea27c232009-08-04 06:00:18 +000083 unsigned getValueID(const Value *V) const;
84
Dan Gohmanf17a25c2007-07-18 16:29:46 +000085 unsigned getTypeID(const Type *T) const {
86 TypeMapType::const_iterator I = TypeMap.find(T);
87 assert(I != TypeMap.end() && "Type not in ValueEnumerator!");
88 return I->second-1;
89 }
Devang Pateladc37612009-09-18 19:26:43 +000090
91 unsigned getInstructionID(const Instruction *I) const;
92 void setInstructionID(const Instruction *I);
93
Devang Pateld222f862008-09-25 21:00:45 +000094 unsigned getAttributeID(const AttrListPtr &PAL) const {
Chris Lattner1c8733e2008-03-12 17:45:29 +000095 if (PAL.isEmpty()) return 0; // Null maps to zero.
Devang Pateld222f862008-09-25 21:00:45 +000096 AttributeMapType::const_iterator I = AttributeMap.find(PAL.getRawPointer());
97 assert(I != AttributeMap.end() && "Attribute not in ValueEnumerator!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000098 return I->second;
99 }
100
101 /// getFunctionConstantRange - Return the range of values that corresponds to
102 /// function-local constants.
103 void getFunctionConstantRange(unsigned &Start, unsigned &End) const {
104 Start = FirstFuncConstantID;
105 End = FirstInstID;
106 }
107
108 const ValueList &getValues() const { return Values; }
Devang Patelea27c232009-08-04 06:00:18 +0000109 const ValueList &getMDValues() const { return MDValues; }
Devang Patel63b11ba2010-06-02 23:05:04 +0000110 const SmallVector<const MDNode *, 8> &getFunctionLocalMDValues() const {
111 return FunctionLocalMDs;
112 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000113 const TypeList &getTypes() const { return Types; }
114 const std::vector<const BasicBlock*> &getBasicBlocks() const {
115 return BasicBlocks;
116 }
Devang Pateld222f862008-09-25 21:00:45 +0000117 const std::vector<AttrListPtr> &getAttributes() const {
118 return Attributes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000119 }
Chris Lattnerd5693a22009-10-28 05:24:40 +0000120
121 /// getGlobalBasicBlockID - This returns the function-specific ID for the
122 /// specified basic block. This is relatively expensive information, so it
123 /// should only be used by rare constructs such as address-of-label.
124 unsigned getGlobalBasicBlockID(const BasicBlock *BB) const;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000125
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000126 /// incorporateFunction/purgeFunction - If you'd like to deal with a function,
127 /// use these two methods to get its data into the ValueEnumerator!
128 ///
129 void incorporateFunction(const Function &F);
130 void purgeFunction();
131
132private:
133 void OptimizeConstants(unsigned CstStart, unsigned CstEnd);
134
Devang Patel0a0ea052010-01-22 22:52:10 +0000135 void EnumerateMetadata(const Value *MD);
Devang Patel3c5c9ee2010-01-09 00:30:14 +0000136 void EnumerateNamedMDNode(const NamedMDNode *NMD);
Victor Hernandez7f3fbe32010-01-14 19:54:11 +0000137 void EnumerateValue(const Value *V);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000138 void EnumerateType(const Type *T);
Victor Hernandez7f3fbe32010-01-14 19:54:11 +0000139 void EnumerateOperandType(const Value *V);
Devang Pateld222f862008-09-25 21:00:45 +0000140 void EnumerateAttributes(const AttrListPtr &PAL);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000141
142 void EnumerateTypeSymbolTable(const TypeSymbolTable &ST);
143 void EnumerateValueSymbolTable(const ValueSymbolTable &ST);
Devang Patel833d4422010-01-07 19:39:36 +0000144 void EnumerateMDSymbolTable(const MDSymbolTable &ST);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000145};
146
147} // End llvm namespace
148
149#endif