Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 1 | //===-- Bitcode/Writer/ValueEnumerator.h - Number values --------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This class gives values and types Unique ID's. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Benjamin Kramer | a7c40ef | 2014-08-13 16:26:38 +0000 | [diff] [blame] | 14 | #ifndef LLVM_LIB_BITCODE_WRITER_VALUEENUMERATOR_H |
| 15 | #define LLVM_LIB_BITCODE_WRITER_VALUEENUMERATOR_H |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 16 | |
| 17 | #include "llvm/ADT/DenseMap.h" |
Devang Patel | df84e8b | 2010-06-02 23:05:04 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallVector.h" |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/UniqueVector.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 20 | #include "llvm/IR/Attributes.h" |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 21 | #include "llvm/IR/UseListOrder.h" |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 22 | #include <vector> |
| 23 | |
| 24 | namespace llvm { |
| 25 | |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 26 | class Type; |
Chris Lattner | 7c37b01 | 2007-04-26 04:42:16 +0000 | [diff] [blame] | 27 | class Value; |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 28 | class Instruction; |
Chris Lattner | 7c37b01 | 2007-04-26 04:42:16 +0000 | [diff] [blame] | 29 | class BasicBlock; |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 30 | class Comdat; |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 31 | class Function; |
Chris Lattner | 7c37b01 | 2007-04-26 04:42:16 +0000 | [diff] [blame] | 32 | class Module; |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 33 | class Metadata; |
| 34 | class LocalAsMetadata; |
Devang Patel | df84e8b | 2010-06-02 23:05:04 +0000 | [diff] [blame] | 35 | class MDNode; |
Devang Patel | 99ff5a8 | 2010-01-09 00:30:14 +0000 | [diff] [blame] | 36 | class NamedMDNode; |
Bill Wendling | e94d843 | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 37 | class AttributeSet; |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 38 | class ValueSymbolTable; |
Devang Patel | fcfee0f | 2010-01-07 19:39:36 +0000 | [diff] [blame] | 39 | class MDSymbolTable; |
Chad Rosier | 78037a9 | 2011-12-07 20:44:46 +0000 | [diff] [blame] | 40 | class raw_ostream; |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 41 | |
Benjamin Kramer | 079b96e | 2013-09-11 18:05:11 +0000 | [diff] [blame] | 42 | class ValueEnumerator { |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 43 | public: |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 44 | typedef std::vector<Type*> TypeList; |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 45 | |
| 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 Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 48 | |
| 49 | UseListOrderStack UseListOrders; |
| 50 | |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 51 | private: |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 52 | typedef DenseMap<Type*, unsigned> TypeMapType; |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 53 | TypeMapType TypeMap; |
Chris Lattner | 5252356 | 2007-04-24 00:16:04 +0000 | [diff] [blame] | 54 | TypeList Types; |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 55 | |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 56 | typedef DenseMap<const Value*, unsigned> ValueMapType; |
| 57 | ValueMapType ValueMap; |
Chris Lattner | 5252356 | 2007-04-24 00:16:04 +0000 | [diff] [blame] | 58 | ValueList Values; |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 59 | |
| 60 | typedef UniqueVector<const Comdat *> ComdatSetType; |
| 61 | ComdatSetType Comdats; |
| 62 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 63 | 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 Smith | 2fcf60e | 2015-01-12 22:30:34 +0000 | [diff] [blame] | 67 | bool HasMDString; |
Duncan P. N. Exon Smith | 6a48483 | 2015-01-13 21:10:44 +0000 | [diff] [blame] | 68 | bool HasMDLocation; |
Duncan P. N. Exon Smith | 4e4aa70 | 2015-02-03 21:54:14 +0000 | [diff] [blame] | 69 | bool HasGenericDebugNode; |
Joe Abbey | 2ad8df2 | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 70 | |
Bill Wendling | 92ed700 | 2013-02-11 22:33:26 +0000 | [diff] [blame] | 71 | typedef DenseMap<AttributeSet, unsigned> AttributeGroupMapType; |
| 72 | AttributeGroupMapType AttributeGroupMap; |
| 73 | std::vector<AttributeSet> AttributeGroups; |
Bill Wendling | 51f612e | 2013-02-10 23:06:02 +0000 | [diff] [blame] | 74 | |
Bill Wendling | 7b5f4f3 | 2013-02-12 08:01:22 +0000 | [diff] [blame] | 75 | typedef DenseMap<AttributeSet, unsigned> AttributeMapType; |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 76 | AttributeMapType AttributeMap; |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 77 | std::vector<AttributeSet> Attribute; |
Joe Abbey | 2ad8df2 | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 78 | |
Chris Lattner | f540d74 | 2009-10-28 05:24:40 +0000 | [diff] [blame] | 79 | /// GlobalBasicBlockIDs - This map memoizes the basic block ID's referenced by |
| 80 | /// the "getGlobalBasicBlockID" method. |
| 81 | mutable DenseMap<const BasicBlock*, unsigned> GlobalBasicBlockIDs; |
Joe Abbey | 2ad8df2 | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 82 | |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 83 | typedef DenseMap<const Instruction*, unsigned> InstructionMapType; |
| 84 | InstructionMapType InstructionMap; |
| 85 | unsigned InstructionCount; |
| 86 | |
Chris Lattner | 7c37b01 | 2007-04-26 04:42:16 +0000 | [diff] [blame] | 87 | /// BasicBlocks - This contains all the basic blocks for the currently |
| 88 | /// incorporated function. Their reverse mapping is stored in ValueMap. |
| 89 | std::vector<const BasicBlock*> BasicBlocks; |
Joe Abbey | 2ad8df2 | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 90 | |
Chris Lattner | 5f640b9 | 2007-04-26 03:50:57 +0000 | [diff] [blame] | 91 | /// When a function is incorporated, this is the size of the Values list |
| 92 | /// before incorporation. |
Chris Lattner | e6e364c | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 93 | unsigned NumModuleValues; |
Dan Gohman | c828c54 | 2010-08-24 02:24:03 +0000 | [diff] [blame] | 94 | |
| 95 | /// When a function is incorporated, this is the size of the MDValues list |
| 96 | /// before incorporation. |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 97 | unsigned NumModuleMDs; |
Dan Gohman | c828c54 | 2010-08-24 02:24:03 +0000 | [diff] [blame] | 98 | |
Chris Lattner | e6e364c | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 99 | unsigned FirstFuncConstantID; |
| 100 | unsigned FirstInstID; |
Craig Topper | a60c0f1 | 2012-09-15 17:09:36 +0000 | [diff] [blame] | 101 | |
Aaron Ballman | f9a1897 | 2015-02-15 22:54:22 +0000 | [diff] [blame^] | 102 | ValueEnumerator(const ValueEnumerator &) = delete; |
| 103 | void operator=(const ValueEnumerator &) = delete; |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 104 | public: |
Rafael Espindola | 49e9bf8 | 2014-11-17 20:06:27 +0000 | [diff] [blame] | 105 | ValueEnumerator(const Module &M); |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 106 | |
Chad Rosier | 78037a9 | 2011-12-07 20:44:46 +0000 | [diff] [blame] | 107 | void dump() const; |
| 108 | void print(raw_ostream &OS, const ValueMapType &Map, const char *Name) const; |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 109 | void print(raw_ostream &OS, const MetadataMapType &Map, |
| 110 | const char *Name) const; |
Chad Rosier | 78037a9 | 2011-12-07 20:44:46 +0000 | [diff] [blame] | 111 | |
Devang Patel | 05eb617 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 112 | unsigned getValueID(const Value *V) const; |
Duncan P. N. Exon Smith | 9a6f64e | 2015-01-20 01:00:23 +0000 | [diff] [blame] | 113 | unsigned getMetadataID(const Metadata *MD) const { |
| 114 | auto ID = getMetadataOrNullID(MD); |
| 115 | assert(ID != 0 && "Metadata not in slotcalculator!"); |
| 116 | return ID - 1; |
| 117 | } |
| 118 | unsigned getMetadataOrNullID(const Metadata *MD) const { |
| 119 | return MDValueMap.lookup(MD); |
| 120 | } |
Devang Patel | 05eb617 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 121 | |
Duncan P. N. Exon Smith | 2fcf60e | 2015-01-12 22:30:34 +0000 | [diff] [blame] | 122 | bool hasMDString() const { return HasMDString; } |
Duncan P. N. Exon Smith | 6a48483 | 2015-01-13 21:10:44 +0000 | [diff] [blame] | 123 | bool hasMDLocation() const { return HasMDLocation; } |
Duncan P. N. Exon Smith | 4e4aa70 | 2015-02-03 21:54:14 +0000 | [diff] [blame] | 124 | bool hasGenericDebugNode() const { return HasGenericDebugNode; } |
Duncan P. N. Exon Smith | 2fcf60e | 2015-01-12 22:30:34 +0000 | [diff] [blame] | 125 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 126 | unsigned getTypeID(Type *T) const { |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 127 | TypeMapType::const_iterator I = TypeMap.find(T); |
| 128 | assert(I != TypeMap.end() && "Type not in ValueEnumerator!"); |
| 129 | return I->second-1; |
| 130 | } |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 131 | |
| 132 | unsigned getInstructionID(const Instruction *I) const; |
| 133 | void setInstructionID(const Instruction *I); |
| 134 | |
Bill Wendling | 92ed700 | 2013-02-11 22:33:26 +0000 | [diff] [blame] | 135 | unsigned getAttributeID(AttributeSet PAL) const { |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 136 | if (PAL.isEmpty()) return 0; // Null maps to zero. |
Bill Wendling | 7b5f4f3 | 2013-02-12 08:01:22 +0000 | [diff] [blame] | 137 | AttributeMapType::const_iterator I = AttributeMap.find(PAL); |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 138 | assert(I != AttributeMap.end() && "Attribute not in ValueEnumerator!"); |
Chris Lattner | e4bbad6 | 2007-05-03 22:46:43 +0000 | [diff] [blame] | 139 | return I->second; |
| 140 | } |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 141 | |
Bill Wendling | 92ed700 | 2013-02-11 22:33:26 +0000 | [diff] [blame] | 142 | unsigned getAttributeGroupID(AttributeSet PAL) const { |
Bill Wendling | 51f612e | 2013-02-10 23:06:02 +0000 | [diff] [blame] | 143 | if (PAL.isEmpty()) return 0; // Null maps to zero. |
Bill Wendling | 92ed700 | 2013-02-11 22:33:26 +0000 | [diff] [blame] | 144 | AttributeGroupMapType::const_iterator I = AttributeGroupMap.find(PAL); |
| 145 | assert(I != AttributeGroupMap.end() && "Attribute not in ValueEnumerator!"); |
Bill Wendling | 51f612e | 2013-02-10 23:06:02 +0000 | [diff] [blame] | 146 | return I->second; |
| 147 | } |
| 148 | |
Chris Lattner | e6e364c | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 149 | /// getFunctionConstantRange - Return the range of values that corresponds to |
| 150 | /// function-local constants. |
| 151 | void getFunctionConstantRange(unsigned &Start, unsigned &End) const { |
| 152 | Start = FirstFuncConstantID; |
| 153 | End = FirstInstID; |
| 154 | } |
Joe Abbey | 2ad8df2 | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 155 | |
Chris Lattner | 5252356 | 2007-04-24 00:16:04 +0000 | [diff] [blame] | 156 | const ValueList &getValues() const { return Values; } |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 157 | const std::vector<const Metadata *> &getMDs() const { return MDs; } |
| 158 | const SmallVectorImpl<const LocalAsMetadata *> &getFunctionLocalMDs() const { |
Devang Patel | df84e8b | 2010-06-02 23:05:04 +0000 | [diff] [blame] | 159 | return FunctionLocalMDs; |
| 160 | } |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 161 | const TypeList &getTypes() const { return Types; } |
Chris Lattner | 7c37b01 | 2007-04-26 04:42:16 +0000 | [diff] [blame] | 162 | const std::vector<const BasicBlock*> &getBasicBlocks() const { |
Joe Abbey | 2ad8df2 | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 163 | return BasicBlocks; |
Chris Lattner | 7c37b01 | 2007-04-26 04:42:16 +0000 | [diff] [blame] | 164 | } |
Bill Wendling | e94d843 | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 165 | const std::vector<AttributeSet> &getAttributes() const { |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 166 | return Attribute; |
Chris Lattner | e4bbad6 | 2007-05-03 22:46:43 +0000 | [diff] [blame] | 167 | } |
Bill Wendling | 92ed700 | 2013-02-11 22:33:26 +0000 | [diff] [blame] | 168 | const std::vector<AttributeSet> &getAttributeGroups() const { |
| 169 | return AttributeGroups; |
Bill Wendling | 51f612e | 2013-02-10 23:06:02 +0000 | [diff] [blame] | 170 | } |
Joe Abbey | 2ad8df2 | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 171 | |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 172 | const ComdatSetType &getComdats() const { return Comdats; } |
| 173 | unsigned getComdatID(const Comdat *C) const; |
| 174 | |
Chris Lattner | f540d74 | 2009-10-28 05:24:40 +0000 | [diff] [blame] | 175 | /// getGlobalBasicBlockID - This returns the function-specific ID for the |
| 176 | /// specified basic block. This is relatively expensive information, so it |
| 177 | /// should only be used by rare constructs such as address-of-label. |
| 178 | unsigned getGlobalBasicBlockID(const BasicBlock *BB) const; |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 179 | |
Chad Rosier | 6a11b64 | 2011-06-03 17:02:19 +0000 | [diff] [blame] | 180 | /// incorporateFunction/purgeFunction - If you'd like to deal with a function, |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 181 | /// use these two methods to get its data into the ValueEnumerator! |
| 182 | /// |
Chad Rosier | 6a11b64 | 2011-06-03 17:02:19 +0000 | [diff] [blame] | 183 | void incorporateFunction(const Function &F); |
| 184 | void purgeFunction(); |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 185 | |
| 186 | private: |
Chris Lattner | 430e80d | 2007-05-04 05:21:47 +0000 | [diff] [blame] | 187 | void OptimizeConstants(unsigned CstStart, unsigned CstEnd); |
Joe Abbey | 2ad8df2 | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 188 | |
Dan Gohman | c828c54 | 2010-08-24 02:24:03 +0000 | [diff] [blame] | 189 | void EnumerateMDNodeOperands(const MDNode *N); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 190 | void EnumerateMetadata(const Metadata *MD); |
| 191 | void EnumerateFunctionLocalMetadata(const LocalAsMetadata *Local); |
Devang Patel | 99ff5a8 | 2010-01-09 00:30:14 +0000 | [diff] [blame] | 192 | void EnumerateNamedMDNode(const NamedMDNode *NMD); |
Victor Hernandez | 572218b | 2010-01-14 19:54:11 +0000 | [diff] [blame] | 193 | void EnumerateValue(const Value *V); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 194 | void EnumerateType(Type *T); |
Victor Hernandez | 572218b | 2010-01-14 19:54:11 +0000 | [diff] [blame] | 195 | void EnumerateOperandType(const Value *V); |
Bill Wendling | 7b5f4f3 | 2013-02-12 08:01:22 +0000 | [diff] [blame] | 196 | void EnumerateAttributes(AttributeSet PAL); |
Joe Abbey | 2ad8df2 | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 197 | |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 198 | void EnumerateValueSymbolTable(const ValueSymbolTable &ST); |
Rafael Espindola | 49e9bf8 | 2014-11-17 20:06:27 +0000 | [diff] [blame] | 199 | void EnumerateNamedMetadata(const Module &M); |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 200 | }; |
| 201 | |
| 202 | } // End llvm namespace |
| 203 | |
| 204 | #endif |