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" |
Rafael Espindola | f5a9056 | 2011-04-06 16:49:37 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/SmallPtrSet.h" |
| 16 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | ff7fc5d | 2007-05-06 00:35:24 +0000 | [diff] [blame] | 17 | #include "llvm/Constants.h" |
Chris Lattner | 50954f5 | 2007-05-03 22:46:43 +0000 | [diff] [blame] | 18 | #include "llvm/DerivedTypes.h" |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 19 | #include "llvm/Module.h" |
| 20 | #include "llvm/TypeSymbolTable.h" |
| 21 | #include "llvm/ValueSymbolTable.h" |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 22 | #include "llvm/Instructions.h" |
Chris Lattner | 12f535b | 2007-05-04 05:05:48 +0000 | [diff] [blame] | 23 | #include <algorithm> |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 24 | using namespace llvm; |
| 25 | |
Chris Lattner | 6da91d3 | 2007-05-04 05:21:47 +0000 | [diff] [blame] | 26 | static bool isIntegerValue(const std::pair<const Value*, unsigned> &V) { |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 27 | return V.first->getType()->isIntegerTy(); |
Chris Lattner | 6da91d3 | 2007-05-04 05:21:47 +0000 | [diff] [blame] | 28 | } |
| 29 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 30 | /// ValueEnumerator - Enumerate module-level information. |
| 31 | ValueEnumerator::ValueEnumerator(const Module *M) { |
| 32 | // Enumerate the global variables. |
| 33 | for (Module::const_global_iterator I = M->global_begin(), |
| 34 | E = M->global_end(); I != E; ++I) |
| 35 | EnumerateValue(I); |
| 36 | |
| 37 | // Enumerate the functions. |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 38 | 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] | 39 | EnumerateValue(I); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 40 | EnumerateAttributes(cast<Function>(I)->getAttributes()); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 41 | } |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 42 | |
Chris Lattner | 07d98b4 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 43 | // Enumerate the aliases. |
| 44 | for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end(); |
| 45 | I != E; ++I) |
| 46 | EnumerateValue(I); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 47 | |
Chris Lattner | 6da91d3 | 2007-05-04 05:21:47 +0000 | [diff] [blame] | 48 | // Remember what is the cutoff between globalvalue's and other constants. |
| 49 | unsigned FirstConstant = Values.size(); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 50 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 51 | // Enumerate the global variable initializers. |
| 52 | for (Module::const_global_iterator I = M->global_begin(), |
| 53 | E = M->global_end(); I != E; ++I) |
| 54 | if (I->hasInitializer()) |
| 55 | EnumerateValue(I->getInitializer()); |
| 56 | |
Chris Lattner | 07d98b4 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 57 | // Enumerate the aliasees. |
| 58 | for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end(); |
| 59 | I != E; ++I) |
| 60 | EnumerateValue(I->getAliasee()); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 61 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 62 | // Enumerate types used by the type symbol table. |
| 63 | EnumerateTypeSymbolTable(M->getTypeSymbolTable()); |
| 64 | |
Bob Wilson | 54eee52 | 2010-06-19 05:33:57 +0000 | [diff] [blame] | 65 | // Insert constants and metadata that are named at module level into the slot |
Devang Patel | 0386f01 | 2010-01-07 19:39:36 +0000 | [diff] [blame] | 66 | // pool so that the module symbol table can refer to them... |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 67 | EnumerateValueSymbolTable(M->getValueSymbolTable()); |
Dan Gohman | 17aa92c | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 68 | EnumerateNamedMetadata(M); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 69 | |
Chris Lattner | cc7b011 | 2009-12-31 00:51:46 +0000 | [diff] [blame] | 70 | SmallVector<std::pair<unsigned, MDNode*>, 8> MDs; |
| 71 | |
Chris Lattner | 8d35c79 | 2007-04-26 03:50:57 +0000 | [diff] [blame] | 72 | // Enumerate types used by function bodies and argument lists. |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 73 | for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) { |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 74 | |
Chris Lattner | 8d35c79 | 2007-04-26 03:50:57 +0000 | [diff] [blame] | 75 | for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end(); |
| 76 | I != E; ++I) |
| 77 | EnumerateType(I->getType()); |
Devang Patel | e8e0213 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 78 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 79 | for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB) |
| 80 | for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;++I){ |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 81 | for (User::const_op_iterator OI = I->op_begin(), E = I->op_end(); |
Victor Hernandez | d7e6457 | 2010-01-14 19:54:11 +0000 | [diff] [blame] | 82 | OI != E; ++OI) { |
| 83 | if (MDNode *MD = dyn_cast<MDNode>(*OI)) |
Victor Hernandez | 2b3365c | 2010-02-06 01:21:09 +0000 | [diff] [blame] | 84 | if (MD->isFunctionLocal() && MD->getFunction()) |
Victor Hernandez | d7e6457 | 2010-01-14 19:54:11 +0000 | [diff] [blame] | 85 | // These will get enumerated during function-incorporation. |
| 86 | continue; |
| 87 | EnumerateOperandType(*OI); |
| 88 | } |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 89 | EnumerateType(I->getType()); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 90 | if (const CallInst *CI = dyn_cast<CallInst>(I)) |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 91 | EnumerateAttributes(CI->getAttributes()); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 92 | else if (const InvokeInst *II = dyn_cast<InvokeInst>(I)) |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 93 | EnumerateAttributes(II->getAttributes()); |
Devang Patel | e8e0213 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 94 | |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 95 | // Enumerate metadata attached with this instruction. |
Devang Patel | f61b237 | 2009-10-22 18:55:16 +0000 | [diff] [blame] | 96 | MDs.clear(); |
Chris Lattner | a624524 | 2010-04-03 02:17:50 +0000 | [diff] [blame] | 97 | I->getAllMetadataOtherThanDebugLoc(MDs); |
Chris Lattner | 3990b12 | 2009-12-28 23:41:32 +0000 | [diff] [blame] | 98 | for (unsigned i = 0, e = MDs.size(); i != e; ++i) |
Victor Hernandez | d7e6457 | 2010-01-14 19:54:11 +0000 | [diff] [blame] | 99 | EnumerateMetadata(MDs[i].second); |
Chris Lattner | a624524 | 2010-04-03 02:17:50 +0000 | [diff] [blame] | 100 | |
| 101 | if (!I->getDebugLoc().isUnknown()) { |
| 102 | MDNode *Scope, *IA; |
| 103 | I->getDebugLoc().getScopeAndInlinedAt(Scope, IA, I->getContext()); |
| 104 | if (Scope) EnumerateMetadata(Scope); |
| 105 | if (IA) EnumerateMetadata(IA); |
| 106 | } |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 107 | } |
| 108 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 109 | |
Chris Lattner | 6da91d3 | 2007-05-04 05:21:47 +0000 | [diff] [blame] | 110 | // Optimize constant ordering. |
| 111 | OptimizeConstants(FirstConstant, Values.size()); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 112 | |
Rafael Espindola | f5a9056 | 2011-04-06 16:49:37 +0000 | [diff] [blame] | 113 | OptimizeTypes(); |
Chris Lattner | 12f535b | 2007-05-04 05:05:48 +0000 | [diff] [blame] | 114 | |
| 115 | // Now that we rearranged the type table, rebuild TypeMap. |
| 116 | for (unsigned i = 0, e = Types.size(); i != e; ++i) |
Rafael Espindola | f5a9056 | 2011-04-06 16:49:37 +0000 | [diff] [blame] | 117 | TypeMap[Types[i]] = i+1; |
| 118 | } |
| 119 | |
| 120 | struct TypeAndDeps { |
| 121 | const Type *Ty; |
| 122 | unsigned NumDeps; |
| 123 | }; |
| 124 | |
| 125 | static int CompareByDeps(const void *a, const void *b) { |
| 126 | const TypeAndDeps &ta = *(const TypeAndDeps*) a; |
| 127 | const TypeAndDeps &tb = *(const TypeAndDeps*) b; |
| 128 | return ta.NumDeps - tb.NumDeps; |
| 129 | } |
| 130 | |
| 131 | static void VisitType(const Type *Ty, SmallPtrSet<const Type*, 16> &Visited, |
| 132 | std::vector<const Type*> &Out) { |
| 133 | if (Visited.count(Ty)) |
| 134 | return; |
| 135 | |
| 136 | Visited.insert(Ty); |
| 137 | |
| 138 | for (Type::subtype_iterator I2 = Ty->subtype_begin(), |
| 139 | E2 = Ty->subtype_end(); I2 != E2; ++I2) { |
| 140 | const Type *InnerType = I2->get(); |
| 141 | VisitType(InnerType, Visited, Out); |
| 142 | } |
| 143 | |
| 144 | Out.push_back(Ty); |
| 145 | } |
| 146 | |
| 147 | void ValueEnumerator::OptimizeTypes(void) { |
| 148 | // If the types form a DAG, this will compute a topological sort and |
| 149 | // no forward references will be needed when reading them in. |
| 150 | // If there are cycles, this is a simple but reasonable heuristic for |
| 151 | // the minimum feedback arc set problem. |
| 152 | const unsigned NumTypes = Types.size(); |
| 153 | std::vector<TypeAndDeps> TypeDeps; |
| 154 | TypeDeps.resize(NumTypes); |
| 155 | |
| 156 | for (unsigned I = 0; I < NumTypes; ++I) { |
| 157 | const Type *Ty = Types[I]; |
| 158 | TypeDeps[I].Ty = Ty; |
| 159 | TypeDeps[I].NumDeps = 0; |
| 160 | } |
| 161 | |
| 162 | for (unsigned I = 0; I < NumTypes; ++I) { |
| 163 | const Type *Ty = TypeDeps[I].Ty; |
| 164 | for (Type::subtype_iterator I2 = Ty->subtype_begin(), |
| 165 | E2 = Ty->subtype_end(); I2 != E2; ++I2) { |
| 166 | const Type *InnerType = I2->get(); |
| 167 | unsigned InnerIndex = TypeMap.lookup(InnerType) - 1; |
| 168 | TypeDeps[InnerIndex].NumDeps++; |
| 169 | } |
| 170 | } |
| 171 | array_pod_sort(TypeDeps.begin(), TypeDeps.end(), CompareByDeps); |
| 172 | |
| 173 | SmallPtrSet<const Type*, 16> Visited; |
| 174 | Types.clear(); |
| 175 | Types.reserve(NumTypes); |
| 176 | for (unsigned I = 0; I < NumTypes; ++I) { |
| 177 | VisitType(TypeDeps[I].Ty, Visited, Types); |
| 178 | } |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Devang Patel | e8e0213 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 181 | unsigned ValueEnumerator::getInstructionID(const Instruction *Inst) const { |
| 182 | InstructionMapType::const_iterator I = InstructionMap.find(Inst); |
| 183 | assert (I != InstructionMap.end() && "Instruction is not mapped!"); |
Dan Gohman | 5c18fa2 | 2010-08-25 17:09:50 +0000 | [diff] [blame] | 184 | return I->second; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 185 | } |
Devang Patel | e8e0213 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 186 | |
| 187 | void ValueEnumerator::setInstructionID(const Instruction *I) { |
| 188 | InstructionMap[I] = InstructionCount++; |
| 189 | } |
| 190 | |
Devang Patel | d5ac404 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 191 | unsigned ValueEnumerator::getValueID(const Value *V) const { |
Devang Patel | bc5201f | 2010-01-22 22:52:10 +0000 | [diff] [blame] | 192 | if (isa<MDNode>(V) || isa<MDString>(V)) { |
Devang Patel | d5ac404 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 193 | ValueMapType::const_iterator I = MDValueMap.find(V); |
| 194 | assert(I != MDValueMap.end() && "Value not in slotcalculator!"); |
| 195 | return I->second-1; |
| 196 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 197 | |
Devang Patel | d5ac404 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 198 | ValueMapType::const_iterator I = ValueMap.find(V); |
| 199 | assert(I != ValueMap.end() && "Value not in slotcalculator!"); |
| 200 | return I->second-1; |
| 201 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 202 | |
Chris Lattner | 6da91d3 | 2007-05-04 05:21:47 +0000 | [diff] [blame] | 203 | // Optimize constant ordering. |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 204 | namespace { |
| 205 | struct CstSortPredicate { |
| 206 | ValueEnumerator &VE; |
| 207 | explicit CstSortPredicate(ValueEnumerator &ve) : VE(ve) {} |
| 208 | bool operator()(const std::pair<const Value*, unsigned> &LHS, |
| 209 | const std::pair<const Value*, unsigned> &RHS) { |
| 210 | // Sort by plane. |
| 211 | if (LHS.first->getType() != RHS.first->getType()) |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 212 | return VE.getTypeID(LHS.first->getType()) < |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 213 | VE.getTypeID(RHS.first->getType()); |
| 214 | // Then by frequency. |
| 215 | return LHS.second > RHS.second; |
| 216 | } |
| 217 | }; |
| 218 | } |
Chris Lattner | 6da91d3 | 2007-05-04 05:21:47 +0000 | [diff] [blame] | 219 | |
| 220 | /// OptimizeConstants - Reorder constant pool for denser encoding. |
| 221 | void ValueEnumerator::OptimizeConstants(unsigned CstStart, unsigned CstEnd) { |
| 222 | if (CstStart == CstEnd || CstStart+1 == CstEnd) return; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 223 | |
Chris Lattner | 6da91d3 | 2007-05-04 05:21:47 +0000 | [diff] [blame] | 224 | CstSortPredicate P(*this); |
| 225 | std::stable_sort(Values.begin()+CstStart, Values.begin()+CstEnd, P); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 226 | |
Chris Lattner | 6da91d3 | 2007-05-04 05:21:47 +0000 | [diff] [blame] | 227 | // Ensure that integer constants are at the start of the constant pool. This |
| 228 | // is important so that GEP structure indices come before gep constant exprs. |
| 229 | std::partition(Values.begin()+CstStart, Values.begin()+CstEnd, |
| 230 | isIntegerValue); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 231 | |
Chris Lattner | 6da91d3 | 2007-05-04 05:21:47 +0000 | [diff] [blame] | 232 | // Rebuild the modified portion of ValueMap. |
| 233 | for (; CstStart != CstEnd; ++CstStart) |
| 234 | ValueMap[Values[CstStart].first] = CstStart+1; |
| 235 | } |
| 236 | |
| 237 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 238 | /// EnumerateTypeSymbolTable - Insert all of the types in the specified symbol |
| 239 | /// table. |
| 240 | void ValueEnumerator::EnumerateTypeSymbolTable(const TypeSymbolTable &TST) { |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 241 | for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end(); |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 242 | TI != TE; ++TI) |
| 243 | EnumerateType(TI->second); |
| 244 | } |
| 245 | |
| 246 | /// EnumerateValueSymbolTable - Insert all of the values in the specified symbol |
| 247 | /// table into the values table. |
| 248 | void ValueEnumerator::EnumerateValueSymbolTable(const ValueSymbolTable &VST) { |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 249 | for (ValueSymbolTable::const_iterator VI = VST.begin(), VE = VST.end(); |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 250 | VI != VE; ++VI) |
| 251 | EnumerateValue(VI->getValue()); |
| 252 | } |
| 253 | |
Dan Gohman | 17aa92c | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 254 | /// EnumerateNamedMetadata - Insert all of the values referenced by |
| 255 | /// named metadata in the specified module. |
| 256 | void ValueEnumerator::EnumerateNamedMetadata(const Module *M) { |
| 257 | for (Module::const_named_metadata_iterator I = M->named_metadata_begin(), |
| 258 | E = M->named_metadata_end(); I != E; ++I) |
| 259 | EnumerateNamedMDNode(I); |
Devang Patel | 0386f01 | 2010-01-07 19:39:36 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Devang Patel | 8fba578 | 2010-01-09 00:30:14 +0000 | [diff] [blame] | 262 | void ValueEnumerator::EnumerateNamedMDNode(const NamedMDNode *MD) { |
Devang Patel | 8fba578 | 2010-01-09 00:30:14 +0000 | [diff] [blame] | 263 | for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i) |
Dan Gohman | 078b053 | 2010-08-24 02:01:24 +0000 | [diff] [blame] | 264 | EnumerateMetadata(MD->getOperand(i)); |
Devang Patel | 8fba578 | 2010-01-09 00:30:14 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Dan Gohman | 309b3af | 2010-08-24 02:24:03 +0000 | [diff] [blame] | 267 | /// EnumerateMDNodeOperands - Enumerate all non-function-local values |
| 268 | /// and types referenced by the given MDNode. |
| 269 | void ValueEnumerator::EnumerateMDNodeOperands(const MDNode *N) { |
| 270 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { |
| 271 | if (Value *V = N->getOperand(i)) { |
| 272 | if (isa<MDNode>(V) || isa<MDString>(V)) |
| 273 | EnumerateMetadata(V); |
| 274 | else if (!isa<Instruction>(V) && !isa<Argument>(V)) |
| 275 | EnumerateValue(V); |
| 276 | } else |
| 277 | EnumerateType(Type::getVoidTy(N->getContext())); |
| 278 | } |
| 279 | } |
| 280 | |
Devang Patel | bc5201f | 2010-01-22 22:52:10 +0000 | [diff] [blame] | 281 | void ValueEnumerator::EnumerateMetadata(const Value *MD) { |
Benjamin Kramer | e88a8e6 | 2010-01-23 09:54:23 +0000 | [diff] [blame] | 282 | assert((isa<MDNode>(MD) || isa<MDString>(MD)) && "Invalid metadata kind"); |
Dan Gohman | 309b3af | 2010-08-24 02:24:03 +0000 | [diff] [blame] | 283 | |
| 284 | // Enumerate the type of this value. |
| 285 | EnumerateType(MD->getType()); |
| 286 | |
| 287 | const MDNode *N = dyn_cast<MDNode>(MD); |
| 288 | |
| 289 | // In the module-level pass, skip function-local nodes themselves, but |
| 290 | // do walk their operands. |
| 291 | if (N && N->isFunctionLocal() && N->getFunction()) { |
| 292 | EnumerateMDNodeOperands(N); |
| 293 | return; |
| 294 | } |
| 295 | |
Devang Patel | d5ac404 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 296 | // Check to see if it's already in! |
| 297 | unsigned &MDValueID = MDValueMap[MD]; |
| 298 | if (MDValueID) { |
| 299 | // Increment use count. |
| 300 | MDValues[MDValueID-1].second++; |
| 301 | return; |
| 302 | } |
Devang Patel | d5ac404 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 303 | MDValues.push_back(std::make_pair(MD, 1U)); |
| 304 | MDValueID = MDValues.size(); |
Dan Gohman | 309b3af | 2010-08-24 02:24:03 +0000 | [diff] [blame] | 305 | |
| 306 | // Enumerate all non-function-local operands. |
| 307 | if (N) |
| 308 | EnumerateMDNodeOperands(N); |
| 309 | } |
| 310 | |
| 311 | /// EnumerateFunctionLocalMetadataa - Incorporate function-local metadata |
| 312 | /// information reachable from the given MDNode. |
| 313 | void ValueEnumerator::EnumerateFunctionLocalMetadata(const MDNode *N) { |
| 314 | assert(N->isFunctionLocal() && N->getFunction() && |
| 315 | "EnumerateFunctionLocalMetadata called on non-function-local mdnode!"); |
| 316 | |
| 317 | // Enumerate the type of this value. |
| 318 | EnumerateType(N->getType()); |
| 319 | |
| 320 | // Check to see if it's already in! |
| 321 | unsigned &MDValueID = MDValueMap[N]; |
| 322 | if (MDValueID) { |
| 323 | // Increment use count. |
| 324 | MDValues[MDValueID-1].second++; |
| 325 | return; |
| 326 | } |
| 327 | MDValues.push_back(std::make_pair(N, 1U)); |
| 328 | MDValueID = MDValues.size(); |
| 329 | |
| 330 | // To incoroporate function-local information visit all function-local |
| 331 | // MDNodes and all function-local values they reference. |
| 332 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) |
| 333 | if (Value *V = N->getOperand(i)) { |
Dan Gohman | d01347e | 2010-08-24 02:40:27 +0000 | [diff] [blame] | 334 | if (MDNode *O = dyn_cast<MDNode>(V)) { |
Dan Gohman | 309b3af | 2010-08-24 02:24:03 +0000 | [diff] [blame] | 335 | if (O->isFunctionLocal() && O->getFunction()) |
| 336 | EnumerateFunctionLocalMetadata(O); |
Dan Gohman | d01347e | 2010-08-24 02:40:27 +0000 | [diff] [blame] | 337 | } else if (isa<Instruction>(V) || isa<Argument>(V)) |
Dan Gohman | 309b3af | 2010-08-24 02:24:03 +0000 | [diff] [blame] | 338 | EnumerateValue(V); |
| 339 | } |
| 340 | |
| 341 | // Also, collect all function-local MDNodes for easy access. |
| 342 | FunctionLocalMDs.push_back(N); |
Devang Patel | d5ac404 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Victor Hernandez | d7e6457 | 2010-01-14 19:54:11 +0000 | [diff] [blame] | 345 | void ValueEnumerator::EnumerateValue(const Value *V) { |
Chris Lattner | cc7b011 | 2009-12-31 00:51:46 +0000 | [diff] [blame] | 346 | assert(!V->getType()->isVoidTy() && "Can't insert void values!"); |
Dan Gohman | 309b3af | 2010-08-24 02:24:03 +0000 | [diff] [blame] | 347 | assert(!isa<MDNode>(V) && !isa<MDString>(V) && |
| 348 | "EnumerateValue doesn't handle Metadata!"); |
Devang Patel | d5ac404 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 349 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 350 | // Check to see if it's already in! |
| 351 | unsigned &ValueID = ValueMap[V]; |
| 352 | if (ValueID) { |
| 353 | // Increment use count. |
| 354 | Values[ValueID-1].second++; |
| 355 | return; |
| 356 | } |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 357 | |
Chris Lattner | 7a303d1 | 2007-05-06 01:00:28 +0000 | [diff] [blame] | 358 | // Enumerate the type of this value. |
| 359 | EnumerateType(V->getType()); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 360 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 361 | if (const Constant *C = dyn_cast<Constant>(V)) { |
| 362 | if (isa<GlobalValue>(C)) { |
| 363 | // Initializers for globals are handled explicitly elsewhere. |
Chris Lattner | ff7fc5d | 2007-05-06 00:35:24 +0000 | [diff] [blame] | 364 | } else if (isa<ConstantArray>(C) && cast<ConstantArray>(C)->isString()) { |
| 365 | // Do not enumerate the initializers for an array of simple characters. |
Chris Lattner | 7a2bdde | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 366 | // The initializers just pollute the value table, and we emit the strings |
Chris Lattner | ff7fc5d | 2007-05-06 00:35:24 +0000 | [diff] [blame] | 367 | // specially. |
Chris Lattner | 7a303d1 | 2007-05-06 01:00:28 +0000 | [diff] [blame] | 368 | } else if (C->getNumOperands()) { |
| 369 | // If a constant has operands, enumerate them. This makes sure that if a |
| 370 | // constant has uses (for example an array of const ints), that they are |
| 371 | // inserted also. |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 372 | |
Chris Lattner | 7a303d1 | 2007-05-06 01:00:28 +0000 | [diff] [blame] | 373 | // We prefer to enumerate them with values before we enumerate the user |
| 374 | // itself. This makes it more likely that we can avoid forward references |
| 375 | // in the reader. We know that there can be no cycles in the constants |
| 376 | // graph that don't go through a global variable. |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 377 | for (User::const_op_iterator I = C->op_begin(), E = C->op_end(); |
| 378 | I != E; ++I) |
Chris Lattner | cdfc940 | 2009-11-01 01:27:45 +0000 | [diff] [blame] | 379 | if (!isa<BasicBlock>(*I)) // Don't enumerate BB operand to BlockAddress. |
Victor Hernandez | d7e6457 | 2010-01-14 19:54:11 +0000 | [diff] [blame] | 380 | EnumerateValue(*I); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 381 | |
Chris Lattner | 7a303d1 | 2007-05-06 01:00:28 +0000 | [diff] [blame] | 382 | // Finally, add the value. Doing this could make the ValueID reference be |
| 383 | // dangling, don't reuse it. |
| 384 | Values.push_back(std::make_pair(V, 1U)); |
| 385 | ValueMap[V] = Values.size(); |
| 386 | return; |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 387 | } |
| 388 | } |
Devang Patel | 104cf9e | 2009-07-23 01:07:34 +0000 | [diff] [blame] | 389 | |
Chris Lattner | 7a303d1 | 2007-05-06 01:00:28 +0000 | [diff] [blame] | 390 | // Add the value. |
| 391 | Values.push_back(std::make_pair(V, 1U)); |
| 392 | ValueID = Values.size(); |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | |
| 396 | void ValueEnumerator::EnumerateType(const Type *Ty) { |
| 397 | unsigned &TypeID = TypeMap[Ty]; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 398 | |
Rafael Espindola | f5a9056 | 2011-04-06 16:49:37 +0000 | [diff] [blame] | 399 | // We've already seen this type. |
| 400 | if (TypeID) |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 401 | return; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 402 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 403 | // First time we saw this type, add it. |
Rafael Espindola | f5a9056 | 2011-04-06 16:49:37 +0000 | [diff] [blame] | 404 | Types.push_back(Ty); |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 405 | TypeID = Types.size(); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 406 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 407 | // Enumerate subtypes. |
| 408 | for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end(); |
| 409 | I != E; ++I) |
| 410 | EnumerateType(*I); |
| 411 | } |
| 412 | |
Chris Lattner | 33f1d5b | 2007-05-06 08:35:19 +0000 | [diff] [blame] | 413 | // Enumerate the types for the specified value. If the value is a constant, |
| 414 | // walk through it, enumerating the types of the constant. |
Victor Hernandez | d7e6457 | 2010-01-14 19:54:11 +0000 | [diff] [blame] | 415 | void ValueEnumerator::EnumerateOperandType(const Value *V) { |
Chris Lattner | 33f1d5b | 2007-05-06 08:35:19 +0000 | [diff] [blame] | 416 | EnumerateType(V->getType()); |
Victor Hernandez | ab9cd10 | 2010-01-13 19:36:16 +0000 | [diff] [blame] | 417 | |
Chris Lattner | 33f1d5b | 2007-05-06 08:35:19 +0000 | [diff] [blame] | 418 | if (const Constant *C = dyn_cast<Constant>(V)) { |
| 419 | // If this constant is already enumerated, ignore it, we know its type must |
| 420 | // be enumerated. |
| 421 | if (ValueMap.count(V)) return; |
| 422 | |
| 423 | // This constant may have operands, make sure to enumerate the types in |
| 424 | // them. |
Chris Lattner | 837e04a | 2009-10-28 05:24:40 +0000 | [diff] [blame] | 425 | for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) { |
Jay Foad | 8340d0b | 2011-04-11 09:48:55 +0000 | [diff] [blame] | 426 | const Value *Op = C->getOperand(i); |
Chris Lattner | 837e04a | 2009-10-28 05:24:40 +0000 | [diff] [blame] | 427 | |
| 428 | // Don't enumerate basic blocks here, this happens as operands to |
| 429 | // blockaddress. |
| 430 | if (isa<BasicBlock>(Op)) continue; |
| 431 | |
Dan Gohman | 879d811 | 2010-08-25 17:09:03 +0000 | [diff] [blame] | 432 | EnumerateOperandType(Op); |
Chris Lattner | 837e04a | 2009-10-28 05:24:40 +0000 | [diff] [blame] | 433 | } |
Nick Lewycky | cb33799 | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 434 | |
| 435 | if (const MDNode *N = dyn_cast<MDNode>(V)) { |
Chris Lattner | 5d0cacd | 2009-12-31 01:22:29 +0000 | [diff] [blame] | 436 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) |
| 437 | if (Value *Elem = N->getOperand(i)) |
Victor Hernandez | d7e6457 | 2010-01-14 19:54:11 +0000 | [diff] [blame] | 438 | EnumerateOperandType(Elem); |
Nick Lewycky | cb33799 | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 439 | } |
Devang Patel | 104cf9e | 2009-07-23 01:07:34 +0000 | [diff] [blame] | 440 | } else if (isa<MDString>(V) || isa<MDNode>(V)) |
Dan Gohman | 78aeae2 | 2010-08-24 02:10:52 +0000 | [diff] [blame] | 441 | EnumerateMetadata(V); |
Chris Lattner | 33f1d5b | 2007-05-06 08:35:19 +0000 | [diff] [blame] | 442 | } |
| 443 | |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 444 | void ValueEnumerator::EnumerateAttributes(const AttrListPtr &PAL) { |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 445 | if (PAL.isEmpty()) return; // null is always 0. |
Chris Lattner | 50954f5 | 2007-05-03 22:46:43 +0000 | [diff] [blame] | 446 | // Do a lookup. |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 447 | unsigned &Entry = AttributeMap[PAL.getRawPointer()]; |
Chris Lattner | 50954f5 | 2007-05-03 22:46:43 +0000 | [diff] [blame] | 448 | if (Entry == 0) { |
| 449 | // Never saw this before, add it. |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 450 | Attributes.push_back(PAL); |
| 451 | Entry = Attributes.size(); |
Chris Lattner | 50954f5 | 2007-05-03 22:46:43 +0000 | [diff] [blame] | 452 | } |
| 453 | } |
| 454 | |
Chad Rosier | 6a0c04d | 2011-06-03 17:02:19 +0000 | [diff] [blame^] | 455 | void ValueEnumerator::incorporateFunction(const Function &F) { |
Nick Lewycky | 9a49f15 | 2010-02-25 08:30:17 +0000 | [diff] [blame] | 456 | InstructionCount = 0; |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 457 | NumModuleValues = Values.size(); |
Dan Gohman | 309b3af | 2010-08-24 02:24:03 +0000 | [diff] [blame] | 458 | NumModuleMDValues = MDValues.size(); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 459 | |
Chris Lattner | 8d35c79 | 2007-04-26 03:50:57 +0000 | [diff] [blame] | 460 | // Adding function arguments to the value table. |
Dan Gohman | 6dd26ba | 2010-07-16 22:58:39 +0000 | [diff] [blame] | 461 | for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end(); |
| 462 | I != E; ++I) |
Chris Lattner | 8d35c79 | 2007-04-26 03:50:57 +0000 | [diff] [blame] | 463 | EnumerateValue(I); |
| 464 | |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 465 | FirstFuncConstantID = Values.size(); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 466 | |
Chris Lattner | 8d35c79 | 2007-04-26 03:50:57 +0000 | [diff] [blame] | 467 | // Add all function-level constants to the value table. |
| 468 | for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) { |
| 469 | for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 470 | for (User::const_op_iterator OI = I->op_begin(), E = I->op_end(); |
Chris Lattner | 8d35c79 | 2007-04-26 03:50:57 +0000 | [diff] [blame] | 471 | OI != E; ++OI) { |
| 472 | if ((isa<Constant>(*OI) && !isa<GlobalValue>(*OI)) || |
| 473 | isa<InlineAsm>(*OI)) |
| 474 | EnumerateValue(*OI); |
| 475 | } |
Chris Lattner | c59c0af | 2007-04-26 04:42:16 +0000 | [diff] [blame] | 476 | BasicBlocks.push_back(BB); |
Chris Lattner | e825ed5 | 2007-05-03 22:18:21 +0000 | [diff] [blame] | 477 | ValueMap[BB] = BasicBlocks.size(); |
Chris Lattner | 8d35c79 | 2007-04-26 03:50:57 +0000 | [diff] [blame] | 478 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 479 | |
Chris Lattner | 6da91d3 | 2007-05-04 05:21:47 +0000 | [diff] [blame] | 480 | // Optimize the constant layout. |
| 481 | OptimizeConstants(FirstFuncConstantID, Values.size()); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 482 | |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 483 | // Add the function's parameter attributes so they are available for use in |
| 484 | // the function's instruction. |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 485 | EnumerateAttributes(F.getAttributes()); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 486 | |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 487 | FirstInstID = Values.size(); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 488 | |
Devang Patel | 6209869 | 2010-06-02 23:05:04 +0000 | [diff] [blame] | 489 | SmallVector<MDNode *, 8> FnLocalMDVector; |
Chris Lattner | 8d35c79 | 2007-04-26 03:50:57 +0000 | [diff] [blame] | 490 | // Add all of the instructions. |
| 491 | 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] | 492 | for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) { |
Victor Hernandez | ab9cd10 | 2010-01-13 19:36:16 +0000 | [diff] [blame] | 493 | for (User::const_op_iterator OI = I->op_begin(), E = I->op_end(); |
| 494 | OI != E; ++OI) { |
Victor Hernandez | d7e6457 | 2010-01-14 19:54:11 +0000 | [diff] [blame] | 495 | if (MDNode *MD = dyn_cast<MDNode>(*OI)) |
Victor Hernandez | 2b3365c | 2010-02-06 01:21:09 +0000 | [diff] [blame] | 496 | if (MD->isFunctionLocal() && MD->getFunction()) |
Victor Hernandez | af6ce14 | 2010-02-04 01:13:08 +0000 | [diff] [blame] | 497 | // Enumerate metadata after the instructions they might refer to. |
Devang Patel | 6209869 | 2010-06-02 23:05:04 +0000 | [diff] [blame] | 498 | FnLocalMDVector.push_back(MD); |
Victor Hernandez | ab9cd10 | 2010-01-13 19:36:16 +0000 | [diff] [blame] | 499 | } |
Dan Gohman | 309b3af | 2010-08-24 02:24:03 +0000 | [diff] [blame] | 500 | |
| 501 | SmallVector<std::pair<unsigned, MDNode*>, 8> MDs; |
| 502 | I->getAllMetadataOtherThanDebugLoc(MDs); |
| 503 | for (unsigned i = 0, e = MDs.size(); i != e; ++i) { |
| 504 | MDNode *N = MDs[i].second; |
| 505 | if (N->isFunctionLocal() && N->getFunction()) |
| 506 | FnLocalMDVector.push_back(N); |
| 507 | } |
| 508 | |
Benjamin Kramer | f012705 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 509 | if (!I->getType()->isVoidTy()) |
Chris Lattner | 8d35c79 | 2007-04-26 03:50:57 +0000 | [diff] [blame] | 510 | EnumerateValue(I); |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 511 | } |
| 512 | } |
Victor Hernandez | af6ce14 | 2010-02-04 01:13:08 +0000 | [diff] [blame] | 513 | |
| 514 | // Add all of the function-local metadata. |
Devang Patel | 6209869 | 2010-06-02 23:05:04 +0000 | [diff] [blame] | 515 | for (unsigned i = 0, e = FnLocalMDVector.size(); i != e; ++i) |
Dan Gohman | 309b3af | 2010-08-24 02:24:03 +0000 | [diff] [blame] | 516 | EnumerateFunctionLocalMetadata(FnLocalMDVector[i]); |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Chad Rosier | 6a0c04d | 2011-06-03 17:02:19 +0000 | [diff] [blame^] | 519 | void ValueEnumerator::purgeFunction() { |
Chris Lattner | 8d35c79 | 2007-04-26 03:50:57 +0000 | [diff] [blame] | 520 | /// Remove purged values from the ValueMap. |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 521 | for (unsigned i = NumModuleValues, e = Values.size(); i != e; ++i) |
Chris Lattner | 8d35c79 | 2007-04-26 03:50:57 +0000 | [diff] [blame] | 522 | ValueMap.erase(Values[i].first); |
Dan Gohman | 309b3af | 2010-08-24 02:24:03 +0000 | [diff] [blame] | 523 | for (unsigned i = NumModuleMDValues, e = MDValues.size(); i != e; ++i) |
| 524 | MDValueMap.erase(MDValues[i].first); |
Chris Lattner | c59c0af | 2007-04-26 04:42:16 +0000 | [diff] [blame] | 525 | for (unsigned i = 0, e = BasicBlocks.size(); i != e; ++i) |
| 526 | ValueMap.erase(BasicBlocks[i]); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 527 | |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 528 | Values.resize(NumModuleValues); |
Dan Gohman | 309b3af | 2010-08-24 02:24:03 +0000 | [diff] [blame] | 529 | MDValues.resize(NumModuleMDValues); |
Chris Lattner | c59c0af | 2007-04-26 04:42:16 +0000 | [diff] [blame] | 530 | BasicBlocks.clear(); |
Dan Gohman | 848c9ae | 2010-08-25 17:11:16 +0000 | [diff] [blame] | 531 | FunctionLocalMDs.clear(); |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 532 | } |
Chris Lattner | 837e04a | 2009-10-28 05:24:40 +0000 | [diff] [blame] | 533 | |
| 534 | static void IncorporateFunctionInfoGlobalBBIDs(const Function *F, |
| 535 | DenseMap<const BasicBlock*, unsigned> &IDMap) { |
| 536 | unsigned Counter = 0; |
| 537 | for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB) |
| 538 | IDMap[BB] = ++Counter; |
| 539 | } |
| 540 | |
| 541 | /// getGlobalBasicBlockID - This returns the function-specific ID for the |
| 542 | /// specified basic block. This is relatively expensive information, so it |
| 543 | /// should only be used by rare constructs such as address-of-label. |
| 544 | unsigned ValueEnumerator::getGlobalBasicBlockID(const BasicBlock *BB) const { |
| 545 | unsigned &Idx = GlobalBasicBlockIDs[BB]; |
| 546 | if (Idx != 0) |
Chris Lattner | cdfc940 | 2009-11-01 01:27:45 +0000 | [diff] [blame] | 547 | return Idx-1; |
Chris Lattner | 837e04a | 2009-10-28 05:24:40 +0000 | [diff] [blame] | 548 | |
| 549 | IncorporateFunctionInfoGlobalBBIDs(BB->getParent(), GlobalBasicBlockIDs); |
| 550 | return getGlobalBasicBlockID(BB); |
| 551 | } |
| 552 | |