blob: b5106f0809f48ebd2f7269342d12458cecd11335 [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;
25class BasicBlock;
Chris Lattnerfd57cec2007-04-22 06:24:45 +000026class Function;
Chris Lattnerc59c0af2007-04-26 04:42:16 +000027class Module;
Devang Pateld5ac4042009-08-04 06:00:18 +000028class MetadataBase;
Devang Patel05988662008-09-25 21:00:45 +000029class AttrListPtr;
Chris Lattnerfd57cec2007-04-22 06:24:45 +000030class TypeSymbolTable;
31class ValueSymbolTable;
Chris Lattnerfd57cec2007-04-22 06:24:45 +000032
33class ValueEnumerator {
34public:
35 // For each type, we remember its Type* and occurrence frequency.
36 typedef std::vector<std::pair<const Type*, unsigned> > TypeList;
37
38 // For each value, we remember its Value* and occurrence frequency.
39 typedef std::vector<std::pair<const Value*, unsigned> > ValueList;
40private:
Chris Lattnerfd57cec2007-04-22 06:24:45 +000041 typedef DenseMap<const Type*, unsigned> TypeMapType;
42 TypeMapType TypeMap;
Chris Lattner2edd22b2007-04-24 00:16:04 +000043 TypeList Types;
Chris Lattnerfd57cec2007-04-22 06:24:45 +000044
Chris Lattnerfd57cec2007-04-22 06:24:45 +000045 typedef DenseMap<const Value*, unsigned> ValueMapType;
46 ValueMapType ValueMap;
Chris Lattner2edd22b2007-04-24 00:16:04 +000047 ValueList Values;
Devang Pateld5ac4042009-08-04 06:00:18 +000048 ValueList MDValues;
49 ValueMapType MDValueMap;
50
Devang Patel05988662008-09-25 21:00:45 +000051 typedef DenseMap<void*, unsigned> AttributeMapType;
52 AttributeMapType AttributeMap;
53 std::vector<AttrListPtr> Attributes;
Chris Lattner50954f52007-05-03 22:46:43 +000054
Chris Lattnerc59c0af2007-04-26 04:42:16 +000055 /// BasicBlocks - This contains all the basic blocks for the currently
56 /// incorporated function. Their reverse mapping is stored in ValueMap.
57 std::vector<const BasicBlock*> BasicBlocks;
58
Chris Lattner8d35c792007-04-26 03:50:57 +000059 /// When a function is incorporated, this is the size of the Values list
60 /// before incorporation.
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +000061 unsigned NumModuleValues;
62 unsigned FirstFuncConstantID;
63 unsigned FirstInstID;
Chris Lattnerfd57cec2007-04-22 06:24:45 +000064
65 ValueEnumerator(const ValueEnumerator &); // DO NOT IMPLEMENT
66 void operator=(const ValueEnumerator &); // DO NOT IMPLEMENT
67public:
68 ValueEnumerator(const Module *M);
69
Devang Pateld5ac4042009-08-04 06:00:18 +000070 unsigned getValueID(const Value *V) const;
71
Chris Lattnerfd57cec2007-04-22 06:24:45 +000072 unsigned getTypeID(const Type *T) const {
73 TypeMapType::const_iterator I = TypeMap.find(T);
74 assert(I != TypeMap.end() && "Type not in ValueEnumerator!");
75 return I->second-1;
76 }
Chris Lattner50954f52007-05-03 22:46:43 +000077
Devang Patel05988662008-09-25 21:00:45 +000078 unsigned getAttributeID(const AttrListPtr &PAL) const {
Chris Lattner58d74912008-03-12 17:45:29 +000079 if (PAL.isEmpty()) return 0; // Null maps to zero.
Devang Patel05988662008-09-25 21:00:45 +000080 AttributeMapType::const_iterator I = AttributeMap.find(PAL.getRawPointer());
81 assert(I != AttributeMap.end() && "Attribute not in ValueEnumerator!");
Chris Lattner50954f52007-05-03 22:46:43 +000082 return I->second;
83 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +000084
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +000085 /// getFunctionConstantRange - Return the range of values that corresponds to
86 /// function-local constants.
87 void getFunctionConstantRange(unsigned &Start, unsigned &End) const {
88 Start = FirstFuncConstantID;
89 End = FirstInstID;
90 }
91
Chris Lattner2edd22b2007-04-24 00:16:04 +000092 const ValueList &getValues() const { return Values; }
Devang Pateld5ac4042009-08-04 06:00:18 +000093 const ValueList &getMDValues() const { return MDValues; }
Chris Lattnerfd57cec2007-04-22 06:24:45 +000094 const TypeList &getTypes() const { return Types; }
Chris Lattnerc59c0af2007-04-26 04:42:16 +000095 const std::vector<const BasicBlock*> &getBasicBlocks() const {
96 return BasicBlocks;
97 }
Devang Patel05988662008-09-25 21:00:45 +000098 const std::vector<AttrListPtr> &getAttributes() const {
99 return Attributes;
Chris Lattner50954f52007-05-03 22:46:43 +0000100 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000101
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000102 /// incorporateFunction/purgeFunction - If you'd like to deal with a function,
103 /// use these two methods to get its data into the ValueEnumerator!
104 ///
Chris Lattner8d35c792007-04-26 03:50:57 +0000105 void incorporateFunction(const Function &F);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000106 void purgeFunction();
107
108private:
Chris Lattner6da91d32007-05-04 05:21:47 +0000109 void OptimizeConstants(unsigned CstStart, unsigned CstEnd);
110
Devang Pateld5ac4042009-08-04 06:00:18 +0000111 void EnumerateMetadata(const MetadataBase *MD);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000112 void EnumerateValue(const Value *V);
113 void EnumerateType(const Type *T);
Chris Lattner33f1d5b2007-05-06 08:35:19 +0000114 void EnumerateOperandType(const Value *V);
Devang Patel05988662008-09-25 21:00:45 +0000115 void EnumerateAttributes(const AttrListPtr &PAL);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000116
117 void EnumerateTypeSymbolTable(const TypeSymbolTable &ST);
118 void EnumerateValueSymbolTable(const ValueSymbolTable &ST);
119};
120
121} // End llvm namespace
122
123#endif