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