blob: 3c83e35695609313927a9674c5c4dbbd13080667 [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 Patele480dfa2008-09-23 23:03:40 +000018#include "llvm/Attributes.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000019#include <vector>
20
21namespace llvm {
22
23class Type;
24class Value;
Devang Pateladc37612009-09-18 19:26:43 +000025class Instruction;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026class BasicBlock;
27class Function;
28class Module;
Devang Patelea27c232009-08-04 06:00:18 +000029class MetadataBase;
Devang Pateld222f862008-09-25 21:00:45 +000030class AttrListPtr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000031class TypeSymbolTable;
32class ValueSymbolTable;
33
34class ValueEnumerator {
35public:
36 // For each type, we remember its Type* and occurrence frequency.
37 typedef std::vector<std::pair<const Type*, unsigned> > TypeList;
38
39 // For each value, we remember its Value* and occurrence frequency.
40 typedef std::vector<std::pair<const Value*, unsigned> > ValueList;
41private:
42 typedef DenseMap<const Type*, unsigned> TypeMapType;
43 TypeMapType TypeMap;
44 TypeList Types;
45
46 typedef DenseMap<const Value*, unsigned> ValueMapType;
47 ValueMapType ValueMap;
48 ValueList Values;
Devang Patelea27c232009-08-04 06:00:18 +000049 ValueList MDValues;
50 ValueMapType MDValueMap;
Devang Pateladc37612009-09-18 19:26:43 +000051
Devang Pateld222f862008-09-25 21:00:45 +000052 typedef DenseMap<void*, unsigned> AttributeMapType;
53 AttributeMapType AttributeMap;
54 std::vector<AttrListPtr> Attributes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055
Chris Lattnerd5693a22009-10-28 05:24:40 +000056 /// GlobalBasicBlockIDs - This map memoizes the basic block ID's referenced by
57 /// the "getGlobalBasicBlockID" method.
58 mutable DenseMap<const BasicBlock*, unsigned> GlobalBasicBlockIDs;
59
Devang Pateladc37612009-09-18 19:26:43 +000060 typedef DenseMap<const Instruction*, unsigned> InstructionMapType;
61 InstructionMapType InstructionMap;
62 unsigned InstructionCount;
63
Dan Gohmanf17a25c2007-07-18 16:29:46 +000064 /// BasicBlocks - This contains all the basic blocks for the currently
65 /// incorporated function. Their reverse mapping is stored in ValueMap.
66 std::vector<const BasicBlock*> BasicBlocks;
67
68 /// When a function is incorporated, this is the size of the Values list
69 /// before incorporation.
70 unsigned NumModuleValues;
71 unsigned FirstFuncConstantID;
72 unsigned FirstInstID;
73
74 ValueEnumerator(const ValueEnumerator &); // DO NOT IMPLEMENT
75 void operator=(const ValueEnumerator &); // DO NOT IMPLEMENT
76public:
77 ValueEnumerator(const Module *M);
78
Devang Patelea27c232009-08-04 06:00:18 +000079 unsigned getValueID(const Value *V) const;
80
Dan Gohmanf17a25c2007-07-18 16:29:46 +000081 unsigned getTypeID(const Type *T) const {
82 TypeMapType::const_iterator I = TypeMap.find(T);
83 assert(I != TypeMap.end() && "Type not in ValueEnumerator!");
84 return I->second-1;
85 }
Devang Pateladc37612009-09-18 19:26:43 +000086
87 unsigned getInstructionID(const Instruction *I) const;
88 void setInstructionID(const Instruction *I);
89
Devang Pateld222f862008-09-25 21:00:45 +000090 unsigned getAttributeID(const AttrListPtr &PAL) const {
Chris Lattner1c8733e2008-03-12 17:45:29 +000091 if (PAL.isEmpty()) return 0; // Null maps to zero.
Devang Pateld222f862008-09-25 21:00:45 +000092 AttributeMapType::const_iterator I = AttributeMap.find(PAL.getRawPointer());
93 assert(I != AttributeMap.end() && "Attribute not in ValueEnumerator!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000094 return I->second;
95 }
96
97 /// getFunctionConstantRange - Return the range of values that corresponds to
98 /// function-local constants.
99 void getFunctionConstantRange(unsigned &Start, unsigned &End) const {
100 Start = FirstFuncConstantID;
101 End = FirstInstID;
102 }
103
104 const ValueList &getValues() const { return Values; }
Devang Patelea27c232009-08-04 06:00:18 +0000105 const ValueList &getMDValues() const { return MDValues; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000106 const TypeList &getTypes() const { return Types; }
107 const std::vector<const BasicBlock*> &getBasicBlocks() const {
108 return BasicBlocks;
109 }
Devang Pateld222f862008-09-25 21:00:45 +0000110 const std::vector<AttrListPtr> &getAttributes() const {
111 return Attributes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000112 }
Chris Lattnerd5693a22009-10-28 05:24:40 +0000113
114 /// getGlobalBasicBlockID - This returns the function-specific ID for the
115 /// specified basic block. This is relatively expensive information, so it
116 /// should only be used by rare constructs such as address-of-label.
117 unsigned getGlobalBasicBlockID(const BasicBlock *BB) const;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000118
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000119 /// incorporateFunction/purgeFunction - If you'd like to deal with a function,
120 /// use these two methods to get its data into the ValueEnumerator!
121 ///
122 void incorporateFunction(const Function &F);
123 void purgeFunction();
124
125private:
126 void OptimizeConstants(unsigned CstStart, unsigned CstEnd);
127
Devang Patelea27c232009-08-04 06:00:18 +0000128 void EnumerateMetadata(const MetadataBase *MD);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000129 void EnumerateValue(const Value *V);
130 void EnumerateType(const Type *T);
131 void EnumerateOperandType(const Value *V);
Devang Pateld222f862008-09-25 21:00:45 +0000132 void EnumerateAttributes(const AttrListPtr &PAL);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000133
134 void EnumerateTypeSymbolTable(const TypeSymbolTable &ST);
135 void EnumerateValueSymbolTable(const ValueSymbolTable &ST);
136};
137
138} // End llvm namespace
139
140#endif