blob: 92d166e3ba92cc5e91699ee1a74a193b681c6e5d [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
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000014#ifndef LLVM_LIB_BITCODE_WRITER_VALUEENUMERATOR_H
15#define LLVM_LIB_BITCODE_WRITER_VALUEENUMERATOR_H
Chris Lattnerc1d10d62007-04-22 06:24:45 +000016
17#include "llvm/ADT/DenseMap.h"
Devang Pateldf84e8b2010-06-02 23:05:04 +000018#include "llvm/ADT/SmallVector.h"
David Majnemerdad0a642014-06-27 18:19:56 +000019#include "llvm/ADT/UniqueVector.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000020#include "llvm/IR/Attributes.h"
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +000021#include "llvm/IR/UseListOrder.h"
Chris Lattnerc1d10d62007-04-22 06:24:45 +000022#include <vector>
23
24namespace llvm {
25
Chris Lattnerc1d10d62007-04-22 06:24:45 +000026class Type;
Chris Lattner7c37b012007-04-26 04:42:16 +000027class Value;
Devang Patelaf206b82009-09-18 19:26:43 +000028class Instruction;
Chris Lattner7c37b012007-04-26 04:42:16 +000029class BasicBlock;
David Majnemerdad0a642014-06-27 18:19:56 +000030class Comdat;
Chris Lattnerc1d10d62007-04-22 06:24:45 +000031class Function;
Chris Lattner7c37b012007-04-26 04:42:16 +000032class Module;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000033class Metadata;
34class LocalAsMetadata;
Devang Pateldf84e8b2010-06-02 23:05:04 +000035class MDNode;
Devang Patel99ff5a82010-01-09 00:30:14 +000036class NamedMDNode;
Bill Wendlinge94d8432012-12-07 23:16:57 +000037class AttributeSet;
Chris Lattnerc1d10d62007-04-22 06:24:45 +000038class ValueSymbolTable;
Devang Patelfcfee0f2010-01-07 19:39:36 +000039class MDSymbolTable;
Chad Rosier78037a92011-12-07 20:44:46 +000040class raw_ostream;
Chris Lattnerc1d10d62007-04-22 06:24:45 +000041
Benjamin Kramer079b96e2013-09-11 18:05:11 +000042class ValueEnumerator {
Chris Lattnerc1d10d62007-04-22 06:24:45 +000043public:
Chris Lattner229907c2011-07-18 04:54:35 +000044 typedef std::vector<Type*> TypeList;
Chris Lattnerc1d10d62007-04-22 06:24:45 +000045
46 // For each value, we remember its Value* and occurrence frequency.
47 typedef std::vector<std::pair<const Value*, unsigned> > ValueList;
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +000048
49 UseListOrderStack UseListOrders;
50
Chris Lattnerc1d10d62007-04-22 06:24:45 +000051private:
Chris Lattner229907c2011-07-18 04:54:35 +000052 typedef DenseMap<Type*, unsigned> TypeMapType;
Chris Lattnerc1d10d62007-04-22 06:24:45 +000053 TypeMapType TypeMap;
Chris Lattner52523562007-04-24 00:16:04 +000054 TypeList Types;
Chris Lattnerc1d10d62007-04-22 06:24:45 +000055
Chris Lattnerc1d10d62007-04-22 06:24:45 +000056 typedef DenseMap<const Value*, unsigned> ValueMapType;
57 ValueMapType ValueMap;
Chris Lattner52523562007-04-24 00:16:04 +000058 ValueList Values;
David Majnemerdad0a642014-06-27 18:19:56 +000059
60 typedef UniqueVector<const Comdat *> ComdatSetType;
61 ComdatSetType Comdats;
62
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000063 std::vector<const Metadata *> MDs;
64 SmallVector<const LocalAsMetadata *, 8> FunctionLocalMDs;
65 typedef DenseMap<const Metadata *, unsigned> MetadataMapType;
66 MetadataMapType MDValueMap;
Duncan P. N. Exon Smith2fcf60e2015-01-12 22:30:34 +000067 bool HasMDString;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000068 bool HasDILocation;
69 bool HasGenericDINode;
Duncan P. N. Exon Smith458593a2015-04-14 23:45:11 +000070 bool ShouldPreserveUseListOrder;
Joe Abbey2ad8df22012-11-25 15:23:39 +000071
Bill Wendling92ed7002013-02-11 22:33:26 +000072 typedef DenseMap<AttributeSet, unsigned> AttributeGroupMapType;
73 AttributeGroupMapType AttributeGroupMap;
74 std::vector<AttributeSet> AttributeGroups;
Bill Wendling51f612e2013-02-10 23:06:02 +000075
Bill Wendling7b5f4f32013-02-12 08:01:22 +000076 typedef DenseMap<AttributeSet, unsigned> AttributeMapType;
Devang Patel4c758ea2008-09-25 21:00:45 +000077 AttributeMapType AttributeMap;
Bill Wendling3d7b0b82012-12-19 07:18:57 +000078 std::vector<AttributeSet> Attribute;
Joe Abbey2ad8df22012-11-25 15:23:39 +000079
Chris Lattnerf540d742009-10-28 05:24:40 +000080 /// GlobalBasicBlockIDs - This map memoizes the basic block ID's referenced by
81 /// the "getGlobalBasicBlockID" method.
82 mutable DenseMap<const BasicBlock*, unsigned> GlobalBasicBlockIDs;
Joe Abbey2ad8df22012-11-25 15:23:39 +000083
Devang Patelaf206b82009-09-18 19:26:43 +000084 typedef DenseMap<const Instruction*, unsigned> InstructionMapType;
85 InstructionMapType InstructionMap;
86 unsigned InstructionCount;
87
Chris Lattner7c37b012007-04-26 04:42:16 +000088 /// BasicBlocks - This contains all the basic blocks for the currently
89 /// incorporated function. Their reverse mapping is stored in ValueMap.
90 std::vector<const BasicBlock*> BasicBlocks;
Joe Abbey2ad8df22012-11-25 15:23:39 +000091
Chris Lattner5f640b92007-04-26 03:50:57 +000092 /// When a function is incorporated, this is the size of the Values list
93 /// before incorporation.
Chris Lattnere6e364c2007-04-26 05:53:54 +000094 unsigned NumModuleValues;
Dan Gohmanc828c542010-08-24 02:24:03 +000095
96 /// When a function is incorporated, this is the size of the MDValues list
97 /// before incorporation.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000098 unsigned NumModuleMDs;
Dan Gohmanc828c542010-08-24 02:24:03 +000099
Chris Lattnere6e364c2007-04-26 05:53:54 +0000100 unsigned FirstFuncConstantID;
101 unsigned FirstInstID;
Craig Toppera60c0f12012-09-15 17:09:36 +0000102
Aaron Ballmanf9a18972015-02-15 22:54:22 +0000103 ValueEnumerator(const ValueEnumerator &) = delete;
104 void operator=(const ValueEnumerator &) = delete;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000105public:
Duncan P. N. Exon Smith458593a2015-04-14 23:45:11 +0000106 ValueEnumerator(const Module &M, bool ShouldPreserveUseListOrder);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000107
Chad Rosier78037a92011-12-07 20:44:46 +0000108 void dump() const;
109 void print(raw_ostream &OS, const ValueMapType &Map, const char *Name) const;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000110 void print(raw_ostream &OS, const MetadataMapType &Map,
111 const char *Name) const;
Chad Rosier78037a92011-12-07 20:44:46 +0000112
Devang Patel05eb6172009-08-04 06:00:18 +0000113 unsigned getValueID(const Value *V) const;
Duncan P. N. Exon Smith9a6f64e2015-01-20 01:00:23 +0000114 unsigned getMetadataID(const Metadata *MD) const {
115 auto ID = getMetadataOrNullID(MD);
116 assert(ID != 0 && "Metadata not in slotcalculator!");
117 return ID - 1;
118 }
119 unsigned getMetadataOrNullID(const Metadata *MD) const {
120 return MDValueMap.lookup(MD);
121 }
Devang Patel05eb6172009-08-04 06:00:18 +0000122
Duncan P. N. Exon Smith2fcf60e2015-01-12 22:30:34 +0000123 bool hasMDString() const { return HasMDString; }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000124 bool hasDILocation() const { return HasDILocation; }
125 bool hasGenericDINode() const { return HasGenericDINode; }
Duncan P. N. Exon Smith2fcf60e2015-01-12 22:30:34 +0000126
Duncan P. N. Exon Smith458593a2015-04-14 23:45:11 +0000127 bool shouldPreserveUseListOrder() const { return ShouldPreserveUseListOrder; }
128
Chris Lattner229907c2011-07-18 04:54:35 +0000129 unsigned getTypeID(Type *T) const {
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000130 TypeMapType::const_iterator I = TypeMap.find(T);
131 assert(I != TypeMap.end() && "Type not in ValueEnumerator!");
132 return I->second-1;
133 }
Devang Patelaf206b82009-09-18 19:26:43 +0000134
135 unsigned getInstructionID(const Instruction *I) const;
136 void setInstructionID(const Instruction *I);
137
Bill Wendling92ed7002013-02-11 22:33:26 +0000138 unsigned getAttributeID(AttributeSet PAL) const {
Chris Lattner8a923e72008-03-12 17:45:29 +0000139 if (PAL.isEmpty()) return 0; // Null maps to zero.
Bill Wendling7b5f4f32013-02-12 08:01:22 +0000140 AttributeMapType::const_iterator I = AttributeMap.find(PAL);
Devang Patel4c758ea2008-09-25 21:00:45 +0000141 assert(I != AttributeMap.end() && "Attribute not in ValueEnumerator!");
Chris Lattnere4bbad62007-05-03 22:46:43 +0000142 return I->second;
143 }
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000144
Bill Wendling92ed7002013-02-11 22:33:26 +0000145 unsigned getAttributeGroupID(AttributeSet PAL) const {
Bill Wendling51f612e2013-02-10 23:06:02 +0000146 if (PAL.isEmpty()) return 0; // Null maps to zero.
Bill Wendling92ed7002013-02-11 22:33:26 +0000147 AttributeGroupMapType::const_iterator I = AttributeGroupMap.find(PAL);
148 assert(I != AttributeGroupMap.end() && "Attribute not in ValueEnumerator!");
Bill Wendling51f612e2013-02-10 23:06:02 +0000149 return I->second;
150 }
151
Chris Lattnere6e364c2007-04-26 05:53:54 +0000152 /// getFunctionConstantRange - Return the range of values that corresponds to
153 /// function-local constants.
154 void getFunctionConstantRange(unsigned &Start, unsigned &End) const {
155 Start = FirstFuncConstantID;
156 End = FirstInstID;
157 }
Joe Abbey2ad8df22012-11-25 15:23:39 +0000158
Chris Lattner52523562007-04-24 00:16:04 +0000159 const ValueList &getValues() const { return Values; }
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000160 const std::vector<const Metadata *> &getMDs() const { return MDs; }
161 const SmallVectorImpl<const LocalAsMetadata *> &getFunctionLocalMDs() const {
Devang Pateldf84e8b2010-06-02 23:05:04 +0000162 return FunctionLocalMDs;
163 }
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000164 const TypeList &getTypes() const { return Types; }
Chris Lattner7c37b012007-04-26 04:42:16 +0000165 const std::vector<const BasicBlock*> &getBasicBlocks() const {
Joe Abbey2ad8df22012-11-25 15:23:39 +0000166 return BasicBlocks;
Chris Lattner7c37b012007-04-26 04:42:16 +0000167 }
Bill Wendlinge94d8432012-12-07 23:16:57 +0000168 const std::vector<AttributeSet> &getAttributes() const {
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000169 return Attribute;
Chris Lattnere4bbad62007-05-03 22:46:43 +0000170 }
Bill Wendling92ed7002013-02-11 22:33:26 +0000171 const std::vector<AttributeSet> &getAttributeGroups() const {
172 return AttributeGroups;
Bill Wendling51f612e2013-02-10 23:06:02 +0000173 }
Joe Abbey2ad8df22012-11-25 15:23:39 +0000174
David Majnemerdad0a642014-06-27 18:19:56 +0000175 const ComdatSetType &getComdats() const { return Comdats; }
176 unsigned getComdatID(const Comdat *C) const;
177
Chris Lattnerf540d742009-10-28 05:24:40 +0000178 /// getGlobalBasicBlockID - This returns the function-specific ID for the
179 /// specified basic block. This is relatively expensive information, so it
180 /// should only be used by rare constructs such as address-of-label.
181 unsigned getGlobalBasicBlockID(const BasicBlock *BB) const;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000182
Chad Rosier6a11b642011-06-03 17:02:19 +0000183 /// incorporateFunction/purgeFunction - If you'd like to deal with a function,
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000184 /// use these two methods to get its data into the ValueEnumerator!
185 ///
Chad Rosier6a11b642011-06-03 17:02:19 +0000186 void incorporateFunction(const Function &F);
187 void purgeFunction();
David Blaikie7b028102015-02-25 00:51:52 +0000188 uint64_t computeBitsRequiredForTypeIndicies() const;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000189
190private:
Chris Lattner430e80d2007-05-04 05:21:47 +0000191 void OptimizeConstants(unsigned CstStart, unsigned CstEnd);
Joe Abbey2ad8df22012-11-25 15:23:39 +0000192
Dan Gohmanc828c542010-08-24 02:24:03 +0000193 void EnumerateMDNodeOperands(const MDNode *N);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000194 void EnumerateMetadata(const Metadata *MD);
195 void EnumerateFunctionLocalMetadata(const LocalAsMetadata *Local);
Devang Patel99ff5a82010-01-09 00:30:14 +0000196 void EnumerateNamedMDNode(const NamedMDNode *NMD);
Victor Hernandez572218b2010-01-14 19:54:11 +0000197 void EnumerateValue(const Value *V);
Chris Lattner229907c2011-07-18 04:54:35 +0000198 void EnumerateType(Type *T);
Victor Hernandez572218b2010-01-14 19:54:11 +0000199 void EnumerateOperandType(const Value *V);
Bill Wendling7b5f4f32013-02-12 08:01:22 +0000200 void EnumerateAttributes(AttributeSet PAL);
Joe Abbey2ad8df22012-11-25 15:23:39 +0000201
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000202 void EnumerateValueSymbolTable(const ValueSymbolTable &ST);
Rafael Espindola49e9bf82014-11-17 20:06:27 +0000203 void EnumerateNamedMetadata(const Module &M);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000204};
205
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000206} // End llvm namespace
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000207
208#endif