1 //===-- ValueEnumerator.cpp - Number values and types for bitcode writer --===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the ValueEnumerator class.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "ValueEnumerator.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/SmallPtrSet.h"
17 #include "llvm/IR/Constants.h"
18 #include "llvm/IR/DerivedTypes.h"
19 #include "llvm/IR/Instructions.h"
20 #include "llvm/IR/Module.h"
21 #include "llvm/IR/UseListOrder.h"
22 #include "llvm/IR/ValueSymbolTable.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/raw_ostream.h"
25 #include <algorithm>
26 using namespace llvm;
27 
28 namespace {
29 struct OrderMap {
30   DenseMap<const Value *, std::pair<unsigned, bool>> IDs;
31   unsigned LastGlobalConstantID;
32   unsigned LastGlobalValueID;
33 
OrderMap__anonbf5e3baf0111::OrderMap34   OrderMap() : LastGlobalConstantID(0), LastGlobalValueID(0) {}
35 
isGlobalConstant__anonbf5e3baf0111::OrderMap36   bool isGlobalConstant(unsigned ID) const {
37     return ID <= LastGlobalConstantID;
38   }
isGlobalValue__anonbf5e3baf0111::OrderMap39   bool isGlobalValue(unsigned ID) const {
40     return ID <= LastGlobalValueID && !isGlobalConstant(ID);
41   }
42 
size__anonbf5e3baf0111::OrderMap43   unsigned size() const { return IDs.size(); }
operator []__anonbf5e3baf0111::OrderMap44   std::pair<unsigned, bool> &operator[](const Value *V) { return IDs[V]; }
lookup__anonbf5e3baf0111::OrderMap45   std::pair<unsigned, bool> lookup(const Value *V) const {
46     return IDs.lookup(V);
47   }
index__anonbf5e3baf0111::OrderMap48   void index(const Value *V) {
49     // Explicitly sequence get-size and insert-value operations to avoid UB.
50     unsigned ID = IDs.size() + 1;
51     IDs[V].first = ID;
52   }
53 };
54 }
55 
orderValue(const Value * V,OrderMap & OM)56 static void orderValue(const Value *V, OrderMap &OM) {
57   if (OM.lookup(V).first)
58     return;
59 
60   if (const Constant *C = dyn_cast<Constant>(V))
61     if (C->getNumOperands() && !isa<GlobalValue>(C))
62       for (const Value *Op : C->operands())
63         if (!isa<BasicBlock>(Op) && !isa<GlobalValue>(Op))
64           orderValue(Op, OM);
65 
66   // Note: we cannot cache this lookup above, since inserting into the map
67   // changes the map's size, and thus affects the other IDs.
68   OM.index(V);
69 }
70 
orderModule(const Module & M)71 static OrderMap orderModule(const Module &M) {
72   // This needs to match the order used by ValueEnumerator::ValueEnumerator()
73   // and ValueEnumerator::incorporateFunction().
74   OrderMap OM;
75 
76   // In the reader, initializers of GlobalValues are set *after* all the
77   // globals have been read.  Rather than awkwardly modeling this behaviour
78   // directly in predictValueUseListOrderImpl(), just assign IDs to
79   // initializers of GlobalValues before GlobalValues themselves to model this
80   // implicitly.
81   for (const GlobalVariable &G : M.globals())
82     if (G.hasInitializer())
83       if (!isa<GlobalValue>(G.getInitializer()))
84         orderValue(G.getInitializer(), OM);
85   for (const GlobalAlias &A : M.aliases())
86     if (!isa<GlobalValue>(A.getAliasee()))
87       orderValue(A.getAliasee(), OM);
88   for (const Function &F : M) {
89     if (F.hasPrefixData())
90       if (!isa<GlobalValue>(F.getPrefixData()))
91         orderValue(F.getPrefixData(), OM);
92     if (F.hasPrologueData())
93       if (!isa<GlobalValue>(F.getPrologueData()))
94         orderValue(F.getPrologueData(), OM);
95   }
96   OM.LastGlobalConstantID = OM.size();
97 
98   // Initializers of GlobalValues are processed in
99   // BitcodeReader::ResolveGlobalAndAliasInits().  Match the order there rather
100   // than ValueEnumerator, and match the code in predictValueUseListOrderImpl()
101   // by giving IDs in reverse order.
102   //
103   // Since GlobalValues never reference each other directly (just through
104   // initializers), their relative IDs only matter for determining order of
105   // uses in their initializers.
106   for (const Function &F : M)
107     orderValue(&F, OM);
108   for (const GlobalAlias &A : M.aliases())
109     orderValue(&A, OM);
110   for (const GlobalVariable &G : M.globals())
111     orderValue(&G, OM);
112   OM.LastGlobalValueID = OM.size();
113 
114   for (const Function &F : M) {
115     if (F.isDeclaration())
116       continue;
117     // Here we need to match the union of ValueEnumerator::incorporateFunction()
118     // and WriteFunction().  Basic blocks are implicitly declared before
119     // anything else (by declaring their size).
120     for (const BasicBlock &BB : F)
121       orderValue(&BB, OM);
122     for (const Argument &A : F.args())
123       orderValue(&A, OM);
124     for (const BasicBlock &BB : F)
125       for (const Instruction &I : BB)
126         for (const Value *Op : I.operands())
127           if ((isa<Constant>(*Op) && !isa<GlobalValue>(*Op)) ||
128               isa<InlineAsm>(*Op))
129             orderValue(Op, OM);
130     for (const BasicBlock &BB : F)
131       for (const Instruction &I : BB)
132         orderValue(&I, OM);
133   }
134   return OM;
135 }
136 
predictValueUseListOrderImpl(const Value * V,const Function * F,unsigned ID,const OrderMap & OM,UseListOrderStack & Stack)137 static void predictValueUseListOrderImpl(const Value *V, const Function *F,
138                                          unsigned ID, const OrderMap &OM,
139                                          UseListOrderStack &Stack) {
140   // Predict use-list order for this one.
141   typedef std::pair<const Use *, unsigned> Entry;
142   SmallVector<Entry, 64> List;
143   for (const Use &U : V->uses())
144     // Check if this user will be serialized.
145     if (OM.lookup(U.getUser()).first)
146       List.push_back(std::make_pair(&U, List.size()));
147 
148   if (List.size() < 2)
149     // We may have lost some users.
150     return;
151 
152   bool IsGlobalValue = OM.isGlobalValue(ID);
153   std::sort(List.begin(), List.end(), [&](const Entry &L, const Entry &R) {
154     const Use *LU = L.first;
155     const Use *RU = R.first;
156     if (LU == RU)
157       return false;
158 
159     auto LID = OM.lookup(LU->getUser()).first;
160     auto RID = OM.lookup(RU->getUser()).first;
161 
162     // Global values are processed in reverse order.
163     //
164     // Moreover, initializers of GlobalValues are set *after* all the globals
165     // have been read (despite having earlier IDs).  Rather than awkwardly
166     // modeling this behaviour here, orderModule() has assigned IDs to
167     // initializers of GlobalValues before GlobalValues themselves.
168     if (OM.isGlobalValue(LID) && OM.isGlobalValue(RID))
169       return LID < RID;
170 
171     // If ID is 4, then expect: 7 6 5 1 2 3.
172     if (LID < RID) {
173       if (RID <= ID)
174         if (!IsGlobalValue) // GlobalValue uses don't get reversed.
175           return true;
176       return false;
177     }
178     if (RID < LID) {
179       if (LID <= ID)
180         if (!IsGlobalValue) // GlobalValue uses don't get reversed.
181           return false;
182       return true;
183     }
184 
185     // LID and RID are equal, so we have different operands of the same user.
186     // Assume operands are added in order for all instructions.
187     if (LID <= ID)
188       if (!IsGlobalValue) // GlobalValue uses don't get reversed.
189         return LU->getOperandNo() < RU->getOperandNo();
190     return LU->getOperandNo() > RU->getOperandNo();
191   });
192 
193   if (std::is_sorted(
194           List.begin(), List.end(),
195           [](const Entry &L, const Entry &R) { return L.second < R.second; }))
196     // Order is already correct.
197     return;
198 
199   // Store the shuffle.
200   Stack.emplace_back(V, F, List.size());
201   assert(List.size() == Stack.back().Shuffle.size() && "Wrong size");
202   for (size_t I = 0, E = List.size(); I != E; ++I)
203     Stack.back().Shuffle[I] = List[I].second;
204 }
205 
predictValueUseListOrder(const Value * V,const Function * F,OrderMap & OM,UseListOrderStack & Stack)206 static void predictValueUseListOrder(const Value *V, const Function *F,
207                                      OrderMap &OM, UseListOrderStack &Stack) {
208   auto &IDPair = OM[V];
209   assert(IDPair.first && "Unmapped value");
210   if (IDPair.second)
211     // Already predicted.
212     return;
213 
214   // Do the actual prediction.
215   IDPair.second = true;
216   if (!V->use_empty() && std::next(V->use_begin()) != V->use_end())
217     predictValueUseListOrderImpl(V, F, IDPair.first, OM, Stack);
218 
219   // Recursive descent into constants.
220   if (const Constant *C = dyn_cast<Constant>(V))
221     if (C->getNumOperands()) // Visit GlobalValues.
222       for (const Value *Op : C->operands())
223         if (isa<Constant>(Op)) // Visit GlobalValues.
224           predictValueUseListOrder(Op, F, OM, Stack);
225 }
226 
predictUseListOrder(const Module & M)227 static UseListOrderStack predictUseListOrder(const Module &M) {
228   OrderMap OM = orderModule(M);
229 
230   // Use-list orders need to be serialized after all the users have been added
231   // to a value, or else the shuffles will be incomplete.  Store them per
232   // function in a stack.
233   //
234   // Aside from function order, the order of values doesn't matter much here.
235   UseListOrderStack Stack;
236 
237   // We want to visit the functions backward now so we can list function-local
238   // constants in the last Function they're used in.  Module-level constants
239   // have already been visited above.
240   for (auto I = M.rbegin(), E = M.rend(); I != E; ++I) {
241     const Function &F = *I;
242     if (F.isDeclaration())
243       continue;
244     for (const BasicBlock &BB : F)
245       predictValueUseListOrder(&BB, &F, OM, Stack);
246     for (const Argument &A : F.args())
247       predictValueUseListOrder(&A, &F, OM, Stack);
248     for (const BasicBlock &BB : F)
249       for (const Instruction &I : BB)
250         for (const Value *Op : I.operands())
251           if (isa<Constant>(*Op) || isa<InlineAsm>(*Op)) // Visit GlobalValues.
252             predictValueUseListOrder(Op, &F, OM, Stack);
253     for (const BasicBlock &BB : F)
254       for (const Instruction &I : BB)
255         predictValueUseListOrder(&I, &F, OM, Stack);
256   }
257 
258   // Visit globals last, since the module-level use-list block will be seen
259   // before the function bodies are processed.
260   for (const GlobalVariable &G : M.globals())
261     predictValueUseListOrder(&G, nullptr, OM, Stack);
262   for (const Function &F : M)
263     predictValueUseListOrder(&F, nullptr, OM, Stack);
264   for (const GlobalAlias &A : M.aliases())
265     predictValueUseListOrder(&A, nullptr, OM, Stack);
266   for (const GlobalVariable &G : M.globals())
267     if (G.hasInitializer())
268       predictValueUseListOrder(G.getInitializer(), nullptr, OM, Stack);
269   for (const GlobalAlias &A : M.aliases())
270     predictValueUseListOrder(A.getAliasee(), nullptr, OM, Stack);
271   for (const Function &F : M) {
272     if (F.hasPrefixData())
273       predictValueUseListOrder(F.getPrefixData(), nullptr, OM, Stack);
274     if (F.hasPrologueData())
275       predictValueUseListOrder(F.getPrologueData(), nullptr, OM, Stack);
276   }
277 
278   return Stack;
279 }
280 
isIntOrIntVectorValue(const std::pair<const Value *,unsigned> & V)281 static bool isIntOrIntVectorValue(const std::pair<const Value*, unsigned> &V) {
282   return V.first->getType()->isIntOrIntVectorTy();
283 }
284 
ValueEnumerator(const Module & M)285 ValueEnumerator::ValueEnumerator(const Module &M)
286     : HasMDString(false), HasMDLocation(false) {
287   if (shouldPreserveBitcodeUseListOrder())
288     UseListOrders = predictUseListOrder(M);
289 
290   // Enumerate the global variables.
291   for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
292        I != E; ++I)
293     EnumerateValue(I);
294 
295   // Enumerate the functions.
296   for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I) {
297     EnumerateValue(I);
298     EnumerateAttributes(cast<Function>(I)->getAttributes());
299   }
300 
301   // Enumerate the aliases.
302   for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
303        I != E; ++I)
304     EnumerateValue(I);
305 
306   // Remember what is the cutoff between globalvalue's and other constants.
307   unsigned FirstConstant = Values.size();
308 
309   // Enumerate the global variable initializers.
310   for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
311        I != E; ++I)
312     if (I->hasInitializer())
313       EnumerateValue(I->getInitializer());
314 
315   // Enumerate the aliasees.
316   for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
317        I != E; ++I)
318     EnumerateValue(I->getAliasee());
319 
320   // Enumerate the prefix data constants.
321   for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
322     if (I->hasPrefixData())
323       EnumerateValue(I->getPrefixData());
324 
325   // Enumerate the prologue data constants.
326   for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
327     if (I->hasPrologueData())
328       EnumerateValue(I->getPrologueData());
329 
330   // Enumerate the metadata type.
331   //
332   // TODO: Move this to ValueEnumerator::EnumerateOperandType() once bitcode
333   // only encodes the metadata type when it's used as a value.
334   EnumerateType(Type::getMetadataTy(M.getContext()));
335 
336   // Insert constants and metadata that are named at module level into the slot
337   // pool so that the module symbol table can refer to them...
338   EnumerateValueSymbolTable(M.getValueSymbolTable());
339   EnumerateNamedMetadata(M);
340 
341   SmallVector<std::pair<unsigned, MDNode *>, 8> MDs;
342 
343   // Enumerate types used by function bodies and argument lists.
344   for (const Function &F : M) {
345     for (const Argument &A : F.args())
346       EnumerateType(A.getType());
347 
348     for (const BasicBlock &BB : F)
349       for (const Instruction &I : BB) {
350         for (const Use &Op : I.operands()) {
351           auto *MD = dyn_cast<MetadataAsValue>(&Op);
352           if (!MD) {
353             EnumerateOperandType(Op);
354             continue;
355           }
356 
357           // Local metadata is enumerated during function-incorporation.
358           if (isa<LocalAsMetadata>(MD->getMetadata()))
359             continue;
360 
361           EnumerateMetadata(MD->getMetadata());
362         }
363         EnumerateType(I.getType());
364         if (const CallInst *CI = dyn_cast<CallInst>(&I))
365           EnumerateAttributes(CI->getAttributes());
366         else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I))
367           EnumerateAttributes(II->getAttributes());
368 
369         // Enumerate metadata attached with this instruction.
370         MDs.clear();
371         I.getAllMetadataOtherThanDebugLoc(MDs);
372         for (unsigned i = 0, e = MDs.size(); i != e; ++i)
373           EnumerateMetadata(MDs[i].second);
374 
375         if (!I.getDebugLoc().isUnknown()) {
376           MDNode *Scope, *IA;
377           I.getDebugLoc().getScopeAndInlinedAt(Scope, IA, I.getContext());
378           if (Scope) EnumerateMetadata(Scope);
379           if (IA) EnumerateMetadata(IA);
380         }
381       }
382   }
383 
384   // Optimize constant ordering.
385   OptimizeConstants(FirstConstant, Values.size());
386 }
387 
getInstructionID(const Instruction * Inst) const388 unsigned ValueEnumerator::getInstructionID(const Instruction *Inst) const {
389   InstructionMapType::const_iterator I = InstructionMap.find(Inst);
390   assert(I != InstructionMap.end() && "Instruction is not mapped!");
391   return I->second;
392 }
393 
getComdatID(const Comdat * C) const394 unsigned ValueEnumerator::getComdatID(const Comdat *C) const {
395   unsigned ComdatID = Comdats.idFor(C);
396   assert(ComdatID && "Comdat not found!");
397   return ComdatID;
398 }
399 
setInstructionID(const Instruction * I)400 void ValueEnumerator::setInstructionID(const Instruction *I) {
401   InstructionMap[I] = InstructionCount++;
402 }
403 
getValueID(const Value * V) const404 unsigned ValueEnumerator::getValueID(const Value *V) const {
405   if (auto *MD = dyn_cast<MetadataAsValue>(V))
406     return getMetadataID(MD->getMetadata());
407 
408   ValueMapType::const_iterator I = ValueMap.find(V);
409   assert(I != ValueMap.end() && "Value not in slotcalculator!");
410   return I->second-1;
411 }
412 
getMetadataID(const Metadata * MD) const413 unsigned ValueEnumerator::getMetadataID(const Metadata *MD) const {
414   auto I = MDValueMap.find(MD);
415   assert(I != MDValueMap.end() && "Metadata not in slotcalculator!");
416   return I->second - 1;
417 }
418 
dump() const419 void ValueEnumerator::dump() const {
420   print(dbgs(), ValueMap, "Default");
421   dbgs() << '\n';
422   print(dbgs(), MDValueMap, "MetaData");
423   dbgs() << '\n';
424 }
425 
print(raw_ostream & OS,const ValueMapType & Map,const char * Name) const426 void ValueEnumerator::print(raw_ostream &OS, const ValueMapType &Map,
427                             const char *Name) const {
428 
429   OS << "Map Name: " << Name << "\n";
430   OS << "Size: " << Map.size() << "\n";
431   for (ValueMapType::const_iterator I = Map.begin(),
432          E = Map.end(); I != E; ++I) {
433 
434     const Value *V = I->first;
435     if (V->hasName())
436       OS << "Value: " << V->getName();
437     else
438       OS << "Value: [null]\n";
439     V->dump();
440 
441     OS << " Uses(" << std::distance(V->use_begin(),V->use_end()) << "):";
442     for (const Use &U : V->uses()) {
443       if (&U != &*V->use_begin())
444         OS << ",";
445       if(U->hasName())
446         OS << " " << U->getName();
447       else
448         OS << " [null]";
449 
450     }
451     OS <<  "\n\n";
452   }
453 }
454 
print(raw_ostream & OS,const MetadataMapType & Map,const char * Name) const455 void ValueEnumerator::print(raw_ostream &OS, const MetadataMapType &Map,
456                             const char *Name) const {
457 
458   OS << "Map Name: " << Name << "\n";
459   OS << "Size: " << Map.size() << "\n";
460   for (auto I = Map.begin(), E = Map.end(); I != E; ++I) {
461     const Metadata *MD = I->first;
462     OS << "Metadata: slot = " << I->second << "\n";
463     MD->print(OS);
464   }
465 }
466 
467 /// OptimizeConstants - Reorder constant pool for denser encoding.
OptimizeConstants(unsigned CstStart,unsigned CstEnd)468 void ValueEnumerator::OptimizeConstants(unsigned CstStart, unsigned CstEnd) {
469   if (CstStart == CstEnd || CstStart+1 == CstEnd) return;
470 
471   if (shouldPreserveBitcodeUseListOrder())
472     // Optimizing constants makes the use-list order difficult to predict.
473     // Disable it for now when trying to preserve the order.
474     return;
475 
476   std::stable_sort(Values.begin() + CstStart, Values.begin() + CstEnd,
477                    [this](const std::pair<const Value *, unsigned> &LHS,
478                           const std::pair<const Value *, unsigned> &RHS) {
479     // Sort by plane.
480     if (LHS.first->getType() != RHS.first->getType())
481       return getTypeID(LHS.first->getType()) < getTypeID(RHS.first->getType());
482     // Then by frequency.
483     return LHS.second > RHS.second;
484   });
485 
486   // Ensure that integer and vector of integer constants are at the start of the
487   // constant pool.  This is important so that GEP structure indices come before
488   // gep constant exprs.
489   std::partition(Values.begin()+CstStart, Values.begin()+CstEnd,
490                  isIntOrIntVectorValue);
491 
492   // Rebuild the modified portion of ValueMap.
493   for (; CstStart != CstEnd; ++CstStart)
494     ValueMap[Values[CstStart].first] = CstStart+1;
495 }
496 
497 
498 /// EnumerateValueSymbolTable - Insert all of the values in the specified symbol
499 /// table into the values table.
EnumerateValueSymbolTable(const ValueSymbolTable & VST)500 void ValueEnumerator::EnumerateValueSymbolTable(const ValueSymbolTable &VST) {
501   for (ValueSymbolTable::const_iterator VI = VST.begin(), VE = VST.end();
502        VI != VE; ++VI)
503     EnumerateValue(VI->getValue());
504 }
505 
506 /// Insert all of the values referenced by named metadata in the specified
507 /// module.
EnumerateNamedMetadata(const Module & M)508 void ValueEnumerator::EnumerateNamedMetadata(const Module &M) {
509   for (Module::const_named_metadata_iterator I = M.named_metadata_begin(),
510                                              E = M.named_metadata_end();
511        I != E; ++I)
512     EnumerateNamedMDNode(I);
513 }
514 
EnumerateNamedMDNode(const NamedMDNode * MD)515 void ValueEnumerator::EnumerateNamedMDNode(const NamedMDNode *MD) {
516   for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i)
517     EnumerateMetadata(MD->getOperand(i));
518 }
519 
520 /// EnumerateMDNodeOperands - Enumerate all non-function-local values
521 /// and types referenced by the given MDNode.
EnumerateMDNodeOperands(const MDNode * N)522 void ValueEnumerator::EnumerateMDNodeOperands(const MDNode *N) {
523   for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
524     Metadata *MD = N->getOperand(i);
525     if (!MD)
526       continue;
527     assert(!isa<LocalAsMetadata>(MD) && "MDNodes cannot be function-local");
528     EnumerateMetadata(MD);
529   }
530 }
531 
EnumerateMetadata(const Metadata * MD)532 void ValueEnumerator::EnumerateMetadata(const Metadata *MD) {
533   assert(
534       (isa<MDNode>(MD) || isa<MDString>(MD) || isa<ConstantAsMetadata>(MD)) &&
535       "Invalid metadata kind");
536 
537   // Insert a dummy ID to block the co-recursive call to
538   // EnumerateMDNodeOperands() from re-visiting MD in a cyclic graph.
539   //
540   // Return early if there's already an ID.
541   if (!MDValueMap.insert(std::make_pair(MD, 0)).second)
542     return;
543 
544   // Visit operands first to minimize RAUW.
545   if (auto *N = dyn_cast<MDNode>(MD))
546     EnumerateMDNodeOperands(N);
547   else if (auto *C = dyn_cast<ConstantAsMetadata>(MD))
548     EnumerateValue(C->getValue());
549 
550   HasMDString |= isa<MDString>(MD);
551   HasMDLocation |= isa<MDLocation>(MD);
552 
553   // Replace the dummy ID inserted above with the correct one.  MDValueMap may
554   // have changed by inserting operands, so we need a fresh lookup here.
555   MDs.push_back(MD);
556   MDValueMap[MD] = MDs.size();
557 }
558 
559 /// EnumerateFunctionLocalMetadataa - Incorporate function-local metadata
560 /// information reachable from the metadata.
EnumerateFunctionLocalMetadata(const LocalAsMetadata * Local)561 void ValueEnumerator::EnumerateFunctionLocalMetadata(
562     const LocalAsMetadata *Local) {
563   // Check to see if it's already in!
564   unsigned &MDValueID = MDValueMap[Local];
565   if (MDValueID)
566     return;
567 
568   MDs.push_back(Local);
569   MDValueID = MDs.size();
570 
571   EnumerateValue(Local->getValue());
572 
573   // Also, collect all function-local metadata for easy access.
574   FunctionLocalMDs.push_back(Local);
575 }
576 
EnumerateValue(const Value * V)577 void ValueEnumerator::EnumerateValue(const Value *V) {
578   assert(!V->getType()->isVoidTy() && "Can't insert void values!");
579   assert(!isa<MetadataAsValue>(V) && "EnumerateValue doesn't handle Metadata!");
580 
581   // Check to see if it's already in!
582   unsigned &ValueID = ValueMap[V];
583   if (ValueID) {
584     // Increment use count.
585     Values[ValueID-1].second++;
586     return;
587   }
588 
589   if (auto *GO = dyn_cast<GlobalObject>(V))
590     if (const Comdat *C = GO->getComdat())
591       Comdats.insert(C);
592 
593   // Enumerate the type of this value.
594   EnumerateType(V->getType());
595 
596   if (const Constant *C = dyn_cast<Constant>(V)) {
597     if (isa<GlobalValue>(C)) {
598       // Initializers for globals are handled explicitly elsewhere.
599     } else if (C->getNumOperands()) {
600       // If a constant has operands, enumerate them.  This makes sure that if a
601       // constant has uses (for example an array of const ints), that they are
602       // inserted also.
603 
604       // We prefer to enumerate them with values before we enumerate the user
605       // itself.  This makes it more likely that we can avoid forward references
606       // in the reader.  We know that there can be no cycles in the constants
607       // graph that don't go through a global variable.
608       for (User::const_op_iterator I = C->op_begin(), E = C->op_end();
609            I != E; ++I)
610         if (!isa<BasicBlock>(*I)) // Don't enumerate BB operand to BlockAddress.
611           EnumerateValue(*I);
612 
613       // Finally, add the value.  Doing this could make the ValueID reference be
614       // dangling, don't reuse it.
615       Values.push_back(std::make_pair(V, 1U));
616       ValueMap[V] = Values.size();
617       return;
618     }
619   }
620 
621   // Add the value.
622   Values.push_back(std::make_pair(V, 1U));
623   ValueID = Values.size();
624 }
625 
626 
EnumerateType(Type * Ty)627 void ValueEnumerator::EnumerateType(Type *Ty) {
628   unsigned *TypeID = &TypeMap[Ty];
629 
630   // We've already seen this type.
631   if (*TypeID)
632     return;
633 
634   // If it is a non-anonymous struct, mark the type as being visited so that we
635   // don't recursively visit it.  This is safe because we allow forward
636   // references of these in the bitcode reader.
637   if (StructType *STy = dyn_cast<StructType>(Ty))
638     if (!STy->isLiteral())
639       *TypeID = ~0U;
640 
641   // Enumerate all of the subtypes before we enumerate this type.  This ensures
642   // that the type will be enumerated in an order that can be directly built.
643   for (Type *SubTy : Ty->subtypes())
644     EnumerateType(SubTy);
645 
646   // Refresh the TypeID pointer in case the table rehashed.
647   TypeID = &TypeMap[Ty];
648 
649   // Check to see if we got the pointer another way.  This can happen when
650   // enumerating recursive types that hit the base case deeper than they start.
651   //
652   // If this is actually a struct that we are treating as forward ref'able,
653   // then emit the definition now that all of its contents are available.
654   if (*TypeID && *TypeID != ~0U)
655     return;
656 
657   // Add this type now that its contents are all happily enumerated.
658   Types.push_back(Ty);
659 
660   *TypeID = Types.size();
661 }
662 
663 // Enumerate the types for the specified value.  If the value is a constant,
664 // walk through it, enumerating the types of the constant.
EnumerateOperandType(const Value * V)665 void ValueEnumerator::EnumerateOperandType(const Value *V) {
666   EnumerateType(V->getType());
667 
668   if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
669     assert(!isa<LocalAsMetadata>(MD->getMetadata()) &&
670            "Function-local metadata should be left for later");
671 
672     EnumerateMetadata(MD->getMetadata());
673     return;
674   }
675 
676   const Constant *C = dyn_cast<Constant>(V);
677   if (!C)
678     return;
679 
680   // If this constant is already enumerated, ignore it, we know its type must
681   // be enumerated.
682   if (ValueMap.count(C))
683     return;
684 
685   // This constant may have operands, make sure to enumerate the types in
686   // them.
687   for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) {
688     const Value *Op = C->getOperand(i);
689 
690     // Don't enumerate basic blocks here, this happens as operands to
691     // blockaddress.
692     if (isa<BasicBlock>(Op))
693       continue;
694 
695     EnumerateOperandType(Op);
696   }
697 }
698 
EnumerateAttributes(AttributeSet PAL)699 void ValueEnumerator::EnumerateAttributes(AttributeSet PAL) {
700   if (PAL.isEmpty()) return;  // null is always 0.
701 
702   // Do a lookup.
703   unsigned &Entry = AttributeMap[PAL];
704   if (Entry == 0) {
705     // Never saw this before, add it.
706     Attribute.push_back(PAL);
707     Entry = Attribute.size();
708   }
709 
710   // Do lookups for all attribute groups.
711   for (unsigned i = 0, e = PAL.getNumSlots(); i != e; ++i) {
712     AttributeSet AS = PAL.getSlotAttributes(i);
713     unsigned &Entry = AttributeGroupMap[AS];
714     if (Entry == 0) {
715       AttributeGroups.push_back(AS);
716       Entry = AttributeGroups.size();
717     }
718   }
719 }
720 
incorporateFunction(const Function & F)721 void ValueEnumerator::incorporateFunction(const Function &F) {
722   InstructionCount = 0;
723   NumModuleValues = Values.size();
724   NumModuleMDs = MDs.size();
725 
726   // Adding function arguments to the value table.
727   for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
728        I != E; ++I)
729     EnumerateValue(I);
730 
731   FirstFuncConstantID = Values.size();
732 
733   // Add all function-level constants to the value table.
734   for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
735     for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I)
736       for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
737            OI != E; ++OI) {
738         if ((isa<Constant>(*OI) && !isa<GlobalValue>(*OI)) ||
739             isa<InlineAsm>(*OI))
740           EnumerateValue(*OI);
741       }
742     BasicBlocks.push_back(BB);
743     ValueMap[BB] = BasicBlocks.size();
744   }
745 
746   // Optimize the constant layout.
747   OptimizeConstants(FirstFuncConstantID, Values.size());
748 
749   // Add the function's parameter attributes so they are available for use in
750   // the function's instruction.
751   EnumerateAttributes(F.getAttributes());
752 
753   FirstInstID = Values.size();
754 
755   SmallVector<LocalAsMetadata *, 8> FnLocalMDVector;
756   // Add all of the instructions.
757   for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
758     for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
759       for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
760            OI != E; ++OI) {
761         if (auto *MD = dyn_cast<MetadataAsValue>(&*OI))
762           if (auto *Local = dyn_cast<LocalAsMetadata>(MD->getMetadata()))
763             // Enumerate metadata after the instructions they might refer to.
764             FnLocalMDVector.push_back(Local);
765       }
766 
767       if (!I->getType()->isVoidTy())
768         EnumerateValue(I);
769     }
770   }
771 
772   // Add all of the function-local metadata.
773   for (unsigned i = 0, e = FnLocalMDVector.size(); i != e; ++i)
774     EnumerateFunctionLocalMetadata(FnLocalMDVector[i]);
775 }
776 
purgeFunction()777 void ValueEnumerator::purgeFunction() {
778   /// Remove purged values from the ValueMap.
779   for (unsigned i = NumModuleValues, e = Values.size(); i != e; ++i)
780     ValueMap.erase(Values[i].first);
781   for (unsigned i = NumModuleMDs, e = MDs.size(); i != e; ++i)
782     MDValueMap.erase(MDs[i]);
783   for (unsigned i = 0, e = BasicBlocks.size(); i != e; ++i)
784     ValueMap.erase(BasicBlocks[i]);
785 
786   Values.resize(NumModuleValues);
787   MDs.resize(NumModuleMDs);
788   BasicBlocks.clear();
789   FunctionLocalMDs.clear();
790 }
791 
IncorporateFunctionInfoGlobalBBIDs(const Function * F,DenseMap<const BasicBlock *,unsigned> & IDMap)792 static void IncorporateFunctionInfoGlobalBBIDs(const Function *F,
793                                  DenseMap<const BasicBlock*, unsigned> &IDMap) {
794   unsigned Counter = 0;
795   for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
796     IDMap[BB] = ++Counter;
797 }
798 
799 /// getGlobalBasicBlockID - This returns the function-specific ID for the
800 /// specified basic block.  This is relatively expensive information, so it
801 /// should only be used by rare constructs such as address-of-label.
getGlobalBasicBlockID(const BasicBlock * BB) const802 unsigned ValueEnumerator::getGlobalBasicBlockID(const BasicBlock *BB) const {
803   unsigned &Idx = GlobalBasicBlockIDs[BB];
804   if (Idx != 0)
805     return Idx-1;
806 
807   IncorporateFunctionInfoGlobalBBIDs(BB->getParent(), GlobalBasicBlockIDs);
808   return getGlobalBasicBlockID(BB);
809 }
810