blob: a1b188ef9ba635eec2a36273d61d3a54c2652f94 [file] [log] [blame]
Chris Lattnerfd57cec2007-04-22 06:24:45 +00001//===-- Bitcode/Writer/ValueEnumerator.h - Number values --------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerfd57cec2007-04-22 06:24:45 +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 Pateleaf42ab2008-09-23 23:03:40 +000018#include "llvm/Attributes.h"
Chris Lattnerfd57cec2007-04-22 06:24:45 +000019#include <vector>
20
21namespace llvm {
22
Chris Lattnerfd57cec2007-04-22 06:24:45 +000023class Type;
Chris Lattnerc59c0af2007-04-26 04:42:16 +000024class Value;
Devang Patele8e02132009-09-18 19:26:43 +000025class Instruction;
Chris Lattnerc59c0af2007-04-26 04:42:16 +000026class BasicBlock;
Chris Lattnerfd57cec2007-04-22 06:24:45 +000027class Function;
Chris Lattnerc59c0af2007-04-26 04:42:16 +000028class Module;
Devang Pateld5ac4042009-08-04 06:00:18 +000029class MetadataBase;
Devang Patel05988662008-09-25 21:00:45 +000030class AttrListPtr;
Chris Lattnerfd57cec2007-04-22 06:24:45 +000031class TypeSymbolTable;
32class ValueSymbolTable;
Devang Patel0386f012010-01-07 19:39:36 +000033class MDSymbolTable;
Chris Lattnerfd57cec2007-04-22 06:24:45 +000034
35class ValueEnumerator {
36public:
37 // For each type, we remember its Type* and occurrence frequency.
38 typedef std::vector<std::pair<const Type*, unsigned> > TypeList;
39
40 // For each value, we remember its Value* and occurrence frequency.
41 typedef std::vector<std::pair<const Value*, unsigned> > ValueList;
42private:
Chris Lattnerfd57cec2007-04-22 06:24:45 +000043 typedef DenseMap<const Type*, unsigned> TypeMapType;
44 TypeMapType TypeMap;
Chris Lattner2edd22b2007-04-24 00:16:04 +000045 TypeList Types;
Chris Lattnerfd57cec2007-04-22 06:24:45 +000046
Chris Lattnerfd57cec2007-04-22 06:24:45 +000047 typedef DenseMap<const Value*, unsigned> ValueMapType;
48 ValueMapType ValueMap;
Chris Lattner2edd22b2007-04-24 00:16:04 +000049 ValueList Values;
Devang Pateld5ac4042009-08-04 06:00:18 +000050 ValueList MDValues;
51 ValueMapType MDValueMap;
Devang Patele8e02132009-09-18 19:26:43 +000052
Devang Patel05988662008-09-25 21:00:45 +000053 typedef DenseMap<void*, unsigned> AttributeMapType;
54 AttributeMapType AttributeMap;
55 std::vector<AttrListPtr> Attributes;
Chris Lattner50954f52007-05-03 22:46:43 +000056
Chris Lattner837e04a2009-10-28 05:24:40 +000057 /// GlobalBasicBlockIDs - This map memoizes the basic block ID's referenced by
58 /// the "getGlobalBasicBlockID" method.
59 mutable DenseMap<const BasicBlock*, unsigned> GlobalBasicBlockIDs;
60
Devang Patele8e02132009-09-18 19:26:43 +000061 typedef DenseMap<const Instruction*, unsigned> InstructionMapType;
62 InstructionMapType InstructionMap;
63 unsigned InstructionCount;
64
Chris Lattnerc59c0af2007-04-26 04:42:16 +000065 /// BasicBlocks - This contains all the basic blocks for the currently
66 /// incorporated function. Their reverse mapping is stored in ValueMap.
67 std::vector<const BasicBlock*> BasicBlocks;
68
Chris Lattner8d35c792007-04-26 03:50:57 +000069 /// When a function is incorporated, this is the size of the Values list
70 /// before incorporation.
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +000071 unsigned NumModuleValues;
72 unsigned FirstFuncConstantID;
73 unsigned FirstInstID;
Chris Lattnerfd57cec2007-04-22 06:24:45 +000074
75 ValueEnumerator(const ValueEnumerator &); // DO NOT IMPLEMENT
76 void operator=(const ValueEnumerator &); // DO NOT IMPLEMENT
77public:
78 ValueEnumerator(const Module *M);
79
Devang Pateld5ac4042009-08-04 06:00:18 +000080 unsigned getValueID(const Value *V) const;
81
Chris Lattnerfd57cec2007-04-22 06:24:45 +000082 unsigned getTypeID(const Type *T) const {
83 TypeMapType::const_iterator I = TypeMap.find(T);
84 assert(I != TypeMap.end() && "Type not in ValueEnumerator!");
85 return I->second-1;
86 }
Devang Patele8e02132009-09-18 19:26:43 +000087
88 unsigned getInstructionID(const Instruction *I) const;
89 void setInstructionID(const Instruction *I);
90
Devang Patel05988662008-09-25 21:00:45 +000091 unsigned getAttributeID(const AttrListPtr &PAL) const {
Chris Lattner58d74912008-03-12 17:45:29 +000092 if (PAL.isEmpty()) return 0; // Null maps to zero.
Devang Patel05988662008-09-25 21:00:45 +000093 AttributeMapType::const_iterator I = AttributeMap.find(PAL.getRawPointer());
94 assert(I != AttributeMap.end() && "Attribute not in ValueEnumerator!");
Chris Lattner50954f52007-05-03 22:46:43 +000095 return I->second;
96 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +000097
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +000098 /// getFunctionConstantRange - Return the range of values that corresponds to
99 /// function-local constants.
100 void getFunctionConstantRange(unsigned &Start, unsigned &End) const {
101 Start = FirstFuncConstantID;
102 End = FirstInstID;
103 }
104
Chris Lattner2edd22b2007-04-24 00:16:04 +0000105 const ValueList &getValues() const { return Values; }
Devang Pateld5ac4042009-08-04 06:00:18 +0000106 const ValueList &getMDValues() const { return MDValues; }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000107 const TypeList &getTypes() const { return Types; }
Chris Lattnerc59c0af2007-04-26 04:42:16 +0000108 const std::vector<const BasicBlock*> &getBasicBlocks() const {
109 return BasicBlocks;
110 }
Devang Patel05988662008-09-25 21:00:45 +0000111 const std::vector<AttrListPtr> &getAttributes() const {
112 return Attributes;
Chris Lattner50954f52007-05-03 22:46:43 +0000113 }
Chris Lattner837e04a2009-10-28 05:24:40 +0000114
115 /// getGlobalBasicBlockID - This returns the function-specific ID for the
116 /// specified basic block. This is relatively expensive information, so it
117 /// should only be used by rare constructs such as address-of-label.
118 unsigned getGlobalBasicBlockID(const BasicBlock *BB) const;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000119
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000120 /// incorporateFunction/purgeFunction - If you'd like to deal with a function,
121 /// use these two methods to get its data into the ValueEnumerator!
122 ///
Chris Lattner8d35c792007-04-26 03:50:57 +0000123 void incorporateFunction(const Function &F);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000124 void purgeFunction();
125
126private:
Chris Lattner6da91d32007-05-04 05:21:47 +0000127 void OptimizeConstants(unsigned CstStart, unsigned CstEnd);
128
Devang Pateld5ac4042009-08-04 06:00:18 +0000129 void EnumerateMetadata(const MetadataBase *MD);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000130 void EnumerateValue(const Value *V);
131 void EnumerateType(const Type *T);
Chris Lattner33f1d5b2007-05-06 08:35:19 +0000132 void EnumerateOperandType(const Value *V);
Devang Patel05988662008-09-25 21:00:45 +0000133 void EnumerateAttributes(const AttrListPtr &PAL);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000134
135 void EnumerateTypeSymbolTable(const TypeSymbolTable &ST);
136 void EnumerateValueSymbolTable(const ValueSymbolTable &ST);
Devang Patel0386f012010-01-07 19:39:36 +0000137 void EnumerateMDSymbolTable(const MDSymbolTable &ST);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000138};
139
140} // End llvm namespace
141
142#endif