blob: c50fe9ce76725e6b1a1a0fa94085e9a2909d38d5 [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 Patel8fba5782010-01-09 00:30:14 +000030class NamedMDNode;
Devang Patel05988662008-09-25 21:00:45 +000031class AttrListPtr;
Chris Lattnerfd57cec2007-04-22 06:24:45 +000032class TypeSymbolTable;
33class ValueSymbolTable;
Devang Patel0386f012010-01-07 19:39:36 +000034class MDSymbolTable;
Chris Lattnerfd57cec2007-04-22 06:24:45 +000035
36class ValueEnumerator {
37public:
38 // For each type, we remember its Type* and occurrence frequency.
39 typedef std::vector<std::pair<const Type*, unsigned> > TypeList;
40
41 // For each value, we remember its Value* and occurrence frequency.
42 typedef std::vector<std::pair<const Value*, unsigned> > ValueList;
43private:
Chris Lattnerfd57cec2007-04-22 06:24:45 +000044 typedef DenseMap<const Type*, unsigned> TypeMapType;
45 TypeMapType TypeMap;
Chris Lattner2edd22b2007-04-24 00:16:04 +000046 TypeList Types;
Chris Lattnerfd57cec2007-04-22 06:24:45 +000047
Chris Lattnerfd57cec2007-04-22 06:24:45 +000048 typedef DenseMap<const Value*, unsigned> ValueMapType;
49 ValueMapType ValueMap;
Chris Lattner2edd22b2007-04-24 00:16:04 +000050 ValueList Values;
Devang Pateld5ac4042009-08-04 06:00:18 +000051 ValueList MDValues;
52 ValueMapType MDValueMap;
Devang Patele8e02132009-09-18 19:26:43 +000053
Devang Patel05988662008-09-25 21:00:45 +000054 typedef DenseMap<void*, unsigned> AttributeMapType;
55 AttributeMapType AttributeMap;
56 std::vector<AttrListPtr> Attributes;
Chris Lattner50954f52007-05-03 22:46:43 +000057
Chris Lattner837e04a2009-10-28 05:24:40 +000058 /// GlobalBasicBlockIDs - This map memoizes the basic block ID's referenced by
59 /// the "getGlobalBasicBlockID" method.
60 mutable DenseMap<const BasicBlock*, unsigned> GlobalBasicBlockIDs;
61
Devang Patele8e02132009-09-18 19:26:43 +000062 typedef DenseMap<const Instruction*, unsigned> InstructionMapType;
63 InstructionMapType InstructionMap;
64 unsigned InstructionCount;
65
Chris Lattnerc59c0af2007-04-26 04:42:16 +000066 /// BasicBlocks - This contains all the basic blocks for the currently
67 /// incorporated function. Their reverse mapping is stored in ValueMap.
68 std::vector<const BasicBlock*> BasicBlocks;
69
Chris Lattner8d35c792007-04-26 03:50:57 +000070 /// When a function is incorporated, this is the size of the Values list
71 /// before incorporation.
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +000072 unsigned NumModuleValues;
73 unsigned FirstFuncConstantID;
74 unsigned FirstInstID;
Chris Lattnerfd57cec2007-04-22 06:24:45 +000075
76 ValueEnumerator(const ValueEnumerator &); // DO NOT IMPLEMENT
77 void operator=(const ValueEnumerator &); // DO NOT IMPLEMENT
78public:
79 ValueEnumerator(const Module *M);
80
Devang Pateld5ac4042009-08-04 06:00:18 +000081 unsigned getValueID(const Value *V) const;
82
Chris Lattnerfd57cec2007-04-22 06:24:45 +000083 unsigned getTypeID(const Type *T) const {
84 TypeMapType::const_iterator I = TypeMap.find(T);
85 assert(I != TypeMap.end() && "Type not in ValueEnumerator!");
86 return I->second-1;
87 }
Devang Patele8e02132009-09-18 19:26:43 +000088
89 unsigned getInstructionID(const Instruction *I) const;
90 void setInstructionID(const Instruction *I);
91
Devang Patel05988662008-09-25 21:00:45 +000092 unsigned getAttributeID(const AttrListPtr &PAL) const {
Chris Lattner58d74912008-03-12 17:45:29 +000093 if (PAL.isEmpty()) return 0; // Null maps to zero.
Devang Patel05988662008-09-25 21:00:45 +000094 AttributeMapType::const_iterator I = AttributeMap.find(PAL.getRawPointer());
95 assert(I != AttributeMap.end() && "Attribute not in ValueEnumerator!");
Chris Lattner50954f52007-05-03 22:46:43 +000096 return I->second;
97 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +000098
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +000099 /// getFunctionConstantRange - Return the range of values that corresponds to
100 /// function-local constants.
101 void getFunctionConstantRange(unsigned &Start, unsigned &End) const {
102 Start = FirstFuncConstantID;
103 End = FirstInstID;
104 }
105
Chris Lattner2edd22b2007-04-24 00:16:04 +0000106 const ValueList &getValues() const { return Values; }
Devang Pateld5ac4042009-08-04 06:00:18 +0000107 const ValueList &getMDValues() const { return MDValues; }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000108 const TypeList &getTypes() const { return Types; }
Chris Lattnerc59c0af2007-04-26 04:42:16 +0000109 const std::vector<const BasicBlock*> &getBasicBlocks() const {
110 return BasicBlocks;
111 }
Devang Patel05988662008-09-25 21:00:45 +0000112 const std::vector<AttrListPtr> &getAttributes() const {
113 return Attributes;
Chris Lattner50954f52007-05-03 22:46:43 +0000114 }
Chris Lattner837e04a2009-10-28 05:24:40 +0000115
116 /// getGlobalBasicBlockID - This returns the function-specific ID for the
117 /// specified basic block. This is relatively expensive information, so it
118 /// should only be used by rare constructs such as address-of-label.
119 unsigned getGlobalBasicBlockID(const BasicBlock *BB) const;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000120
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000121 /// incorporateFunction/purgeFunction - If you'd like to deal with a function,
122 /// use these two methods to get its data into the ValueEnumerator!
123 ///
Chris Lattner8d35c792007-04-26 03:50:57 +0000124 void incorporateFunction(const Function &F);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000125 void purgeFunction();
126
127private:
Chris Lattner6da91d32007-05-04 05:21:47 +0000128 void OptimizeConstants(unsigned CstStart, unsigned CstEnd);
129
Devang Pateld5ac4042009-08-04 06:00:18 +0000130 void EnumerateMetadata(const MetadataBase *MD);
Devang Patel8fba5782010-01-09 00:30:14 +0000131 void EnumerateNamedMDNode(const NamedMDNode *NMD);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000132 void EnumerateValue(const Value *V);
133 void EnumerateType(const Type *T);
Chris Lattner33f1d5b2007-05-06 08:35:19 +0000134 void EnumerateOperandType(const Value *V);
Devang Patel05988662008-09-25 21:00:45 +0000135 void EnumerateAttributes(const AttrListPtr &PAL);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000136
137 void EnumerateTypeSymbolTable(const TypeSymbolTable &ST);
138 void EnumerateValueSymbolTable(const ValueSymbolTable &ST);
Devang Patel0386f012010-01-07 19:39:36 +0000139 void EnumerateMDSymbolTable(const MDSymbolTable &ST);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000140};
141
142} // End llvm namespace
143
144#endif