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" |
Duncan P. N. Exon Smith | d9901ff | 2015-02-02 18:53:21 +0000 | [diff] [blame] | 18 | #include "llvm/IR/DebugInfoMetadata.h" |
Chandler Carruth | 9fb823b | 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" |
Duncan P. N. Exon Smith | 15eb0ab | 2014-07-25 16:13:16 +0000 | [diff] [blame] | 22 | #include "llvm/IR/UseListOrder.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 23 | #include "llvm/IR/ValueSymbolTable.h" |
Chad Rosier | 78037a9 | 2011-12-07 20:44:46 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Debug.h" |
| 25 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | a8713be | 2007-05-04 05:05:48 +0000 | [diff] [blame] | 26 | #include <algorithm> |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 27 | using namespace llvm; |
| 28 | |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 29 | namespace { |
Duncan P. N. Exon Smith | 2e6a87b | 2014-07-29 23:03:40 +0000 | [diff] [blame] | 30 | struct OrderMap { |
| 31 | DenseMap<const Value *, std::pair<unsigned, bool>> IDs; |
Duncan P. N. Exon Smith | 3cbca20 | 2014-07-30 01:22:16 +0000 | [diff] [blame] | 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 | } |
Duncan P. N. Exon Smith | 2e6a87b | 2014-07-29 23:03:40 +0000 | [diff] [blame] | 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 | } |
Duncan P. N. Exon Smith | ba4576d | 2014-07-30 01:20:26 +0000 | [diff] [blame] | 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 | } |
Duncan P. N. Exon Smith | 2e6a87b | 2014-07-29 23:03:40 +0000 | [diff] [blame] | 54 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 55 | } |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 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()) |
Duncan P. N. Exon Smith | 3cbca20 | 2014-07-30 01:22:16 +0000 | [diff] [blame] | 64 | if (!isa<BasicBlock>(Op) && !isa<GlobalValue>(Op)) |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 65 | orderValue(Op, OM); |
| 66 | |
| 67 | // 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] | 68 | // changes the map's size, and thus affects the other IDs. |
| 69 | OM.index(V); |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Rafael Espindola | 49e9bf8 | 2014-11-17 20:06:27 +0000 | [diff] [blame] | 72 | static OrderMap orderModule(const Module &M) { |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 73 | // This needs to match the order used by ValueEnumerator::ValueEnumerator() |
| 74 | // and ValueEnumerator::incorporateFunction(). |
| 75 | OrderMap OM; |
| 76 | |
Duncan P. N. Exon Smith | 3cbca20 | 2014-07-30 01:22:16 +0000 | [diff] [blame] | 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. |
Rafael Espindola | 49e9bf8 | 2014-11-17 20:06:27 +0000 | [diff] [blame] | 82 | for (const GlobalVariable &G : M.globals()) |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 83 | if (G.hasInitializer()) |
Duncan P. N. Exon Smith | 9177867 | 2014-07-31 00:13:28 +0000 | [diff] [blame] | 84 | if (!isa<GlobalValue>(G.getInitializer())) |
| 85 | orderValue(G.getInitializer(), OM); |
Rafael Espindola | 49e9bf8 | 2014-11-17 20:06:27 +0000 | [diff] [blame] | 86 | for (const GlobalAlias &A : M.aliases()) |
Duncan P. N. Exon Smith | 9177867 | 2014-07-31 00:13:28 +0000 | [diff] [blame] | 87 | if (!isa<GlobalValue>(A.getAliasee())) |
| 88 | orderValue(A.getAliasee(), OM); |
Dmitry Polukhin | a1feff7 | 2016-04-07 12:32:19 +0000 | [diff] [blame] | 89 | for (const GlobalIFunc &I : M.ifuncs()) |
| 90 | if (!isa<GlobalValue>(I.getResolver())) |
| 91 | orderValue(I.getResolver(), OM); |
Peter Collingbourne | 51d2de7 | 2014-12-03 02:08:38 +0000 | [diff] [blame] | 92 | for (const Function &F : M) { |
Vedant Kumar | 3a63fb3 | 2015-12-19 08:52:49 +0000 | [diff] [blame] | 93 | for (const Use &U : F.operands()) |
| 94 | if (!isa<GlobalValue>(U.get())) |
| 95 | orderValue(U.get(), OM); |
Peter Collingbourne | 51d2de7 | 2014-12-03 02:08:38 +0000 | [diff] [blame] | 96 | } |
Duncan P. N. Exon Smith | 3cbca20 | 2014-07-30 01:22:16 +0000 | [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. |
Rafael Espindola | 49e9bf8 | 2014-11-17 20:06:27 +0000 | [diff] [blame] | 107 | for (const Function &F : M) |
Duncan P. N. Exon Smith | 3cbca20 | 2014-07-30 01:22:16 +0000 | [diff] [blame] | 108 | orderValue(&F, OM); |
Rafael Espindola | 49e9bf8 | 2014-11-17 20:06:27 +0000 | [diff] [blame] | 109 | for (const GlobalAlias &A : M.aliases()) |
Duncan P. N. Exon Smith | 3cbca20 | 2014-07-30 01:22:16 +0000 | [diff] [blame] | 110 | orderValue(&A, OM); |
Dmitry Polukhin | a1feff7 | 2016-04-07 12:32:19 +0000 | [diff] [blame] | 111 | for (const GlobalIFunc &I : M.ifuncs()) |
| 112 | orderValue(&I, OM); |
Rafael Espindola | 49e9bf8 | 2014-11-17 20:06:27 +0000 | [diff] [blame] | 113 | for (const GlobalVariable &G : M.globals()) |
Duncan P. N. Exon Smith | 3cbca20 | 2014-07-30 01:22:16 +0000 | [diff] [blame] | 114 | orderValue(&G, OM); |
| 115 | OM.LastGlobalValueID = OM.size(); |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 116 | |
Rafael Espindola | 49e9bf8 | 2014-11-17 20:06:27 +0000 | [diff] [blame] | 117 | for (const Function &F : M) { |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 118 | if (F.isDeclaration()) |
| 119 | continue; |
| 120 | // Here we need to match the union of ValueEnumerator::incorporateFunction() |
| 121 | // and WriteFunction(). Basic blocks are implicitly declared before |
| 122 | // anything else (by declaring their size). |
| 123 | for (const BasicBlock &BB : F) |
| 124 | orderValue(&BB, OM); |
| 125 | for (const Argument &A : F.args()) |
| 126 | orderValue(&A, OM); |
| 127 | for (const BasicBlock &BB : F) |
| 128 | for (const Instruction &I : BB) |
| 129 | for (const Value *Op : I.operands()) |
| 130 | if ((isa<Constant>(*Op) && !isa<GlobalValue>(*Op)) || |
| 131 | isa<InlineAsm>(*Op)) |
| 132 | orderValue(Op, OM); |
| 133 | for (const BasicBlock &BB : F) |
| 134 | for (const Instruction &I : BB) |
| 135 | orderValue(&I, OM); |
| 136 | } |
| 137 | return OM; |
| 138 | } |
| 139 | |
| 140 | static void predictValueUseListOrderImpl(const Value *V, const Function *F, |
| 141 | unsigned ID, const OrderMap &OM, |
| 142 | UseListOrderStack &Stack) { |
| 143 | // Predict use-list order for this one. |
| 144 | typedef std::pair<const Use *, unsigned> Entry; |
| 145 | SmallVector<Entry, 64> List; |
| 146 | for (const Use &U : V->uses()) |
| 147 | // Check if this user will be serialized. |
| 148 | if (OM.lookup(U.getUser()).first) |
| 149 | List.push_back(std::make_pair(&U, List.size())); |
| 150 | |
| 151 | if (List.size() < 2) |
| 152 | // We may have lost some users. |
| 153 | return; |
| 154 | |
Duncan P. N. Exon Smith | 3cbca20 | 2014-07-30 01:22:16 +0000 | [diff] [blame] | 155 | bool IsGlobalValue = OM.isGlobalValue(ID); |
| 156 | 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] | 157 | const Use *LU = L.first; |
| 158 | const Use *RU = R.first; |
Duncan P. N. Exon Smith | 3f0fc7b | 2014-07-29 01:13:56 +0000 | [diff] [blame] | 159 | if (LU == RU) |
| 160 | return false; |
| 161 | |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 162 | auto LID = OM.lookup(LU->getUser()).first; |
| 163 | auto RID = OM.lookup(RU->getUser()).first; |
Duncan P. N. Exon Smith | 3cbca20 | 2014-07-30 01:22:16 +0000 | [diff] [blame] | 164 | |
| 165 | // Global values are processed in reverse order. |
| 166 | // |
| 167 | // Moreover, initializers of GlobalValues are set *after* all the globals |
| 168 | // have been read (despite having earlier IDs). Rather than awkwardly |
| 169 | // modeling this behaviour here, orderModule() has assigned IDs to |
| 170 | // initializers of GlobalValues before GlobalValues themselves. |
| 171 | if (OM.isGlobalValue(LID) && OM.isGlobalValue(RID)) |
| 172 | return LID < RID; |
| 173 | |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 174 | // If ID is 4, then expect: 7 6 5 1 2 3. |
| 175 | if (LID < RID) { |
Duncan P. N. Exon Smith | ab6adeb | 2014-07-31 18:33:12 +0000 | [diff] [blame] | 176 | if (RID <= ID) |
Duncan P. N. Exon Smith | 3cbca20 | 2014-07-30 01:22:16 +0000 | [diff] [blame] | 177 | if (!IsGlobalValue) // GlobalValue uses don't get reversed. |
| 178 | return true; |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 179 | return false; |
| 180 | } |
| 181 | if (RID < LID) { |
Duncan P. N. Exon Smith | ab6adeb | 2014-07-31 18:33:12 +0000 | [diff] [blame] | 182 | if (LID <= ID) |
Duncan P. N. Exon Smith | 3cbca20 | 2014-07-30 01:22:16 +0000 | [diff] [blame] | 183 | if (!IsGlobalValue) // GlobalValue uses don't get reversed. |
| 184 | return false; |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 185 | return true; |
| 186 | } |
Duncan P. N. Exon Smith | 3cbca20 | 2014-07-30 01:22:16 +0000 | [diff] [blame] | 187 | |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 188 | // LID and RID are equal, so we have different operands of the same user. |
| 189 | // Assume operands are added in order for all instructions. |
Duncan P. N. Exon Smith | ab6adeb | 2014-07-31 18:33:12 +0000 | [diff] [blame] | 190 | if (LID <= ID) |
Duncan P. N. Exon Smith | 3cbca20 | 2014-07-30 01:22:16 +0000 | [diff] [blame] | 191 | if (!IsGlobalValue) // GlobalValue uses don't get reversed. |
| 192 | return LU->getOperandNo() < RU->getOperandNo(); |
| 193 | return LU->getOperandNo() > RU->getOperandNo(); |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 194 | }); |
| 195 | |
| 196 | if (std::is_sorted( |
| 197 | List.begin(), List.end(), |
| 198 | [](const Entry &L, const Entry &R) { return L.second < R.second; })) |
| 199 | // Order is already correct. |
| 200 | return; |
| 201 | |
| 202 | // Store the shuffle. |
Duncan P. N. Exon Smith | d7a281a | 2014-07-29 16:58:18 +0000 | [diff] [blame] | 203 | Stack.emplace_back(V, F, List.size()); |
| 204 | assert(List.size() == Stack.back().Shuffle.size() && "Wrong size"); |
Duncan P. N. Exon Smith | f849ace | 2014-07-28 22:41:50 +0000 | [diff] [blame] | 205 | 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] | 206 | Stack.back().Shuffle[I] = List[I].second; |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | static void predictValueUseListOrder(const Value *V, const Function *F, |
| 210 | OrderMap &OM, UseListOrderStack &Stack) { |
| 211 | auto &IDPair = OM[V]; |
| 212 | assert(IDPair.first && "Unmapped value"); |
| 213 | if (IDPair.second) |
| 214 | // Already predicted. |
| 215 | return; |
| 216 | |
| 217 | // Do the actual prediction. |
| 218 | IDPair.second = true; |
| 219 | if (!V->use_empty() && std::next(V->use_begin()) != V->use_end()) |
| 220 | predictValueUseListOrderImpl(V, F, IDPair.first, OM, Stack); |
| 221 | |
| 222 | // Recursive descent into constants. |
| 223 | if (const Constant *C = dyn_cast<Constant>(V)) |
Duncan P. N. Exon Smith | c69b516 | 2014-07-30 17:51:09 +0000 | [diff] [blame] | 224 | if (C->getNumOperands()) // Visit GlobalValues. |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 225 | for (const Value *Op : C->operands()) |
Duncan P. N. Exon Smith | c69b516 | 2014-07-30 17:51:09 +0000 | [diff] [blame] | 226 | if (isa<Constant>(Op)) // Visit GlobalValues. |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 227 | predictValueUseListOrder(Op, F, OM, Stack); |
| 228 | } |
| 229 | |
Rafael Espindola | 49e9bf8 | 2014-11-17 20:06:27 +0000 | [diff] [blame] | 230 | static UseListOrderStack predictUseListOrder(const Module &M) { |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 231 | OrderMap OM = orderModule(M); |
| 232 | |
| 233 | // Use-list orders need to be serialized after all the users have been added |
| 234 | // to a value, or else the shuffles will be incomplete. Store them per |
| 235 | // function in a stack. |
| 236 | // |
| 237 | // Aside from function order, the order of values doesn't matter much here. |
| 238 | UseListOrderStack Stack; |
| 239 | |
| 240 | // We want to visit the functions backward now so we can list function-local |
| 241 | // constants in the last Function they're used in. Module-level constants |
| 242 | // have already been visited above. |
Rafael Espindola | 49e9bf8 | 2014-11-17 20:06:27 +0000 | [diff] [blame] | 243 | for (auto I = M.rbegin(), E = M.rend(); I != E; ++I) { |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 244 | const Function &F = *I; |
| 245 | if (F.isDeclaration()) |
| 246 | continue; |
| 247 | for (const BasicBlock &BB : F) |
| 248 | predictValueUseListOrder(&BB, &F, OM, Stack); |
| 249 | for (const Argument &A : F.args()) |
| 250 | predictValueUseListOrder(&A, &F, OM, Stack); |
| 251 | for (const BasicBlock &BB : F) |
| 252 | for (const Instruction &I : BB) |
| 253 | for (const Value *Op : I.operands()) |
Duncan P. N. Exon Smith | c69b516 | 2014-07-30 17:51:09 +0000 | [diff] [blame] | 254 | if (isa<Constant>(*Op) || isa<InlineAsm>(*Op)) // Visit GlobalValues. |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 255 | predictValueUseListOrder(Op, &F, OM, Stack); |
| 256 | for (const BasicBlock &BB : F) |
| 257 | for (const Instruction &I : BB) |
| 258 | predictValueUseListOrder(&I, &F, OM, Stack); |
| 259 | } |
| 260 | |
| 261 | // Visit globals last, since the module-level use-list block will be seen |
| 262 | // before the function bodies are processed. |
Rafael Espindola | 49e9bf8 | 2014-11-17 20:06:27 +0000 | [diff] [blame] | 263 | for (const GlobalVariable &G : M.globals()) |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 264 | predictValueUseListOrder(&G, nullptr, OM, Stack); |
Rafael Espindola | 49e9bf8 | 2014-11-17 20:06:27 +0000 | [diff] [blame] | 265 | for (const Function &F : M) |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 266 | predictValueUseListOrder(&F, nullptr, OM, Stack); |
Rafael Espindola | 49e9bf8 | 2014-11-17 20:06:27 +0000 | [diff] [blame] | 267 | for (const GlobalAlias &A : M.aliases()) |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 268 | predictValueUseListOrder(&A, nullptr, OM, Stack); |
Dmitry Polukhin | a1feff7 | 2016-04-07 12:32:19 +0000 | [diff] [blame] | 269 | for (const GlobalIFunc &I : M.ifuncs()) |
| 270 | predictValueUseListOrder(&I, nullptr, OM, Stack); |
Rafael Espindola | 49e9bf8 | 2014-11-17 20:06:27 +0000 | [diff] [blame] | 271 | for (const GlobalVariable &G : M.globals()) |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 272 | if (G.hasInitializer()) |
| 273 | predictValueUseListOrder(G.getInitializer(), nullptr, OM, Stack); |
Rafael Espindola | 49e9bf8 | 2014-11-17 20:06:27 +0000 | [diff] [blame] | 274 | for (const GlobalAlias &A : M.aliases()) |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 275 | predictValueUseListOrder(A.getAliasee(), nullptr, OM, Stack); |
Dmitry Polukhin | a1feff7 | 2016-04-07 12:32:19 +0000 | [diff] [blame] | 276 | for (const GlobalIFunc &I : M.ifuncs()) |
| 277 | predictValueUseListOrder(I.getResolver(), nullptr, OM, Stack); |
Peter Collingbourne | 51d2de7 | 2014-12-03 02:08:38 +0000 | [diff] [blame] | 278 | for (const Function &F : M) { |
Vedant Kumar | 3a63fb3 | 2015-12-19 08:52:49 +0000 | [diff] [blame] | 279 | for (const Use &U : F.operands()) |
| 280 | predictValueUseListOrder(U.get(), nullptr, OM, Stack); |
Peter Collingbourne | 51d2de7 | 2014-12-03 02:08:38 +0000 | [diff] [blame] | 281 | } |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 282 | |
| 283 | return Stack; |
| 284 | } |
| 285 | |
Duncan Sands | e6beec6 | 2012-11-13 12:59:33 +0000 | [diff] [blame] | 286 | static bool isIntOrIntVectorValue(const std::pair<const Value*, unsigned> &V) { |
| 287 | return V.first->getType()->isIntOrIntVectorTy(); |
Chris Lattner | 430e80d | 2007-05-04 05:21:47 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Duncan P. N. Exon Smith | 458593a | 2015-04-14 23:45:11 +0000 | [diff] [blame] | 290 | ValueEnumerator::ValueEnumerator(const Module &M, |
| 291 | bool ShouldPreserveUseListOrder) |
Duncan P. N. Exon Smith | 6565a0d | 2016-03-27 23:17:54 +0000 | [diff] [blame] | 292 | : ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) { |
Duncan P. N. Exon Smith | 458593a | 2015-04-14 23:45:11 +0000 | [diff] [blame] | 293 | if (ShouldPreserveUseListOrder) |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 294 | UseListOrders = predictUseListOrder(M); |
| 295 | |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 296 | // Enumerate the global variables. |
Yaron Keren | 4c20deb | 2015-06-12 20:18:20 +0000 | [diff] [blame] | 297 | for (const GlobalVariable &GV : M.globals()) |
| 298 | EnumerateValue(&GV); |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 299 | |
| 300 | // Enumerate the functions. |
Yaron Keren | 4c20deb | 2015-06-12 20:18:20 +0000 | [diff] [blame] | 301 | for (const Function & F : M) { |
| 302 | EnumerateValue(&F); |
| 303 | EnumerateAttributes(F.getAttributes()); |
Duncan Sands | ad0ea2d | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 304 | } |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 305 | |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 306 | // Enumerate the aliases. |
Yaron Keren | 4c20deb | 2015-06-12 20:18:20 +0000 | [diff] [blame] | 307 | for (const GlobalAlias &GA : M.aliases()) |
| 308 | EnumerateValue(&GA); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 309 | |
Dmitry Polukhin | a1feff7 | 2016-04-07 12:32:19 +0000 | [diff] [blame] | 310 | // Enumerate the ifuncs. |
| 311 | for (const GlobalIFunc &GIF : M.ifuncs()) |
| 312 | EnumerateValue(&GIF); |
| 313 | |
Chris Lattner | 430e80d | 2007-05-04 05:21:47 +0000 | [diff] [blame] | 314 | // Remember what is the cutoff between globalvalue's and other constants. |
| 315 | unsigned FirstConstant = Values.size(); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 316 | |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 317 | // Enumerate the global variable initializers. |
Yaron Keren | 4c20deb | 2015-06-12 20:18:20 +0000 | [diff] [blame] | 318 | for (const GlobalVariable &GV : M.globals()) |
| 319 | if (GV.hasInitializer()) |
| 320 | EnumerateValue(GV.getInitializer()); |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 321 | |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 322 | // Enumerate the aliasees. |
Yaron Keren | 4c20deb | 2015-06-12 20:18:20 +0000 | [diff] [blame] | 323 | for (const GlobalAlias &GA : M.aliases()) |
| 324 | EnumerateValue(GA.getAliasee()); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 325 | |
Dmitry Polukhin | a1feff7 | 2016-04-07 12:32:19 +0000 | [diff] [blame] | 326 | // Enumerate the ifunc resolvers. |
| 327 | for (const GlobalIFunc &GIF : M.ifuncs()) |
| 328 | EnumerateValue(GIF.getResolver()); |
| 329 | |
Vedant Kumar | 3a63fb3 | 2015-12-19 08:52:49 +0000 | [diff] [blame] | 330 | // Enumerate any optional Function data. |
Yaron Keren | 4c20deb | 2015-06-12 20:18:20 +0000 | [diff] [blame] | 331 | for (const Function &F : M) |
Vedant Kumar | 3a63fb3 | 2015-12-19 08:52:49 +0000 | [diff] [blame] | 332 | for (const Use &U : F.operands()) |
| 333 | EnumerateValue(U.get()); |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 334 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 335 | // Enumerate the metadata type. |
| 336 | // |
| 337 | // TODO: Move this to ValueEnumerator::EnumerateOperandType() once bitcode |
| 338 | // only encodes the metadata type when it's used as a value. |
| 339 | EnumerateType(Type::getMetadataTy(M.getContext())); |
| 340 | |
Joe Abbey | bc6f4ba | 2013-04-01 02:28:07 +0000 | [diff] [blame] | 341 | // 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] | 342 | // pool so that the module symbol table can refer to them... |
Rafael Espindola | 49e9bf8 | 2014-11-17 20:06:27 +0000 | [diff] [blame] | 343 | EnumerateValueSymbolTable(M.getValueSymbolTable()); |
Dan Gohman | 2637cc1 | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 344 | EnumerateNamedMetadata(M); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 345 | |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 346 | SmallVector<std::pair<unsigned, MDNode *>, 8> MDs; |
Peter Collingbourne | cceae7f | 2016-05-31 23:01:54 +0000 | [diff] [blame] | 347 | for (const GlobalVariable &GV : M.globals()) { |
Peter Collingbourne | 382d81c | 2016-06-01 01:17:57 +0000 | [diff] [blame] | 348 | MDs.clear(); |
Peter Collingbourne | cceae7f | 2016-05-31 23:01:54 +0000 | [diff] [blame] | 349 | GV.getAllMetadata(MDs); |
| 350 | for (const auto &I : MDs) |
Peter Collingbourne | 2152189 | 2016-06-21 23:42:48 +0000 | [diff] [blame] | 351 | // FIXME: Pass GV to EnumerateMetadata and arrange for the bitcode writer |
| 352 | // to write metadata to the global variable's own metadata block |
| 353 | // (PR28134). |
| 354 | EnumerateMetadata(nullptr, I.second); |
Peter Collingbourne | cceae7f | 2016-05-31 23:01:54 +0000 | [diff] [blame] | 355 | } |
Chris Lattner | 8dace89 | 2009-12-31 00:51:46 +0000 | [diff] [blame] | 356 | |
Chris Lattner | 5f640b9 | 2007-04-26 03:50:57 +0000 | [diff] [blame] | 357 | // Enumerate types used by function bodies and argument lists. |
Rafael Espindola | 49e9bf8 | 2014-11-17 20:06:27 +0000 | [diff] [blame] | 358 | for (const Function &F : M) { |
Rafael Espindola | 087d627 | 2014-06-17 03:00:40 +0000 | [diff] [blame] | 359 | for (const Argument &A : F.args()) |
| 360 | EnumerateType(A.getType()); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 361 | |
Duncan P. N. Exon Smith | 3d4cd75 | 2015-04-24 22:04:41 +0000 | [diff] [blame] | 362 | // Enumerate metadata attached to this function. |
Peter Collingbourne | 382d81c | 2016-06-01 01:17:57 +0000 | [diff] [blame] | 363 | MDs.clear(); |
Duncan P. N. Exon Smith | 3d4cd75 | 2015-04-24 22:04:41 +0000 | [diff] [blame] | 364 | F.getAllMetadata(MDs); |
| 365 | for (const auto &I : MDs) |
Peter Collingbourne | 2152189 | 2016-06-21 23:42:48 +0000 | [diff] [blame] | 366 | EnumerateMetadata(F.isDeclaration() ? nullptr : &F, I.second); |
Duncan P. N. Exon Smith | 3d4cd75 | 2015-04-24 22:04:41 +0000 | [diff] [blame] | 367 | |
Rafael Espindola | 087d627 | 2014-06-17 03:00:40 +0000 | [diff] [blame] | 368 | for (const BasicBlock &BB : F) |
| 369 | for (const Instruction &I : BB) { |
| 370 | for (const Use &Op : I.operands()) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 371 | auto *MD = dyn_cast<MetadataAsValue>(&Op); |
| 372 | if (!MD) { |
| 373 | EnumerateOperandType(Op); |
| 374 | continue; |
| 375 | } |
| 376 | |
| 377 | // Local metadata is enumerated during function-incorporation. |
| 378 | if (isa<LocalAsMetadata>(MD->getMetadata())) |
| 379 | continue; |
| 380 | |
Duncan P. N. Exon Smith | 520f854 | 2016-04-02 15:22:57 +0000 | [diff] [blame] | 381 | EnumerateMetadata(&F, MD->getMetadata()); |
Victor Hernandez | 572218b | 2010-01-14 19:54:11 +0000 | [diff] [blame] | 382 | } |
Rafael Espindola | 087d627 | 2014-06-17 03:00:40 +0000 | [diff] [blame] | 383 | EnumerateType(I.getType()); |
| 384 | if (const CallInst *CI = dyn_cast<CallInst>(&I)) |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 385 | EnumerateAttributes(CI->getAttributes()); |
Rafael Espindola | 087d627 | 2014-06-17 03:00:40 +0000 | [diff] [blame] | 386 | else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 387 | EnumerateAttributes(II->getAttributes()); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 388 | |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 389 | // Enumerate metadata attached with this instruction. |
Devang Patel | 6da5dbf | 2009-10-22 18:55:16 +0000 | [diff] [blame] | 390 | MDs.clear(); |
Rafael Espindola | 087d627 | 2014-06-17 03:00:40 +0000 | [diff] [blame] | 391 | I.getAllMetadataOtherThanDebugLoc(MDs); |
Chris Lattner | 2f2aa2b | 2009-12-28 23:41:32 +0000 | [diff] [blame] | 392 | for (unsigned i = 0, e = MDs.size(); i != e; ++i) |
Duncan P. N. Exon Smith | 520f854 | 2016-04-02 15:22:57 +0000 | [diff] [blame] | 393 | EnumerateMetadata(&F, MDs[i].second); |
Joe Abbey | 2ad8df2 | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 394 | |
Duncan P. N. Exon Smith | ab659fb3 | 2015-03-30 19:40:05 +0000 | [diff] [blame] | 395 | // Don't enumerate the location directly -- it has a special record |
| 396 | // type -- but enumerate its operands. |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 397 | if (DILocation *L = I.getDebugLoc()) |
Duncan P. N. Exon Smith | 9695eb3 | 2016-04-19 03:46:51 +0000 | [diff] [blame] | 398 | for (const Metadata *Op : L->operands()) |
| 399 | EnumerateMetadata(&F, Op); |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 400 | } |
| 401 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 402 | |
Chris Lattner | 430e80d | 2007-05-04 05:21:47 +0000 | [diff] [blame] | 403 | // Optimize constant ordering. |
| 404 | OptimizeConstants(FirstConstant, Values.size()); |
Duncan P. N. Exon Smith | 6565a0d | 2016-03-27 23:17:54 +0000 | [diff] [blame] | 405 | |
| 406 | // Organize metadata ordering. |
| 407 | organizeMetadata(); |
Rafael Espindola | 337a1b2 | 2011-04-06 16:49:37 +0000 | [diff] [blame] | 408 | } |
| 409 | |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 410 | unsigned ValueEnumerator::getInstructionID(const Instruction *Inst) const { |
| 411 | InstructionMapType::const_iterator I = InstructionMap.find(Inst); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 412 | assert(I != InstructionMap.end() && "Instruction is not mapped!"); |
Dan Gohman | 1f4b028 | 2010-08-25 17:09:50 +0000 | [diff] [blame] | 413 | return I->second; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 414 | } |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 415 | |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 416 | unsigned ValueEnumerator::getComdatID(const Comdat *C) const { |
| 417 | unsigned ComdatID = Comdats.idFor(C); |
| 418 | assert(ComdatID && "Comdat not found!"); |
| 419 | return ComdatID; |
| 420 | } |
| 421 | |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 422 | void ValueEnumerator::setInstructionID(const Instruction *I) { |
| 423 | InstructionMap[I] = InstructionCount++; |
| 424 | } |
| 425 | |
Devang Patel | 05eb617 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 426 | unsigned ValueEnumerator::getValueID(const Value *V) const { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 427 | if (auto *MD = dyn_cast<MetadataAsValue>(V)) |
| 428 | return getMetadataID(MD->getMetadata()); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 429 | |
Devang Patel | 05eb617 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 430 | ValueMapType::const_iterator I = ValueMap.find(V); |
| 431 | assert(I != ValueMap.end() && "Value not in slotcalculator!"); |
| 432 | return I->second-1; |
| 433 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 434 | |
Yaron Keren | eb2a254 | 2016-01-29 20:50:44 +0000 | [diff] [blame] | 435 | LLVM_DUMP_METHOD void ValueEnumerator::dump() const { |
Chad Rosier | 78037a9 | 2011-12-07 20:44:46 +0000 | [diff] [blame] | 436 | print(dbgs(), ValueMap, "Default"); |
| 437 | dbgs() << '\n'; |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 438 | print(dbgs(), MetadataMap, "MetaData"); |
Chad Rosier | 78037a9 | 2011-12-07 20:44:46 +0000 | [diff] [blame] | 439 | dbgs() << '\n'; |
| 440 | } |
| 441 | |
| 442 | void ValueEnumerator::print(raw_ostream &OS, const ValueMapType &Map, |
| 443 | const char *Name) const { |
| 444 | |
| 445 | OS << "Map Name: " << Name << "\n"; |
| 446 | OS << "Size: " << Map.size() << "\n"; |
| 447 | for (ValueMapType::const_iterator I = Map.begin(), |
| 448 | E = Map.end(); I != E; ++I) { |
| 449 | |
| 450 | const Value *V = I->first; |
| 451 | if (V->hasName()) |
| 452 | OS << "Value: " << V->getName(); |
| 453 | else |
| 454 | OS << "Value: [null]\n"; |
| 455 | V->dump(); |
| 456 | |
| 457 | OS << " Uses(" << std::distance(V->use_begin(),V->use_end()) << "):"; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 458 | for (const Use &U : V->uses()) { |
| 459 | if (&U != &*V->use_begin()) |
Chad Rosier | 78037a9 | 2011-12-07 20:44:46 +0000 | [diff] [blame] | 460 | OS << ","; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 461 | if(U->hasName()) |
| 462 | OS << " " << U->getName(); |
Chad Rosier | 78037a9 | 2011-12-07 20:44:46 +0000 | [diff] [blame] | 463 | else |
| 464 | OS << " [null]"; |
| 465 | |
| 466 | } |
| 467 | OS << "\n\n"; |
| 468 | } |
| 469 | } |
| 470 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 471 | void ValueEnumerator::print(raw_ostream &OS, const MetadataMapType &Map, |
| 472 | const char *Name) const { |
| 473 | |
| 474 | OS << "Map Name: " << Name << "\n"; |
| 475 | OS << "Size: " << Map.size() << "\n"; |
| 476 | for (auto I = Map.begin(), E = Map.end(); I != E; ++I) { |
| 477 | const Metadata *MD = I->first; |
Duncan P. N. Exon Smith | 520f854 | 2016-04-02 15:22:57 +0000 | [diff] [blame] | 478 | OS << "Metadata: slot = " << I->second.ID << "\n"; |
| 479 | OS << "Metadata: function = " << I->second.F << "\n"; |
Nick Lewycky | ee0a3a7 | 2014-12-17 01:52:08 +0000 | [diff] [blame] | 480 | MD->print(OS); |
Duncan P. N. Exon Smith | 520f854 | 2016-04-02 15:22:57 +0000 | [diff] [blame] | 481 | OS << "\n"; |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 482 | } |
| 483 | } |
| 484 | |
Chris Lattner | 430e80d | 2007-05-04 05:21:47 +0000 | [diff] [blame] | 485 | /// OptimizeConstants - Reorder constant pool for denser encoding. |
| 486 | void ValueEnumerator::OptimizeConstants(unsigned CstStart, unsigned CstEnd) { |
| 487 | if (CstStart == CstEnd || CstStart+1 == CstEnd) return; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 488 | |
Duncan P. N. Exon Smith | 458593a | 2015-04-14 23:45:11 +0000 | [diff] [blame] | 489 | if (ShouldPreserveUseListOrder) |
Duncan P. N. Exon Smith | 15eb0ab | 2014-07-25 16:13:16 +0000 | [diff] [blame] | 490 | // Optimizing constants makes the use-list order difficult to predict. |
| 491 | // Disable it for now when trying to preserve the order. |
| 492 | return; |
| 493 | |
Benjamin Kramer | 3a377bc | 2014-03-01 11:47:00 +0000 | [diff] [blame] | 494 | std::stable_sort(Values.begin() + CstStart, Values.begin() + CstEnd, |
| 495 | [this](const std::pair<const Value *, unsigned> &LHS, |
| 496 | const std::pair<const Value *, unsigned> &RHS) { |
| 497 | // Sort by plane. |
| 498 | if (LHS.first->getType() != RHS.first->getType()) |
| 499 | return getTypeID(LHS.first->getType()) < getTypeID(RHS.first->getType()); |
| 500 | // Then by frequency. |
| 501 | return LHS.second > RHS.second; |
| 502 | }); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 503 | |
Duncan Sands | e6beec6 | 2012-11-13 12:59:33 +0000 | [diff] [blame] | 504 | // Ensure that integer and vector of integer constants are at the start of the |
| 505 | // constant pool. This is important so that GEP structure indices come before |
| 506 | // gep constant exprs. |
Duncan P. N. Exon Smith | bdde9e1 | 2016-03-25 02:20:28 +0000 | [diff] [blame] | 507 | std::stable_partition(Values.begin() + CstStart, Values.begin() + CstEnd, |
| 508 | isIntOrIntVectorValue); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 509 | |
Chris Lattner | 430e80d | 2007-05-04 05:21:47 +0000 | [diff] [blame] | 510 | // Rebuild the modified portion of ValueMap. |
| 511 | for (; CstStart != CstEnd; ++CstStart) |
| 512 | ValueMap[Values[CstStart].first] = CstStart+1; |
| 513 | } |
| 514 | |
| 515 | |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 516 | /// EnumerateValueSymbolTable - Insert all of the values in the specified symbol |
| 517 | /// table into the values table. |
| 518 | void ValueEnumerator::EnumerateValueSymbolTable(const ValueSymbolTable &VST) { |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 519 | for (ValueSymbolTable::const_iterator VI = VST.begin(), VE = VST.end(); |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 520 | VI != VE; ++VI) |
| 521 | EnumerateValue(VI->getValue()); |
| 522 | } |
| 523 | |
Rafael Espindola | 49e9bf8 | 2014-11-17 20:06:27 +0000 | [diff] [blame] | 524 | /// Insert all of the values referenced by named metadata in the specified |
| 525 | /// module. |
| 526 | void ValueEnumerator::EnumerateNamedMetadata(const Module &M) { |
Duncan P. N. Exon Smith | 584af87 | 2015-10-13 03:26:19 +0000 | [diff] [blame] | 527 | for (const auto &I : M.named_metadata()) |
| 528 | EnumerateNamedMDNode(&I); |
Devang Patel | fcfee0f | 2010-01-07 19:39:36 +0000 | [diff] [blame] | 529 | } |
| 530 | |
Devang Patel | 99ff5a8 | 2010-01-09 00:30:14 +0000 | [diff] [blame] | 531 | void ValueEnumerator::EnumerateNamedMDNode(const NamedMDNode *MD) { |
Devang Patel | 99ff5a8 | 2010-01-09 00:30:14 +0000 | [diff] [blame] | 532 | for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i) |
Duncan P. N. Exon Smith | 520f854 | 2016-04-02 15:22:57 +0000 | [diff] [blame] | 533 | EnumerateMetadata(nullptr, MD->getOperand(i)); |
| 534 | } |
| 535 | |
Peter Collingbourne | 2152189 | 2016-06-21 23:42:48 +0000 | [diff] [blame] | 536 | unsigned ValueEnumerator::getMetadataFunctionID(const Function *F) const { |
| 537 | return F ? getValueID(F) + 1 : 0; |
Duncan P. N. Exon Smith | 520f854 | 2016-04-02 15:22:57 +0000 | [diff] [blame] | 538 | } |
| 539 | |
Peter Collingbourne | 2152189 | 2016-06-21 23:42:48 +0000 | [diff] [blame] | 540 | void ValueEnumerator::EnumerateMetadata(const Function *F, const Metadata *MD) { |
| 541 | EnumerateMetadata(getMetadataFunctionID(F), MD); |
Duncan P. N. Exon Smith | 520f854 | 2016-04-02 15:22:57 +0000 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | void ValueEnumerator::EnumerateFunctionLocalMetadata( |
| 545 | const Function &F, const LocalAsMetadata *Local) { |
Peter Collingbourne | 2152189 | 2016-06-21 23:42:48 +0000 | [diff] [blame] | 546 | EnumerateFunctionLocalMetadata(getMetadataFunctionID(&F), Local); |
Devang Patel | 99ff5a8 | 2010-01-09 00:30:14 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Duncan P. N. Exon Smith | 9695eb3 | 2016-04-19 03:46:51 +0000 | [diff] [blame] | 549 | void ValueEnumerator::dropFunctionFromMetadata( |
| 550 | MetadataMapType::value_type &FirstMD) { |
Duncan P. N. Exon Smith | 134cb5d | 2016-04-18 01:24:58 +0000 | [diff] [blame] | 551 | SmallVector<const MDNode *, 64> Worklist; |
Malcolm Parsons | 17d266b | 2017-01-13 17:12:16 +0000 | [diff] [blame] | 552 | auto push = [&Worklist](MetadataMapType::value_type &MD) { |
Duncan P. N. Exon Smith | 9695eb3 | 2016-04-19 03:46:51 +0000 | [diff] [blame] | 553 | auto &Entry = MD.second; |
| 554 | |
| 555 | // Nothing to do if this metadata isn't tagged. |
| 556 | if (!Entry.F) |
| 557 | return; |
| 558 | |
| 559 | // Drop the function tag. |
| 560 | Entry.F = 0; |
| 561 | |
| 562 | // If this is has an ID and is an MDNode, then its operands have entries as |
| 563 | // well. We need to drop the function from them too. |
| 564 | if (Entry.ID) |
| 565 | if (auto *N = dyn_cast<MDNode>(MD.first)) |
| 566 | Worklist.push_back(N); |
| 567 | }; |
| 568 | push(FirstMD); |
| 569 | while (!Worklist.empty()) |
Duncan P. N. Exon Smith | 134cb5d | 2016-04-18 01:24:58 +0000 | [diff] [blame] | 570 | for (const Metadata *Op : Worklist.pop_back_val()->operands()) { |
Duncan P. N. Exon Smith | 520f854 | 2016-04-02 15:22:57 +0000 | [diff] [blame] | 571 | if (!Op) |
| 572 | continue; |
Duncan P. N. Exon Smith | 9695eb3 | 2016-04-19 03:46:51 +0000 | [diff] [blame] | 573 | auto MD = MetadataMap.find(Op); |
| 574 | if (MD != MetadataMap.end()) |
| 575 | push(*MD); |
Duncan P. N. Exon Smith | 520f854 | 2016-04-02 15:22:57 +0000 | [diff] [blame] | 576 | } |
Duncan P. N. Exon Smith | 520f854 | 2016-04-02 15:22:57 +0000 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | void ValueEnumerator::EnumerateMetadata(unsigned F, const Metadata *MD) { |
Duncan P. N. Exon Smith | 30805b2 | 2016-04-23 04:59:22 +0000 | [diff] [blame] | 580 | // It's vital for reader efficiency that uniqued subgraphs are done in |
| 581 | // post-order; it's expensive when their operands have forward references. |
| 582 | // If a distinct node is referenced from a uniqued node, it'll be delayed |
| 583 | // until the uniqued subgraph has been completely traversed. |
| 584 | SmallVector<const MDNode *, 32> DelayedDistinctNodes; |
| 585 | |
Duncan P. N. Exon Smith | 9695eb3 | 2016-04-19 03:46:51 +0000 | [diff] [blame] | 586 | // Start by enumerating MD, and then work through its transitive operands in |
Duncan P. N. Exon Smith | c196531 | 2016-04-21 01:55:12 +0000 | [diff] [blame] | 587 | // post-order. This requires a depth-first search. |
Duncan P. N. Exon Smith | 71480bd | 2016-04-22 02:33:06 +0000 | [diff] [blame] | 588 | SmallVector<std::pair<const MDNode *, MDNode::op_iterator>, 32> Worklist; |
| 589 | if (const MDNode *N = enumerateMetadataImpl(F, MD)) |
| 590 | Worklist.push_back(std::make_pair(N, N->op_begin())); |
| 591 | |
Duncan P. N. Exon Smith | 9695eb3 | 2016-04-19 03:46:51 +0000 | [diff] [blame] | 592 | while (!Worklist.empty()) { |
| 593 | const MDNode *N = Worklist.back().first; |
Duncan P. N. Exon Smith | c196531 | 2016-04-21 01:55:12 +0000 | [diff] [blame] | 594 | |
Duncan P. N. Exon Smith | d9bbdce | 2016-04-23 04:22:38 +0000 | [diff] [blame] | 595 | // Enumerate operands until we hit a new node. We need to traverse these |
| 596 | // nodes' operands before visiting the rest of N's operands. |
| 597 | MDNode::op_iterator I = std::find_if( |
| 598 | Worklist.back().second, N->op_end(), |
| 599 | [&](const Metadata *MD) { return enumerateMetadataImpl(F, MD); }); |
| 600 | if (I != N->op_end()) { |
| 601 | auto *Op = cast<MDNode>(*I); |
| 602 | Worklist.back().second = ++I; |
Duncan P. N. Exon Smith | 30805b2 | 2016-04-23 04:59:22 +0000 | [diff] [blame] | 603 | |
| 604 | // Delay traversing Op if it's a distinct node and N is uniqued. |
| 605 | if (Op->isDistinct() && !N->isDistinct()) |
| 606 | DelayedDistinctNodes.push_back(Op); |
| 607 | else |
| 608 | Worklist.push_back(std::make_pair(Op, Op->op_begin())); |
Duncan P. N. Exon Smith | 9695eb3 | 2016-04-19 03:46:51 +0000 | [diff] [blame] | 609 | continue; |
Duncan P. N. Exon Smith | 71480bd | 2016-04-22 02:33:06 +0000 | [diff] [blame] | 610 | } |
Duncan P. N. Exon Smith | 9695eb3 | 2016-04-19 03:46:51 +0000 | [diff] [blame] | 611 | |
| 612 | // All the operands have been visited. Now assign an ID. |
| 613 | Worklist.pop_back(); |
| 614 | MDs.push_back(N); |
| 615 | MetadataMap[N].ID = MDs.size(); |
Duncan P. N. Exon Smith | 30805b2 | 2016-04-23 04:59:22 +0000 | [diff] [blame] | 616 | |
| 617 | // Flush out any delayed distinct nodes; these are all the distinct nodes |
| 618 | // that are leaves in last uniqued subgraph. |
| 619 | if (Worklist.empty() || Worklist.back().first->isDistinct()) { |
| 620 | for (const MDNode *N : DelayedDistinctNodes) |
| 621 | Worklist.push_back(std::make_pair(N, N->op_begin())); |
| 622 | DelayedDistinctNodes.clear(); |
| 623 | } |
Duncan P. N. Exon Smith | 9695eb3 | 2016-04-19 03:46:51 +0000 | [diff] [blame] | 624 | } |
| 625 | } |
| 626 | |
Duncan P. N. Exon Smith | 71480bd | 2016-04-22 02:33:06 +0000 | [diff] [blame] | 627 | const MDNode *ValueEnumerator::enumerateMetadataImpl(unsigned F, const Metadata *MD) { |
Duncan P. N. Exon Smith | 9695eb3 | 2016-04-19 03:46:51 +0000 | [diff] [blame] | 628 | if (!MD) |
Duncan P. N. Exon Smith | 71480bd | 2016-04-22 02:33:06 +0000 | [diff] [blame] | 629 | return nullptr; |
Duncan P. N. Exon Smith | 9695eb3 | 2016-04-19 03:46:51 +0000 | [diff] [blame] | 630 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 631 | assert( |
| 632 | (isa<MDNode>(MD) || isa<MDString>(MD) || isa<ConstantAsMetadata>(MD)) && |
| 633 | "Invalid metadata kind"); |
Dan Gohman | c828c54 | 2010-08-24 02:24:03 +0000 | [diff] [blame] | 634 | |
Duncan P. N. Exon Smith | 9695eb3 | 2016-04-19 03:46:51 +0000 | [diff] [blame] | 635 | auto Insertion = MetadataMap.insert(std::make_pair(MD, MDIndex(F))); |
| 636 | MDIndex &Entry = Insertion.first->second; |
| 637 | if (!Insertion.second) { |
| 638 | // Already mapped. If F doesn't match the function tag, drop it. |
| 639 | if (Entry.hasDifferentFunction(F)) |
| 640 | dropFunctionFromMetadata(*Insertion.first); |
Duncan P. N. Exon Smith | 71480bd | 2016-04-22 02:33:06 +0000 | [diff] [blame] | 641 | return nullptr; |
Duncan P. N. Exon Smith | 9695eb3 | 2016-04-19 03:46:51 +0000 | [diff] [blame] | 642 | } |
Duncan P. N. Exon Smith | 60d87e7 | 2014-10-21 22:13:34 +0000 | [diff] [blame] | 643 | |
Duncan P. N. Exon Smith | 71480bd | 2016-04-22 02:33:06 +0000 | [diff] [blame] | 644 | // Don't assign IDs to metadata nodes. |
| 645 | if (auto *N = dyn_cast<MDNode>(MD)) |
| 646 | return N; |
Duncan P. N. Exon Smith | 2fcf60e | 2015-01-12 22:30:34 +0000 | [diff] [blame] | 647 | |
Duncan P. N. Exon Smith | 520f854 | 2016-04-02 15:22:57 +0000 | [diff] [blame] | 648 | // Save the metadata. |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 649 | MDs.push_back(MD); |
Duncan P. N. Exon Smith | 9695eb3 | 2016-04-19 03:46:51 +0000 | [diff] [blame] | 650 | Entry.ID = MDs.size(); |
| 651 | |
| 652 | // Enumerate the constant, if any. |
| 653 | if (auto *C = dyn_cast<ConstantAsMetadata>(MD)) |
| 654 | EnumerateValue(C->getValue()); |
Duncan P. N. Exon Smith | c196531 | 2016-04-21 01:55:12 +0000 | [diff] [blame] | 655 | |
Duncan P. N. Exon Smith | 71480bd | 2016-04-22 02:33:06 +0000 | [diff] [blame] | 656 | return nullptr; |
Dan Gohman | c828c54 | 2010-08-24 02:24:03 +0000 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | /// EnumerateFunctionLocalMetadataa - Incorporate function-local metadata |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 660 | /// information reachable from the metadata. |
| 661 | void ValueEnumerator::EnumerateFunctionLocalMetadata( |
Duncan P. N. Exon Smith | 520f854 | 2016-04-02 15:22:57 +0000 | [diff] [blame] | 662 | unsigned F, const LocalAsMetadata *Local) { |
| 663 | assert(F && "Expected a function"); |
| 664 | |
Dan Gohman | c828c54 | 2010-08-24 02:24:03 +0000 | [diff] [blame] | 665 | // Check to see if it's already in! |
Duncan P. N. Exon Smith | 520f854 | 2016-04-02 15:22:57 +0000 | [diff] [blame] | 666 | MDIndex &Index = MetadataMap[Local]; |
| 667 | if (Index.ID) { |
| 668 | assert(Index.F == F && "Expected the same function"); |
Dan Gohman | c828c54 | 2010-08-24 02:24:03 +0000 | [diff] [blame] | 669 | return; |
Duncan P. N. Exon Smith | 520f854 | 2016-04-02 15:22:57 +0000 | [diff] [blame] | 670 | } |
Duncan P. N. Exon Smith | 60d87e7 | 2014-10-21 22:13:34 +0000 | [diff] [blame] | 671 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 672 | MDs.push_back(Local); |
Duncan P. N. Exon Smith | 520f854 | 2016-04-02 15:22:57 +0000 | [diff] [blame] | 673 | Index.F = F; |
| 674 | Index.ID = MDs.size(); |
Dan Gohman | c828c54 | 2010-08-24 02:24:03 +0000 | [diff] [blame] | 675 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 676 | EnumerateValue(Local->getValue()); |
Devang Patel | 05eb617 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 677 | } |
| 678 | |
Duncan P. N. Exon Smith | 1483fff | 2016-04-23 04:42:39 +0000 | [diff] [blame] | 679 | static unsigned getMetadataTypeOrder(const Metadata *MD) { |
| 680 | // Strings are emitted in bulk and must come first. |
| 681 | if (isa<MDString>(MD)) |
| 682 | return 0; |
| 683 | |
| 684 | // ConstantAsMetadata doesn't reference anything. We may as well shuffle it |
| 685 | // to the front since we can detect it. |
| 686 | auto *N = dyn_cast<MDNode>(MD); |
| 687 | if (!N) |
| 688 | return 1; |
| 689 | |
| 690 | // The reader is fast forward references for distinct node operands, but slow |
| 691 | // when uniqued operands are unresolved. |
| 692 | return N->isDistinct() ? 2 : 3; |
| 693 | } |
| 694 | |
Duncan P. N. Exon Smith | 6565a0d | 2016-03-27 23:17:54 +0000 | [diff] [blame] | 695 | void ValueEnumerator::organizeMetadata() { |
Duncan P. N. Exon Smith | 520f854 | 2016-04-02 15:22:57 +0000 | [diff] [blame] | 696 | assert(MetadataMap.size() == MDs.size() && |
| 697 | "Metadata map and vector out of sync"); |
| 698 | |
| 699 | if (MDs.empty()) |
Duncan P. N. Exon Smith | 6565a0d | 2016-03-27 23:17:54 +0000 | [diff] [blame] | 700 | return; |
| 701 | |
Duncan P. N. Exon Smith | 520f854 | 2016-04-02 15:22:57 +0000 | [diff] [blame] | 702 | // Copy out the index information from MetadataMap in order to choose a new |
| 703 | // order. |
| 704 | SmallVector<MDIndex, 64> Order; |
| 705 | Order.reserve(MetadataMap.size()); |
| 706 | for (const Metadata *MD : MDs) |
| 707 | Order.push_back(MetadataMap.lookup(MD)); |
Duncan P. N. Exon Smith | 6565a0d | 2016-03-27 23:17:54 +0000 | [diff] [blame] | 708 | |
Duncan P. N. Exon Smith | 520f854 | 2016-04-02 15:22:57 +0000 | [diff] [blame] | 709 | // Partition: |
| 710 | // - by function, then |
| 711 | // - by isa<MDString> |
| 712 | // and then sort by the original/current ID. Since the IDs are guaranteed to |
| 713 | // be unique, the result of std::sort will be deterministic. There's no need |
| 714 | // for std::stable_sort. |
| 715 | std::sort(Order.begin(), Order.end(), [this](MDIndex LHS, MDIndex RHS) { |
Duncan P. N. Exon Smith | 1483fff | 2016-04-23 04:42:39 +0000 | [diff] [blame] | 716 | return std::make_tuple(LHS.F, getMetadataTypeOrder(LHS.get(MDs)), LHS.ID) < |
| 717 | std::make_tuple(RHS.F, getMetadataTypeOrder(RHS.get(MDs)), RHS.ID); |
Duncan P. N. Exon Smith | 520f854 | 2016-04-02 15:22:57 +0000 | [diff] [blame] | 718 | }); |
| 719 | |
Duncan P. N. Exon Smith | 520f854 | 2016-04-02 15:22:57 +0000 | [diff] [blame] | 720 | // Rebuild MDs, index the metadata ranges for each function in FunctionMDs, |
| 721 | // and fix up MetadataMap. |
| 722 | std::vector<const Metadata *> OldMDs = std::move(MDs); |
| 723 | MDs.reserve(OldMDs.size()); |
| 724 | for (unsigned I = 0, E = Order.size(); I != E && !Order[I].F; ++I) { |
| 725 | auto *MD = Order[I].get(OldMDs); |
| 726 | MDs.push_back(MD); |
| 727 | MetadataMap[MD].ID = I + 1; |
| 728 | if (isa<MDString>(MD)) |
| 729 | ++NumMDStrings; |
| 730 | } |
| 731 | |
| 732 | // Return early if there's nothing for the functions. |
| 733 | if (MDs.size() == Order.size()) |
| 734 | return; |
| 735 | |
| 736 | // Build the function metadata ranges. |
| 737 | MDRange R; |
| 738 | FunctionMDs.reserve(OldMDs.size()); |
| 739 | unsigned PrevF = 0; |
| 740 | for (unsigned I = MDs.size(), E = Order.size(), ID = MDs.size(); I != E; |
| 741 | ++I) { |
| 742 | unsigned F = Order[I].F; |
| 743 | if (!PrevF) { |
| 744 | PrevF = F; |
| 745 | } else if (PrevF != F) { |
| 746 | R.Last = FunctionMDs.size(); |
| 747 | std::swap(R, FunctionMDInfo[PrevF]); |
| 748 | R.First = FunctionMDs.size(); |
| 749 | |
| 750 | ID = MDs.size(); |
| 751 | PrevF = F; |
| 752 | } |
| 753 | |
| 754 | auto *MD = Order[I].get(OldMDs); |
| 755 | FunctionMDs.push_back(MD); |
| 756 | MetadataMap[MD].ID = ++ID; |
| 757 | if (isa<MDString>(MD)) |
| 758 | ++R.NumStrings; |
| 759 | } |
| 760 | R.Last = FunctionMDs.size(); |
| 761 | FunctionMDInfo[PrevF] = R; |
| 762 | } |
| 763 | |
| 764 | void ValueEnumerator::incorporateFunctionMetadata(const Function &F) { |
| 765 | NumModuleMDs = MDs.size(); |
| 766 | |
| 767 | auto R = FunctionMDInfo.lookup(getValueID(&F) + 1); |
| 768 | NumMDStrings = R.NumStrings; |
| 769 | MDs.insert(MDs.end(), FunctionMDs.begin() + R.First, |
| 770 | FunctionMDs.begin() + R.Last); |
Duncan P. N. Exon Smith | 6565a0d | 2016-03-27 23:17:54 +0000 | [diff] [blame] | 771 | } |
| 772 | |
Victor Hernandez | 572218b | 2010-01-14 19:54:11 +0000 | [diff] [blame] | 773 | void ValueEnumerator::EnumerateValue(const Value *V) { |
Chris Lattner | 8dace89 | 2009-12-31 00:51:46 +0000 | [diff] [blame] | 774 | assert(!V->getType()->isVoidTy() && "Can't insert void values!"); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 775 | assert(!isa<MetadataAsValue>(V) && "EnumerateValue doesn't handle Metadata!"); |
Devang Patel | 05eb617 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 776 | |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 777 | // Check to see if it's already in! |
| 778 | unsigned &ValueID = ValueMap[V]; |
| 779 | if (ValueID) { |
| 780 | // Increment use count. |
| 781 | Values[ValueID-1].second++; |
| 782 | return; |
| 783 | } |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 784 | |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 785 | if (auto *GO = dyn_cast<GlobalObject>(V)) |
| 786 | if (const Comdat *C = GO->getComdat()) |
| 787 | Comdats.insert(C); |
| 788 | |
Chris Lattner | 9ee4836 | 2007-05-06 01:00:28 +0000 | [diff] [blame] | 789 | // Enumerate the type of this value. |
| 790 | EnumerateType(V->getType()); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 791 | |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 792 | if (const Constant *C = dyn_cast<Constant>(V)) { |
| 793 | if (isa<GlobalValue>(C)) { |
| 794 | // Initializers for globals are handled explicitly elsewhere. |
Chris Lattner | 9ee4836 | 2007-05-06 01:00:28 +0000 | [diff] [blame] | 795 | } else if (C->getNumOperands()) { |
| 796 | // If a constant has operands, enumerate them. This makes sure that if a |
| 797 | // constant has uses (for example an array of const ints), that they are |
| 798 | // inserted also. |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 799 | |
Chris Lattner | 9ee4836 | 2007-05-06 01:00:28 +0000 | [diff] [blame] | 800 | // We prefer to enumerate them with values before we enumerate the user |
| 801 | // itself. This makes it more likely that we can avoid forward references |
| 802 | // in the reader. We know that there can be no cycles in the constants |
| 803 | // graph that don't go through a global variable. |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 804 | for (User::const_op_iterator I = C->op_begin(), E = C->op_end(); |
| 805 | I != E; ++I) |
Chris Lattner | aa99c94 | 2009-11-01 01:27:45 +0000 | [diff] [blame] | 806 | if (!isa<BasicBlock>(*I)) // Don't enumerate BB operand to BlockAddress. |
Victor Hernandez | 572218b | 2010-01-14 19:54:11 +0000 | [diff] [blame] | 807 | EnumerateValue(*I); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 808 | |
Chris Lattner | 9ee4836 | 2007-05-06 01:00:28 +0000 | [diff] [blame] | 809 | // Finally, add the value. Doing this could make the ValueID reference be |
| 810 | // dangling, don't reuse it. |
| 811 | Values.push_back(std::make_pair(V, 1U)); |
| 812 | ValueMap[V] = Values.size(); |
| 813 | return; |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 814 | } |
| 815 | } |
Devang Patel | e059ba6e | 2009-07-23 01:07:34 +0000 | [diff] [blame] | 816 | |
Chris Lattner | 9ee4836 | 2007-05-06 01:00:28 +0000 | [diff] [blame] | 817 | // Add the value. |
| 818 | Values.push_back(std::make_pair(V, 1U)); |
| 819 | ValueID = Values.size(); |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 823 | void ValueEnumerator::EnumerateType(Type *Ty) { |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 824 | unsigned *TypeID = &TypeMap[Ty]; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 825 | |
Rafael Espindola | 337a1b2 | 2011-04-06 16:49:37 +0000 | [diff] [blame] | 826 | // We've already seen this type. |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 827 | if (*TypeID) |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 828 | return; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 829 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 830 | // If it is a non-anonymous struct, mark the type as being visited so that we |
| 831 | // don't recursively visit it. This is safe because we allow forward |
| 832 | // references of these in the bitcode reader. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 833 | if (StructType *STy = dyn_cast<StructType>(Ty)) |
Chris Lattner | 335d399 | 2011-08-12 18:06:37 +0000 | [diff] [blame] | 834 | if (!STy->isLiteral()) |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 835 | *TypeID = ~0U; |
Joe Abbey | 2ad8df2 | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 836 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 837 | // Enumerate all of the subtypes before we enumerate this type. This ensures |
| 838 | // that the type will be enumerated in an order that can be directly built. |
Rafael Espindola | ffbfcf2 | 2014-11-24 20:44:36 +0000 | [diff] [blame] | 839 | for (Type *SubTy : Ty->subtypes()) |
| 840 | EnumerateType(SubTy); |
Joe Abbey | 2ad8df2 | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 841 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 842 | // Refresh the TypeID pointer in case the table rehashed. |
| 843 | TypeID = &TypeMap[Ty]; |
Joe Abbey | 2ad8df2 | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 844 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 845 | // Check to see if we got the pointer another way. This can happen when |
| 846 | // enumerating recursive types that hit the base case deeper than they start. |
| 847 | // |
| 848 | // If this is actually a struct that we are treating as forward ref'able, |
| 849 | // then emit the definition now that all of its contents are available. |
| 850 | if (*TypeID && *TypeID != ~0U) |
| 851 | return; |
Joe Abbey | 2ad8df2 | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 852 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 853 | // Add this type now that its contents are all happily enumerated. |
| 854 | Types.push_back(Ty); |
Joe Abbey | 2ad8df2 | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 855 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 856 | *TypeID = Types.size(); |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 857 | } |
| 858 | |
Chris Lattner | 76fd90f | 2007-05-06 08:35:19 +0000 | [diff] [blame] | 859 | // Enumerate the types for the specified value. If the value is a constant, |
| 860 | // walk through it, enumerating the types of the constant. |
Victor Hernandez | 572218b | 2010-01-14 19:54:11 +0000 | [diff] [blame] | 861 | void ValueEnumerator::EnumerateOperandType(const Value *V) { |
Chris Lattner | 76fd90f | 2007-05-06 08:35:19 +0000 | [diff] [blame] | 862 | EnumerateType(V->getType()); |
Joe Abbey | 2ad8df2 | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 863 | |
Duncan P. N. Exon Smith | 544e4f9 | 2016-03-28 00:03:12 +0000 | [diff] [blame] | 864 | assert(!isa<MetadataAsValue>(V) && "Unexpected metadata operand"); |
Joe Abbey | 2ad8df2 | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 865 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 866 | const Constant *C = dyn_cast<Constant>(V); |
| 867 | if (!C) |
| 868 | return; |
Joe Abbey | 2ad8df2 | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 869 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 870 | // If this constant is already enumerated, ignore it, we know its type must |
| 871 | // be enumerated. |
| 872 | if (ValueMap.count(C)) |
| 873 | return; |
Nick Lewycky | b8f9b7a | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 874 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 875 | // This constant may have operands, make sure to enumerate the types in |
| 876 | // them. |
Pete Cooper | 125ad17 | 2015-06-25 20:51:38 +0000 | [diff] [blame] | 877 | for (const Value *Op : C->operands()) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 878 | // Don't enumerate basic blocks here, this happens as operands to |
| 879 | // blockaddress. |
| 880 | if (isa<BasicBlock>(Op)) |
| 881 | continue; |
| 882 | |
| 883 | EnumerateOperandType(Op); |
| 884 | } |
Chris Lattner | 76fd90f | 2007-05-06 08:35:19 +0000 | [diff] [blame] | 885 | } |
| 886 | |
Bill Wendling | 7b5f4f3 | 2013-02-12 08:01:22 +0000 | [diff] [blame] | 887 | void ValueEnumerator::EnumerateAttributes(AttributeSet PAL) { |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 888 | if (PAL.isEmpty()) return; // null is always 0. |
Bill Wendling | 7b5f4f3 | 2013-02-12 08:01:22 +0000 | [diff] [blame] | 889 | |
Chris Lattner | e4bbad6 | 2007-05-03 22:46:43 +0000 | [diff] [blame] | 890 | // Do a lookup. |
Bill Wendling | 7b5f4f3 | 2013-02-12 08:01:22 +0000 | [diff] [blame] | 891 | unsigned &Entry = AttributeMap[PAL]; |
Chris Lattner | e4bbad6 | 2007-05-03 22:46:43 +0000 | [diff] [blame] | 892 | if (Entry == 0) { |
| 893 | // Never saw this before, add it. |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 894 | Attribute.push_back(PAL); |
| 895 | Entry = Attribute.size(); |
Chris Lattner | e4bbad6 | 2007-05-03 22:46:43 +0000 | [diff] [blame] | 896 | } |
Bill Wendling | 51f612e | 2013-02-10 23:06:02 +0000 | [diff] [blame] | 897 | |
| 898 | // Do lookups for all attribute groups. |
| 899 | for (unsigned i = 0, e = PAL.getNumSlots(); i != e; ++i) { |
| 900 | AttributeSet AS = PAL.getSlotAttributes(i); |
Bill Wendling | 92ed700 | 2013-02-11 22:33:26 +0000 | [diff] [blame] | 901 | unsigned &Entry = AttributeGroupMap[AS]; |
Bill Wendling | 51f612e | 2013-02-10 23:06:02 +0000 | [diff] [blame] | 902 | if (Entry == 0) { |
Bill Wendling | 92ed700 | 2013-02-11 22:33:26 +0000 | [diff] [blame] | 903 | AttributeGroups.push_back(AS); |
| 904 | Entry = AttributeGroups.size(); |
Bill Wendling | 51f612e | 2013-02-10 23:06:02 +0000 | [diff] [blame] | 905 | } |
| 906 | } |
Chris Lattner | e4bbad6 | 2007-05-03 22:46:43 +0000 | [diff] [blame] | 907 | } |
| 908 | |
Chad Rosier | 6a11b64 | 2011-06-03 17:02:19 +0000 | [diff] [blame] | 909 | void ValueEnumerator::incorporateFunction(const Function &F) { |
Nick Lewycky | a72e1af | 2010-02-25 08:30:17 +0000 | [diff] [blame] | 910 | InstructionCount = 0; |
Chris Lattner | e6e364c | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 911 | NumModuleValues = Values.size(); |
Duncan P. N. Exon Smith | 520f854 | 2016-04-02 15:22:57 +0000 | [diff] [blame] | 912 | |
| 913 | // Add global metadata to the function block. This doesn't include |
| 914 | // LocalAsMetadata. |
| 915 | incorporateFunctionMetadata(F); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 916 | |
Chris Lattner | 5f640b9 | 2007-04-26 03:50:57 +0000 | [diff] [blame] | 917 | // Adding function arguments to the value table. |
Duncan P. N. Exon Smith | 584af87 | 2015-10-13 03:26:19 +0000 | [diff] [blame] | 918 | for (const auto &I : F.args()) |
| 919 | EnumerateValue(&I); |
Chris Lattner | 5f640b9 | 2007-04-26 03:50:57 +0000 | [diff] [blame] | 920 | |
Chris Lattner | e6e364c | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 921 | FirstFuncConstantID = Values.size(); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 922 | |
Chris Lattner | 5f640b9 | 2007-04-26 03:50:57 +0000 | [diff] [blame] | 923 | // Add all function-level constants to the value table. |
Duncan P. N. Exon Smith | 584af87 | 2015-10-13 03:26:19 +0000 | [diff] [blame] | 924 | for (const BasicBlock &BB : F) { |
| 925 | for (const Instruction &I : BB) |
| 926 | for (const Use &OI : I.operands()) { |
| 927 | if ((isa<Constant>(OI) && !isa<GlobalValue>(OI)) || isa<InlineAsm>(OI)) |
| 928 | EnumerateValue(OI); |
Chris Lattner | 5f640b9 | 2007-04-26 03:50:57 +0000 | [diff] [blame] | 929 | } |
Duncan P. N. Exon Smith | 584af87 | 2015-10-13 03:26:19 +0000 | [diff] [blame] | 930 | BasicBlocks.push_back(&BB); |
| 931 | ValueMap[&BB] = BasicBlocks.size(); |
Chris Lattner | 5f640b9 | 2007-04-26 03:50:57 +0000 | [diff] [blame] | 932 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 933 | |
Chris Lattner | 430e80d | 2007-05-04 05:21:47 +0000 | [diff] [blame] | 934 | // Optimize the constant layout. |
| 935 | OptimizeConstants(FirstFuncConstantID, Values.size()); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 936 | |
Duncan Sands | ad0ea2d | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 937 | // Add the function's parameter attributes so they are available for use in |
| 938 | // the function's instruction. |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 939 | EnumerateAttributes(F.getAttributes()); |
Duncan Sands | ad0ea2d | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 940 | |
Chris Lattner | e6e364c | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 941 | FirstInstID = Values.size(); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 942 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 943 | SmallVector<LocalAsMetadata *, 8> FnLocalMDVector; |
Chris Lattner | 5f640b9 | 2007-04-26 03:50:57 +0000 | [diff] [blame] | 944 | // Add all of the instructions. |
Duncan P. N. Exon Smith | 584af87 | 2015-10-13 03:26:19 +0000 | [diff] [blame] | 945 | for (const BasicBlock &BB : F) { |
| 946 | for (const Instruction &I : BB) { |
| 947 | for (const Use &OI : I.operands()) { |
| 948 | if (auto *MD = dyn_cast<MetadataAsValue>(&OI)) |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 949 | if (auto *Local = dyn_cast<LocalAsMetadata>(MD->getMetadata())) |
Victor Hernandez | d44ee35 | 2010-02-04 01:13:08 +0000 | [diff] [blame] | 950 | // Enumerate metadata after the instructions they might refer to. |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 951 | FnLocalMDVector.push_back(Local); |
Dan Gohman | c828c54 | 2010-08-24 02:24:03 +0000 | [diff] [blame] | 952 | } |
Joe Abbey | 2ad8df2 | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 953 | |
Duncan P. N. Exon Smith | 584af87 | 2015-10-13 03:26:19 +0000 | [diff] [blame] | 954 | if (!I.getType()->isVoidTy()) |
| 955 | EnumerateValue(&I); |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 956 | } |
| 957 | } |
Victor Hernandez | d44ee35 | 2010-02-04 01:13:08 +0000 | [diff] [blame] | 958 | |
| 959 | // Add all of the function-local metadata. |
Mehdi Amini | a5cbf43 | 2016-07-08 01:13:41 +0000 | [diff] [blame] | 960 | for (unsigned i = 0, e = FnLocalMDVector.size(); i != e; ++i) { |
| 961 | // At this point, every local values have been incorporated, we shouldn't |
| 962 | // have a metadata operand that references a value that hasn't been seen. |
| 963 | assert(ValueMap.count(FnLocalMDVector[i]->getValue()) && |
| 964 | "Missing value for metadata operand"); |
Duncan P. N. Exon Smith | 520f854 | 2016-04-02 15:22:57 +0000 | [diff] [blame] | 965 | EnumerateFunctionLocalMetadata(F, FnLocalMDVector[i]); |
Mehdi Amini | a5cbf43 | 2016-07-08 01:13:41 +0000 | [diff] [blame] | 966 | } |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 967 | } |
| 968 | |
Chad Rosier | 6a11b64 | 2011-06-03 17:02:19 +0000 | [diff] [blame] | 969 | void ValueEnumerator::purgeFunction() { |
Chris Lattner | 5f640b9 | 2007-04-26 03:50:57 +0000 | [diff] [blame] | 970 | /// Remove purged values from the ValueMap. |
Chris Lattner | e6e364c | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 971 | for (unsigned i = NumModuleValues, e = Values.size(); i != e; ++i) |
Chris Lattner | 5f640b9 | 2007-04-26 03:50:57 +0000 | [diff] [blame] | 972 | ValueMap.erase(Values[i].first); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 973 | for (unsigned i = NumModuleMDs, e = MDs.size(); i != e; ++i) |
Teresa Johnson | 61b406e | 2015-12-29 23:00:22 +0000 | [diff] [blame] | 974 | MetadataMap.erase(MDs[i]); |
Chris Lattner | 7c37b01 | 2007-04-26 04:42:16 +0000 | [diff] [blame] | 975 | for (unsigned i = 0, e = BasicBlocks.size(); i != e; ++i) |
| 976 | ValueMap.erase(BasicBlocks[i]); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 977 | |
Chris Lattner | e6e364c | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 978 | Values.resize(NumModuleValues); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 979 | MDs.resize(NumModuleMDs); |
Chris Lattner | 7c37b01 | 2007-04-26 04:42:16 +0000 | [diff] [blame] | 980 | BasicBlocks.clear(); |
Duncan P. N. Exon Smith | 520f854 | 2016-04-02 15:22:57 +0000 | [diff] [blame] | 981 | NumMDStrings = 0; |
Chris Lattner | c1d10d6 | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 982 | } |
Chris Lattner | f540d74 | 2009-10-28 05:24:40 +0000 | [diff] [blame] | 983 | |
| 984 | static void IncorporateFunctionInfoGlobalBBIDs(const Function *F, |
| 985 | DenseMap<const BasicBlock*, unsigned> &IDMap) { |
| 986 | unsigned Counter = 0; |
Duncan P. N. Exon Smith | 584af87 | 2015-10-13 03:26:19 +0000 | [diff] [blame] | 987 | for (const BasicBlock &BB : *F) |
| 988 | IDMap[&BB] = ++Counter; |
Chris Lattner | f540d74 | 2009-10-28 05:24:40 +0000 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | /// getGlobalBasicBlockID - This returns the function-specific ID for the |
| 992 | /// specified basic block. This is relatively expensive information, so it |
| 993 | /// should only be used by rare constructs such as address-of-label. |
| 994 | unsigned ValueEnumerator::getGlobalBasicBlockID(const BasicBlock *BB) const { |
| 995 | unsigned &Idx = GlobalBasicBlockIDs[BB]; |
| 996 | if (Idx != 0) |
Chris Lattner | aa99c94 | 2009-11-01 01:27:45 +0000 | [diff] [blame] | 997 | return Idx-1; |
Chris Lattner | f540d74 | 2009-10-28 05:24:40 +0000 | [diff] [blame] | 998 | |
| 999 | IncorporateFunctionInfoGlobalBBIDs(BB->getParent(), GlobalBasicBlockIDs); |
| 1000 | return getGlobalBasicBlockID(BB); |
| 1001 | } |
David Blaikie | 7b02810 | 2015-02-25 00:51:52 +0000 | [diff] [blame] | 1002 | |
| 1003 | uint64_t ValueEnumerator::computeBitsRequiredForTypeIndicies() const { |
| 1004 | return Log2_32_Ceil(getTypes().size() + 1); |
| 1005 | } |