blob: 0af6164c944f83b0f20ea0e5595392b0cd9a8e6b [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"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000019#include "llvm/IR/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;
Bill Wendling99faa3b2012-12-07 23:16:57 +000032class AttributeSet;
Chris Lattnerfd57cec2007-04-22 06:24:45 +000033class ValueSymbolTable;
Devang Patel0386f012010-01-07 19:39:36 +000034class MDSymbolTable;
Chad Rosier4e6c03f2011-12-07 20:44:46 +000035class raw_ostream;
Chris Lattnerfd57cec2007-04-22 06:24:45 +000036
37class ValueEnumerator {
38public:
Chris Lattnerdb125cf2011-07-18 04:54:35 +000039 typedef std::vector<Type*> TypeList;
Chris Lattnerfd57cec2007-04-22 06:24:45 +000040
41 // For each value, we remember its Value* and occurrence frequency.
42 typedef std::vector<std::pair<const Value*, unsigned> > ValueList;
43private:
Chris Lattnerdb125cf2011-07-18 04:54:35 +000044 typedef DenseMap<Type*, unsigned> TypeMapType;
Chris Lattnerfd57cec2007-04-22 06:24:45 +000045 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;
Devang Patel62098692010-06-02 23:05:04 +000052 SmallVector<const MDNode *, 8> FunctionLocalMDs;
Devang Pateld5ac4042009-08-04 06:00:18 +000053 ValueMapType MDValueMap;
Joe Abbey170a15e2012-11-25 15:23:39 +000054
Bill Wendlinge9229a62013-02-11 22:33:26 +000055 typedef DenseMap<AttributeSet, unsigned> AttributeGroupMapType;
56 AttributeGroupMapType AttributeGroupMap;
57 std::vector<AttributeSet> AttributeGroups;
Bill Wendling8c2e77f2013-02-10 23:06:02 +000058
Bill Wendling105ea3d2013-02-12 08:01:22 +000059 typedef DenseMap<AttributeSet, unsigned> AttributeMapType;
Devang Patel05988662008-09-25 21:00:45 +000060 AttributeMapType AttributeMap;
Bill Wendling034b94b2012-12-19 07:18:57 +000061 std::vector<AttributeSet> Attribute;
Joe Abbey170a15e2012-11-25 15:23:39 +000062
Chris Lattner837e04a2009-10-28 05:24:40 +000063 /// GlobalBasicBlockIDs - This map memoizes the basic block ID's referenced by
64 /// the "getGlobalBasicBlockID" method.
65 mutable DenseMap<const BasicBlock*, unsigned> GlobalBasicBlockIDs;
Joe Abbey170a15e2012-11-25 15:23:39 +000066
Devang Patele8e02132009-09-18 19:26:43 +000067 typedef DenseMap<const Instruction*, unsigned> InstructionMapType;
68 InstructionMapType InstructionMap;
69 unsigned InstructionCount;
70
Chris Lattnerc59c0af2007-04-26 04:42:16 +000071 /// BasicBlocks - This contains all the basic blocks for the currently
72 /// incorporated function. Their reverse mapping is stored in ValueMap.
73 std::vector<const BasicBlock*> BasicBlocks;
Joe Abbey170a15e2012-11-25 15:23:39 +000074
Chris Lattner8d35c792007-04-26 03:50:57 +000075 /// When a function is incorporated, this is the size of the Values list
76 /// before incorporation.
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +000077 unsigned NumModuleValues;
Dan Gohman309b3af2010-08-24 02:24:03 +000078
79 /// When a function is incorporated, this is the size of the MDValues list
80 /// before incorporation.
81 unsigned NumModuleMDValues;
82
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +000083 unsigned FirstFuncConstantID;
84 unsigned FirstInstID;
Craig Topper86a1c322012-09-15 17:09:36 +000085
86 ValueEnumerator(const ValueEnumerator &) LLVM_DELETED_FUNCTION;
87 void operator=(const ValueEnumerator &) LLVM_DELETED_FUNCTION;
Chris Lattnerfd57cec2007-04-22 06:24:45 +000088public:
89 ValueEnumerator(const Module *M);
90
Chad Rosier4e6c03f2011-12-07 20:44:46 +000091 void dump() const;
92 void print(raw_ostream &OS, const ValueMapType &Map, const char *Name) const;
93
Devang Pateld5ac4042009-08-04 06:00:18 +000094 unsigned getValueID(const Value *V) const;
95
Chris Lattnerdb125cf2011-07-18 04:54:35 +000096 unsigned getTypeID(Type *T) const {
Chris Lattnerfd57cec2007-04-22 06:24:45 +000097 TypeMapType::const_iterator I = TypeMap.find(T);
98 assert(I != TypeMap.end() && "Type not in ValueEnumerator!");
99 return I->second-1;
100 }
Devang Patele8e02132009-09-18 19:26:43 +0000101
102 unsigned getInstructionID(const Instruction *I) const;
103 void setInstructionID(const Instruction *I);
104
Bill Wendlinge9229a62013-02-11 22:33:26 +0000105 unsigned getAttributeID(AttributeSet PAL) const {
Chris Lattner58d74912008-03-12 17:45:29 +0000106 if (PAL.isEmpty()) return 0; // Null maps to zero.
Bill Wendling105ea3d2013-02-12 08:01:22 +0000107 AttributeMapType::const_iterator I = AttributeMap.find(PAL);
Devang Patel05988662008-09-25 21:00:45 +0000108 assert(I != AttributeMap.end() && "Attribute not in ValueEnumerator!");
Chris Lattner50954f52007-05-03 22:46:43 +0000109 return I->second;
110 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000111
Bill Wendlinge9229a62013-02-11 22:33:26 +0000112 unsigned getAttributeGroupID(AttributeSet PAL) const {
Bill Wendling8c2e77f2013-02-10 23:06:02 +0000113 if (PAL.isEmpty()) return 0; // Null maps to zero.
Bill Wendlinge9229a62013-02-11 22:33:26 +0000114 AttributeGroupMapType::const_iterator I = AttributeGroupMap.find(PAL);
115 assert(I != AttributeGroupMap.end() && "Attribute not in ValueEnumerator!");
Bill Wendling8c2e77f2013-02-10 23:06:02 +0000116 return I->second;
117 }
118
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000119 /// getFunctionConstantRange - Return the range of values that corresponds to
120 /// function-local constants.
121 void getFunctionConstantRange(unsigned &Start, unsigned &End) const {
122 Start = FirstFuncConstantID;
123 End = FirstInstID;
124 }
Joe Abbey170a15e2012-11-25 15:23:39 +0000125
Chris Lattner2edd22b2007-04-24 00:16:04 +0000126 const ValueList &getValues() const { return Values; }
Devang Pateld5ac4042009-08-04 06:00:18 +0000127 const ValueList &getMDValues() const { return MDValues; }
Joe Abbey170a15e2012-11-25 15:23:39 +0000128 const SmallVector<const MDNode *, 8> &getFunctionLocalMDValues() const {
Devang Patel62098692010-06-02 23:05:04 +0000129 return FunctionLocalMDs;
130 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000131 const TypeList &getTypes() const { return Types; }
Chris Lattnerc59c0af2007-04-26 04:42:16 +0000132 const std::vector<const BasicBlock*> &getBasicBlocks() const {
Joe Abbey170a15e2012-11-25 15:23:39 +0000133 return BasicBlocks;
Chris Lattnerc59c0af2007-04-26 04:42:16 +0000134 }
Bill Wendling99faa3b2012-12-07 23:16:57 +0000135 const std::vector<AttributeSet> &getAttributes() const {
Bill Wendling034b94b2012-12-19 07:18:57 +0000136 return Attribute;
Chris Lattner50954f52007-05-03 22:46:43 +0000137 }
Bill Wendlinge9229a62013-02-11 22:33:26 +0000138 const std::vector<AttributeSet> &getAttributeGroups() const {
139 return AttributeGroups;
Bill Wendling8c2e77f2013-02-10 23:06:02 +0000140 }
Joe Abbey170a15e2012-11-25 15:23:39 +0000141
Chris Lattner837e04a2009-10-28 05:24:40 +0000142 /// getGlobalBasicBlockID - This returns the function-specific ID for the
143 /// specified basic block. This is relatively expensive information, so it
144 /// should only be used by rare constructs such as address-of-label.
145 unsigned getGlobalBasicBlockID(const BasicBlock *BB) const;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000146
Chad Rosier6a0c04d2011-06-03 17:02:19 +0000147 /// incorporateFunction/purgeFunction - If you'd like to deal with a function,
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000148 /// use these two methods to get its data into the ValueEnumerator!
149 ///
Chad Rosier6a0c04d2011-06-03 17:02:19 +0000150 void incorporateFunction(const Function &F);
151 void purgeFunction();
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000152
153private:
Chris Lattner6da91d32007-05-04 05:21:47 +0000154 void OptimizeConstants(unsigned CstStart, unsigned CstEnd);
Joe Abbey170a15e2012-11-25 15:23:39 +0000155
Dan Gohman309b3af2010-08-24 02:24:03 +0000156 void EnumerateMDNodeOperands(const MDNode *N);
Devang Patelbc5201f2010-01-22 22:52:10 +0000157 void EnumerateMetadata(const Value *MD);
Dan Gohman309b3af2010-08-24 02:24:03 +0000158 void EnumerateFunctionLocalMetadata(const MDNode *N);
Devang Patel8fba5782010-01-09 00:30:14 +0000159 void EnumerateNamedMDNode(const NamedMDNode *NMD);
Victor Hernandezd7e64572010-01-14 19:54:11 +0000160 void EnumerateValue(const Value *V);
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000161 void EnumerateType(Type *T);
Victor Hernandezd7e64572010-01-14 19:54:11 +0000162 void EnumerateOperandType(const Value *V);
Bill Wendling105ea3d2013-02-12 08:01:22 +0000163 void EnumerateAttributes(AttributeSet PAL);
Joe Abbey170a15e2012-11-25 15:23:39 +0000164
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000165 void EnumerateValueSymbolTable(const ValueSymbolTable &ST);
Dan Gohman17aa92c2010-07-21 23:38:33 +0000166 void EnumerateNamedMetadata(const Module *M);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000167};
168
169} // End llvm namespace
170
171#endif