blob: cd1d2371b701a30429bfb5f275518560616e5960 [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 Patel62098692010-06-02 23:05:04 +000018#include "llvm/ADT/SmallVector.h"
Devang Pateleaf42ab2008-09-23 23:03:40 +000019#include "llvm/Attributes.h"
Chris Lattnerfd57cec2007-04-22 06:24:45 +000020#include <vector>
21
22namespace llvm {
23
Chris Lattnerfd57cec2007-04-22 06:24:45 +000024class Type;
Chris Lattnerc59c0af2007-04-26 04:42:16 +000025class Value;
Devang Patele8e02132009-09-18 19:26:43 +000026class Instruction;
Chris Lattnerc59c0af2007-04-26 04:42:16 +000027class BasicBlock;
Chris Lattnerfd57cec2007-04-22 06:24:45 +000028class Function;
Chris Lattnerc59c0af2007-04-26 04:42:16 +000029class Module;
Devang Patel62098692010-06-02 23:05:04 +000030class MDNode;
Devang Patel8fba5782010-01-09 00:30:14 +000031class NamedMDNode;
Devang Patel05988662008-09-25 21:00:45 +000032class AttrListPtr;
Chris Lattnerfd57cec2007-04-22 06:24:45 +000033class TypeSymbolTable;
34class ValueSymbolTable;
Devang Patel0386f012010-01-07 19:39:36 +000035class MDSymbolTable;
Chris Lattnerfd57cec2007-04-22 06:24:45 +000036
37class ValueEnumerator {
38public:
39 // For each type, we remember its Type* and occurrence frequency.
40 typedef std::vector<std::pair<const Type*, unsigned> > TypeList;
41
42 // For each value, we remember its Value* and occurrence frequency.
43 typedef std::vector<std::pair<const Value*, unsigned> > ValueList;
44private:
Chris Lattnerfd57cec2007-04-22 06:24:45 +000045 typedef DenseMap<const Type*, unsigned> TypeMapType;
46 TypeMapType TypeMap;
Chris Lattner2edd22b2007-04-24 00:16:04 +000047 TypeList Types;
Chris Lattnerfd57cec2007-04-22 06:24:45 +000048
Chris Lattnerfd57cec2007-04-22 06:24:45 +000049 typedef DenseMap<const Value*, unsigned> ValueMapType;
50 ValueMapType ValueMap;
Chris Lattner2edd22b2007-04-24 00:16:04 +000051 ValueList Values;
Devang Pateld5ac4042009-08-04 06:00:18 +000052 ValueList MDValues;
Devang Patel62098692010-06-02 23:05:04 +000053 SmallVector<const MDNode *, 8> FunctionLocalMDs;
Devang Pateld5ac4042009-08-04 06:00:18 +000054 ValueMapType MDValueMap;
Devang Patele8e02132009-09-18 19:26:43 +000055
Devang Patel05988662008-09-25 21:00:45 +000056 typedef DenseMap<void*, unsigned> AttributeMapType;
57 AttributeMapType AttributeMap;
58 std::vector<AttrListPtr> Attributes;
Chris Lattner50954f52007-05-03 22:46:43 +000059
Chris Lattner837e04a2009-10-28 05:24:40 +000060 /// GlobalBasicBlockIDs - This map memoizes the basic block ID's referenced by
61 /// the "getGlobalBasicBlockID" method.
62 mutable DenseMap<const BasicBlock*, unsigned> GlobalBasicBlockIDs;
63
Devang Patele8e02132009-09-18 19:26:43 +000064 typedef DenseMap<const Instruction*, unsigned> InstructionMapType;
65 InstructionMapType InstructionMap;
66 unsigned InstructionCount;
67
Chris Lattnerc59c0af2007-04-26 04:42:16 +000068 /// BasicBlocks - This contains all the basic blocks for the currently
69 /// incorporated function. Their reverse mapping is stored in ValueMap.
70 std::vector<const BasicBlock*> BasicBlocks;
71
Chris Lattner8d35c792007-04-26 03:50:57 +000072 /// When a function is incorporated, this is the size of the Values list
73 /// before incorporation.
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +000074 unsigned NumModuleValues;
Dan Gohman309b3af2010-08-24 02:24:03 +000075
76 /// When a function is incorporated, this is the size of the MDValues list
77 /// before incorporation.
78 unsigned NumModuleMDValues;
79
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +000080 unsigned FirstFuncConstantID;
81 unsigned FirstInstID;
Chris Lattnerfd57cec2007-04-22 06:24:45 +000082
83 ValueEnumerator(const ValueEnumerator &); // DO NOT IMPLEMENT
84 void operator=(const ValueEnumerator &); // DO NOT IMPLEMENT
85public:
86 ValueEnumerator(const Module *M);
87
Devang Pateld5ac4042009-08-04 06:00:18 +000088 unsigned getValueID(const Value *V) const;
89
Chris Lattnerfd57cec2007-04-22 06:24:45 +000090 unsigned getTypeID(const Type *T) const {
91 TypeMapType::const_iterator I = TypeMap.find(T);
92 assert(I != TypeMap.end() && "Type not in ValueEnumerator!");
93 return I->second-1;
94 }
Devang Patele8e02132009-09-18 19:26:43 +000095
96 unsigned getInstructionID(const Instruction *I) const;
97 void setInstructionID(const Instruction *I);
98
Devang Patel05988662008-09-25 21:00:45 +000099 unsigned getAttributeID(const AttrListPtr &PAL) const {
Chris Lattner58d74912008-03-12 17:45:29 +0000100 if (PAL.isEmpty()) return 0; // Null maps to zero.
Devang Patel05988662008-09-25 21:00:45 +0000101 AttributeMapType::const_iterator I = AttributeMap.find(PAL.getRawPointer());
102 assert(I != AttributeMap.end() && "Attribute not in ValueEnumerator!");
Chris Lattner50954f52007-05-03 22:46:43 +0000103 return I->second;
104 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000105
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000106 /// getFunctionConstantRange - Return the range of values that corresponds to
107 /// function-local constants.
108 void getFunctionConstantRange(unsigned &Start, unsigned &End) const {
109 Start = FirstFuncConstantID;
110 End = FirstInstID;
111 }
112
Chris Lattner2edd22b2007-04-24 00:16:04 +0000113 const ValueList &getValues() const { return Values; }
Devang Pateld5ac4042009-08-04 06:00:18 +0000114 const ValueList &getMDValues() const { return MDValues; }
Devang Patel62098692010-06-02 23:05:04 +0000115 const SmallVector<const MDNode *, 8> &getFunctionLocalMDValues() const {
116 return FunctionLocalMDs;
117 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000118 const TypeList &getTypes() const { return Types; }
Chris Lattnerc59c0af2007-04-26 04:42:16 +0000119 const std::vector<const BasicBlock*> &getBasicBlocks() const {
120 return BasicBlocks;
121 }
Devang Patel05988662008-09-25 21:00:45 +0000122 const std::vector<AttrListPtr> &getAttributes() const {
123 return Attributes;
Chris Lattner50954f52007-05-03 22:46:43 +0000124 }
Chris Lattner837e04a2009-10-28 05:24:40 +0000125
126 /// getGlobalBasicBlockID - This returns the function-specific ID for the
127 /// specified basic block. This is relatively expensive information, so it
128 /// should only be used by rare constructs such as address-of-label.
129 unsigned getGlobalBasicBlockID(const BasicBlock *BB) const;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000130
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000131 /// incorporateFunction/purgeFunction - If you'd like to deal with a function,
132 /// use these two methods to get its data into the ValueEnumerator!
133 ///
Chris Lattner8d35c792007-04-26 03:50:57 +0000134 void incorporateFunction(const Function &F);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000135 void purgeFunction();
136
137private:
Chris Lattner6da91d32007-05-04 05:21:47 +0000138 void OptimizeConstants(unsigned CstStart, unsigned CstEnd);
139
Dan Gohman309b3af2010-08-24 02:24:03 +0000140 void EnumerateMDNodeOperands(const MDNode *N);
Devang Patelbc5201f2010-01-22 22:52:10 +0000141 void EnumerateMetadata(const Value *MD);
Dan Gohman309b3af2010-08-24 02:24:03 +0000142 void EnumerateFunctionLocalMetadata(const MDNode *N);
Devang Patel8fba5782010-01-09 00:30:14 +0000143 void EnumerateNamedMDNode(const NamedMDNode *NMD);
Victor Hernandezd7e64572010-01-14 19:54:11 +0000144 void EnumerateValue(const Value *V);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000145 void EnumerateType(const Type *T);
Victor Hernandezd7e64572010-01-14 19:54:11 +0000146 void EnumerateOperandType(const Value *V);
Devang Patel05988662008-09-25 21:00:45 +0000147 void EnumerateAttributes(const AttrListPtr &PAL);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000148
149 void EnumerateTypeSymbolTable(const TypeSymbolTable &ST);
150 void EnumerateValueSymbolTable(const ValueSymbolTable &ST);
Dan Gohman17aa92c2010-07-21 23:38:33 +0000151 void EnumerateNamedMetadata(const Module *M);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000152};
153
154} // End llvm namespace
155
156#endif