blob: da63dde2a2799ff3534e8d307bb3a5e07e29535f [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;
Chris Lattnerfd57cec2007-04-22 06:24:45 +000033
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:
Chris Lattnerfd57cec2007-04-22 06:24:45 +000042 typedef DenseMap<const Type*, unsigned> TypeMapType;
43 TypeMapType TypeMap;
Chris Lattner2edd22b2007-04-24 00:16:04 +000044 TypeList Types;
Chris Lattnerfd57cec2007-04-22 06:24:45 +000045
Chris Lattnerfd57cec2007-04-22 06:24:45 +000046 typedef DenseMap<const Value*, unsigned> ValueMapType;
47 ValueMapType ValueMap;
Chris Lattner2edd22b2007-04-24 00:16:04 +000048 ValueList Values;
Devang Pateld5ac4042009-08-04 06:00:18 +000049 ValueList MDValues;
50 ValueMapType MDValueMap;
Devang Patele8e02132009-09-18 19:26:43 +000051
Devang Patel05988662008-09-25 21:00:45 +000052 typedef DenseMap<void*, unsigned> AttributeMapType;
53 AttributeMapType AttributeMap;
54 std::vector<AttrListPtr> Attributes;
Chris Lattner50954f52007-05-03 22:46:43 +000055
Devang Patele8e02132009-09-18 19:26:43 +000056 typedef DenseMap<const Instruction*, unsigned> InstructionMapType;
57 InstructionMapType InstructionMap;
58 unsigned InstructionCount;
59
Chris Lattnerc59c0af2007-04-26 04:42:16 +000060 /// BasicBlocks - This contains all the basic blocks for the currently
61 /// incorporated function. Their reverse mapping is stored in ValueMap.
62 std::vector<const BasicBlock*> BasicBlocks;
63
Chris Lattner8d35c792007-04-26 03:50:57 +000064 /// When a function is incorporated, this is the size of the Values list
65 /// before incorporation.
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +000066 unsigned NumModuleValues;
67 unsigned FirstFuncConstantID;
68 unsigned FirstInstID;
Chris Lattnerfd57cec2007-04-22 06:24:45 +000069
70 ValueEnumerator(const ValueEnumerator &); // DO NOT IMPLEMENT
71 void operator=(const ValueEnumerator &); // DO NOT IMPLEMENT
72public:
73 ValueEnumerator(const Module *M);
74
Devang Pateld5ac4042009-08-04 06:00:18 +000075 unsigned getValueID(const Value *V) const;
76
Chris Lattnerfd57cec2007-04-22 06:24:45 +000077 unsigned getTypeID(const Type *T) const {
78 TypeMapType::const_iterator I = TypeMap.find(T);
79 assert(I != TypeMap.end() && "Type not in ValueEnumerator!");
80 return I->second-1;
81 }
Devang Patele8e02132009-09-18 19:26:43 +000082
83 unsigned getInstructionID(const Instruction *I) const;
84 void setInstructionID(const Instruction *I);
85
Devang Patel05988662008-09-25 21:00:45 +000086 unsigned getAttributeID(const AttrListPtr &PAL) const {
Chris Lattner58d74912008-03-12 17:45:29 +000087 if (PAL.isEmpty()) return 0; // Null maps to zero.
Devang Patel05988662008-09-25 21:00:45 +000088 AttributeMapType::const_iterator I = AttributeMap.find(PAL.getRawPointer());
89 assert(I != AttributeMap.end() && "Attribute not in ValueEnumerator!");
Chris Lattner50954f52007-05-03 22:46:43 +000090 return I->second;
91 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +000092
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +000093 /// getFunctionConstantRange - Return the range of values that corresponds to
94 /// function-local constants.
95 void getFunctionConstantRange(unsigned &Start, unsigned &End) const {
96 Start = FirstFuncConstantID;
97 End = FirstInstID;
98 }
99
Chris Lattner2edd22b2007-04-24 00:16:04 +0000100 const ValueList &getValues() const { return Values; }
Devang Pateld5ac4042009-08-04 06:00:18 +0000101 const ValueList &getMDValues() const { return MDValues; }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000102 const TypeList &getTypes() const { return Types; }
Chris Lattnerc59c0af2007-04-26 04:42:16 +0000103 const std::vector<const BasicBlock*> &getBasicBlocks() const {
104 return BasicBlocks;
105 }
Devang Patel05988662008-09-25 21:00:45 +0000106 const std::vector<AttrListPtr> &getAttributes() const {
107 return Attributes;
Chris Lattner50954f52007-05-03 22:46:43 +0000108 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000109
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000110 /// incorporateFunction/purgeFunction - If you'd like to deal with a function,
111 /// use these two methods to get its data into the ValueEnumerator!
112 ///
Chris Lattner8d35c792007-04-26 03:50:57 +0000113 void incorporateFunction(const Function &F);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000114 void purgeFunction();
115
116private:
Chris Lattner6da91d32007-05-04 05:21:47 +0000117 void OptimizeConstants(unsigned CstStart, unsigned CstEnd);
118
Devang Pateld5ac4042009-08-04 06:00:18 +0000119 void EnumerateMetadata(const MetadataBase *MD);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000120 void EnumerateValue(const Value *V);
121 void EnumerateType(const Type *T);
Chris Lattner33f1d5b2007-05-06 08:35:19 +0000122 void EnumerateOperandType(const Value *V);
Devang Patel05988662008-09-25 21:00:45 +0000123 void EnumerateAttributes(const AttrListPtr &PAL);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000124
125 void EnumerateTypeSymbolTable(const TypeSymbolTable &ST);
126 void EnumerateValueSymbolTable(const ValueSymbolTable &ST);
127};
128
129} // End llvm namespace
130
131#endif