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