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