blob: dc40d016dcaaef94e7d203ab095f7d48a5ad9087 [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"
Chris Lattner58d74912008-03-12 17:45:29 +000018#include "llvm/ParameterAttributes.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;
Chris Lattner58d74912008-03-12 17:45:29 +000028class PAListPtr;
Chris Lattnerfd57cec2007-04-22 06:24:45 +000029class TypeSymbolTable;
30class ValueSymbolTable;
Chris Lattnerfd57cec2007-04-22 06:24:45 +000031
32class ValueEnumerator {
33public:
34 // For each type, we remember its Type* and occurrence frequency.
35 typedef std::vector<std::pair<const Type*, unsigned> > TypeList;
36
37 // For each value, we remember its Value* and occurrence frequency.
38 typedef std::vector<std::pair<const Value*, unsigned> > ValueList;
39private:
Chris Lattnerfd57cec2007-04-22 06:24:45 +000040 typedef DenseMap<const Type*, unsigned> TypeMapType;
41 TypeMapType TypeMap;
Chris Lattner2edd22b2007-04-24 00:16:04 +000042 TypeList Types;
Chris Lattnerfd57cec2007-04-22 06:24:45 +000043
Chris Lattnerfd57cec2007-04-22 06:24:45 +000044 typedef DenseMap<const Value*, unsigned> ValueMapType;
45 ValueMapType ValueMap;
Chris Lattner2edd22b2007-04-24 00:16:04 +000046 ValueList Values;
Chris Lattnerfd57cec2007-04-22 06:24:45 +000047
Chris Lattner58d74912008-03-12 17:45:29 +000048 typedef DenseMap<void*, unsigned> ParamAttrMapType;
Chris Lattner50954f52007-05-03 22:46:43 +000049 ParamAttrMapType ParamAttrMap;
Chris Lattner58d74912008-03-12 17:45:29 +000050 std::vector<PAListPtr> ParamAttrs;
Chris Lattner50954f52007-05-03 22:46:43 +000051
Chris Lattnerc59c0af2007-04-26 04:42:16 +000052 /// BasicBlocks - This contains all the basic blocks for the currently
53 /// incorporated function. Their reverse mapping is stored in ValueMap.
54 std::vector<const BasicBlock*> BasicBlocks;
55
Chris Lattner8d35c792007-04-26 03:50:57 +000056 /// When a function is incorporated, this is the size of the Values list
57 /// before incorporation.
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +000058 unsigned NumModuleValues;
59 unsigned FirstFuncConstantID;
60 unsigned FirstInstID;
Chris Lattnerfd57cec2007-04-22 06:24:45 +000061
62 ValueEnumerator(const ValueEnumerator &); // DO NOT IMPLEMENT
63 void operator=(const ValueEnumerator &); // DO NOT IMPLEMENT
64public:
65 ValueEnumerator(const Module *M);
66
67 unsigned getValueID(const Value *V) const {
68 ValueMapType::const_iterator I = ValueMap.find(V);
69 assert(I != ValueMap.end() && "Value not in slotcalculator!");
Chris Lattner8c99a8e2007-04-23 21:23:41 +000070 return I->second-1;
Chris Lattnerfd57cec2007-04-22 06:24:45 +000071 }
72
73 unsigned getTypeID(const Type *T) const {
74 TypeMapType::const_iterator I = TypeMap.find(T);
75 assert(I != TypeMap.end() && "Type not in ValueEnumerator!");
76 return I->second-1;
77 }
Chris Lattner50954f52007-05-03 22:46:43 +000078
Chris Lattner58d74912008-03-12 17:45:29 +000079 unsigned getParamAttrID(const PAListPtr &PAL) const {
80 if (PAL.isEmpty()) return 0; // Null maps to zero.
81 ParamAttrMapType::const_iterator I = ParamAttrMap.find(PAL.getRawPointer());
Chris Lattner50954f52007-05-03 22:46:43 +000082 assert(I != ParamAttrMap.end() && "ParamAttr not in ValueEnumerator!");
83 return I->second;
84 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +000085
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +000086 /// getFunctionConstantRange - Return the range of values that corresponds to
87 /// function-local constants.
88 void getFunctionConstantRange(unsigned &Start, unsigned &End) const {
89 Start = FirstFuncConstantID;
90 End = FirstInstID;
91 }
92
Chris Lattner2edd22b2007-04-24 00:16:04 +000093 const ValueList &getValues() const { return Values; }
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 }
Chris Lattner58d74912008-03-12 17:45:29 +000098 const std::vector<PAListPtr> &getParamAttrs() const {
Chris Lattner50954f52007-05-03 22:46:43 +000099 return ParamAttrs;
100 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000101
Chris Lattner198f34a2007-04-26 03:27:58 +0000102 /// PurgeAggregateValues - If there are any aggregate values at the end of the
103 /// value list, remove them and return the count of the remaining values. If
104 /// there are none, return -1.
105 int PurgeAggregateValues();
106
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000107 /// incorporateFunction/purgeFunction - If you'd like to deal with a function,
108 /// use these two methods to get its data into the ValueEnumerator!
109 ///
Chris Lattner8d35c792007-04-26 03:50:57 +0000110 void incorporateFunction(const Function &F);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000111 void purgeFunction();
112
113private:
Chris Lattner6da91d32007-05-04 05:21:47 +0000114 void OptimizeConstants(unsigned CstStart, unsigned CstEnd);
115
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000116 void EnumerateValue(const Value *V);
117 void EnumerateType(const Type *T);
Chris Lattner33f1d5b2007-05-06 08:35:19 +0000118 void EnumerateOperandType(const Value *V);
Chris Lattner58d74912008-03-12 17:45:29 +0000119 void EnumerateParamAttrs(const PAListPtr &PAL);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000120
121 void EnumerateTypeSymbolTable(const TypeSymbolTable &ST);
122 void EnumerateValueSymbolTable(const ValueSymbolTable &ST);
123};
124
125} // End llvm namespace
126
127#endif