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/STLExtras.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/SmallPtrSet.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 17 | #include "llvm/IR/Constants.h" |
| 18 | #include "llvm/IR/DerivedTypes.h" |
| 19 | #include "llvm/IR/Instructions.h" |
| 20 | #include "llvm/IR/Module.h" |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 21 | #include "llvm/IR/UseListOrder.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 22 | #include "llvm/IR/ValueSymbolTable.h" |
Chad Rosier | 4e6c03f | 2011-12-07 20:44:46 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Debug.h" |
| 24 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 12f535b | 2007-05-04 05:05:48 +0000 | [diff] [blame] | 25 | #include <algorithm> |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 26 | using namespace llvm; |
| 27 | |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 28 | namespace { |
| 29 | struct OrderMap { |
| 30 | DenseMap<const Value *, std::pair<unsigned, bool>> IDs; |
| 31 | unsigned LastGlobalConstantID; |
| 32 | unsigned LastGlobalValueID; |
| 33 | |
| 34 | OrderMap() : LastGlobalConstantID(0), LastGlobalValueID(0) {} |
| 35 | |
| 36 | bool isGlobalConstant(unsigned ID) const { |
| 37 | return ID <= LastGlobalConstantID; |
| 38 | } |
| 39 | bool isGlobalValue(unsigned ID) const { |
| 40 | return ID <= LastGlobalValueID && !isGlobalConstant(ID); |
| 41 | } |
| 42 | |
| 43 | unsigned size() const { return IDs.size(); } |
| 44 | std::pair<unsigned, bool> &operator[](const Value *V) { return IDs[V]; } |
| 45 | std::pair<unsigned, bool> lookup(const Value *V) const { |
| 46 | return IDs.lookup(V); |
| 47 | } |
| 48 | void index(const Value *V) { |
| 49 | // Explicitly sequence get-size and insert-value operations to avoid UB. |
| 50 | unsigned ID = IDs.size() + 1; |
| 51 | IDs[V].first = ID; |
| 52 | } |
| 53 | }; |
| 54 | } |
| 55 | |
| 56 | static void orderValue(const Value *V, OrderMap &OM) { |
| 57 | if (OM.lookup(V).first) |
| 58 | return; |
| 59 | |
| 60 | if (const Constant *C = dyn_cast<Constant>(V)) |
| 61 | if (C->getNumOperands() && !isa<GlobalValue>(C)) |
| 62 | for (const Value *Op : C->operands()) |
| 63 | if (!isa<BasicBlock>(Op) && !isa<GlobalValue>(Op)) |
| 64 | orderValue(Op, OM); |
| 65 | |
| 66 | // Note: we cannot cache this lookup above, since inserting into the map |
| 67 | // changes the map's size, and thus affects the other IDs. |
| 68 | OM.index(V); |
| 69 | } |
| 70 | |
| 71 | static OrderMap orderModule(const Module &M) { |
| 72 | // This needs to match the order used by ValueEnumerator::ValueEnumerator() |
| 73 | // and ValueEnumerator::incorporateFunction(). |
| 74 | OrderMap OM; |
| 75 | |
| 76 | // In the reader, initializers of GlobalValues are set *after* all the |
| 77 | // globals have been read. Rather than awkwardly modeling this behaviour |
| 78 | // directly in predictValueUseListOrderImpl(), just assign IDs to |
| 79 | // initializers of GlobalValues before GlobalValues themselves to model this |
| 80 | // implicitly. |
| 81 | for (const GlobalVariable &G : M.globals()) |
| 82 | if (G.hasInitializer()) |
| 83 | if (!isa<GlobalValue>(G.getInitializer())) |
| 84 | orderValue(G.getInitializer(), OM); |
| 85 | for (const GlobalAlias &A : M.aliases()) |
| 86 | if (!isa<GlobalValue>(A.getAliasee())) |
| 87 | orderValue(A.getAliasee(), OM); |
| 88 | for (const Function &F : M) |
| 89 | if (F.hasPrefixData()) |
| 90 | if (!isa<GlobalValue>(F.getPrefixData())) |
| 91 | orderValue(F.getPrefixData(), OM); |
| 92 | OM.LastGlobalConstantID = OM.size(); |
| 93 | |
| 94 | // Initializers of GlobalValues are processed in |
| 95 | // BitcodeReader::ResolveGlobalAndAliasInits(). Match the order there rather |
| 96 | // than ValueEnumerator, and match the code in predictValueUseListOrderImpl() |
| 97 | // by giving IDs in reverse order. |
| 98 | // |
| 99 | // Since GlobalValues never reference each other directly (just through |
| 100 | // initializers), their relative IDs only matter for determining order of |
| 101 | // uses in their initializers. |
| 102 | for (const Function &F : M) |
| 103 | orderValue(&F, OM); |
| 104 | for (const GlobalAlias &A : M.aliases()) |
| 105 | orderValue(&A, OM); |
| 106 | for (const GlobalVariable &G : M.globals()) |
| 107 | orderValue(&G, OM); |
| 108 | OM.LastGlobalValueID = OM.size(); |
| 109 | |
| 110 | for (const Function &F : M) { |
| 111 | if (F.isDeclaration()) |
| 112 | continue; |
| 113 | // Here we need to match the union of ValueEnumerator::incorporateFunction() |
| 114 | // and WriteFunction(). Basic blocks are implicitly declared before |
| 115 | // anything else (by declaring their size). |
| 116 | for (const BasicBlock &BB : F) |
| 117 | orderValue(&BB, OM); |
| 118 | for (const Argument &A : F.args()) |
| 119 | orderValue(&A, OM); |
| 120 | for (const BasicBlock &BB : F) |
| 121 | for (const Instruction &I : BB) |
| 122 | for (const Value *Op : I.operands()) |
| 123 | if ((isa<Constant>(*Op) && !isa<GlobalValue>(*Op)) || |
| 124 | isa<InlineAsm>(*Op)) |
| 125 | orderValue(Op, OM); |
| 126 | for (const BasicBlock &BB : F) |
| 127 | for (const Instruction &I : BB) |
| 128 | orderValue(&I, OM); |
| 129 | } |
| 130 | return OM; |
| 131 | } |
| 132 | |
| 133 | static void predictValueUseListOrderImpl(const Value *V, const Function *F, |
| 134 | unsigned ID, const OrderMap &OM, |
| 135 | UseListOrderStack &Stack) { |
| 136 | // Predict use-list order for this one. |
| 137 | typedef std::pair<const Use *, unsigned> Entry; |
| 138 | SmallVector<Entry, 64> List; |
| 139 | for (const Use &U : V->uses()) |
| 140 | // Check if this user will be serialized. |
| 141 | if (OM.lookup(U.getUser()).first) |
| 142 | List.push_back(std::make_pair(&U, List.size())); |
| 143 | |
| 144 | if (List.size() < 2) |
| 145 | // We may have lost some users. |
| 146 | return; |
| 147 | |
| 148 | bool IsGlobalValue = OM.isGlobalValue(ID); |
| 149 | std::sort(List.begin(), List.end(), [&](const Entry &L, const Entry &R) { |
| 150 | const Use *LU = L.first; |
| 151 | const Use *RU = R.first; |
| 152 | if (LU == RU) |
| 153 | return false; |
| 154 | |
| 155 | auto LID = OM.lookup(LU->getUser()).first; |
| 156 | auto RID = OM.lookup(RU->getUser()).first; |
| 157 | |
| 158 | // Global values are processed in reverse order. |
| 159 | // |
| 160 | // Moreover, initializers of GlobalValues are set *after* all the globals |
| 161 | // have been read (despite having earlier IDs). Rather than awkwardly |
| 162 | // modeling this behaviour here, orderModule() has assigned IDs to |
| 163 | // initializers of GlobalValues before GlobalValues themselves. |
| 164 | if (OM.isGlobalValue(LID) && OM.isGlobalValue(RID)) |
| 165 | return LID < RID; |
| 166 | |
| 167 | // If ID is 4, then expect: 7 6 5 1 2 3. |
| 168 | if (LID < RID) { |
| 169 | if (RID <= ID) |
| 170 | if (!IsGlobalValue) // GlobalValue uses don't get reversed. |
| 171 | return true; |
| 172 | return false; |
| 173 | } |
| 174 | if (RID < LID) { |
| 175 | if (LID <= ID) |
| 176 | if (!IsGlobalValue) // GlobalValue uses don't get reversed. |
| 177 | return false; |
| 178 | return true; |
| 179 | } |
| 180 | |
| 181 | // LID and RID are equal, so we have different operands of the same user. |
| 182 | // Assume operands are added in order for all instructions. |
| 183 | if (LID <= ID) |
| 184 | if (!IsGlobalValue) // GlobalValue uses don't get reversed. |
| 185 | return LU->getOperandNo() < RU->getOperandNo(); |
| 186 | return LU->getOperandNo() > RU->getOperandNo(); |
| 187 | }); |
| 188 | |
| 189 | if (std::is_sorted( |
| 190 | List.begin(), List.end(), |
| 191 | [](const Entry &L, const Entry &R) { return L.second < R.second; })) |
| 192 | // Order is already correct. |
| 193 | return; |
| 194 | |
| 195 | // Store the shuffle. |
| 196 | Stack.emplace_back(V, F, List.size()); |
| 197 | assert(List.size() == Stack.back().Shuffle.size() && "Wrong size"); |
| 198 | for (size_t I = 0, E = List.size(); I != E; ++I) |
| 199 | Stack.back().Shuffle[I] = List[I].second; |
| 200 | } |
| 201 | |
| 202 | static void predictValueUseListOrder(const Value *V, const Function *F, |
| 203 | OrderMap &OM, UseListOrderStack &Stack) { |
| 204 | auto &IDPair = OM[V]; |
| 205 | assert(IDPair.first && "Unmapped value"); |
| 206 | if (IDPair.second) |
| 207 | // Already predicted. |
| 208 | return; |
| 209 | |
| 210 | // Do the actual prediction. |
| 211 | IDPair.second = true; |
| 212 | if (!V->use_empty() && std::next(V->use_begin()) != V->use_end()) |
| 213 | predictValueUseListOrderImpl(V, F, IDPair.first, OM, Stack); |
| 214 | |
| 215 | // Recursive descent into constants. |
| 216 | if (const Constant *C = dyn_cast<Constant>(V)) |
| 217 | if (C->getNumOperands()) // Visit GlobalValues. |
| 218 | for (const Value *Op : C->operands()) |
| 219 | if (isa<Constant>(Op)) // Visit GlobalValues. |
| 220 | predictValueUseListOrder(Op, F, OM, Stack); |
| 221 | } |
| 222 | |
| 223 | static UseListOrderStack predictUseListOrder(const Module &M) { |
| 224 | OrderMap OM = orderModule(M); |
| 225 | |
| 226 | // Use-list orders need to be serialized after all the users have been added |
| 227 | // to a value, or else the shuffles will be incomplete. Store them per |
| 228 | // function in a stack. |
| 229 | // |
| 230 | // Aside from function order, the order of values doesn't matter much here. |
| 231 | UseListOrderStack Stack; |
| 232 | |
| 233 | // We want to visit the functions backward now so we can list function-local |
| 234 | // constants in the last Function they're used in. Module-level constants |
| 235 | // have already been visited above. |
| 236 | for (auto I = M.rbegin(), E = M.rend(); I != E; ++I) { |
| 237 | const Function &F = *I; |
| 238 | if (F.isDeclaration()) |
| 239 | continue; |
| 240 | for (const BasicBlock &BB : F) |
| 241 | predictValueUseListOrder(&BB, &F, OM, Stack); |
| 242 | for (const Argument &A : F.args()) |
| 243 | predictValueUseListOrder(&A, &F, OM, Stack); |
| 244 | for (const BasicBlock &BB : F) |
| 245 | for (const Instruction &I : BB) |
| 246 | for (const Value *Op : I.operands()) |
| 247 | if (isa<Constant>(*Op) || isa<InlineAsm>(*Op)) // Visit GlobalValues. |
| 248 | predictValueUseListOrder(Op, &F, OM, Stack); |
| 249 | for (const BasicBlock &BB : F) |
| 250 | for (const Instruction &I : BB) |
| 251 | predictValueUseListOrder(&I, &F, OM, Stack); |
| 252 | } |
| 253 | |
| 254 | // Visit globals last, since the module-level use-list block will be seen |
| 255 | // before the function bodies are processed. |
| 256 | for (const GlobalVariable &G : M.globals()) |
| 257 | predictValueUseListOrder(&G, nullptr, OM, Stack); |
| 258 | for (const Function &F : M) |
| 259 | predictValueUseListOrder(&F, nullptr, OM, Stack); |
| 260 | for (const GlobalAlias &A : M.aliases()) |
| 261 | predictValueUseListOrder(&A, nullptr, OM, Stack); |
| 262 | for (const GlobalVariable &G : M.globals()) |
| 263 | if (G.hasInitializer()) |
| 264 | predictValueUseListOrder(G.getInitializer(), nullptr, OM, Stack); |
| 265 | for (const GlobalAlias &A : M.aliases()) |
| 266 | predictValueUseListOrder(A.getAliasee(), nullptr, OM, Stack); |
| 267 | for (const Function &F : M) |
| 268 | if (F.hasPrefixData()) |
| 269 | predictValueUseListOrder(F.getPrefixData(), nullptr, OM, Stack); |
| 270 | |
| 271 | return Stack; |
| 272 | } |
| 273 | |
Duncan Sands | 2333e29 | 2012-11-13 12:59:33 +0000 | [diff] [blame] | 274 | static bool isIntOrIntVectorValue(const std::pair<const Value*, unsigned> &V) { |
| 275 | return V.first->getType()->isIntOrIntVectorTy(); |
Chris Lattner | 6da91d3 | 2007-05-04 05:21:47 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 278 | ValueEnumerator::ValueEnumerator(const Module &M) { |
| 279 | if (shouldPreserveBitcodeUseListOrder()) |
| 280 | UseListOrders = predictUseListOrder(M); |
| 281 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 282 | // Enumerate the global variables. |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 283 | for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); |
| 284 | I != E; ++I) |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 285 | EnumerateValue(I); |
| 286 | |
| 287 | // Enumerate the functions. |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 288 | 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] | 289 | EnumerateValue(I); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 290 | EnumerateAttributes(cast<Function>(I)->getAttributes()); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 291 | } |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 292 | |
Chris Lattner | 07d98b4 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 293 | // Enumerate the aliases. |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 294 | for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end(); |
Chris Lattner | 07d98b4 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 295 | I != E; ++I) |
| 296 | EnumerateValue(I); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 297 | |
Chris Lattner | 6da91d3 | 2007-05-04 05:21:47 +0000 | [diff] [blame] | 298 | // Remember what is the cutoff between globalvalue's and other constants. |
| 299 | unsigned FirstConstant = Values.size(); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 300 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 301 | // Enumerate the global variable initializers. |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 302 | for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); |
| 303 | I != E; ++I) |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 304 | if (I->hasInitializer()) |
| 305 | EnumerateValue(I->getInitializer()); |
| 306 | |
Chris Lattner | 07d98b4 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 307 | // Enumerate the aliasees. |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 308 | for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end(); |
Chris Lattner | 07d98b4 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 309 | I != E; ++I) |
| 310 | EnumerateValue(I->getAliasee()); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 311 | |
Peter Collingbourne | 1e3037f | 2013-09-16 01:08:15 +0000 | [diff] [blame] | 312 | // Enumerate the prefix data constants. |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 313 | for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I) |
Peter Collingbourne | 1e3037f | 2013-09-16 01:08:15 +0000 | [diff] [blame] | 314 | if (I->hasPrefixData()) |
| 315 | EnumerateValue(I->getPrefixData()); |
| 316 | |
Joe Abbey | ef7964c | 2013-04-01 02:28:07 +0000 | [diff] [blame] | 317 | // 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] | 318 | // pool so that the module symbol table can refer to them... |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 319 | EnumerateValueSymbolTable(M.getValueSymbolTable()); |
Dan Gohman | 17aa92c | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 320 | EnumerateNamedMetadata(M); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 321 | |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 322 | SmallVector<std::pair<unsigned, MDNode *>, 8> MDs; |
Chris Lattner | cc7b011 | 2009-12-31 00:51:46 +0000 | [diff] [blame] | 323 | |
Chris Lattner | 8d35c79 | 2007-04-26 03:50:57 +0000 | [diff] [blame] | 324 | // Enumerate types used by function bodies and argument lists. |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 325 | for (const Function &F : M) { |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 326 | for (const Argument &A : F.args()) |
| 327 | EnumerateType(A.getType()); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 328 | |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 329 | for (const BasicBlock &BB : F) |
| 330 | for (const Instruction &I : BB) { |
| 331 | for (const Use &Op : I.operands()) { |
| 332 | if (MDNode *MD = dyn_cast<MDNode>(&Op)) |
Victor Hernandez | 2b3365c | 2010-02-06 01:21:09 +0000 | [diff] [blame] | 333 | if (MD->isFunctionLocal() && MD->getFunction()) |
Victor Hernandez | d7e6457 | 2010-01-14 19:54:11 +0000 | [diff] [blame] | 334 | // These will get enumerated during function-incorporation. |
| 335 | continue; |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 336 | EnumerateOperandType(Op); |
Victor Hernandez | d7e6457 | 2010-01-14 19:54:11 +0000 | [diff] [blame] | 337 | } |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 338 | EnumerateType(I.getType()); |
| 339 | if (const CallInst *CI = dyn_cast<CallInst>(&I)) |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 340 | EnumerateAttributes(CI->getAttributes()); |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 341 | else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 342 | EnumerateAttributes(II->getAttributes()); |
Devang Patel | e8e0213 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 343 | |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 344 | // Enumerate metadata attached with this instruction. |
Devang Patel | f61b237 | 2009-10-22 18:55:16 +0000 | [diff] [blame] | 345 | MDs.clear(); |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 346 | I.getAllMetadataOtherThanDebugLoc(MDs); |
Chris Lattner | 3990b12 | 2009-12-28 23:41:32 +0000 | [diff] [blame] | 347 | for (unsigned i = 0, e = MDs.size(); i != e; ++i) |
Victor Hernandez | d7e6457 | 2010-01-14 19:54:11 +0000 | [diff] [blame] | 348 | EnumerateMetadata(MDs[i].second); |
Joe Abbey | 170a15e | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 349 | |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 350 | if (!I.getDebugLoc().isUnknown()) { |
Chris Lattner | a624524 | 2010-04-03 02:17:50 +0000 | [diff] [blame] | 351 | MDNode *Scope, *IA; |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 352 | I.getDebugLoc().getScopeAndInlinedAt(Scope, IA, I.getContext()); |
Chris Lattner | a624524 | 2010-04-03 02:17:50 +0000 | [diff] [blame] | 353 | if (Scope) EnumerateMetadata(Scope); |
| 354 | if (IA) EnumerateMetadata(IA); |
| 355 | } |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 356 | } |
| 357 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 358 | |
Chris Lattner | 6da91d3 | 2007-05-04 05:21:47 +0000 | [diff] [blame] | 359 | // Optimize constant ordering. |
| 360 | OptimizeConstants(FirstConstant, Values.size()); |
Rafael Espindola | f5a9056 | 2011-04-06 16:49:37 +0000 | [diff] [blame] | 361 | } |
| 362 | |
Devang Patel | e8e0213 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 363 | unsigned ValueEnumerator::getInstructionID(const Instruction *Inst) const { |
| 364 | InstructionMapType::const_iterator I = InstructionMap.find(Inst); |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 365 | assert(I != InstructionMap.end() && "Instruction is not mapped!"); |
Dan Gohman | 5c18fa2 | 2010-08-25 17:09:50 +0000 | [diff] [blame] | 366 | return I->second; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 367 | } |
Devang Patel | e8e0213 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 368 | |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 369 | unsigned ValueEnumerator::getComdatID(const Comdat *C) const { |
| 370 | unsigned ComdatID = Comdats.idFor(C); |
| 371 | assert(ComdatID && "Comdat not found!"); |
| 372 | return ComdatID; |
| 373 | } |
| 374 | |
Devang Patel | e8e0213 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 375 | void ValueEnumerator::setInstructionID(const Instruction *I) { |
| 376 | InstructionMap[I] = InstructionCount++; |
| 377 | } |
| 378 | |
Devang Patel | d5ac404 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 379 | unsigned ValueEnumerator::getValueID(const Value *V) const { |
Devang Patel | bc5201f | 2010-01-22 22:52:10 +0000 | [diff] [blame] | 380 | if (isa<MDNode>(V) || isa<MDString>(V)) { |
Devang Patel | d5ac404 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 381 | ValueMapType::const_iterator I = MDValueMap.find(V); |
| 382 | assert(I != MDValueMap.end() && "Value not in slotcalculator!"); |
| 383 | return I->second-1; |
| 384 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 385 | |
Devang Patel | d5ac404 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 386 | ValueMapType::const_iterator I = ValueMap.find(V); |
| 387 | assert(I != ValueMap.end() && "Value not in slotcalculator!"); |
| 388 | return I->second-1; |
| 389 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 390 | |
Chad Rosier | 4e6c03f | 2011-12-07 20:44:46 +0000 | [diff] [blame] | 391 | void ValueEnumerator::dump() const { |
| 392 | print(dbgs(), ValueMap, "Default"); |
| 393 | dbgs() << '\n'; |
| 394 | print(dbgs(), MDValueMap, "MetaData"); |
| 395 | dbgs() << '\n'; |
| 396 | } |
| 397 | |
| 398 | void ValueEnumerator::print(raw_ostream &OS, const ValueMapType &Map, |
| 399 | const char *Name) const { |
| 400 | |
| 401 | OS << "Map Name: " << Name << "\n"; |
| 402 | OS << "Size: " << Map.size() << "\n"; |
| 403 | for (ValueMapType::const_iterator I = Map.begin(), |
| 404 | E = Map.end(); I != E; ++I) { |
| 405 | |
| 406 | const Value *V = I->first; |
| 407 | if (V->hasName()) |
| 408 | OS << "Value: " << V->getName(); |
| 409 | else |
| 410 | OS << "Value: [null]\n"; |
| 411 | V->dump(); |
| 412 | |
| 413 | OS << " Uses(" << std::distance(V->use_begin(),V->use_end()) << "):"; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 414 | for (const Use &U : V->uses()) { |
| 415 | if (&U != &*V->use_begin()) |
Chad Rosier | 4e6c03f | 2011-12-07 20:44:46 +0000 | [diff] [blame] | 416 | OS << ","; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 417 | if(U->hasName()) |
| 418 | OS << " " << U->getName(); |
Chad Rosier | 4e6c03f | 2011-12-07 20:44:46 +0000 | [diff] [blame] | 419 | else |
| 420 | OS << " [null]"; |
| 421 | |
| 422 | } |
| 423 | OS << "\n\n"; |
| 424 | } |
| 425 | } |
| 426 | |
Chris Lattner | 6da91d3 | 2007-05-04 05:21:47 +0000 | [diff] [blame] | 427 | /// OptimizeConstants - Reorder constant pool for denser encoding. |
| 428 | void ValueEnumerator::OptimizeConstants(unsigned CstStart, unsigned CstEnd) { |
| 429 | if (CstStart == CstEnd || CstStart+1 == CstEnd) return; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 430 | |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 431 | if (shouldPreserveBitcodeUseListOrder()) |
| 432 | // Optimizing constants makes the use-list order difficult to predict. |
| 433 | // Disable it for now when trying to preserve the order. |
| 434 | return; |
| 435 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 436 | std::stable_sort(Values.begin() + CstStart, Values.begin() + CstEnd, |
| 437 | [this](const std::pair<const Value *, unsigned> &LHS, |
| 438 | const std::pair<const Value *, unsigned> &RHS) { |
| 439 | // Sort by plane. |
| 440 | if (LHS.first->getType() != RHS.first->getType()) |
| 441 | return getTypeID(LHS.first->getType()) < getTypeID(RHS.first->getType()); |
| 442 | // Then by frequency. |
| 443 | return LHS.second > RHS.second; |
| 444 | }); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 445 | |
Duncan Sands | 2333e29 | 2012-11-13 12:59:33 +0000 | [diff] [blame] | 446 | // Ensure that integer and vector of integer constants are at the start of the |
| 447 | // constant pool. This is important so that GEP structure indices come before |
| 448 | // gep constant exprs. |
Chris Lattner | 6da91d3 | 2007-05-04 05:21:47 +0000 | [diff] [blame] | 449 | std::partition(Values.begin()+CstStart, Values.begin()+CstEnd, |
Duncan Sands | 2333e29 | 2012-11-13 12:59:33 +0000 | [diff] [blame] | 450 | isIntOrIntVectorValue); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 451 | |
Chris Lattner | 6da91d3 | 2007-05-04 05:21:47 +0000 | [diff] [blame] | 452 | // Rebuild the modified portion of ValueMap. |
| 453 | for (; CstStart != CstEnd; ++CstStart) |
| 454 | ValueMap[Values[CstStart].first] = CstStart+1; |
| 455 | } |
| 456 | |
| 457 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 458 | /// EnumerateValueSymbolTable - Insert all of the values in the specified symbol |
| 459 | /// table into the values table. |
| 460 | void ValueEnumerator::EnumerateValueSymbolTable(const ValueSymbolTable &VST) { |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 461 | for (ValueSymbolTable::const_iterator VI = VST.begin(), VE = VST.end(); |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 462 | VI != VE; ++VI) |
| 463 | EnumerateValue(VI->getValue()); |
| 464 | } |
| 465 | |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 466 | /// Insert all of the values referenced by named metadata in the specified |
| 467 | /// module. |
| 468 | void ValueEnumerator::EnumerateNamedMetadata(const Module &M) { |
| 469 | for (Module::const_named_metadata_iterator I = M.named_metadata_begin(), |
| 470 | E = M.named_metadata_end(); |
| 471 | I != E; ++I) |
Dan Gohman | 17aa92c | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 472 | EnumerateNamedMDNode(I); |
Devang Patel | 0386f01 | 2010-01-07 19:39:36 +0000 | [diff] [blame] | 473 | } |
| 474 | |
Devang Patel | 8fba578 | 2010-01-09 00:30:14 +0000 | [diff] [blame] | 475 | void ValueEnumerator::EnumerateNamedMDNode(const NamedMDNode *MD) { |
Devang Patel | 8fba578 | 2010-01-09 00:30:14 +0000 | [diff] [blame] | 476 | for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i) |
Dan Gohman | 078b053 | 2010-08-24 02:01:24 +0000 | [diff] [blame] | 477 | EnumerateMetadata(MD->getOperand(i)); |
Devang Patel | 8fba578 | 2010-01-09 00:30:14 +0000 | [diff] [blame] | 478 | } |
| 479 | |
Dan Gohman | 309b3af | 2010-08-24 02:24:03 +0000 | [diff] [blame] | 480 | /// EnumerateMDNodeOperands - Enumerate all non-function-local values |
| 481 | /// and types referenced by the given MDNode. |
| 482 | void ValueEnumerator::EnumerateMDNodeOperands(const MDNode *N) { |
| 483 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { |
| 484 | if (Value *V = N->getOperand(i)) { |
| 485 | if (isa<MDNode>(V) || isa<MDString>(V)) |
| 486 | EnumerateMetadata(V); |
| 487 | else if (!isa<Instruction>(V) && !isa<Argument>(V)) |
| 488 | EnumerateValue(V); |
| 489 | } else |
| 490 | EnumerateType(Type::getVoidTy(N->getContext())); |
| 491 | } |
| 492 | } |
| 493 | |
Devang Patel | bc5201f | 2010-01-22 22:52:10 +0000 | [diff] [blame] | 494 | void ValueEnumerator::EnumerateMetadata(const Value *MD) { |
Benjamin Kramer | e88a8e6 | 2010-01-23 09:54:23 +0000 | [diff] [blame] | 495 | assert((isa<MDNode>(MD) || isa<MDString>(MD)) && "Invalid metadata kind"); |
Dan Gohman | 309b3af | 2010-08-24 02:24:03 +0000 | [diff] [blame] | 496 | |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 497 | // Skip function-local nodes themselves, but walk their operands. |
Dan Gohman | 309b3af | 2010-08-24 02:24:03 +0000 | [diff] [blame] | 498 | const MDNode *N = dyn_cast<MDNode>(MD); |
Dan Gohman | 309b3af | 2010-08-24 02:24:03 +0000 | [diff] [blame] | 499 | if (N && N->isFunctionLocal() && N->getFunction()) { |
| 500 | EnumerateMDNodeOperands(N); |
| 501 | return; |
| 502 | } |
| 503 | |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 504 | // Insert a dummy ID to block the co-recursive call to |
| 505 | // EnumerateMDNodeOperands() from re-visiting MD in a cyclic graph. |
| 506 | // |
| 507 | // Return early if there's already an ID. |
| 508 | if (!MDValueMap.insert(std::make_pair(MD, 0)).second) |
Devang Patel | d5ac404 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 509 | return; |
Dan Gohman | 309b3af | 2010-08-24 02:24:03 +0000 | [diff] [blame] | 510 | |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 511 | // Enumerate the type of this value. |
| 512 | EnumerateType(MD->getType()); |
| 513 | |
| 514 | // Visit operands first to minimize RAUW. |
Dan Gohman | 309b3af | 2010-08-24 02:24:03 +0000 | [diff] [blame] | 515 | if (N) |
| 516 | EnumerateMDNodeOperands(N); |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 517 | |
| 518 | // Replace the dummy ID inserted above with the correct one. MDValueMap may |
| 519 | // have changed by inserting operands, so we need a fresh lookup here. |
| 520 | MDValues.push_back(MD); |
| 521 | MDValueMap[MD] = MDValues.size(); |
Dan Gohman | 309b3af | 2010-08-24 02:24:03 +0000 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | /// EnumerateFunctionLocalMetadataa - Incorporate function-local metadata |
| 525 | /// information reachable from the given MDNode. |
| 526 | void ValueEnumerator::EnumerateFunctionLocalMetadata(const MDNode *N) { |
| 527 | assert(N->isFunctionLocal() && N->getFunction() && |
| 528 | "EnumerateFunctionLocalMetadata called on non-function-local mdnode!"); |
| 529 | |
| 530 | // Enumerate the type of this value. |
| 531 | EnumerateType(N->getType()); |
| 532 | |
| 533 | // Check to see if it's already in! |
| 534 | unsigned &MDValueID = MDValueMap[N]; |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 535 | if (MDValueID) |
Dan Gohman | 309b3af | 2010-08-24 02:24:03 +0000 | [diff] [blame] | 536 | return; |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 537 | |
| 538 | MDValues.push_back(N); |
Dan Gohman | 309b3af | 2010-08-24 02:24:03 +0000 | [diff] [blame] | 539 | MDValueID = MDValues.size(); |
| 540 | |
| 541 | // To incoroporate function-local information visit all function-local |
| 542 | // MDNodes and all function-local values they reference. |
| 543 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) |
| 544 | if (Value *V = N->getOperand(i)) { |
Dan Gohman | d01347e | 2010-08-24 02:40:27 +0000 | [diff] [blame] | 545 | if (MDNode *O = dyn_cast<MDNode>(V)) { |
Dan Gohman | 309b3af | 2010-08-24 02:24:03 +0000 | [diff] [blame] | 546 | if (O->isFunctionLocal() && O->getFunction()) |
| 547 | EnumerateFunctionLocalMetadata(O); |
Dan Gohman | d01347e | 2010-08-24 02:40:27 +0000 | [diff] [blame] | 548 | } else if (isa<Instruction>(V) || isa<Argument>(V)) |
Dan Gohman | 309b3af | 2010-08-24 02:24:03 +0000 | [diff] [blame] | 549 | EnumerateValue(V); |
| 550 | } |
| 551 | |
| 552 | // Also, collect all function-local MDNodes for easy access. |
| 553 | FunctionLocalMDs.push_back(N); |
Devang Patel | d5ac404 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 554 | } |
| 555 | |
Victor Hernandez | d7e6457 | 2010-01-14 19:54:11 +0000 | [diff] [blame] | 556 | void ValueEnumerator::EnumerateValue(const Value *V) { |
Chris Lattner | cc7b011 | 2009-12-31 00:51:46 +0000 | [diff] [blame] | 557 | assert(!V->getType()->isVoidTy() && "Can't insert void values!"); |
Dan Gohman | 309b3af | 2010-08-24 02:24:03 +0000 | [diff] [blame] | 558 | assert(!isa<MDNode>(V) && !isa<MDString>(V) && |
| 559 | "EnumerateValue doesn't handle Metadata!"); |
Devang Patel | d5ac404 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 560 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 561 | // Check to see if it's already in! |
| 562 | unsigned &ValueID = ValueMap[V]; |
| 563 | if (ValueID) { |
| 564 | // Increment use count. |
| 565 | Values[ValueID-1].second++; |
| 566 | return; |
| 567 | } |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 568 | |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 569 | if (auto *GO = dyn_cast<GlobalObject>(V)) |
| 570 | if (const Comdat *C = GO->getComdat()) |
| 571 | Comdats.insert(C); |
| 572 | |
Chris Lattner | 7a303d1 | 2007-05-06 01:00:28 +0000 | [diff] [blame] | 573 | // Enumerate the type of this value. |
| 574 | EnumerateType(V->getType()); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 575 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 576 | if (const Constant *C = dyn_cast<Constant>(V)) { |
| 577 | if (isa<GlobalValue>(C)) { |
| 578 | // Initializers for globals are handled explicitly elsewhere. |
Chris Lattner | 7a303d1 | 2007-05-06 01:00:28 +0000 | [diff] [blame] | 579 | } else if (C->getNumOperands()) { |
| 580 | // If a constant has operands, enumerate them. This makes sure that if a |
| 581 | // constant has uses (for example an array of const ints), that they are |
| 582 | // inserted also. |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 583 | |
Chris Lattner | 7a303d1 | 2007-05-06 01:00:28 +0000 | [diff] [blame] | 584 | // We prefer to enumerate them with values before we enumerate the user |
| 585 | // itself. This makes it more likely that we can avoid forward references |
| 586 | // in the reader. We know that there can be no cycles in the constants |
| 587 | // graph that don't go through a global variable. |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 588 | for (User::const_op_iterator I = C->op_begin(), E = C->op_end(); |
| 589 | I != E; ++I) |
Chris Lattner | cdfc940 | 2009-11-01 01:27:45 +0000 | [diff] [blame] | 590 | if (!isa<BasicBlock>(*I)) // Don't enumerate BB operand to BlockAddress. |
Victor Hernandez | d7e6457 | 2010-01-14 19:54:11 +0000 | [diff] [blame] | 591 | EnumerateValue(*I); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 592 | |
Chris Lattner | 7a303d1 | 2007-05-06 01:00:28 +0000 | [diff] [blame] | 593 | // Finally, add the value. Doing this could make the ValueID reference be |
| 594 | // dangling, don't reuse it. |
| 595 | Values.push_back(std::make_pair(V, 1U)); |
| 596 | ValueMap[V] = Values.size(); |
| 597 | return; |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 598 | } |
| 599 | } |
Devang Patel | 104cf9e | 2009-07-23 01:07:34 +0000 | [diff] [blame] | 600 | |
Chris Lattner | 7a303d1 | 2007-05-06 01:00:28 +0000 | [diff] [blame] | 601 | // Add the value. |
| 602 | Values.push_back(std::make_pair(V, 1U)); |
| 603 | ValueID = Values.size(); |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 607 | void ValueEnumerator::EnumerateType(Type *Ty) { |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 608 | unsigned *TypeID = &TypeMap[Ty]; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 609 | |
Rafael Espindola | f5a9056 | 2011-04-06 16:49:37 +0000 | [diff] [blame] | 610 | // We've already seen this type. |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 611 | if (*TypeID) |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 612 | return; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 613 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 614 | // If it is a non-anonymous struct, mark the type as being visited so that we |
| 615 | // don't recursively visit it. This is safe because we allow forward |
| 616 | // references of these in the bitcode reader. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 617 | if (StructType *STy = dyn_cast<StructType>(Ty)) |
Chris Lattner | 3ebb649 | 2011-08-12 18:06:37 +0000 | [diff] [blame] | 618 | if (!STy->isLiteral()) |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 619 | *TypeID = ~0U; |
Joe Abbey | 170a15e | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 620 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 621 | // Enumerate all of the subtypes before we enumerate this type. This ensures |
| 622 | // that the type will be enumerated in an order that can be directly built. |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 623 | for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end(); |
| 624 | I != E; ++I) |
| 625 | EnumerateType(*I); |
Joe Abbey | 170a15e | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 626 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 627 | // Refresh the TypeID pointer in case the table rehashed. |
| 628 | TypeID = &TypeMap[Ty]; |
Joe Abbey | 170a15e | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 629 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 630 | // Check to see if we got the pointer another way. This can happen when |
| 631 | // enumerating recursive types that hit the base case deeper than they start. |
| 632 | // |
| 633 | // If this is actually a struct that we are treating as forward ref'able, |
| 634 | // then emit the definition now that all of its contents are available. |
| 635 | if (*TypeID && *TypeID != ~0U) |
| 636 | return; |
Joe Abbey | 170a15e | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 637 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 638 | // Add this type now that its contents are all happily enumerated. |
| 639 | Types.push_back(Ty); |
Joe Abbey | 170a15e | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 640 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 641 | *TypeID = Types.size(); |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 642 | } |
| 643 | |
Chris Lattner | 33f1d5b | 2007-05-06 08:35:19 +0000 | [diff] [blame] | 644 | // Enumerate the types for the specified value. If the value is a constant, |
| 645 | // walk through it, enumerating the types of the constant. |
Victor Hernandez | d7e6457 | 2010-01-14 19:54:11 +0000 | [diff] [blame] | 646 | void ValueEnumerator::EnumerateOperandType(const Value *V) { |
Chris Lattner | 33f1d5b | 2007-05-06 08:35:19 +0000 | [diff] [blame] | 647 | EnumerateType(V->getType()); |
Joe Abbey | 170a15e | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 648 | |
Chris Lattner | 33f1d5b | 2007-05-06 08:35:19 +0000 | [diff] [blame] | 649 | if (const Constant *C = dyn_cast<Constant>(V)) { |
| 650 | // If this constant is already enumerated, ignore it, we know its type must |
| 651 | // be enumerated. |
| 652 | if (ValueMap.count(V)) return; |
| 653 | |
| 654 | // This constant may have operands, make sure to enumerate the types in |
| 655 | // them. |
Chris Lattner | 837e04a | 2009-10-28 05:24:40 +0000 | [diff] [blame] | 656 | for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) { |
Jay Foad | 8340d0b | 2011-04-11 09:48:55 +0000 | [diff] [blame] | 657 | const Value *Op = C->getOperand(i); |
Joe Abbey | 170a15e | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 658 | |
Chris Lattner | 837e04a | 2009-10-28 05:24:40 +0000 | [diff] [blame] | 659 | // Don't enumerate basic blocks here, this happens as operands to |
| 660 | // blockaddress. |
| 661 | if (isa<BasicBlock>(Op)) continue; |
Joe Abbey | 170a15e | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 662 | |
Dan Gohman | 879d811 | 2010-08-25 17:09:03 +0000 | [diff] [blame] | 663 | EnumerateOperandType(Op); |
Chris Lattner | 837e04a | 2009-10-28 05:24:40 +0000 | [diff] [blame] | 664 | } |
Nick Lewycky | cb33799 | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 665 | |
| 666 | if (const MDNode *N = dyn_cast<MDNode>(V)) { |
Chris Lattner | 5d0cacd | 2009-12-31 01:22:29 +0000 | [diff] [blame] | 667 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) |
| 668 | if (Value *Elem = N->getOperand(i)) |
Victor Hernandez | d7e6457 | 2010-01-14 19:54:11 +0000 | [diff] [blame] | 669 | EnumerateOperandType(Elem); |
Nick Lewycky | cb33799 | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 670 | } |
Devang Patel | 104cf9e | 2009-07-23 01:07:34 +0000 | [diff] [blame] | 671 | } else if (isa<MDString>(V) || isa<MDNode>(V)) |
Dan Gohman | 78aeae2 | 2010-08-24 02:10:52 +0000 | [diff] [blame] | 672 | EnumerateMetadata(V); |
Chris Lattner | 33f1d5b | 2007-05-06 08:35:19 +0000 | [diff] [blame] | 673 | } |
| 674 | |
Bill Wendling | 105ea3d | 2013-02-12 08:01:22 +0000 | [diff] [blame] | 675 | void ValueEnumerator::EnumerateAttributes(AttributeSet PAL) { |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 676 | if (PAL.isEmpty()) return; // null is always 0. |
Bill Wendling | 105ea3d | 2013-02-12 08:01:22 +0000 | [diff] [blame] | 677 | |
Chris Lattner | 50954f5 | 2007-05-03 22:46:43 +0000 | [diff] [blame] | 678 | // Do a lookup. |
Bill Wendling | 105ea3d | 2013-02-12 08:01:22 +0000 | [diff] [blame] | 679 | unsigned &Entry = AttributeMap[PAL]; |
Chris Lattner | 50954f5 | 2007-05-03 22:46:43 +0000 | [diff] [blame] | 680 | if (Entry == 0) { |
| 681 | // Never saw this before, add it. |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 682 | Attribute.push_back(PAL); |
| 683 | Entry = Attribute.size(); |
Chris Lattner | 50954f5 | 2007-05-03 22:46:43 +0000 | [diff] [blame] | 684 | } |
Bill Wendling | 8c2e77f | 2013-02-10 23:06:02 +0000 | [diff] [blame] | 685 | |
| 686 | // Do lookups for all attribute groups. |
| 687 | for (unsigned i = 0, e = PAL.getNumSlots(); i != e; ++i) { |
| 688 | AttributeSet AS = PAL.getSlotAttributes(i); |
Bill Wendling | e9229a6 | 2013-02-11 22:33:26 +0000 | [diff] [blame] | 689 | unsigned &Entry = AttributeGroupMap[AS]; |
Bill Wendling | 8c2e77f | 2013-02-10 23:06:02 +0000 | [diff] [blame] | 690 | if (Entry == 0) { |
Bill Wendling | e9229a6 | 2013-02-11 22:33:26 +0000 | [diff] [blame] | 691 | AttributeGroups.push_back(AS); |
| 692 | Entry = AttributeGroups.size(); |
Bill Wendling | 8c2e77f | 2013-02-10 23:06:02 +0000 | [diff] [blame] | 693 | } |
| 694 | } |
Chris Lattner | 50954f5 | 2007-05-03 22:46:43 +0000 | [diff] [blame] | 695 | } |
| 696 | |
Chad Rosier | 6a0c04d | 2011-06-03 17:02:19 +0000 | [diff] [blame] | 697 | void ValueEnumerator::incorporateFunction(const Function &F) { |
Nick Lewycky | 9a49f15 | 2010-02-25 08:30:17 +0000 | [diff] [blame] | 698 | InstructionCount = 0; |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 699 | NumModuleValues = Values.size(); |
Dan Gohman | 309b3af | 2010-08-24 02:24:03 +0000 | [diff] [blame] | 700 | NumModuleMDValues = MDValues.size(); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 701 | |
Chris Lattner | 8d35c79 | 2007-04-26 03:50:57 +0000 | [diff] [blame] | 702 | // Adding function arguments to the value table. |
Dan Gohman | 6dd26ba | 2010-07-16 22:58:39 +0000 | [diff] [blame] | 703 | for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end(); |
| 704 | I != E; ++I) |
Chris Lattner | 8d35c79 | 2007-04-26 03:50:57 +0000 | [diff] [blame] | 705 | EnumerateValue(I); |
| 706 | |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 707 | FirstFuncConstantID = Values.size(); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 708 | |
Chris Lattner | 8d35c79 | 2007-04-26 03:50:57 +0000 | [diff] [blame] | 709 | // Add all function-level constants to the value table. |
| 710 | for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) { |
| 711 | 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] | 712 | 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] | 713 | OI != E; ++OI) { |
| 714 | if ((isa<Constant>(*OI) && !isa<GlobalValue>(*OI)) || |
| 715 | isa<InlineAsm>(*OI)) |
| 716 | EnumerateValue(*OI); |
| 717 | } |
Chris Lattner | c59c0af | 2007-04-26 04:42:16 +0000 | [diff] [blame] | 718 | BasicBlocks.push_back(BB); |
Chris Lattner | e825ed5 | 2007-05-03 22:18:21 +0000 | [diff] [blame] | 719 | ValueMap[BB] = BasicBlocks.size(); |
Chris Lattner | 8d35c79 | 2007-04-26 03:50:57 +0000 | [diff] [blame] | 720 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 721 | |
Chris Lattner | 6da91d3 | 2007-05-04 05:21:47 +0000 | [diff] [blame] | 722 | // Optimize the constant layout. |
| 723 | OptimizeConstants(FirstFuncConstantID, Values.size()); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 724 | |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 725 | // Add the function's parameter attributes so they are available for use in |
| 726 | // the function's instruction. |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 727 | EnumerateAttributes(F.getAttributes()); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 728 | |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 729 | FirstInstID = Values.size(); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 730 | |
Devang Patel | 6209869 | 2010-06-02 23:05:04 +0000 | [diff] [blame] | 731 | SmallVector<MDNode *, 8> FnLocalMDVector; |
Chris Lattner | 8d35c79 | 2007-04-26 03:50:57 +0000 | [diff] [blame] | 732 | // Add all of the instructions. |
| 733 | 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] | 734 | 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] | 735 | for (User::const_op_iterator OI = I->op_begin(), E = I->op_end(); |
| 736 | OI != E; ++OI) { |
Victor Hernandez | d7e6457 | 2010-01-14 19:54:11 +0000 | [diff] [blame] | 737 | if (MDNode *MD = dyn_cast<MDNode>(*OI)) |
Victor Hernandez | 2b3365c | 2010-02-06 01:21:09 +0000 | [diff] [blame] | 738 | if (MD->isFunctionLocal() && MD->getFunction()) |
Victor Hernandez | af6ce14 | 2010-02-04 01:13:08 +0000 | [diff] [blame] | 739 | // Enumerate metadata after the instructions they might refer to. |
Devang Patel | 6209869 | 2010-06-02 23:05:04 +0000 | [diff] [blame] | 740 | FnLocalMDVector.push_back(MD); |
Victor Hernandez | ab9cd10 | 2010-01-13 19:36:16 +0000 | [diff] [blame] | 741 | } |
Dan Gohman | 309b3af | 2010-08-24 02:24:03 +0000 | [diff] [blame] | 742 | |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 743 | SmallVector<std::pair<unsigned, MDNode *>, 8> MDs; |
Dan Gohman | 309b3af | 2010-08-24 02:24:03 +0000 | [diff] [blame] | 744 | I->getAllMetadataOtherThanDebugLoc(MDs); |
| 745 | for (unsigned i = 0, e = MDs.size(); i != e; ++i) { |
| 746 | MDNode *N = MDs[i].second; |
| 747 | if (N->isFunctionLocal() && N->getFunction()) |
| 748 | FnLocalMDVector.push_back(N); |
| 749 | } |
Joe Abbey | 170a15e | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 750 | |
Benjamin Kramer | f012705 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 751 | if (!I->getType()->isVoidTy()) |
Chris Lattner | 8d35c79 | 2007-04-26 03:50:57 +0000 | [diff] [blame] | 752 | EnumerateValue(I); |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 753 | } |
| 754 | } |
Victor Hernandez | af6ce14 | 2010-02-04 01:13:08 +0000 | [diff] [blame] | 755 | |
| 756 | // Add all of the function-local metadata. |
Devang Patel | 6209869 | 2010-06-02 23:05:04 +0000 | [diff] [blame] | 757 | for (unsigned i = 0, e = FnLocalMDVector.size(); i != e; ++i) |
Dan Gohman | 309b3af | 2010-08-24 02:24:03 +0000 | [diff] [blame] | 758 | EnumerateFunctionLocalMetadata(FnLocalMDVector[i]); |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 759 | } |
| 760 | |
Chad Rosier | 6a0c04d | 2011-06-03 17:02:19 +0000 | [diff] [blame] | 761 | void ValueEnumerator::purgeFunction() { |
Chris Lattner | 8d35c79 | 2007-04-26 03:50:57 +0000 | [diff] [blame] | 762 | /// Remove purged values from the ValueMap. |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 763 | for (unsigned i = NumModuleValues, e = Values.size(); i != e; ++i) |
Chris Lattner | 8d35c79 | 2007-04-26 03:50:57 +0000 | [diff] [blame] | 764 | ValueMap.erase(Values[i].first); |
Dan Gohman | 309b3af | 2010-08-24 02:24:03 +0000 | [diff] [blame] | 765 | for (unsigned i = NumModuleMDValues, e = MDValues.size(); i != e; ++i) |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 766 | MDValueMap.erase(MDValues[i]); |
Chris Lattner | c59c0af | 2007-04-26 04:42:16 +0000 | [diff] [blame] | 767 | for (unsigned i = 0, e = BasicBlocks.size(); i != e; ++i) |
| 768 | ValueMap.erase(BasicBlocks[i]); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 769 | |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 770 | Values.resize(NumModuleValues); |
Dan Gohman | 309b3af | 2010-08-24 02:24:03 +0000 | [diff] [blame] | 771 | MDValues.resize(NumModuleMDValues); |
Chris Lattner | c59c0af | 2007-04-26 04:42:16 +0000 | [diff] [blame] | 772 | BasicBlocks.clear(); |
Dan Gohman | 848c9ae | 2010-08-25 17:11:16 +0000 | [diff] [blame] | 773 | FunctionLocalMDs.clear(); |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 774 | } |
Chris Lattner | 837e04a | 2009-10-28 05:24:40 +0000 | [diff] [blame] | 775 | |
| 776 | static void IncorporateFunctionInfoGlobalBBIDs(const Function *F, |
| 777 | DenseMap<const BasicBlock*, unsigned> &IDMap) { |
| 778 | unsigned Counter = 0; |
| 779 | for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB) |
| 780 | IDMap[BB] = ++Counter; |
| 781 | } |
| 782 | |
| 783 | /// getGlobalBasicBlockID - This returns the function-specific ID for the |
| 784 | /// specified basic block. This is relatively expensive information, so it |
| 785 | /// should only be used by rare constructs such as address-of-label. |
| 786 | unsigned ValueEnumerator::getGlobalBasicBlockID(const BasicBlock *BB) const { |
| 787 | unsigned &Idx = GlobalBasicBlockIDs[BB]; |
| 788 | if (Idx != 0) |
Chris Lattner | cdfc940 | 2009-11-01 01:27:45 +0000 | [diff] [blame] | 789 | return Idx-1; |
Chris Lattner | 837e04a | 2009-10-28 05:24:40 +0000 | [diff] [blame] | 790 | |
| 791 | IncorporateFunctionInfoGlobalBBIDs(BB->getParent(), GlobalBasicBlockIDs); |
| 792 | return getGlobalBasicBlockID(BB); |
| 793 | } |
| 794 | |