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