blob: b6fc920e412bf11933bf9083753b8de02dc6053a [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 ValueSymbolTable;
Devang Patel0386f012010-01-07 19:39:36 +000034class MDSymbolTable;
Chris Lattnerfd57cec2007-04-22 06:24:45 +000035
36class ValueEnumerator {
37public:
Chris Lattnerdb125cf2011-07-18 04:54:35 +000038 typedef std::vector<Type*> TypeList;
Chris Lattnerfd57cec2007-04-22 06:24:45 +000039
40 // For each value, we remember its Value* and occurrence frequency.
41 typedef std::vector<std::pair<const Value*, unsigned> > ValueList;
42private:
Chris Lattnerdb125cf2011-07-18 04:54:35 +000043 typedef DenseMap<Type*, unsigned> TypeMapType;
Chris Lattnerfd57cec2007-04-22 06:24:45 +000044 TypeMapType TypeMap;
Chris Lattner2edd22b2007-04-24 00:16:04 +000045 TypeList Types;
Chris Lattnerfd57cec2007-04-22 06:24:45 +000046
Chris Lattnerfd57cec2007-04-22 06:24:45 +000047 typedef DenseMap<const Value*, unsigned> ValueMapType;
48 ValueMapType ValueMap;
Chris Lattner2edd22b2007-04-24 00:16:04 +000049 ValueList Values;
Devang Pateld5ac4042009-08-04 06:00:18 +000050 ValueList MDValues;
Devang Patel62098692010-06-02 23:05:04 +000051 SmallVector<const MDNode *, 8> FunctionLocalMDs;
Devang Pateld5ac4042009-08-04 06:00:18 +000052 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;
Dan Gohman309b3af2010-08-24 02:24:03 +000073
74 /// When a function is incorporated, this is the size of the MDValues list
75 /// before incorporation.
76 unsigned NumModuleMDValues;
77
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +000078 unsigned FirstFuncConstantID;
79 unsigned FirstInstID;
Chris Lattnerfd57cec2007-04-22 06:24:45 +000080
81 ValueEnumerator(const ValueEnumerator &); // DO NOT IMPLEMENT
82 void operator=(const ValueEnumerator &); // DO NOT IMPLEMENT
83public:
84 ValueEnumerator(const Module *M);
85
Devang Pateld5ac4042009-08-04 06:00:18 +000086 unsigned getValueID(const Value *V) const;
87
Chris Lattnerdb125cf2011-07-18 04:54:35 +000088 unsigned getTypeID(Type *T) const {
Chris Lattnerfd57cec2007-04-22 06:24:45 +000089 TypeMapType::const_iterator I = TypeMap.find(T);
90 assert(I != TypeMap.end() && "Type not in ValueEnumerator!");
91 return I->second-1;
92 }
Devang Patele8e02132009-09-18 19:26:43 +000093
94 unsigned getInstructionID(const Instruction *I) const;
95 void setInstructionID(const Instruction *I);
96
Devang Patel05988662008-09-25 21:00:45 +000097 unsigned getAttributeID(const AttrListPtr &PAL) const {
Chris Lattner58d74912008-03-12 17:45:29 +000098 if (PAL.isEmpty()) return 0; // Null maps to zero.
Devang Patel05988662008-09-25 21:00:45 +000099 AttributeMapType::const_iterator I = AttributeMap.find(PAL.getRawPointer());
100 assert(I != AttributeMap.end() && "Attribute not in ValueEnumerator!");
Chris Lattner50954f52007-05-03 22:46:43 +0000101 return I->second;
102 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000103
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000104 /// getFunctionConstantRange - Return the range of values that corresponds to
105 /// function-local constants.
106 void getFunctionConstantRange(unsigned &Start, unsigned &End) const {
107 Start = FirstFuncConstantID;
108 End = FirstInstID;
109 }
110
Chris Lattner2edd22b2007-04-24 00:16:04 +0000111 const ValueList &getValues() const { return Values; }
Devang Pateld5ac4042009-08-04 06:00:18 +0000112 const ValueList &getMDValues() const { return MDValues; }
Devang Patel62098692010-06-02 23:05:04 +0000113 const SmallVector<const MDNode *, 8> &getFunctionLocalMDValues() const {
114 return FunctionLocalMDs;
115 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000116 const TypeList &getTypes() const { return Types; }
Chris Lattnerc59c0af2007-04-26 04:42:16 +0000117 const std::vector<const BasicBlock*> &getBasicBlocks() const {
118 return BasicBlocks;
119 }
Devang Patel05988662008-09-25 21:00:45 +0000120 const std::vector<AttrListPtr> &getAttributes() const {
121 return Attributes;
Chris Lattner50954f52007-05-03 22:46:43 +0000122 }
Chris Lattner837e04a2009-10-28 05:24:40 +0000123
124 /// getGlobalBasicBlockID - This returns the function-specific ID for the
125 /// specified basic block. This is relatively expensive information, so it
126 /// should only be used by rare constructs such as address-of-label.
127 unsigned getGlobalBasicBlockID(const BasicBlock *BB) const;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000128
Chad Rosier6a0c04d2011-06-03 17:02:19 +0000129 /// incorporateFunction/purgeFunction - If you'd like to deal with a function,
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000130 /// use these two methods to get its data into the ValueEnumerator!
131 ///
Chad Rosier6a0c04d2011-06-03 17:02:19 +0000132 void incorporateFunction(const Function &F);
133 void purgeFunction();
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000134
135private:
Chris Lattner6da91d32007-05-04 05:21:47 +0000136 void OptimizeConstants(unsigned CstStart, unsigned CstEnd);
137
Dan Gohman309b3af2010-08-24 02:24:03 +0000138 void EnumerateMDNodeOperands(const MDNode *N);
Devang Patelbc5201f2010-01-22 22:52:10 +0000139 void EnumerateMetadata(const Value *MD);
Dan Gohman309b3af2010-08-24 02:24:03 +0000140 void EnumerateFunctionLocalMetadata(const MDNode *N);
Devang Patel8fba5782010-01-09 00:30:14 +0000141 void EnumerateNamedMDNode(const NamedMDNode *NMD);
Victor Hernandezd7e64572010-01-14 19:54:11 +0000142 void EnumerateValue(const Value *V);
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000143 void EnumerateType(Type *T);
Victor Hernandezd7e64572010-01-14 19:54:11 +0000144 void EnumerateOperandType(const Value *V);
Devang Patel05988662008-09-25 21:00:45 +0000145 void EnumerateAttributes(const AttrListPtr &PAL);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000146
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000147 void EnumerateValueSymbolTable(const ValueSymbolTable &ST);
Dan Gohman17aa92c2010-07-21 23:38:33 +0000148 void EnumerateNamedMetadata(const Module *M);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000149};
150
151} // End llvm namespace
152
153#endif