blob: 9fb916a5158fa84412bf30257fae9728df2f92d3 [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"
18#include <vector>
19
20namespace llvm {
21
Chris Lattnerfd57cec2007-04-22 06:24:45 +000022class Type;
Chris Lattnerc59c0af2007-04-26 04:42:16 +000023class Value;
24class BasicBlock;
Chris Lattnerfd57cec2007-04-22 06:24:45 +000025class Function;
Chris Lattnerc59c0af2007-04-26 04:42:16 +000026class Module;
Chris Lattner50954f52007-05-03 22:46:43 +000027class ParamAttrsList;
Chris Lattnerfd57cec2007-04-22 06:24:45 +000028class TypeSymbolTable;
29class ValueSymbolTable;
Chris Lattnerfd57cec2007-04-22 06:24:45 +000030
31class ValueEnumerator {
32public:
33 // For each type, we remember its Type* and occurrence frequency.
34 typedef std::vector<std::pair<const Type*, unsigned> > TypeList;
35
36 // For each value, we remember its Value* and occurrence frequency.
37 typedef std::vector<std::pair<const Value*, unsigned> > ValueList;
38private:
Chris Lattnerfd57cec2007-04-22 06:24:45 +000039 typedef DenseMap<const Type*, unsigned> TypeMapType;
40 TypeMapType TypeMap;
Chris Lattner2edd22b2007-04-24 00:16:04 +000041 TypeList Types;
Chris Lattnerfd57cec2007-04-22 06:24:45 +000042
Chris Lattnerfd57cec2007-04-22 06:24:45 +000043 typedef DenseMap<const Value*, unsigned> ValueMapType;
44 ValueMapType ValueMap;
Chris Lattner2edd22b2007-04-24 00:16:04 +000045 ValueList Values;
Chris Lattnerfd57cec2007-04-22 06:24:45 +000046
Chris Lattner50954f52007-05-03 22:46:43 +000047 typedef DenseMap<const ParamAttrsList*, unsigned> ParamAttrMapType;
48 ParamAttrMapType ParamAttrMap;
49 std::vector<const ParamAttrsList*> ParamAttrs;
50
Chris Lattnerc59c0af2007-04-26 04:42:16 +000051 /// BasicBlocks - This contains all the basic blocks for the currently
52 /// incorporated function. Their reverse mapping is stored in ValueMap.
53 std::vector<const BasicBlock*> BasicBlocks;
54
Chris Lattner8d35c792007-04-26 03:50:57 +000055 /// When a function is incorporated, this is the size of the Values list
56 /// before incorporation.
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +000057 unsigned NumModuleValues;
58 unsigned FirstFuncConstantID;
59 unsigned FirstInstID;
Chris Lattnerfd57cec2007-04-22 06:24:45 +000060
61 ValueEnumerator(const ValueEnumerator &); // DO NOT IMPLEMENT
62 void operator=(const ValueEnumerator &); // DO NOT IMPLEMENT
63public:
64 ValueEnumerator(const Module *M);
65
66 unsigned getValueID(const Value *V) const {
67 ValueMapType::const_iterator I = ValueMap.find(V);
68 assert(I != ValueMap.end() && "Value not in slotcalculator!");
Chris Lattner8c99a8e2007-04-23 21:23:41 +000069 return I->second-1;
Chris Lattnerfd57cec2007-04-22 06:24:45 +000070 }
71
72 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
78 unsigned getParamAttrID(const ParamAttrsList *PAL) const {
79 if (PAL == 0) return 0; // Null maps to zero.
80 ParamAttrMapType::const_iterator I = ParamAttrMap.find(PAL);
81 assert(I != ParamAttrMap.end() && "ParamAttr not in ValueEnumerator!");
82 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; }
Chris Lattnerfd57cec2007-04-22 06:24:45 +000093 const TypeList &getTypes() const { return Types; }
Chris Lattnerc59c0af2007-04-26 04:42:16 +000094 const std::vector<const BasicBlock*> &getBasicBlocks() const {
95 return BasicBlocks;
96 }
Chris Lattner2020ca22007-05-04 00:45:24 +000097 const std::vector<const ParamAttrsList*> &getParamAttrs() const {
Chris Lattner50954f52007-05-03 22:46:43 +000098 return ParamAttrs;
99 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000100
Chris Lattner198f34a2007-04-26 03:27:58 +0000101 /// PurgeAggregateValues - If there are any aggregate values at the end of the
102 /// value list, remove them and return the count of the remaining values. If
103 /// there are none, return -1.
104 int PurgeAggregateValues();
105
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000106 /// incorporateFunction/purgeFunction - If you'd like to deal with a function,
107 /// use these two methods to get its data into the ValueEnumerator!
108 ///
Chris Lattner8d35c792007-04-26 03:50:57 +0000109 void incorporateFunction(const Function &F);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000110 void purgeFunction();
111
112private:
Chris Lattner6da91d32007-05-04 05:21:47 +0000113 void OptimizeConstants(unsigned CstStart, unsigned CstEnd);
114
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000115 void EnumerateValue(const Value *V);
116 void EnumerateType(const Type *T);
Chris Lattner33f1d5b2007-05-06 08:35:19 +0000117 void EnumerateOperandType(const Value *V);
Chris Lattner50954f52007-05-03 22:46:43 +0000118 void EnumerateParamAttrs(const ParamAttrsList *PAL);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000119
120 void EnumerateTypeSymbolTable(const TypeSymbolTable &ST);
121 void EnumerateValueSymbolTable(const ValueSymbolTable &ST);
122};
123
124} // End llvm namespace
125
126#endif