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" |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 17 | #include "llvm/Module.h" |
| 18 | #include "llvm/TypeSymbolTable.h" |
| 19 | #include "llvm/ValueSymbolTable.h" |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 20 | #include "llvm/Instructions.h" |
Chris Lattner | 12f535b | 2007-05-04 05:05:48 +0000 | [diff] [blame] | 21 | #include <algorithm> |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 22 | using namespace llvm; |
| 23 | |
Chris Lattner | 12f535b | 2007-05-04 05:05:48 +0000 | [diff] [blame] | 24 | static bool isFirstClassType(const std::pair<const llvm::Type*, |
| 25 | unsigned int> &P) { |
| 26 | return P.first->isFirstClassType(); |
| 27 | } |
| 28 | |
Chris Lattner | 6da91d3 | 2007-05-04 05:21:47 +0000 | [diff] [blame] | 29 | static bool isIntegerValue(const std::pair<const Value*, unsigned> &V) { |
| 30 | return isa<IntegerType>(V.first->getType()); |
| 31 | } |
| 32 | |
Chris Lattner | 12f535b | 2007-05-04 05:05:48 +0000 | [diff] [blame] | 33 | static bool CompareByFrequency(const std::pair<const llvm::Type*, |
| 34 | unsigned int> &P1, |
| 35 | const std::pair<const llvm::Type*, |
| 36 | unsigned int> &P2) { |
| 37 | return P1.second > P2.second; |
| 38 | } |
| 39 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 40 | /// ValueEnumerator - Enumerate module-level information. |
| 41 | ValueEnumerator::ValueEnumerator(const Module *M) { |
| 42 | // Enumerate the global variables. |
| 43 | for (Module::const_global_iterator I = M->global_begin(), |
| 44 | E = M->global_end(); I != E; ++I) |
| 45 | EnumerateValue(I); |
| 46 | |
| 47 | // Enumerate the functions. |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 48 | 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] | 49 | EnumerateValue(I); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 50 | EnumerateParamAttrs(cast<Function>(I)->getParamAttrs()); |
| 51 | } |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 52 | |
Chris Lattner | 07d98b4 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 53 | // Enumerate the aliases. |
| 54 | for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end(); |
| 55 | I != E; ++I) |
| 56 | EnumerateValue(I); |
| 57 | |
Chris Lattner | 6da91d3 | 2007-05-04 05:21:47 +0000 | [diff] [blame] | 58 | // Remember what is the cutoff between globalvalue's and other constants. |
| 59 | unsigned FirstConstant = Values.size(); |
| 60 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 61 | // Enumerate the global variable initializers. |
| 62 | for (Module::const_global_iterator I = M->global_begin(), |
| 63 | E = M->global_end(); I != E; ++I) |
| 64 | if (I->hasInitializer()) |
| 65 | EnumerateValue(I->getInitializer()); |
| 66 | |
Chris Lattner | 07d98b4 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 67 | // Enumerate the aliasees. |
| 68 | for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end(); |
| 69 | I != E; ++I) |
| 70 | EnumerateValue(I->getAliasee()); |
| 71 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 72 | // Enumerate types used by the type symbol table. |
| 73 | EnumerateTypeSymbolTable(M->getTypeSymbolTable()); |
| 74 | |
| 75 | // Insert constants that are named at module level into the slot pool so that |
| 76 | // the module symbol table can refer to them... |
| 77 | EnumerateValueSymbolTable(M->getValueSymbolTable()); |
| 78 | |
Chris Lattner | 8d35c79 | 2007-04-26 03:50:57 +0000 | [diff] [blame] | 79 | // Enumerate types used by function bodies and argument lists. |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 80 | 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] | 81 | |
| 82 | for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end(); |
| 83 | I != E; ++I) |
| 84 | EnumerateType(I->getType()); |
| 85 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 86 | for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB) |
| 87 | for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;++I){ |
| 88 | for (User::const_op_iterator OI = I->op_begin(), E = I->op_end(); |
| 89 | OI != E; ++OI) |
Chris Lattner | 33f1d5b | 2007-05-06 08:35:19 +0000 | [diff] [blame] | 90 | EnumerateOperandType(*OI); |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 91 | EnumerateType(I->getType()); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 92 | if (const CallInst *CI = dyn_cast<CallInst>(I)) |
| 93 | EnumerateParamAttrs(CI->getParamAttrs()); |
| 94 | else if (const InvokeInst *II = dyn_cast<InvokeInst>(I)) |
| 95 | EnumerateParamAttrs(II->getParamAttrs()); |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 96 | } |
| 97 | } |
Chris Lattner | 12f535b | 2007-05-04 05:05:48 +0000 | [diff] [blame] | 98 | |
Chris Lattner | 6da91d3 | 2007-05-04 05:21:47 +0000 | [diff] [blame] | 99 | // Optimize constant ordering. |
| 100 | OptimizeConstants(FirstConstant, Values.size()); |
| 101 | |
Chris Lattner | 12f535b | 2007-05-04 05:05:48 +0000 | [diff] [blame] | 102 | // Sort the type table by frequency so that most commonly used types are early |
| 103 | // in the table (have low bit-width). |
| 104 | std::stable_sort(Types.begin(), Types.end(), CompareByFrequency); |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 105 | |
Chris Lattner | 12f535b | 2007-05-04 05:05:48 +0000 | [diff] [blame] | 106 | // Partition the Type ID's so that the first-class types occur before the |
| 107 | // aggregate types. This allows the aggregate types to be dropped from the |
| 108 | // type table after parsing the global variable initializers. |
| 109 | std::partition(Types.begin(), Types.end(), isFirstClassType); |
| 110 | |
| 111 | // Now that we rearranged the type table, rebuild TypeMap. |
| 112 | for (unsigned i = 0, e = Types.size(); i != e; ++i) |
| 113 | TypeMap[Types[i].first] = i+1; |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Chris Lattner | 6da91d3 | 2007-05-04 05:21:47 +0000 | [diff] [blame] | 116 | // Optimize constant ordering. |
| 117 | struct CstSortPredicate { |
| 118 | ValueEnumerator &VE; |
| 119 | CstSortPredicate(ValueEnumerator &ve) : VE(ve) {} |
| 120 | bool operator()(const std::pair<const Value*, unsigned> &LHS, |
| 121 | const std::pair<const Value*, unsigned> &RHS) { |
| 122 | // Sort by plane. |
| 123 | if (LHS.first->getType() != RHS.first->getType()) |
| 124 | return VE.getTypeID(LHS.first->getType()) < |
| 125 | VE.getTypeID(RHS.first->getType()); |
| 126 | // Then by frequency. |
| 127 | return LHS.second > RHS.second; |
| 128 | } |
| 129 | }; |
| 130 | |
| 131 | /// OptimizeConstants - Reorder constant pool for denser encoding. |
| 132 | void ValueEnumerator::OptimizeConstants(unsigned CstStart, unsigned CstEnd) { |
| 133 | if (CstStart == CstEnd || CstStart+1 == CstEnd) return; |
| 134 | |
| 135 | CstSortPredicate P(*this); |
| 136 | std::stable_sort(Values.begin()+CstStart, Values.begin()+CstEnd, P); |
| 137 | |
| 138 | // Ensure that integer constants are at the start of the constant pool. This |
| 139 | // is important so that GEP structure indices come before gep constant exprs. |
| 140 | std::partition(Values.begin()+CstStart, Values.begin()+CstEnd, |
| 141 | isIntegerValue); |
| 142 | |
| 143 | // Rebuild the modified portion of ValueMap. |
| 144 | for (; CstStart != CstEnd; ++CstStart) |
| 145 | ValueMap[Values[CstStart].first] = CstStart+1; |
| 146 | } |
| 147 | |
| 148 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 149 | /// EnumerateTypeSymbolTable - Insert all of the types in the specified symbol |
| 150 | /// table. |
| 151 | void ValueEnumerator::EnumerateTypeSymbolTable(const TypeSymbolTable &TST) { |
| 152 | for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end(); |
| 153 | TI != TE; ++TI) |
| 154 | EnumerateType(TI->second); |
| 155 | } |
| 156 | |
| 157 | /// EnumerateValueSymbolTable - Insert all of the values in the specified symbol |
| 158 | /// table into the values table. |
| 159 | void ValueEnumerator::EnumerateValueSymbolTable(const ValueSymbolTable &VST) { |
| 160 | for (ValueSymbolTable::const_iterator VI = VST.begin(), VE = VST.end(); |
| 161 | VI != VE; ++VI) |
| 162 | EnumerateValue(VI->getValue()); |
| 163 | } |
| 164 | |
| 165 | void ValueEnumerator::EnumerateValue(const Value *V) { |
| 166 | assert(V->getType() != Type::VoidTy && "Can't insert void values!"); |
| 167 | |
| 168 | // Check to see if it's already in! |
| 169 | unsigned &ValueID = ValueMap[V]; |
| 170 | if (ValueID) { |
| 171 | // Increment use count. |
| 172 | Values[ValueID-1].second++; |
| 173 | return; |
| 174 | } |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 175 | |
Chris Lattner | 7a303d1 | 2007-05-06 01:00:28 +0000 | [diff] [blame] | 176 | // Enumerate the type of this value. |
| 177 | EnumerateType(V->getType()); |
| 178 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 179 | if (const Constant *C = dyn_cast<Constant>(V)) { |
| 180 | if (isa<GlobalValue>(C)) { |
| 181 | // Initializers for globals are handled explicitly elsewhere. |
Chris Lattner | ff7fc5d | 2007-05-06 00:35:24 +0000 | [diff] [blame] | 182 | } else if (isa<ConstantArray>(C) && cast<ConstantArray>(C)->isString()) { |
| 183 | // Do not enumerate the initializers for an array of simple characters. |
| 184 | // The initializers just polute the value table, and we emit the strings |
| 185 | // specially. |
Chris Lattner | 7a303d1 | 2007-05-06 01:00:28 +0000 | [diff] [blame] | 186 | } else if (C->getNumOperands()) { |
| 187 | // If a constant has operands, enumerate them. This makes sure that if a |
| 188 | // constant has uses (for example an array of const ints), that they are |
| 189 | // inserted also. |
| 190 | |
| 191 | // We prefer to enumerate them with values before we enumerate the user |
| 192 | // itself. This makes it more likely that we can avoid forward references |
| 193 | // in the reader. We know that there can be no cycles in the constants |
| 194 | // graph that don't go through a global variable. |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 195 | for (User::const_op_iterator I = C->op_begin(), E = C->op_end(); |
| 196 | I != E; ++I) |
| 197 | EnumerateValue(*I); |
Chris Lattner | 7a303d1 | 2007-05-06 01:00:28 +0000 | [diff] [blame] | 198 | |
| 199 | // Finally, add the value. Doing this could make the ValueID reference be |
| 200 | // dangling, don't reuse it. |
| 201 | Values.push_back(std::make_pair(V, 1U)); |
| 202 | ValueMap[V] = Values.size(); |
| 203 | return; |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 204 | } |
| 205 | } |
Chris Lattner | 7a303d1 | 2007-05-06 01:00:28 +0000 | [diff] [blame] | 206 | |
| 207 | // Add the value. |
| 208 | Values.push_back(std::make_pair(V, 1U)); |
| 209 | ValueID = Values.size(); |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | |
| 213 | void ValueEnumerator::EnumerateType(const Type *Ty) { |
| 214 | unsigned &TypeID = TypeMap[Ty]; |
| 215 | |
| 216 | if (TypeID) { |
| 217 | // If we've already seen this type, just increase its occurrence count. |
| 218 | Types[TypeID-1].second++; |
| 219 | return; |
| 220 | } |
| 221 | |
| 222 | // First time we saw this type, add it. |
| 223 | Types.push_back(std::make_pair(Ty, 1U)); |
| 224 | TypeID = Types.size(); |
| 225 | |
| 226 | // Enumerate subtypes. |
| 227 | for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end(); |
| 228 | I != E; ++I) |
| 229 | EnumerateType(*I); |
| 230 | } |
| 231 | |
Chris Lattner | 33f1d5b | 2007-05-06 08:35:19 +0000 | [diff] [blame] | 232 | // Enumerate the types for the specified value. If the value is a constant, |
| 233 | // walk through it, enumerating the types of the constant. |
| 234 | void ValueEnumerator::EnumerateOperandType(const Value *V) { |
| 235 | EnumerateType(V->getType()); |
| 236 | if (const Constant *C = dyn_cast<Constant>(V)) { |
| 237 | // If this constant is already enumerated, ignore it, we know its type must |
| 238 | // be enumerated. |
| 239 | if (ValueMap.count(V)) return; |
| 240 | |
| 241 | // This constant may have operands, make sure to enumerate the types in |
| 242 | // them. |
| 243 | for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) |
| 244 | EnumerateOperandType(C->getOperand(i)); |
| 245 | } |
| 246 | } |
| 247 | |
Chris Lattner | 50954f5 | 2007-05-03 22:46:43 +0000 | [diff] [blame] | 248 | void ValueEnumerator::EnumerateParamAttrs(const ParamAttrsList *PAL) { |
| 249 | if (PAL == 0) return; // null is always 0. |
| 250 | // Do a lookup. |
| 251 | unsigned &Entry = ParamAttrMap[PAL]; |
| 252 | if (Entry == 0) { |
| 253 | // Never saw this before, add it. |
| 254 | ParamAttrs.push_back(PAL); |
| 255 | Entry = ParamAttrs.size(); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | |
Chris Lattner | 198f34a | 2007-04-26 03:27:58 +0000 | [diff] [blame] | 260 | /// PurgeAggregateValues - If there are any aggregate values at the end of the |
| 261 | /// value list, remove them and return the count of the remaining values. If |
| 262 | /// there are none, return -1. |
| 263 | int ValueEnumerator::PurgeAggregateValues() { |
| 264 | // If there are no aggregate values at the end of the list, return -1. |
| 265 | if (Values.empty() || Values.back().first->getType()->isFirstClassType()) |
| 266 | return -1; |
| 267 | |
| 268 | // Otherwise, remove aggregate values... |
| 269 | while (!Values.empty() && !Values.back().first->getType()->isFirstClassType()) |
| 270 | Values.pop_back(); |
| 271 | |
| 272 | // ... and return the new size. |
| 273 | return Values.size(); |
| 274 | } |
| 275 | |
Chris Lattner | 8d35c79 | 2007-04-26 03:50:57 +0000 | [diff] [blame] | 276 | void ValueEnumerator::incorporateFunction(const Function &F) { |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 277 | NumModuleValues = Values.size(); |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 278 | |
Chris Lattner | 8d35c79 | 2007-04-26 03:50:57 +0000 | [diff] [blame] | 279 | // Adding function arguments to the value table. |
| 280 | 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] | 281 | I != E; ++I) |
Chris Lattner | 8d35c79 | 2007-04-26 03:50:57 +0000 | [diff] [blame] | 282 | EnumerateValue(I); |
| 283 | |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 284 | FirstFuncConstantID = Values.size(); |
| 285 | |
Chris Lattner | 8d35c79 | 2007-04-26 03:50:57 +0000 | [diff] [blame] | 286 | // Add all function-level constants to the value table. |
| 287 | for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) { |
| 288 | for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) |
| 289 | for (User::const_op_iterator OI = I->op_begin(), E = I->op_end(); |
| 290 | OI != E; ++OI) { |
| 291 | if ((isa<Constant>(*OI) && !isa<GlobalValue>(*OI)) || |
| 292 | isa<InlineAsm>(*OI)) |
| 293 | EnumerateValue(*OI); |
| 294 | } |
Chris Lattner | c59c0af | 2007-04-26 04:42:16 +0000 | [diff] [blame] | 295 | BasicBlocks.push_back(BB); |
Chris Lattner | e825ed5 | 2007-05-03 22:18:21 +0000 | [diff] [blame] | 296 | ValueMap[BB] = BasicBlocks.size(); |
Chris Lattner | 8d35c79 | 2007-04-26 03:50:57 +0000 | [diff] [blame] | 297 | } |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 298 | |
Chris Lattner | 6da91d3 | 2007-05-04 05:21:47 +0000 | [diff] [blame] | 299 | // Optimize the constant layout. |
| 300 | OptimizeConstants(FirstFuncConstantID, Values.size()); |
| 301 | |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 302 | // Add the function's parameter attributes so they are available for use in |
| 303 | // the function's instruction. |
| 304 | EnumerateParamAttrs(F.getParamAttrs()); |
| 305 | |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 306 | FirstInstID = Values.size(); |
| 307 | |
Chris Lattner | 8d35c79 | 2007-04-26 03:50:57 +0000 | [diff] [blame] | 308 | // Add all of the instructions. |
| 309 | 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] | 310 | for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) { |
| 311 | if (I->getType() != Type::VoidTy) |
Chris Lattner | 8d35c79 | 2007-04-26 03:50:57 +0000 | [diff] [blame] | 312 | EnumerateValue(I); |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 313 | } |
| 314 | } |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Chris Lattner | 8d35c79 | 2007-04-26 03:50:57 +0000 | [diff] [blame] | 317 | void ValueEnumerator::purgeFunction() { |
| 318 | /// Remove purged values from the ValueMap. |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 319 | for (unsigned i = NumModuleValues, e = Values.size(); i != e; ++i) |
Chris Lattner | 8d35c79 | 2007-04-26 03:50:57 +0000 | [diff] [blame] | 320 | ValueMap.erase(Values[i].first); |
Chris Lattner | c59c0af | 2007-04-26 04:42:16 +0000 | [diff] [blame] | 321 | for (unsigned i = 0, e = BasicBlocks.size(); i != e; ++i) |
| 322 | ValueMap.erase(BasicBlocks[i]); |
| 323 | |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 324 | Values.resize(NumModuleValues); |
Chris Lattner | c59c0af | 2007-04-26 04:42:16 +0000 | [diff] [blame] | 325 | BasicBlocks.clear(); |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 326 | } |
| 327 | |