blob: 77f27dad68bc146d044ecac4841f43f4debb1b25 [file] [log] [blame]
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001//===-- Bitcode/Writer/ValueEnumerator.h - Number values --------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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 Lattnerc1d10d62007-04-22 06:24:45 +000022class Type;
Chris Lattner7c37b012007-04-26 04:42:16 +000023class Value;
24class BasicBlock;
Chris Lattnerc1d10d62007-04-22 06:24:45 +000025class Function;
Chris Lattner7c37b012007-04-26 04:42:16 +000026class Module;
Chris Lattnerc1d10d62007-04-22 06:24:45 +000027class TypeSymbolTable;
28class ValueSymbolTable;
Chris Lattnerc1d10d62007-04-22 06:24:45 +000029
30class ValueEnumerator {
31public:
32 // For each type, we remember its Type* and occurrence frequency.
33 typedef std::vector<std::pair<const Type*, unsigned> > TypeList;
34
35 // For each value, we remember its Value* and occurrence frequency.
36 typedef std::vector<std::pair<const Value*, unsigned> > ValueList;
37private:
Chris Lattnerc1d10d62007-04-22 06:24:45 +000038 typedef DenseMap<const Type*, unsigned> TypeMapType;
39 TypeMapType TypeMap;
Chris Lattner52523562007-04-24 00:16:04 +000040 TypeList Types;
Chris Lattnerc1d10d62007-04-22 06:24:45 +000041
Chris Lattnerc1d10d62007-04-22 06:24:45 +000042 typedef DenseMap<const Value*, unsigned> ValueMapType;
43 ValueMapType ValueMap;
Chris Lattner52523562007-04-24 00:16:04 +000044 ValueList Values;
Chris Lattnerc1d10d62007-04-22 06:24:45 +000045
Chris Lattner7c37b012007-04-26 04:42:16 +000046 /// BasicBlocks - This contains all the basic blocks for the currently
47 /// incorporated function. Their reverse mapping is stored in ValueMap.
48 std::vector<const BasicBlock*> BasicBlocks;
49
Chris Lattner5f640b92007-04-26 03:50:57 +000050 /// When a function is incorporated, this is the size of the Values list
51 /// before incorporation.
Chris Lattnere6e364c2007-04-26 05:53:54 +000052 unsigned NumModuleValues;
53 unsigned FirstFuncConstantID;
54 unsigned FirstInstID;
Chris Lattnerc1d10d62007-04-22 06:24:45 +000055
56 ValueEnumerator(const ValueEnumerator &); // DO NOT IMPLEMENT
57 void operator=(const ValueEnumerator &); // DO NOT IMPLEMENT
58public:
59 ValueEnumerator(const Module *M);
60
61 unsigned getValueID(const Value *V) const {
62 ValueMapType::const_iterator I = ValueMap.find(V);
63 assert(I != ValueMap.end() && "Value not in slotcalculator!");
Chris Lattner1e93f5b2007-04-23 21:23:41 +000064 return I->second-1;
Chris Lattnerc1d10d62007-04-22 06:24:45 +000065 }
66
67 unsigned getTypeID(const Type *T) const {
68 TypeMapType::const_iterator I = TypeMap.find(T);
69 assert(I != TypeMap.end() && "Type not in ValueEnumerator!");
70 return I->second-1;
71 }
72
Chris Lattnere6e364c2007-04-26 05:53:54 +000073 /// getFunctionConstantRange - Return the range of values that corresponds to
74 /// function-local constants.
75 void getFunctionConstantRange(unsigned &Start, unsigned &End) const {
76 Start = FirstFuncConstantID;
77 End = FirstInstID;
78 }
79
Chris Lattner52523562007-04-24 00:16:04 +000080 const ValueList &getValues() const { return Values; }
Chris Lattnerc1d10d62007-04-22 06:24:45 +000081 const TypeList &getTypes() const { return Types; }
Chris Lattner7c37b012007-04-26 04:42:16 +000082 const std::vector<const BasicBlock*> &getBasicBlocks() const {
83 return BasicBlocks;
84 }
Chris Lattnerc1d10d62007-04-22 06:24:45 +000085
Chris Lattner831d4202007-04-26 03:27:58 +000086 /// PurgeAggregateValues - If there are any aggregate values at the end of the
87 /// value list, remove them and return the count of the remaining values. If
88 /// there are none, return -1.
89 int PurgeAggregateValues();
90
Chris Lattnerc1d10d62007-04-22 06:24:45 +000091 /// incorporateFunction/purgeFunction - If you'd like to deal with a function,
92 /// use these two methods to get its data into the ValueEnumerator!
93 ///
Chris Lattner5f640b92007-04-26 03:50:57 +000094 void incorporateFunction(const Function &F);
Chris Lattnerc1d10d62007-04-22 06:24:45 +000095 void purgeFunction();
96
97private:
98 void EnumerateValue(const Value *V);
99 void EnumerateType(const Type *T);
100
101 void EnumerateTypeSymbolTable(const TypeSymbolTable &ST);
102 void EnumerateValueSymbolTable(const ValueSymbolTable &ST);
103};
104
105} // End llvm namespace
106
107#endif