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