Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===-- ValueEnumerator.cpp - Number values and types for bitcode writer --===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 081ce94 | 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. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the ValueEnumerator class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "ValueEnumerator.h" |
| 15 | #include "llvm/Constants.h" |
| 16 | #include "llvm/DerivedTypes.h" |
Devang Patel | 7c36885 | 2009-07-28 21:49:47 +0000 | [diff] [blame] | 17 | #include "llvm/Metadata.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 18 | #include "llvm/Module.h" |
| 19 | #include "llvm/TypeSymbolTable.h" |
| 20 | #include "llvm/ValueSymbolTable.h" |
Duncan Sands | f5588dc | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 21 | #include "llvm/Instructions.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 22 | #include <algorithm> |
| 23 | using namespace llvm; |
| 24 | |
Dan Gohman | e6b1ee6 | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 25 | static bool isSingleValueType(const std::pair<const llvm::Type*, |
| 26 | unsigned int> &P) { |
| 27 | return P.first->isSingleValueType(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | static bool isIntegerValue(const std::pair<const Value*, unsigned> &V) { |
| 31 | return isa<IntegerType>(V.first->getType()); |
| 32 | } |
| 33 | |
| 34 | static bool CompareByFrequency(const std::pair<const llvm::Type*, |
| 35 | unsigned int> &P1, |
| 36 | const std::pair<const llvm::Type*, |
| 37 | unsigned int> &P2) { |
| 38 | return P1.second > P2.second; |
| 39 | } |
| 40 | |
| 41 | /// ValueEnumerator - Enumerate module-level information. |
| 42 | ValueEnumerator::ValueEnumerator(const Module *M) { |
Devang Patel | adc3761 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 43 | InstructionCount = 0; |
| 44 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 45 | // Enumerate the global variables. |
| 46 | for (Module::const_global_iterator I = M->global_begin(), |
| 47 | E = M->global_end(); I != E; ++I) |
| 48 | EnumerateValue(I); |
| 49 | |
| 50 | // Enumerate the functions. |
Duncan Sands | f5588dc | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 51 | for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 52 | EnumerateValue(I); |
Devang Patel | d222f86 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 53 | EnumerateAttributes(cast<Function>(I)->getAttributes()); |
Duncan Sands | f5588dc | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 54 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 55 | |
| 56 | // Enumerate the aliases. |
| 57 | for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end(); |
| 58 | I != E; ++I) |
| 59 | EnumerateValue(I); |
Daniel Dunbar | 3be44e6 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 60 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 61 | // Remember what is the cutoff between globalvalue's and other constants. |
| 62 | unsigned FirstConstant = Values.size(); |
Daniel Dunbar | 3be44e6 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 63 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 64 | // Enumerate the global variable initializers. |
| 65 | for (Module::const_global_iterator I = M->global_begin(), |
| 66 | E = M->global_end(); I != E; ++I) |
| 67 | if (I->hasInitializer()) |
| 68 | EnumerateValue(I->getInitializer()); |
| 69 | |
| 70 | // Enumerate the aliasees. |
| 71 | for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end(); |
| 72 | I != E; ++I) |
| 73 | EnumerateValue(I->getAliasee()); |
Daniel Dunbar | 3be44e6 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 74 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 75 | // Enumerate types used by the type symbol table. |
| 76 | EnumerateTypeSymbolTable(M->getTypeSymbolTable()); |
| 77 | |
| 78 | // Insert constants that are named at module level into the slot pool so that |
| 79 | // the module symbol table can refer to them... |
| 80 | EnumerateValueSymbolTable(M->getValueSymbolTable()); |
Daniel Dunbar | 3be44e6 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 81 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 82 | // Enumerate types used by function bodies and argument lists. |
| 83 | for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) { |
Daniel Dunbar | 3be44e6 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 84 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 85 | for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end(); |
| 86 | I != E; ++I) |
| 87 | EnumerateType(I->getType()); |
Devang Patel | adc3761 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 88 | |
Devang Patel | 6de78e2 | 2009-09-28 21:41:20 +0000 | [diff] [blame] | 89 | MetadataContext &TheMetadata = F->getContext().getMetadata(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 90 | for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB) |
| 91 | for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;++I){ |
Daniel Dunbar | 3be44e6 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 92 | for (User::const_op_iterator OI = I->op_begin(), E = I->op_end(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 93 | OI != E; ++OI) |
| 94 | EnumerateOperandType(*OI); |
| 95 | EnumerateType(I->getType()); |
Duncan Sands | f5588dc | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 96 | if (const CallInst *CI = dyn_cast<CallInst>(I)) |
Devang Patel | d222f86 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 97 | EnumerateAttributes(CI->getAttributes()); |
Duncan Sands | f5588dc | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 98 | else if (const InvokeInst *II = dyn_cast<InvokeInst>(I)) |
Devang Patel | d222f86 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 99 | EnumerateAttributes(II->getAttributes()); |
Devang Patel | adc3761 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 100 | |
Daniel Dunbar | 3be44e6 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 101 | // Enumerate metadata attached with this instruction. |
Devang Patel | 6de78e2 | 2009-09-28 21:41:20 +0000 | [diff] [blame] | 102 | const MetadataContext::MDMapTy *MDs = TheMetadata.getMDs(I); |
Daniel Dunbar | 3be44e6 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 103 | if (MDs) |
Devang Patel | 6de78e2 | 2009-09-28 21:41:20 +0000 | [diff] [blame] | 104 | for (MetadataContext::MDMapTy::const_iterator MI = MDs->begin(), |
Daniel Dunbar | 3be44e6 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 105 | ME = MDs->end(); MI != ME; ++MI) |
Devang Patel | 0aa3004 | 2009-10-22 18:25:28 +0000 | [diff] [blame^] | 106 | EnumerateMetadata(MI->second); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 107 | } |
| 108 | } |
Daniel Dunbar | 3be44e6 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 109 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 110 | // Optimize constant ordering. |
| 111 | OptimizeConstants(FirstConstant, Values.size()); |
Daniel Dunbar | 3be44e6 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 112 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 113 | // Sort the type table by frequency so that most commonly used types are early |
| 114 | // in the table (have low bit-width). |
| 115 | std::stable_sort(Types.begin(), Types.end(), CompareByFrequency); |
Daniel Dunbar | 3be44e6 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 116 | |
Dan Gohman | e6b1ee6 | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 117 | // Partition the Type ID's so that the single-value types occur before the |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 118 | // aggregate types. This allows the aggregate types to be dropped from the |
| 119 | // type table after parsing the global variable initializers. |
Dan Gohman | e6b1ee6 | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 120 | std::partition(Types.begin(), Types.end(), isSingleValueType); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 121 | |
| 122 | // Now that we rearranged the type table, rebuild TypeMap. |
| 123 | for (unsigned i = 0, e = Types.size(); i != e; ++i) |
| 124 | TypeMap[Types[i].first] = i+1; |
| 125 | } |
| 126 | |
Devang Patel | adc3761 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 127 | unsigned ValueEnumerator::getInstructionID(const Instruction *Inst) const { |
| 128 | InstructionMapType::const_iterator I = InstructionMap.find(Inst); |
| 129 | assert (I != InstructionMap.end() && "Instruction is not mapped!"); |
| 130 | return I->second; |
Daniel Dunbar | 3be44e6 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 131 | } |
Devang Patel | adc3761 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 132 | |
| 133 | void ValueEnumerator::setInstructionID(const Instruction *I) { |
| 134 | InstructionMap[I] = InstructionCount++; |
| 135 | } |
| 136 | |
Devang Patel | ea27c23 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 137 | unsigned ValueEnumerator::getValueID(const Value *V) const { |
| 138 | if (isa<MetadataBase>(V)) { |
| 139 | ValueMapType::const_iterator I = MDValueMap.find(V); |
| 140 | assert(I != MDValueMap.end() && "Value not in slotcalculator!"); |
| 141 | return I->second-1; |
| 142 | } |
Daniel Dunbar | 3be44e6 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 143 | |
Devang Patel | ea27c23 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 144 | ValueMapType::const_iterator I = ValueMap.find(V); |
| 145 | assert(I != ValueMap.end() && "Value not in slotcalculator!"); |
| 146 | return I->second-1; |
| 147 | } |
Daniel Dunbar | 3be44e6 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 148 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 149 | // Optimize constant ordering. |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 150 | namespace { |
| 151 | struct CstSortPredicate { |
| 152 | ValueEnumerator &VE; |
| 153 | explicit CstSortPredicate(ValueEnumerator &ve) : VE(ve) {} |
| 154 | bool operator()(const std::pair<const Value*, unsigned> &LHS, |
| 155 | const std::pair<const Value*, unsigned> &RHS) { |
| 156 | // Sort by plane. |
| 157 | if (LHS.first->getType() != RHS.first->getType()) |
Daniel Dunbar | 3be44e6 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 158 | return VE.getTypeID(LHS.first->getType()) < |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 159 | VE.getTypeID(RHS.first->getType()); |
| 160 | // Then by frequency. |
| 161 | return LHS.second > RHS.second; |
| 162 | } |
| 163 | }; |
| 164 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 165 | |
| 166 | /// OptimizeConstants - Reorder constant pool for denser encoding. |
| 167 | void ValueEnumerator::OptimizeConstants(unsigned CstStart, unsigned CstEnd) { |
| 168 | if (CstStart == CstEnd || CstStart+1 == CstEnd) return; |
Daniel Dunbar | 3be44e6 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 169 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 170 | CstSortPredicate P(*this); |
| 171 | std::stable_sort(Values.begin()+CstStart, Values.begin()+CstEnd, P); |
Daniel Dunbar | 3be44e6 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 172 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 173 | // Ensure that integer constants are at the start of the constant pool. This |
| 174 | // is important so that GEP structure indices come before gep constant exprs. |
| 175 | std::partition(Values.begin()+CstStart, Values.begin()+CstEnd, |
| 176 | isIntegerValue); |
Daniel Dunbar | 3be44e6 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 177 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 178 | // Rebuild the modified portion of ValueMap. |
| 179 | for (; CstStart != CstEnd; ++CstStart) |
| 180 | ValueMap[Values[CstStart].first] = CstStart+1; |
| 181 | } |
| 182 | |
| 183 | |
| 184 | /// EnumerateTypeSymbolTable - Insert all of the types in the specified symbol |
| 185 | /// table. |
| 186 | void ValueEnumerator::EnumerateTypeSymbolTable(const TypeSymbolTable &TST) { |
Daniel Dunbar | 3be44e6 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 187 | for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 188 | TI != TE; ++TI) |
| 189 | EnumerateType(TI->second); |
| 190 | } |
| 191 | |
| 192 | /// EnumerateValueSymbolTable - Insert all of the values in the specified symbol |
| 193 | /// table into the values table. |
| 194 | void ValueEnumerator::EnumerateValueSymbolTable(const ValueSymbolTable &VST) { |
Daniel Dunbar | 3be44e6 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 195 | for (ValueSymbolTable::const_iterator VI = VST.begin(), VE = VST.end(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 196 | VI != VE; ++VI) |
| 197 | EnumerateValue(VI->getValue()); |
| 198 | } |
| 199 | |
Devang Patel | ea27c23 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 200 | void ValueEnumerator::EnumerateMetadata(const MetadataBase *MD) { |
| 201 | // Check to see if it's already in! |
| 202 | unsigned &MDValueID = MDValueMap[MD]; |
| 203 | if (MDValueID) { |
| 204 | // Increment use count. |
| 205 | MDValues[MDValueID-1].second++; |
| 206 | return; |
| 207 | } |
| 208 | |
| 209 | // Enumerate the type of this value. |
| 210 | EnumerateType(MD->getType()); |
| 211 | |
| 212 | if (const MDNode *N = dyn_cast<MDNode>(MD)) { |
| 213 | MDValues.push_back(std::make_pair(MD, 1U)); |
| 214 | MDValueMap[MD] = MDValues.size(); |
| 215 | MDValueID = MDValues.size(); |
Devang Patel | 26c7b90 | 2009-10-21 21:25:09 +0000 | [diff] [blame] | 216 | for (unsigned i = 0, e = N->getNumElements(); i != e; ++i) { |
| 217 | if (Value *V = N->getElement(i)) |
| 218 | EnumerateValue(V); |
Devang Patel | ea27c23 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 219 | else |
Owen Anderson | 35b4707 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 220 | EnumerateType(Type::getVoidTy(MD->getContext())); |
Devang Patel | ea27c23 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 221 | } |
| 222 | return; |
| 223 | } else if (const NamedMDNode *N = dyn_cast<NamedMDNode>(MD)) { |
| 224 | for(NamedMDNode::const_elem_iterator I = N->elem_begin(), |
| 225 | E = N->elem_end(); I != E; ++I) { |
| 226 | MetadataBase *M = *I; |
| 227 | EnumerateValue(M); |
| 228 | } |
| 229 | MDValues.push_back(std::make_pair(MD, 1U)); |
| 230 | MDValueMap[MD] = Values.size(); |
| 231 | return; |
| 232 | } |
| 233 | |
| 234 | // Add the value. |
| 235 | MDValues.push_back(std::make_pair(MD, 1U)); |
| 236 | MDValueID = MDValues.size(); |
| 237 | } |
| 238 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 239 | void ValueEnumerator::EnumerateValue(const Value *V) { |
Owen Anderson | 35b4707 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 240 | assert(V->getType() != Type::getVoidTy(V->getContext()) && |
| 241 | "Can't insert void values!"); |
Devang Patel | ea27c23 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 242 | if (const MetadataBase *MB = dyn_cast<MetadataBase>(V)) |
| 243 | return EnumerateMetadata(MB); |
| 244 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 245 | // Check to see if it's already in! |
| 246 | unsigned &ValueID = ValueMap[V]; |
| 247 | if (ValueID) { |
| 248 | // Increment use count. |
| 249 | Values[ValueID-1].second++; |
| 250 | return; |
| 251 | } |
| 252 | |
| 253 | // Enumerate the type of this value. |
| 254 | EnumerateType(V->getType()); |
Daniel Dunbar | 3be44e6 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 255 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 256 | if (const Constant *C = dyn_cast<Constant>(V)) { |
| 257 | if (isa<GlobalValue>(C)) { |
| 258 | // Initializers for globals are handled explicitly elsewhere. |
| 259 | } else if (isa<ConstantArray>(C) && cast<ConstantArray>(C)->isString()) { |
| 260 | // Do not enumerate the initializers for an array of simple characters. |
| 261 | // The initializers just polute the value table, and we emit the strings |
| 262 | // specially. |
| 263 | } else if (C->getNumOperands()) { |
| 264 | // If a constant has operands, enumerate them. This makes sure that if a |
| 265 | // constant has uses (for example an array of const ints), that they are |
| 266 | // inserted also. |
Daniel Dunbar | 3be44e6 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 267 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 268 | // We prefer to enumerate them with values before we enumerate the user |
| 269 | // itself. This makes it more likely that we can avoid forward references |
| 270 | // in the reader. We know that there can be no cycles in the constants |
| 271 | // graph that don't go through a global variable. |
| 272 | for (User::const_op_iterator I = C->op_begin(), E = C->op_end(); |
| 273 | I != E; ++I) |
| 274 | EnumerateValue(*I); |
Daniel Dunbar | 3be44e6 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 275 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 276 | // Finally, add the value. Doing this could make the ValueID reference be |
| 277 | // dangling, don't reuse it. |
| 278 | Values.push_back(std::make_pair(V, 1U)); |
| 279 | ValueMap[V] = Values.size(); |
| 280 | return; |
| 281 | } |
| 282 | } |
Devang Patel | 54199ef | 2009-07-23 01:07:34 +0000 | [diff] [blame] | 283 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 284 | // Add the value. |
| 285 | Values.push_back(std::make_pair(V, 1U)); |
| 286 | ValueID = Values.size(); |
| 287 | } |
| 288 | |
| 289 | |
| 290 | void ValueEnumerator::EnumerateType(const Type *Ty) { |
| 291 | unsigned &TypeID = TypeMap[Ty]; |
Daniel Dunbar | 3be44e6 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 292 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 293 | if (TypeID) { |
| 294 | // If we've already seen this type, just increase its occurrence count. |
| 295 | Types[TypeID-1].second++; |
| 296 | return; |
| 297 | } |
Daniel Dunbar | 3be44e6 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 298 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 299 | // First time we saw this type, add it. |
| 300 | Types.push_back(std::make_pair(Ty, 1U)); |
| 301 | TypeID = Types.size(); |
Daniel Dunbar | 3be44e6 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 302 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 303 | // Enumerate subtypes. |
| 304 | for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end(); |
| 305 | I != E; ++I) |
| 306 | EnumerateType(*I); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | // Enumerate the types for the specified value. If the value is a constant, |
| 310 | // walk through it, enumerating the types of the constant. |
| 311 | void ValueEnumerator::EnumerateOperandType(const Value *V) { |
| 312 | EnumerateType(V->getType()); |
| 313 | if (const Constant *C = dyn_cast<Constant>(V)) { |
| 314 | // If this constant is already enumerated, ignore it, we know its type must |
| 315 | // be enumerated. |
| 316 | if (ValueMap.count(V)) return; |
| 317 | |
| 318 | // This constant may have operands, make sure to enumerate the types in |
| 319 | // them. |
| 320 | for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) |
| 321 | EnumerateOperandType(C->getOperand(i)); |
Nick Lewycky | 117f438 | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 322 | |
| 323 | if (const MDNode *N = dyn_cast<MDNode>(V)) { |
Devang Patel | 0c0a6ca | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 324 | for (unsigned i = 0, e = N->getNumElements(); i != e; ++i) { |
| 325 | Value *Elem = N->getElement(i); |
| 326 | if (Elem) |
| 327 | EnumerateOperandType(Elem); |
| 328 | } |
Nick Lewycky | 117f438 | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 329 | } |
Devang Patel | 54199ef | 2009-07-23 01:07:34 +0000 | [diff] [blame] | 330 | } else if (isa<MDString>(V) || isa<MDNode>(V)) |
Devang Patel | 0c0a6ca | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 331 | EnumerateValue(V); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 332 | } |
| 333 | |
Devang Patel | d222f86 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 334 | void ValueEnumerator::EnumerateAttributes(const AttrListPtr &PAL) { |
Chris Lattner | 1c8733e | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 335 | if (PAL.isEmpty()) return; // null is always 0. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 336 | // Do a lookup. |
Devang Patel | d222f86 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 337 | unsigned &Entry = AttributeMap[PAL.getRawPointer()]; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 338 | if (Entry == 0) { |
| 339 | // Never saw this before, add it. |
Devang Patel | d222f86 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 340 | Attributes.push_back(PAL); |
| 341 | Entry = Attributes.size(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 342 | } |
| 343 | } |
| 344 | |
| 345 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 346 | void ValueEnumerator::incorporateFunction(const Function &F) { |
| 347 | NumModuleValues = Values.size(); |
Daniel Dunbar | 3be44e6 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 348 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 349 | // Adding function arguments to the value table. |
| 350 | for(Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end(); |
| 351 | I != E; ++I) |
| 352 | EnumerateValue(I); |
| 353 | |
| 354 | FirstFuncConstantID = Values.size(); |
Daniel Dunbar | 3be44e6 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 355 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 356 | // Add all function-level constants to the value table. |
| 357 | for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) { |
| 358 | for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) |
Daniel Dunbar | 3be44e6 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 359 | for (User::const_op_iterator OI = I->op_begin(), E = I->op_end(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 360 | OI != E; ++OI) { |
| 361 | if ((isa<Constant>(*OI) && !isa<GlobalValue>(*OI)) || |
| 362 | isa<InlineAsm>(*OI)) |
| 363 | EnumerateValue(*OI); |
| 364 | } |
| 365 | BasicBlocks.push_back(BB); |
| 366 | ValueMap[BB] = BasicBlocks.size(); |
| 367 | } |
Daniel Dunbar | 3be44e6 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 368 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 369 | // Optimize the constant layout. |
| 370 | OptimizeConstants(FirstFuncConstantID, Values.size()); |
Daniel Dunbar | 3be44e6 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 371 | |
Duncan Sands | f5588dc | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 372 | // Add the function's parameter attributes so they are available for use in |
| 373 | // the function's instruction. |
Devang Patel | d222f86 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 374 | EnumerateAttributes(F.getAttributes()); |
Duncan Sands | f5588dc | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 375 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 376 | FirstInstID = Values.size(); |
Daniel Dunbar | 3be44e6 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 377 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 378 | // Add all of the instructions. |
| 379 | for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) { |
| 380 | for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) { |
Owen Anderson | 35b4707 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 381 | if (I->getType() != Type::getVoidTy(F.getContext())) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 382 | EnumerateValue(I); |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | void ValueEnumerator::purgeFunction() { |
| 388 | /// Remove purged values from the ValueMap. |
| 389 | for (unsigned i = NumModuleValues, e = Values.size(); i != e; ++i) |
| 390 | ValueMap.erase(Values[i].first); |
| 391 | for (unsigned i = 0, e = BasicBlocks.size(); i != e; ++i) |
| 392 | ValueMap.erase(BasicBlocks[i]); |
Daniel Dunbar | 3be44e6 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 393 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 394 | Values.resize(NumModuleValues); |
| 395 | BasicBlocks.clear(); |
| 396 | } |