blob: 2d3d570354ba408987a0590fd085cfd08f860f05 [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//
Chris Lattnerf3ebc3f2007-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 Lattnerc1d10d62007-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 Pateldf84e8b2010-06-02 23:05:04 +000018#include "llvm/ADT/SmallVector.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000019#include "llvm/IR/Attributes.h"
Chris Lattnerc1d10d62007-04-22 06:24:45 +000020#include <vector>
21
22namespace llvm {
23
Chris Lattnerc1d10d62007-04-22 06:24:45 +000024class Type;
Chris Lattner7c37b012007-04-26 04:42:16 +000025class Value;
Devang Patelaf206b82009-09-18 19:26:43 +000026class Instruction;
Chris Lattner7c37b012007-04-26 04:42:16 +000027class BasicBlock;
Chris Lattnerc1d10d62007-04-22 06:24:45 +000028class Function;
Chris Lattner7c37b012007-04-26 04:42:16 +000029class Module;
Devang Pateldf84e8b2010-06-02 23:05:04 +000030class MDNode;
Devang Patel99ff5a82010-01-09 00:30:14 +000031class NamedMDNode;
Bill Wendlinge94d8432012-12-07 23:16:57 +000032class AttributeSet;
Chris Lattnerc1d10d62007-04-22 06:24:45 +000033class ValueSymbolTable;
Devang Patelfcfee0f2010-01-07 19:39:36 +000034class MDSymbolTable;
Chad Rosier78037a92011-12-07 20:44:46 +000035class raw_ostream;
Chris Lattnerc1d10d62007-04-22 06:24:45 +000036
37class ValueEnumerator {
38public:
Chris Lattner229907c2011-07-18 04:54:35 +000039 typedef std::vector<Type*> TypeList;
Chris Lattnerc1d10d62007-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 Lattner229907c2011-07-18 04:54:35 +000044 typedef DenseMap<Type*, unsigned> TypeMapType;
Chris Lattnerc1d10d62007-04-22 06:24:45 +000045 TypeMapType TypeMap;
Chris Lattner52523562007-04-24 00:16:04 +000046 TypeList Types;
Chris Lattnerc1d10d62007-04-22 06:24:45 +000047
Chris Lattnerc1d10d62007-04-22 06:24:45 +000048 typedef DenseMap<const Value*, unsigned> ValueMapType;
49 ValueMapType ValueMap;
Chris Lattner52523562007-04-24 00:16:04 +000050 ValueList Values;
Devang Patel05eb6172009-08-04 06:00:18 +000051 ValueList MDValues;
Devang Pateldf84e8b2010-06-02 23:05:04 +000052 SmallVector<const MDNode *, 8> FunctionLocalMDs;
Devang Patel05eb6172009-08-04 06:00:18 +000053 ValueMapType MDValueMap;
Joe Abbey2ad8df22012-11-25 15:23:39 +000054
Devang Patel4c758ea2008-09-25 21:00:45 +000055 typedef DenseMap<void*, unsigned> AttributeMapType;
56 AttributeMapType AttributeMap;
Bill Wendling3d7b0b82012-12-19 07:18:57 +000057 std::vector<AttributeSet> Attribute;
Joe Abbey2ad8df22012-11-25 15:23:39 +000058
Chris Lattnerf540d742009-10-28 05:24:40 +000059 /// GlobalBasicBlockIDs - This map memoizes the basic block ID's referenced by
60 /// the "getGlobalBasicBlockID" method.
61 mutable DenseMap<const BasicBlock*, unsigned> GlobalBasicBlockIDs;
Joe Abbey2ad8df22012-11-25 15:23:39 +000062
Devang Patelaf206b82009-09-18 19:26:43 +000063 typedef DenseMap<const Instruction*, unsigned> InstructionMapType;
64 InstructionMapType InstructionMap;
65 unsigned InstructionCount;
66
Chris Lattner7c37b012007-04-26 04:42:16 +000067 /// BasicBlocks - This contains all the basic blocks for the currently
68 /// incorporated function. Their reverse mapping is stored in ValueMap.
69 std::vector<const BasicBlock*> BasicBlocks;
Joe Abbey2ad8df22012-11-25 15:23:39 +000070
Chris Lattner5f640b92007-04-26 03:50:57 +000071 /// When a function is incorporated, this is the size of the Values list
72 /// before incorporation.
Chris Lattnere6e364c2007-04-26 05:53:54 +000073 unsigned NumModuleValues;
Dan Gohmanc828c542010-08-24 02:24:03 +000074
75 /// When a function is incorporated, this is the size of the MDValues list
76 /// before incorporation.
77 unsigned NumModuleMDValues;
78
Chris Lattnere6e364c2007-04-26 05:53:54 +000079 unsigned FirstFuncConstantID;
80 unsigned FirstInstID;
Craig Toppera60c0f12012-09-15 17:09:36 +000081
82 ValueEnumerator(const ValueEnumerator &) LLVM_DELETED_FUNCTION;
83 void operator=(const ValueEnumerator &) LLVM_DELETED_FUNCTION;
Chris Lattnerc1d10d62007-04-22 06:24:45 +000084public:
85 ValueEnumerator(const Module *M);
86
Chad Rosier78037a92011-12-07 20:44:46 +000087 void dump() const;
88 void print(raw_ostream &OS, const ValueMapType &Map, const char *Name) const;
89
Devang Patel05eb6172009-08-04 06:00:18 +000090 unsigned getValueID(const Value *V) const;
91
Chris Lattner229907c2011-07-18 04:54:35 +000092 unsigned getTypeID(Type *T) const {
Chris Lattnerc1d10d62007-04-22 06:24:45 +000093 TypeMapType::const_iterator I = TypeMap.find(T);
94 assert(I != TypeMap.end() && "Type not in ValueEnumerator!");
95 return I->second-1;
96 }
Devang Patelaf206b82009-09-18 19:26:43 +000097
98 unsigned getInstructionID(const Instruction *I) const;
99 void setInstructionID(const Instruction *I);
100
Bill Wendlinge94d8432012-12-07 23:16:57 +0000101 unsigned getAttributeID(const AttributeSet &PAL) const {
Chris Lattner8a923e72008-03-12 17:45:29 +0000102 if (PAL.isEmpty()) return 0; // Null maps to zero.
Devang Patel4c758ea2008-09-25 21:00:45 +0000103 AttributeMapType::const_iterator I = AttributeMap.find(PAL.getRawPointer());
104 assert(I != AttributeMap.end() && "Attribute not in ValueEnumerator!");
Chris Lattnere4bbad62007-05-03 22:46:43 +0000105 return I->second;
106 }
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000107
Chris Lattnere6e364c2007-04-26 05:53:54 +0000108 /// getFunctionConstantRange - Return the range of values that corresponds to
109 /// function-local constants.
110 void getFunctionConstantRange(unsigned &Start, unsigned &End) const {
111 Start = FirstFuncConstantID;
112 End = FirstInstID;
113 }
Joe Abbey2ad8df22012-11-25 15:23:39 +0000114
Chris Lattner52523562007-04-24 00:16:04 +0000115 const ValueList &getValues() const { return Values; }
Devang Patel05eb6172009-08-04 06:00:18 +0000116 const ValueList &getMDValues() const { return MDValues; }
Joe Abbey2ad8df22012-11-25 15:23:39 +0000117 const SmallVector<const MDNode *, 8> &getFunctionLocalMDValues() const {
Devang Pateldf84e8b2010-06-02 23:05:04 +0000118 return FunctionLocalMDs;
119 }
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000120 const TypeList &getTypes() const { return Types; }
Chris Lattner7c37b012007-04-26 04:42:16 +0000121 const std::vector<const BasicBlock*> &getBasicBlocks() const {
Joe Abbey2ad8df22012-11-25 15:23:39 +0000122 return BasicBlocks;
Chris Lattner7c37b012007-04-26 04:42:16 +0000123 }
Bill Wendlinge94d8432012-12-07 23:16:57 +0000124 const std::vector<AttributeSet> &getAttributes() const {
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000125 return Attribute;
Chris Lattnere4bbad62007-05-03 22:46:43 +0000126 }
Joe Abbey2ad8df22012-11-25 15:23:39 +0000127
Chris Lattnerf540d742009-10-28 05:24:40 +0000128 /// getGlobalBasicBlockID - This returns the function-specific ID for the
129 /// specified basic block. This is relatively expensive information, so it
130 /// should only be used by rare constructs such as address-of-label.
131 unsigned getGlobalBasicBlockID(const BasicBlock *BB) const;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000132
Chad Rosier6a11b642011-06-03 17:02:19 +0000133 /// incorporateFunction/purgeFunction - If you'd like to deal with a function,
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000134 /// use these two methods to get its data into the ValueEnumerator!
135 ///
Chad Rosier6a11b642011-06-03 17:02:19 +0000136 void incorporateFunction(const Function &F);
137 void purgeFunction();
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000138
139private:
Chris Lattner430e80d2007-05-04 05:21:47 +0000140 void OptimizeConstants(unsigned CstStart, unsigned CstEnd);
Joe Abbey2ad8df22012-11-25 15:23:39 +0000141
Dan Gohmanc828c542010-08-24 02:24:03 +0000142 void EnumerateMDNodeOperands(const MDNode *N);
Devang Patelac277eb2010-01-22 22:52:10 +0000143 void EnumerateMetadata(const Value *MD);
Dan Gohmanc828c542010-08-24 02:24:03 +0000144 void EnumerateFunctionLocalMetadata(const MDNode *N);
Devang Patel99ff5a82010-01-09 00:30:14 +0000145 void EnumerateNamedMDNode(const NamedMDNode *NMD);
Victor Hernandez572218b2010-01-14 19:54:11 +0000146 void EnumerateValue(const Value *V);
Chris Lattner229907c2011-07-18 04:54:35 +0000147 void EnumerateType(Type *T);
Victor Hernandez572218b2010-01-14 19:54:11 +0000148 void EnumerateOperandType(const Value *V);
Bill Wendlinge94d8432012-12-07 23:16:57 +0000149 void EnumerateAttributes(const AttributeSet &PAL);
Joe Abbey2ad8df22012-11-25 15:23:39 +0000150
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000151 void EnumerateValueSymbolTable(const ValueSymbolTable &ST);
Dan Gohman2637cc12010-07-21 23:38:33 +0000152 void EnumerateNamedMetadata(const Module *M);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000153};
154
155} // End llvm namespace
156
157#endif