Chris Lattner | c1d10d6 | 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 | f3ebc3f | 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 | c1d10d6 | 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 | 337a1b2 | 2011-04-06 16:49:37 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/SmallPtrSet.h" |
Chandler Carruth | 9fb823b | 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" |
Duncan P. N. Exon Smith | 15eb0ab | 2014-07-25 16:13:16 +0000 | [diff] [blame] | 21 | #include "llvm/IR/UseListOrder.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 22 | #include "llvm/IR/ValueSymbolTable.h" |
Chad Rosier | 78037a9 | 2011-12-07 20:44:46 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Debug.h" |
| 24 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | a8713be | 2007-05-04 05:05:48 +0000 | [diff] [blame] | 25 | #include <algorithm> |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 26 | using namespace llvm; |
| 27 | |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 28 | namespace { |
Duncan P. N. Exon Smith | 2e6a87b | 2014-07-29 23:03:40 +0000 | [diff] [blame] | 29 | struct OrderMap { |
| 30 | DenseMap<const Value *, std::pair<unsigned, bool>> IDs; |
Duncan P. N. Exon Smith | 3cbca20 | 2014-07-30 01:22:16 +0000 | [diff] [blame] | 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 | } |
Duncan P. N. Exon Smith | 2e6a87b | 2014-07-29 23:03:40 +0000 | [diff] [blame] | 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 | } |
Duncan P. N. Exon Smith | ba4576d | 2014-07-30 01:20:26 +0000 | [diff] [blame] | 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 | } |
Duncan P. N. Exon Smith | 2e6a87b | 2014-07-29 23:03:40 +0000 | [diff] [blame] | 53 | }; |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 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()) |
Duncan P. N. Exon Smith | 3cbca20 | 2014-07-30 01:22:16 +0000 | [diff] [blame] | 63 | if (!isa<BasicBlock>(Op) && !isa<GlobalValue>(Op)) |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 64 | orderValue(Op, OM); |
| 65 | |
| 66 | // Note: we cannot cache this lookup above, since inserting into the map |
Duncan P. N. Exon Smith | ba4576d | 2014-07-30 01:20:26 +0000 | [diff] [blame] | 67 | // changes the map's size, and thus affects the other IDs. |
| 68 | OM.index(V); |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 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 | |
Duncan P. N. Exon Smith | 3cbca20 | 2014-07-30 01:22:16 +0000 | [diff] [blame] | 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. |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 81 | for (const GlobalVariable &G : M->globals()) |
| 82 | if (G.hasInitializer()) |
| 83 | orderValue(G.getInitializer(), OM); |
| 84 | for (const GlobalAlias &A : M->aliases()) |
| 85 | orderValue(A.getAliasee(), OM); |
| 86 | for (const Function &F : *M) |
| 87 | if (F.hasPrefixData()) |
| 88 | orderValue(F.getPrefixData(), OM); |
Duncan P. N. Exon Smith | 3cbca20 | 2014-07-30 01:22:16 +0000 | [diff] [blame] | 89 | OM.LastGlobalConstantID = OM.size(); |
| 90 | |
| 91 | // Initializers of GlobalValues are processed in |
| 92 | // BitcodeReader::ResolveGlobalAndAliasInits(). Match the order there rather |
| 93 | // than ValueEnumerator, and match the code in predictValueUseListOrderImpl() |
| 94 | // by giving IDs in reverse order. |
| 95 | // |
| 96 | // Since GlobalValues never reference each other directly (just through |
| 97 | // initializers), their relative IDs only matter for determining order of |
| 98 | // uses in their initializers. |
| 99 | for (const Function &F : *M) |
| 100 | orderValue(&F, OM); |
| 101 | for (const GlobalAlias &A : M->aliases()) |
| 102 | orderValue(&A, OM); |
| 103 | for (const GlobalVariable &G : M->globals()) |
| 104 | orderValue(&G, OM); |
| 105 | OM.LastGlobalValueID = OM.size(); |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 106 | |
| 107 | for (const Function &F : *M) { |
| 108 | if (F.isDeclaration()) |
| 109 | continue; |
| 110 | // Here we need to match the union of ValueEnumerator::incorporateFunction() |
| 111 | // and WriteFunction(). Basic blocks are implicitly declared before |
| 112 | // anything else (by declaring their size). |
| 113 | for (const BasicBlock &BB : F) |
| 114 | orderValue(&BB, OM); |
| 115 | for (const Argument &A : F.args()) |
| 116 | orderValue(&A, OM); |
| 117 | for (const BasicBlock &BB : F) |
| 118 | for (const Instruction &I : BB) |
| 119 | for (const Value *Op : I.operands()) |
| 120 | if ((isa<Constant>(*Op) && !isa<GlobalValue>(*Op)) || |
| 121 | isa<InlineAsm>(*Op)) |
| 122 | orderValue(Op, OM); |
| 123 | for (const BasicBlock &BB : F) |
| 124 | for (const Instruction &I : BB) |
| 125 | orderValue(&I, OM); |
| 126 | } |
| 127 | return OM; |
| 128 | } |
| 129 | |
| 130 | static void predictValueUseListOrderImpl(const Value *V, const Function *F, |
| 131 | unsigned ID, const OrderMap &OM, |
| 132 | UseListOrderStack &Stack) { |
| 133 | // Predict use-list order for this one. |
| 134 | typedef std::pair<const Use *, unsigned> Entry; |
| 135 | SmallVector<Entry, 64> List; |
| 136 | for (const Use &U : V->uses()) |
| 137 | // Check if this user will be serialized. |
| 138 | if (OM.lookup(U.getUser()).first) |
| 139 | List.push_back(std::make_pair(&U, List.size())); |
| 140 | |
| 141 | if (List.size() < 2) |
| 142 | // We may have lost some users. |
| 143 | return; |
| 144 | |
Duncan P. N. Exon Smith | 3cbca20 | 2014-07-30 01:22:16 +0000 | [diff] [blame] | 145 | bool IsGlobalValue = OM.isGlobalValue(ID); |
| 146 | std::sort(List.begin(), List.end(), [&](const Entry &L, const Entry &R) { |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 147 | const Use *LU = L.first; |
| 148 | const Use *RU = R.first; |
Duncan P. N. Exon Smith | 3f0fc7b | 2014-07-29 01:13:56 +0000 | [diff] [blame] | 149 | if (LU == RU) |
| 150 | return false; |
| 151 | |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 152 | auto LID = OM.lookup(LU->getUser()).first; |
| 153 | auto RID = OM.lookup(RU->getUser()).first; |
Duncan P. N. Exon Smith | 3cbca20 | 2014-07-30 01:22:16 +0000 | [diff] [blame] | 154 | |
| 155 | // Global values are processed in reverse order. |
| 156 | // |
| 157 | // Moreover, initializers of GlobalValues are set *after* all the globals |
| 158 | // have been read (despite having earlier IDs). Rather than awkwardly |
| 159 | // modeling this behaviour here, orderModule() has assigned IDs to |
| 160 | // initializers of GlobalValues before GlobalValues themselves. |
| 161 | if (OM.isGlobalValue(LID) && OM.isGlobalValue(RID)) |
| 162 | return LID < RID; |
| 163 | |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 164 | // If ID is 4, then expect: 7 6 5 1 2 3. |
| 165 | if (LID < RID) { |
| 166 | if (RID < ID) |
Duncan P. N. Exon Smith | 3cbca20 | 2014-07-30 01:22:16 +0000 | [diff] [blame] | 167 | if (!IsGlobalValue) // GlobalValue uses don't get reversed. |
| 168 | return true; |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 169 | return false; |
| 170 | } |
| 171 | if (RID < LID) { |
| 172 | if (LID < ID) |
Duncan P. N. Exon Smith | 3cbca20 | 2014-07-30 01:22:16 +0000 | [diff] [blame] | 173 | if (!IsGlobalValue) // GlobalValue uses don't get reversed. |
| 174 | return false; |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 175 | return true; |
| 176 | } |
Duncan P. N. Exon Smith | 3cbca20 | 2014-07-30 01:22:16 +0000 | [diff] [blame] | 177 | |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 178 | // LID and RID are equal, so we have different operands of the same user. |
| 179 | // Assume operands are added in order for all instructions. |
Duncan P. N. Exon Smith | 3cbca20 | 2014-07-30 01:22:16 +0000 | [diff] [blame] | 180 | if (LID < ID) |
| 181 | if (!IsGlobalValue) // GlobalValue uses don't get reversed. |
| 182 | return LU->getOperandNo() < RU->getOperandNo(); |
| 183 | return LU->getOperandNo() > RU->getOperandNo(); |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 184 | }); |
| 185 | |
| 186 | if (std::is_sorted( |
| 187 | List.begin(), List.end(), |
| 188 | [](const Entry &L, const Entry &R) { return L.second < R.second; })) |
| 189 | // Order is already correct. |
| 190 | return; |
| 191 | |
| 192 | // Store the shuffle. |
Duncan P. N. Exon Smith | d7a281a | 2014-07-29 16:58:18 +0000 | [diff] [blame] | 193 | Stack.emplace_back(V, F, List.size()); |
| 194 | assert(List.size() == Stack.back().Shuffle.size() && "Wrong size"); |
Duncan P. N. Exon Smith | f849ace | 2014-07-28 22:41:50 +0000 | [diff] [blame] | 195 | for (size_t I = 0, E = List.size(); I != E; ++I) |
Duncan P. N. Exon Smith | d7a281a | 2014-07-29 16:58:18 +0000 | [diff] [blame] | 196 | Stack.back().Shuffle[I] = List[I].second; |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | static void predictValueUseListOrder(const Value *V, const Function *F, |
| 200 | OrderMap &OM, UseListOrderStack &Stack) { |
| 201 | auto &IDPair = OM[V]; |
| 202 | assert(IDPair.first && "Unmapped value"); |
| 203 | if (IDPair.second) |
| 204 | // Already predicted. |
| 205 | return; |
| 206 | |
| 207 | // Do the actual prediction. |
| 208 | IDPair.second = true; |
| 209 | if (!V->use_empty() && std::next(V->use_begin()) != V->use_end()) |
| 210 | predictValueUseListOrderImpl(V, F, IDPair.first, OM, Stack); |
| 211 | |
| 212 | // Recursive descent into constants. |
| 213 | if (const Constant *C = dyn_cast<Constant>(V)) |
Duncan P. N. Exon Smith | c69b516 | 2014-07-30 17:51:09 +0000 | [diff] [blame] | 214 | if (C->getNumOperands()) // Visit GlobalValues. |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 215 | for (const Value *Op : C->operands()) |
Duncan P. N. Exon Smith | c69b516 | 2014-07-30 17:51:09 +0000 | [diff] [blame] | 216 | if (isa<Constant>(Op)) // Visit GlobalValues. |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 217 | predictValueUseListOrder(Op, F, OM, Stack); |
| 218 | } |
| 219 | |
| 220 | static UseListOrderStack predictUseListOrder(const Module *M) { |
| 221 | OrderMap OM = orderModule(M); |
| 222 | |
| 223 | // Use-list orders need to be serialized after all the users have been added |
| 224 | // to a value, or else the shuffles will be incomplete. Store them per |
| 225 | // function in a stack. |
| 226 | // |
| 227 | // Aside from function order, the order of values doesn't matter much here. |
| 228 | UseListOrderStack Stack; |
| 229 | |
| 230 | // We want to visit the functions backward now so we can list function-local |
| 231 | // constants in the last Function they're used in. Module-level constants |
| 232 | // have already been visited above. |
| 233 | for (auto I = M->rbegin(), E = M->rend(); I != E; ++I) { |
| 234 | const Function &F = *I; |
| 235 | if (F.isDeclaration()) |
| 236 | continue; |
| 237 | for (const BasicBlock &BB : F) |
| 238 | predictValueUseListOrder(&BB, &F, OM, Stack); |
| 239 | for (const Argument &A : F.args()) |
| 240 | predictValueUseListOrder(&A, &F, OM, Stack); |
| 241 | for (const BasicBlock &BB : F) |
| 242 | for (const Instruction &I : BB) |
| 243 | for (const Value *Op : I.operands()) |
Duncan P. N. Exon Smith | c69b516 | 2014-07-30 17:51:09 +0000 | [diff] [blame] | 244 | if (isa<Constant>(*Op) || isa<InlineAsm>(*Op)) // Visit GlobalValues. |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 245 | predictValueUseListOrder(Op, &F, OM, Stack); |
| 246 | for (const BasicBlock &BB : F) |
| 247 | for (const Instruction &I : BB) |
| 248 | predictValueUseListOrder(&I, &F, OM, Stack); |
| 249 | } |
| 250 | |
| 251 | // Visit globals last, since the module-level use-list block will be seen |
| 252 | // before the function bodies are processed. |
| 253 | for (const GlobalVariable &G : M->globals()) |
| 254 | predictValueUseListOrder(&G, nullptr, OM, Stack); |
| 255 | for (const Function &F : *M) |
| 256 | predictValueUseListOrder(&F, nullptr, OM, Stack); |
| 257 | for (const GlobalAlias &A : M->aliases()) |
| 258 | predictValueUseListOrder(&A, nullptr, OM, Stack); |
| 259 | for (const GlobalVariable &G : M->globals()) |
| 260 | if (G.hasInitializer()) |
| 261 | predictValueUseListOrder(G.getInitializer(), nullptr, OM, Stack); |
| 262 | for (const GlobalAlias &A : M->aliases()) |
| 263 | predictValueUseListOrder(A.getAliasee(), nullptr, OM, Stack); |
| 264 | for (const Function &F : *M) |
| 265 | if (F.hasPrefixData()) |
| 266 | predictValueUseListOrder(F.getPrefixData(), nullptr, OM, Stack); |
| 267 | |
| 268 | return Stack; |
| 269 | } |
| 270 | |
Duncan Sands | e6beec6 | 2012-11-13 12:59:33 +0000 | [diff] [blame] | 271 | static bool isIntOrIntVectorValue(const std::pair<const Value*, unsigned> &V) { |
| 272 | return V.first->getType()->isIntOrIntVectorTy(); |
Chris Lattner | 430e80d | 2007-05-04 05:21:47 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 275 | /// ValueEnumerator - Enumerate module-level information. |
| 276 | ValueEnumerator::ValueEnumerator(const Module *M) { |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 277 | if (shouldPreserveBitcodeUseListOrder()) |
| 278 | UseListOrders = predictUseListOrder(M); |
| 279 | |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 280 | // Enumerate the global variables. |
| 281 | for (Module::const_global_iterator I = M->global_begin(), |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 282 | |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 283 | E = M->global_end(); I != E; ++I) |
| 284 | EnumerateValue(I); |
| 285 | |
| 286 | // Enumerate the functions. |
Duncan Sands | ad0ea2d | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 287 | for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) { |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 288 | EnumerateValue(I); |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 289 | EnumerateAttributes(cast<Function>(I)->getAttributes()); |
Duncan Sands | ad0ea2d | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 290 | } |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 291 | |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 292 | // Enumerate the aliases. |
| 293 | for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end(); |
| 294 | I != E; ++I) |
| 295 | EnumerateValue(I); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 296 | |
Chris Lattner | 430e80d | 2007-05-04 05:21:47 +0000 | [diff] [blame] | 297 | // Remember what is the cutoff between globalvalue's and other constants. |
| 298 | unsigned FirstConstant = Values.size(); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 299 | |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 300 | // Enumerate the global variable initializers. |
| 301 | for (Module::const_global_iterator I = M->global_begin(), |
| 302 | E = M->global_end(); I != E; ++I) |
| 303 | if (I->hasInitializer()) |
| 304 | EnumerateValue(I->getInitializer()); |
| 305 | |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 306 | // Enumerate the aliasees. |
| 307 | for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end(); |
| 308 | I != E; ++I) |
| 309 | EnumerateValue(I->getAliasee()); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 310 | |
Peter Collingbourne | 3fa50f9 | 2013-09-16 01:08:15 +0000 | [diff] [blame] | 311 | // Enumerate the prefix data constants. |
| 312 | for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 313 | if (I->hasPrefixData()) |
| 314 | EnumerateValue(I->getPrefixData()); |
| 315 | |
Joe Abbey | bc6f4ba | 2013-04-01 02:28:07 +0000 | [diff] [blame] | 316 | // Insert constants and metadata that are named at module level into the slot |
Devang Patel | fcfee0f | 2010-01-07 19:39:36 +0000 | [diff] [blame] | 317 | // pool so that the module symbol table can refer to them... |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 318 | EnumerateValueSymbolTable(M->getValueSymbolTable()); |
Dan Gohman | 2637cc1 | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 319 | EnumerateNamedMetadata(M); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 320 | |
Chris Lattner | 8dace89 | 2009-12-31 00:51:46 +0000 | [diff] [blame] | 321 | SmallVector<std::pair<unsigned, MDNode*>, 8> MDs; |
| 322 | |
Chris Lattner | 5f640b9 | 2007-04-26 03:50:57 +0000 | [diff] [blame] | 323 | // Enumerate types used by function bodies and argument lists. |
Rafael Espindola | 087d627 | 2014-06-17 03:00:40 +0000 | [diff] [blame] | 324 | for (const Function &F : *M) { |
| 325 | for (const Argument &A : F.args()) |
| 326 | EnumerateType(A.getType()); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 327 | |
Rafael Espindola | 087d627 | 2014-06-17 03:00:40 +0000 | [diff] [blame] | 328 | for (const BasicBlock &BB : F) |
| 329 | for (const Instruction &I : BB) { |
| 330 | for (const Use &Op : I.operands()) { |
| 331 | if (MDNode *MD = dyn_cast<MDNode>(&Op)) |
Victor Hernandez | 1b08138 | 2010-02-06 01:21:09 +0000 | [diff] [blame] | 332 | if (MD->isFunctionLocal() && MD->getFunction()) |
Victor Hernandez | 572218b | 2010-01-14 19:54:11 +0000 | [diff] [blame] | 333 | // These will get enumerated during function-incorporation. |
| 334 | continue; |
Rafael Espindola | 087d627 | 2014-06-17 03:00:40 +0000 | [diff] [blame] | 335 | EnumerateOperandType(Op); |
Victor Hernandez | 572218b | 2010-01-14 19:54:11 +0000 | [diff] [blame] | 336 | } |
Rafael Espindola | 087d627 | 2014-06-17 03:00:40 +0000 | [diff] [blame] | 337 | EnumerateType(I.getType()); |
| 338 | if (const CallInst *CI = dyn_cast<CallInst>(&I)) |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 339 | EnumerateAttributes(CI->getAttributes()); |
Rafael Espindola | 087d627 | 2014-06-17 03:00:40 +0000 | [diff] [blame] | 340 | else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 341 | EnumerateAttributes(II->getAttributes()); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 342 | |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 343 | // Enumerate metadata attached with this instruction. |
Devang Patel | 6da5dbf | 2009-10-22 18:55:16 +0000 | [diff] [blame] | 344 | MDs.clear(); |
Rafael Espindola | 087d627 | 2014-06-17 03:00:40 +0000 | [diff] [blame] | 345 | I.getAllMetadataOtherThanDebugLoc(MDs); |
Chris Lattner | 2f2aa2b | 2009-12-28 23:41:32 +0000 | [diff] [blame] | 346 | for (unsigned i = 0, e = MDs.size(); i != e; ++i) |
Victor Hernandez | 572218b | 2010-01-14 19:54:11 +0000 | [diff] [blame] | 347 | EnumerateMetadata(MDs[i].second); |
Joe Abbey | 2ad8df2 | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 348 | |
Rafael Espindola | 087d627 | 2014-06-17 03:00:40 +0000 | [diff] [blame] | 349 | if (!I.getDebugLoc().isUnknown()) { |
Chris Lattner | 07d09ed | 2010-04-03 02:17:50 +0000 | [diff] [blame] | 350 | MDNode *Scope, *IA; |
Rafael Espindola | 087d627 | 2014-06-17 03:00:40 +0000 | [diff] [blame] | 351 | I.getDebugLoc().getScopeAndInlinedAt(Scope, IA, I.getContext()); |
Chris Lattner | 07d09ed | 2010-04-03 02:17:50 +0000 | [diff] [blame] | 352 | if (Scope) EnumerateMetadata(Scope); |
| 353 | if (IA) EnumerateMetadata(IA); |
| 354 | } |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 355 | } |
| 356 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 357 | |
Chris Lattner | 430e80d | 2007-05-04 05:21:47 +0000 | [diff] [blame] | 358 | // Optimize constant ordering. |
| 359 | OptimizeConstants(FirstConstant, Values.size()); |
Rafael Espindola | 337a1b2 | 2011-04-06 16:49:37 +0000 | [diff] [blame] | 360 | } |
| 361 | |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 362 | unsigned ValueEnumerator::getInstructionID(const Instruction *Inst) const { |
| 363 | InstructionMapType::const_iterator I = InstructionMap.find(Inst); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 364 | assert(I != InstructionMap.end() && "Instruction is not mapped!"); |
Dan Gohman | 1f4b028 | 2010-08-25 17:09:50 +0000 | [diff] [blame] | 365 | return I->second; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 366 | } |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 367 | |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 368 | unsigned ValueEnumerator::getComdatID(const Comdat *C) const { |
| 369 | unsigned ComdatID = Comdats.idFor(C); |
| 370 | assert(ComdatID && "Comdat not found!"); |
| 371 | return ComdatID; |
| 372 | } |
| 373 | |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 374 | void ValueEnumerator::setInstructionID(const Instruction *I) { |
| 375 | InstructionMap[I] = InstructionCount++; |
| 376 | } |
| 377 | |
Devang Patel | 05eb617 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 378 | unsigned ValueEnumerator::getValueID(const Value *V) const { |
Devang Patel | ac277eb | 2010-01-22 22:52:10 +0000 | [diff] [blame] | 379 | if (isa<MDNode>(V) || isa<MDString>(V)) { |
Devang Patel | 05eb617 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 380 | ValueMapType::const_iterator I = MDValueMap.find(V); |
| 381 | assert(I != MDValueMap.end() && "Value not in slotcalculator!"); |
| 382 | return I->second-1; |
| 383 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 384 | |
Devang Patel | 05eb617 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 385 | ValueMapType::const_iterator I = ValueMap.find(V); |
| 386 | assert(I != ValueMap.end() && "Value not in slotcalculator!"); |
| 387 | return I->second-1; |
| 388 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 389 | |
Chad Rosier | 78037a9 | 2011-12-07 20:44:46 +0000 | [diff] [blame] | 390 | void ValueEnumerator::dump() const { |
| 391 | print(dbgs(), ValueMap, "Default"); |
| 392 | dbgs() << '\n'; |
| 393 | print(dbgs(), MDValueMap, "MetaData"); |
| 394 | dbgs() << '\n'; |
| 395 | } |
| 396 | |
| 397 | void ValueEnumerator::print(raw_ostream &OS, const ValueMapType &Map, |
| 398 | const char *Name) const { |
| 399 | |
| 400 | OS << "Map Name: " << Name << "\n"; |
| 401 | OS << "Size: " << Map.size() << "\n"; |
| 402 | for (ValueMapType::const_iterator I = Map.begin(), |
| 403 | E = Map.end(); I != E; ++I) { |
| 404 | |
| 405 | const Value *V = I->first; |
| 406 | if (V->hasName()) |
| 407 | OS << "Value: " << V->getName(); |
| 408 | else |
| 409 | OS << "Value: [null]\n"; |
| 410 | V->dump(); |
| 411 | |
| 412 | OS << " Uses(" << std::distance(V->use_begin(),V->use_end()) << "):"; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 413 | for (const Use &U : V->uses()) { |
| 414 | if (&U != &*V->use_begin()) |
Chad Rosier | 78037a9 | 2011-12-07 20:44:46 +0000 | [diff] [blame] | 415 | OS << ","; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 416 | if(U->hasName()) |
| 417 | OS << " " << U->getName(); |
Chad Rosier | 78037a9 | 2011-12-07 20:44:46 +0000 | [diff] [blame] | 418 | else |
| 419 | OS << " [null]"; |
| 420 | |
| 421 | } |
| 422 | OS << "\n\n"; |
| 423 | } |
| 424 | } |
| 425 | |
Chris Lattner | 430e80d | 2007-05-04 05:21:47 +0000 | [diff] [blame] | 426 | /// OptimizeConstants - Reorder constant pool for denser encoding. |
| 427 | void ValueEnumerator::OptimizeConstants(unsigned CstStart, unsigned CstEnd) { |
| 428 | if (CstStart == CstEnd || CstStart+1 == CstEnd) return; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 429 | |
Duncan P. N. Exon Smith | 15eb0ab | 2014-07-25 16:13:16 +0000 | [diff] [blame] | 430 | if (shouldPreserveBitcodeUseListOrder()) |
| 431 | // Optimizing constants makes the use-list order difficult to predict. |
| 432 | // Disable it for now when trying to preserve the order. |
| 433 | return; |
| 434 | |
Benjamin Kramer | 3a377bc | 2014-03-01 11:47:00 +0000 | [diff] [blame] | 435 | std::stable_sort(Values.begin() + CstStart, Values.begin() + CstEnd, |
| 436 | [this](const std::pair<const Value *, unsigned> &LHS, |
| 437 | const std::pair<const Value *, unsigned> &RHS) { |
| 438 | // Sort by plane. |
| 439 | if (LHS.first->getType() != RHS.first->getType()) |
| 440 | return getTypeID(LHS.first->getType()) < getTypeID(RHS.first->getType()); |
| 441 | // Then by frequency. |
| 442 | return LHS.second > RHS.second; |
| 443 | }); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 444 | |
Duncan Sands | e6beec6 | 2012-11-13 12:59:33 +0000 | [diff] [blame] | 445 | // Ensure that integer and vector of integer constants are at the start of the |
| 446 | // constant pool. This is important so that GEP structure indices come before |
| 447 | // gep constant exprs. |
Chris Lattner | 430e80d | 2007-05-04 05:21:47 +0000 | [diff] [blame] | 448 | std::partition(Values.begin()+CstStart, Values.begin()+CstEnd, |
Duncan Sands | e6beec6 | 2012-11-13 12:59:33 +0000 | [diff] [blame] | 449 | isIntOrIntVectorValue); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 450 | |
Chris Lattner | 430e80d | 2007-05-04 05:21:47 +0000 | [diff] [blame] | 451 | // Rebuild the modified portion of ValueMap. |
| 452 | for (; CstStart != CstEnd; ++CstStart) |
| 453 | ValueMap[Values[CstStart].first] = CstStart+1; |
| 454 | } |
| 455 | |
| 456 | |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 457 | /// EnumerateValueSymbolTable - Insert all of the values in the specified symbol |
| 458 | /// table into the values table. |
| 459 | void ValueEnumerator::EnumerateValueSymbolTable(const ValueSymbolTable &VST) { |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 460 | for (ValueSymbolTable::const_iterator VI = VST.begin(), VE = VST.end(); |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 461 | VI != VE; ++VI) |
| 462 | EnumerateValue(VI->getValue()); |
| 463 | } |
| 464 | |
Dan Gohman | 2637cc1 | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 465 | /// EnumerateNamedMetadata - Insert all of the values referenced by |
| 466 | /// named metadata in the specified module. |
| 467 | void ValueEnumerator::EnumerateNamedMetadata(const Module *M) { |
| 468 | for (Module::const_named_metadata_iterator I = M->named_metadata_begin(), |
| 469 | E = M->named_metadata_end(); I != E; ++I) |
| 470 | EnumerateNamedMDNode(I); |
Devang Patel | fcfee0f | 2010-01-07 19:39:36 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Devang Patel | 99ff5a8 | 2010-01-09 00:30:14 +0000 | [diff] [blame] | 473 | void ValueEnumerator::EnumerateNamedMDNode(const NamedMDNode *MD) { |
Devang Patel | 99ff5a8 | 2010-01-09 00:30:14 +0000 | [diff] [blame] | 474 | for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i) |
Dan Gohman | d3d2bbe | 2010-08-24 02:01:24 +0000 | [diff] [blame] | 475 | EnumerateMetadata(MD->getOperand(i)); |
Devang Patel | 99ff5a8 | 2010-01-09 00:30:14 +0000 | [diff] [blame] | 476 | } |
| 477 | |
Dan Gohman | c828c54 | 2010-08-24 02:24:03 +0000 | [diff] [blame] | 478 | /// EnumerateMDNodeOperands - Enumerate all non-function-local values |
| 479 | /// and types referenced by the given MDNode. |
| 480 | void ValueEnumerator::EnumerateMDNodeOperands(const MDNode *N) { |
| 481 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { |
| 482 | if (Value *V = N->getOperand(i)) { |
| 483 | if (isa<MDNode>(V) || isa<MDString>(V)) |
| 484 | EnumerateMetadata(V); |
| 485 | else if (!isa<Instruction>(V) && !isa<Argument>(V)) |
| 486 | EnumerateValue(V); |
| 487 | } else |
| 488 | EnumerateType(Type::getVoidTy(N->getContext())); |
| 489 | } |
| 490 | } |
| 491 | |
Devang Patel | ac277eb | 2010-01-22 22:52:10 +0000 | [diff] [blame] | 492 | void ValueEnumerator::EnumerateMetadata(const Value *MD) { |
Benjamin Kramer | 14bb114 | 2010-01-23 09:54:23 +0000 | [diff] [blame] | 493 | assert((isa<MDNode>(MD) || isa<MDString>(MD)) && "Invalid metadata kind"); |
Dan Gohman | c828c54 | 2010-08-24 02:24:03 +0000 | [diff] [blame] | 494 | |
| 495 | // Enumerate the type of this value. |
| 496 | EnumerateType(MD->getType()); |
| 497 | |
| 498 | const MDNode *N = dyn_cast<MDNode>(MD); |
| 499 | |
| 500 | // In the module-level pass, skip function-local nodes themselves, but |
| 501 | // do walk their operands. |
| 502 | if (N && N->isFunctionLocal() && N->getFunction()) { |
| 503 | EnumerateMDNodeOperands(N); |
| 504 | return; |
| 505 | } |
| 506 | |
Devang Patel | 05eb617 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 507 | // Check to see if it's already in! |
| 508 | unsigned &MDValueID = MDValueMap[MD]; |
| 509 | if (MDValueID) { |
| 510 | // Increment use count. |
| 511 | MDValues[MDValueID-1].second++; |
| 512 | return; |
| 513 | } |
Devang Patel | 05eb617 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 514 | MDValues.push_back(std::make_pair(MD, 1U)); |
| 515 | MDValueID = MDValues.size(); |
Dan Gohman | c828c54 | 2010-08-24 02:24:03 +0000 | [diff] [blame] | 516 | |
| 517 | // Enumerate all non-function-local operands. |
| 518 | if (N) |
| 519 | EnumerateMDNodeOperands(N); |
| 520 | } |
| 521 | |
| 522 | /// EnumerateFunctionLocalMetadataa - Incorporate function-local metadata |
| 523 | /// information reachable from the given MDNode. |
| 524 | void ValueEnumerator::EnumerateFunctionLocalMetadata(const MDNode *N) { |
| 525 | assert(N->isFunctionLocal() && N->getFunction() && |
| 526 | "EnumerateFunctionLocalMetadata called on non-function-local mdnode!"); |
| 527 | |
| 528 | // Enumerate the type of this value. |
| 529 | EnumerateType(N->getType()); |
| 530 | |
| 531 | // Check to see if it's already in! |
| 532 | unsigned &MDValueID = MDValueMap[N]; |
| 533 | if (MDValueID) { |
| 534 | // Increment use count. |
| 535 | MDValues[MDValueID-1].second++; |
| 536 | return; |
| 537 | } |
| 538 | MDValues.push_back(std::make_pair(N, 1U)); |
| 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 | 10215a1 | 2010-08-24 02:40:27 +0000 | [diff] [blame] | 545 | if (MDNode *O = dyn_cast<MDNode>(V)) { |
Dan Gohman | c828c54 | 2010-08-24 02:24:03 +0000 | [diff] [blame] | 546 | if (O->isFunctionLocal() && O->getFunction()) |
| 547 | EnumerateFunctionLocalMetadata(O); |
Dan Gohman | 10215a1 | 2010-08-24 02:40:27 +0000 | [diff] [blame] | 548 | } else if (isa<Instruction>(V) || isa<Argument>(V)) |
Dan Gohman | c828c54 | 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 | 05eb617 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 554 | } |
| 555 | |
Victor Hernandez | 572218b | 2010-01-14 19:54:11 +0000 | [diff] [blame] | 556 | void ValueEnumerator::EnumerateValue(const Value *V) { |
Chris Lattner | 8dace89 | 2009-12-31 00:51:46 +0000 | [diff] [blame] | 557 | assert(!V->getType()->isVoidTy() && "Can't insert void values!"); |
Dan Gohman | c828c54 | 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 | 05eb617 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 560 | |
Chris Lattner | c1d10d6 | 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 | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 568 | |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 569 | if (auto *GO = dyn_cast<GlobalObject>(V)) |
| 570 | if (const Comdat *C = GO->getComdat()) |
| 571 | Comdats.insert(C); |
| 572 | |
Chris Lattner | 9ee4836 | 2007-05-06 01:00:28 +0000 | [diff] [blame] | 573 | // Enumerate the type of this value. |
| 574 | EnumerateType(V->getType()); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 575 | |
Chris Lattner | c1d10d6 | 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 | 9ee4836 | 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 | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 583 | |
Chris Lattner | 9ee4836 | 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 | c1d10d6 | 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 | aa99c94 | 2009-11-01 01:27:45 +0000 | [diff] [blame] | 590 | if (!isa<BasicBlock>(*I)) // Don't enumerate BB operand to BlockAddress. |
Victor Hernandez | 572218b | 2010-01-14 19:54:11 +0000 | [diff] [blame] | 591 | EnumerateValue(*I); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 592 | |
Chris Lattner | 9ee4836 | 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 | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 598 | } |
| 599 | } |
Devang Patel | e059ba6e | 2009-07-23 01:07:34 +0000 | [diff] [blame] | 600 | |
Chris Lattner | 9ee4836 | 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 | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 607 | void ValueEnumerator::EnumerateType(Type *Ty) { |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 608 | unsigned *TypeID = &TypeMap[Ty]; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 609 | |
Rafael Espindola | 337a1b2 | 2011-04-06 16:49:37 +0000 | [diff] [blame] | 610 | // We've already seen this type. |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 611 | if (*TypeID) |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 612 | return; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 613 | |
Chris Lattner | b1ed91f | 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 | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 617 | if (StructType *STy = dyn_cast<StructType>(Ty)) |
Chris Lattner | 335d399 | 2011-08-12 18:06:37 +0000 | [diff] [blame] | 618 | if (!STy->isLiteral()) |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 619 | *TypeID = ~0U; |
Joe Abbey | 2ad8df2 | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 620 | |
Chris Lattner | b1ed91f | 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 | c1d10d6 | 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 | 2ad8df2 | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 626 | |
Chris Lattner | b1ed91f | 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 | 2ad8df2 | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 629 | |
Chris Lattner | b1ed91f | 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 | 2ad8df2 | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 637 | |
Chris Lattner | b1ed91f | 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 | 2ad8df2 | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 640 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 641 | *TypeID = Types.size(); |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 642 | } |
| 643 | |
Chris Lattner | 76fd90f | 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 | 572218b | 2010-01-14 19:54:11 +0000 | [diff] [blame] | 646 | void ValueEnumerator::EnumerateOperandType(const Value *V) { |
Chris Lattner | 76fd90f | 2007-05-06 08:35:19 +0000 | [diff] [blame] | 647 | EnumerateType(V->getType()); |
Joe Abbey | 2ad8df2 | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 648 | |
Chris Lattner | 76fd90f | 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 | f540d74 | 2009-10-28 05:24:40 +0000 | [diff] [blame] | 656 | for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) { |
Jay Foad | 0159a1e | 2011-04-11 09:48:55 +0000 | [diff] [blame] | 657 | const Value *Op = C->getOperand(i); |
Joe Abbey | 2ad8df2 | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 658 | |
Chris Lattner | f540d74 | 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 | 2ad8df2 | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 662 | |
Dan Gohman | 9cfe532 | 2010-08-25 17:09:03 +0000 | [diff] [blame] | 663 | EnumerateOperandType(Op); |
Chris Lattner | f540d74 | 2009-10-28 05:24:40 +0000 | [diff] [blame] | 664 | } |
Nick Lewycky | b8f9b7a | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 665 | |
| 666 | if (const MDNode *N = dyn_cast<MDNode>(V)) { |
Chris Lattner | 9b49302 | 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 | 572218b | 2010-01-14 19:54:11 +0000 | [diff] [blame] | 669 | EnumerateOperandType(Elem); |
Nick Lewycky | b8f9b7a | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 670 | } |
Devang Patel | e059ba6e | 2009-07-23 01:07:34 +0000 | [diff] [blame] | 671 | } else if (isa<MDString>(V) || isa<MDNode>(V)) |
Dan Gohman | ab09a12 | 2010-08-24 02:10:52 +0000 | [diff] [blame] | 672 | EnumerateMetadata(V); |
Chris Lattner | 76fd90f | 2007-05-06 08:35:19 +0000 | [diff] [blame] | 673 | } |
| 674 | |
Bill Wendling | 7b5f4f3 | 2013-02-12 08:01:22 +0000 | [diff] [blame] | 675 | void ValueEnumerator::EnumerateAttributes(AttributeSet PAL) { |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 676 | if (PAL.isEmpty()) return; // null is always 0. |
Bill Wendling | 7b5f4f3 | 2013-02-12 08:01:22 +0000 | [diff] [blame] | 677 | |
Chris Lattner | e4bbad6 | 2007-05-03 22:46:43 +0000 | [diff] [blame] | 678 | // Do a lookup. |
Bill Wendling | 7b5f4f3 | 2013-02-12 08:01:22 +0000 | [diff] [blame] | 679 | unsigned &Entry = AttributeMap[PAL]; |
Chris Lattner | e4bbad6 | 2007-05-03 22:46:43 +0000 | [diff] [blame] | 680 | if (Entry == 0) { |
| 681 | // Never saw this before, add it. |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 682 | Attribute.push_back(PAL); |
| 683 | Entry = Attribute.size(); |
Chris Lattner | e4bbad6 | 2007-05-03 22:46:43 +0000 | [diff] [blame] | 684 | } |
Bill Wendling | 51f612e | 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 | 92ed700 | 2013-02-11 22:33:26 +0000 | [diff] [blame] | 689 | unsigned &Entry = AttributeGroupMap[AS]; |
Bill Wendling | 51f612e | 2013-02-10 23:06:02 +0000 | [diff] [blame] | 690 | if (Entry == 0) { |
Bill Wendling | 92ed700 | 2013-02-11 22:33:26 +0000 | [diff] [blame] | 691 | AttributeGroups.push_back(AS); |
| 692 | Entry = AttributeGroups.size(); |
Bill Wendling | 51f612e | 2013-02-10 23:06:02 +0000 | [diff] [blame] | 693 | } |
| 694 | } |
Chris Lattner | e4bbad6 | 2007-05-03 22:46:43 +0000 | [diff] [blame] | 695 | } |
| 696 | |
Chad Rosier | 6a11b64 | 2011-06-03 17:02:19 +0000 | [diff] [blame] | 697 | void ValueEnumerator::incorporateFunction(const Function &F) { |
Nick Lewycky | a72e1af | 2010-02-25 08:30:17 +0000 | [diff] [blame] | 698 | InstructionCount = 0; |
Chris Lattner | e6e364c | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 699 | NumModuleValues = Values.size(); |
Dan Gohman | c828c54 | 2010-08-24 02:24:03 +0000 | [diff] [blame] | 700 | NumModuleMDValues = MDValues.size(); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 701 | |
Chris Lattner | 5f640b9 | 2007-04-26 03:50:57 +0000 | [diff] [blame] | 702 | // Adding function arguments to the value table. |
Dan Gohman | 9a54c17 | 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 | 5f640b9 | 2007-04-26 03:50:57 +0000 | [diff] [blame] | 705 | EnumerateValue(I); |
| 706 | |
Chris Lattner | e6e364c | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 707 | FirstFuncConstantID = Values.size(); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 708 | |
Chris Lattner | 5f640b9 | 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 | 7d6781b | 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 | 5f640b9 | 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 | 7c37b01 | 2007-04-26 04:42:16 +0000 | [diff] [blame] | 718 | BasicBlocks.push_back(BB); |
Chris Lattner | 6be58c6 | 2007-05-03 22:18:21 +0000 | [diff] [blame] | 719 | ValueMap[BB] = BasicBlocks.size(); |
Chris Lattner | 5f640b9 | 2007-04-26 03:50:57 +0000 | [diff] [blame] | 720 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 721 | |
Chris Lattner | 430e80d | 2007-05-04 05:21:47 +0000 | [diff] [blame] | 722 | // Optimize the constant layout. |
| 723 | OptimizeConstants(FirstFuncConstantID, Values.size()); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 724 | |
Duncan Sands | ad0ea2d | 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 | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 727 | EnumerateAttributes(F.getAttributes()); |
Duncan Sands | ad0ea2d | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 728 | |
Chris Lattner | e6e364c | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 729 | FirstInstID = Values.size(); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 730 | |
Devang Patel | df84e8b | 2010-06-02 23:05:04 +0000 | [diff] [blame] | 731 | SmallVector<MDNode *, 8> FnLocalMDVector; |
Chris Lattner | 5f640b9 | 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 | c1d10d6 | 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 | cad7328 | 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 | 572218b | 2010-01-14 19:54:11 +0000 | [diff] [blame] | 737 | if (MDNode *MD = dyn_cast<MDNode>(*OI)) |
Victor Hernandez | 1b08138 | 2010-02-06 01:21:09 +0000 | [diff] [blame] | 738 | if (MD->isFunctionLocal() && MD->getFunction()) |
Victor Hernandez | d44ee35 | 2010-02-04 01:13:08 +0000 | [diff] [blame] | 739 | // Enumerate metadata after the instructions they might refer to. |
Devang Patel | df84e8b | 2010-06-02 23:05:04 +0000 | [diff] [blame] | 740 | FnLocalMDVector.push_back(MD); |
Victor Hernandez | cad7328 | 2010-01-13 19:36:16 +0000 | [diff] [blame] | 741 | } |
Dan Gohman | c828c54 | 2010-08-24 02:24:03 +0000 | [diff] [blame] | 742 | |
| 743 | SmallVector<std::pair<unsigned, MDNode*>, 8> MDs; |
| 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 | 2ad8df2 | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 750 | |
Benjamin Kramer | ccce8ba | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 751 | if (!I->getType()->isVoidTy()) |
Chris Lattner | 5f640b9 | 2007-04-26 03:50:57 +0000 | [diff] [blame] | 752 | EnumerateValue(I); |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 753 | } |
| 754 | } |
Victor Hernandez | d44ee35 | 2010-02-04 01:13:08 +0000 | [diff] [blame] | 755 | |
| 756 | // Add all of the function-local metadata. |
Devang Patel | df84e8b | 2010-06-02 23:05:04 +0000 | [diff] [blame] | 757 | for (unsigned i = 0, e = FnLocalMDVector.size(); i != e; ++i) |
Dan Gohman | c828c54 | 2010-08-24 02:24:03 +0000 | [diff] [blame] | 758 | EnumerateFunctionLocalMetadata(FnLocalMDVector[i]); |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 759 | } |
| 760 | |
Chad Rosier | 6a11b64 | 2011-06-03 17:02:19 +0000 | [diff] [blame] | 761 | void ValueEnumerator::purgeFunction() { |
Chris Lattner | 5f640b9 | 2007-04-26 03:50:57 +0000 | [diff] [blame] | 762 | /// Remove purged values from the ValueMap. |
Chris Lattner | e6e364c | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 763 | for (unsigned i = NumModuleValues, e = Values.size(); i != e; ++i) |
Chris Lattner | 5f640b9 | 2007-04-26 03:50:57 +0000 | [diff] [blame] | 764 | ValueMap.erase(Values[i].first); |
Dan Gohman | c828c54 | 2010-08-24 02:24:03 +0000 | [diff] [blame] | 765 | for (unsigned i = NumModuleMDValues, e = MDValues.size(); i != e; ++i) |
| 766 | MDValueMap.erase(MDValues[i].first); |
Chris Lattner | 7c37b01 | 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 | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 769 | |
Chris Lattner | e6e364c | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 770 | Values.resize(NumModuleValues); |
Dan Gohman | c828c54 | 2010-08-24 02:24:03 +0000 | [diff] [blame] | 771 | MDValues.resize(NumModuleMDValues); |
Chris Lattner | 7c37b01 | 2007-04-26 04:42:16 +0000 | [diff] [blame] | 772 | BasicBlocks.clear(); |
Dan Gohman | 22161da | 2010-08-25 17:11:16 +0000 | [diff] [blame] | 773 | FunctionLocalMDs.clear(); |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 774 | } |
Chris Lattner | f540d74 | 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 | aa99c94 | 2009-11-01 01:27:45 +0000 | [diff] [blame] | 789 | return Idx-1; |
Chris Lattner | f540d74 | 2009-10-28 05:24:40 +0000 | [diff] [blame] | 790 | |
| 791 | IncorporateFunctionInfoGlobalBBIDs(BB->getParent(), GlobalBasicBlockIDs); |
| 792 | return getGlobalBasicBlockID(BB); |
| 793 | } |
| 794 | |