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