LLVM API Documentation

ValueEnumerator.cpp
Go to the documentation of this file.
00001 //===-- ValueEnumerator.cpp - Number values and types for bitcode writer --===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the ValueEnumerator class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "ValueEnumerator.h"
00015 #include "llvm/ADT/STLExtras.h"
00016 #include "llvm/ADT/SmallPtrSet.h"
00017 #include "llvm/IR/Constants.h"
00018 #include "llvm/IR/DerivedTypes.h"
00019 #include "llvm/IR/Instructions.h"
00020 #include "llvm/IR/Module.h"
00021 #include "llvm/IR/UseListOrder.h"
00022 #include "llvm/IR/ValueSymbolTable.h"
00023 #include "llvm/Support/Debug.h"
00024 #include "llvm/Support/raw_ostream.h"
00025 #include <algorithm>
00026 using namespace llvm;
00027 
00028 namespace {
00029 struct OrderMap {
00030   DenseMap<const Value *, std::pair<unsigned, bool>> IDs;
00031   unsigned LastGlobalConstantID;
00032   unsigned LastGlobalValueID;
00033 
00034   OrderMap() : LastGlobalConstantID(0), LastGlobalValueID(0) {}
00035 
00036   bool isGlobalConstant(unsigned ID) const {
00037     return ID <= LastGlobalConstantID;
00038   }
00039   bool isGlobalValue(unsigned ID) const {
00040     return ID <= LastGlobalValueID && !isGlobalConstant(ID);
00041   }
00042 
00043   unsigned size() const { return IDs.size(); }
00044   std::pair<unsigned, bool> &operator[](const Value *V) { return IDs[V]; }
00045   std::pair<unsigned, bool> lookup(const Value *V) const {
00046     return IDs.lookup(V);
00047   }
00048   void index(const Value *V) {
00049     // Explicitly sequence get-size and insert-value operations to avoid UB.
00050     unsigned ID = IDs.size() + 1;
00051     IDs[V].first = ID;
00052   }
00053 };
00054 }
00055 
00056 static void orderValue(const Value *V, OrderMap &OM) {
00057   if (OM.lookup(V).first)
00058     return;
00059 
00060   if (const Constant *C = dyn_cast<Constant>(V))
00061     if (C->getNumOperands() && !isa<GlobalValue>(C))
00062       for (const Value *Op : C->operands())
00063         if (!isa<BasicBlock>(Op) && !isa<GlobalValue>(Op))
00064           orderValue(Op, OM);
00065 
00066   // Note: we cannot cache this lookup above, since inserting into the map
00067   // changes the map's size, and thus affects the other IDs.
00068   OM.index(V);
00069 }
00070 
00071 static OrderMap orderModule(const Module &M) {
00072   // This needs to match the order used by ValueEnumerator::ValueEnumerator()
00073   // and ValueEnumerator::incorporateFunction().
00074   OrderMap OM;
00075 
00076   // In the reader, initializers of GlobalValues are set *after* all the
00077   // globals have been read.  Rather than awkwardly modeling this behaviour
00078   // directly in predictValueUseListOrderImpl(), just assign IDs to
00079   // initializers of GlobalValues before GlobalValues themselves to model this
00080   // implicitly.
00081   for (const GlobalVariable &G : M.globals())
00082     if (G.hasInitializer())
00083       if (!isa<GlobalValue>(G.getInitializer()))
00084         orderValue(G.getInitializer(), OM);
00085   for (const GlobalAlias &A : M.aliases())
00086     if (!isa<GlobalValue>(A.getAliasee()))
00087       orderValue(A.getAliasee(), OM);
00088   for (const Function &F : M) {
00089     if (F.hasPrefixData())
00090       if (!isa<GlobalValue>(F.getPrefixData()))
00091         orderValue(F.getPrefixData(), OM);
00092     if (F.hasPrologueData())
00093       if (!isa<GlobalValue>(F.getPrologueData()))
00094         orderValue(F.getPrologueData(), OM);
00095   }
00096   OM.LastGlobalConstantID = OM.size();
00097 
00098   // Initializers of GlobalValues are processed in
00099   // BitcodeReader::ResolveGlobalAndAliasInits().  Match the order there rather
00100   // than ValueEnumerator, and match the code in predictValueUseListOrderImpl()
00101   // by giving IDs in reverse order.
00102   //
00103   // Since GlobalValues never reference each other directly (just through
00104   // initializers), their relative IDs only matter for determining order of
00105   // uses in their initializers.
00106   for (const Function &F : M)
00107     orderValue(&F, OM);
00108   for (const GlobalAlias &A : M.aliases())
00109     orderValue(&A, OM);
00110   for (const GlobalVariable &G : M.globals())
00111     orderValue(&G, OM);
00112   OM.LastGlobalValueID = OM.size();
00113 
00114   for (const Function &F : M) {
00115     if (F.isDeclaration())
00116       continue;
00117     // Here we need to match the union of ValueEnumerator::incorporateFunction()
00118     // and WriteFunction().  Basic blocks are implicitly declared before
00119     // anything else (by declaring their size).
00120     for (const BasicBlock &BB : F)
00121       orderValue(&BB, OM);
00122     for (const Argument &A : F.args())
00123       orderValue(&A, OM);
00124     for (const BasicBlock &BB : F)
00125       for (const Instruction &I : BB)
00126         for (const Value *Op : I.operands())
00127           if ((isa<Constant>(*Op) && !isa<GlobalValue>(*Op)) ||
00128               isa<InlineAsm>(*Op))
00129             orderValue(Op, OM);
00130     for (const BasicBlock &BB : F)
00131       for (const Instruction &I : BB)
00132         orderValue(&I, OM);
00133   }
00134   return OM;
00135 }
00136 
00137 static void predictValueUseListOrderImpl(const Value *V, const Function *F,
00138                                          unsigned ID, const OrderMap &OM,
00139                                          UseListOrderStack &Stack) {
00140   // Predict use-list order for this one.
00141   typedef std::pair<const Use *, unsigned> Entry;
00142   SmallVector<Entry, 64> List;
00143   for (const Use &U : V->uses())
00144     // Check if this user will be serialized.
00145     if (OM.lookup(U.getUser()).first)
00146       List.push_back(std::make_pair(&U, List.size()));
00147 
00148   if (List.size() < 2)
00149     // We may have lost some users.
00150     return;
00151 
00152   bool IsGlobalValue = OM.isGlobalValue(ID);
00153   std::sort(List.begin(), List.end(), [&](const Entry &L, const Entry &R) {
00154     const Use *LU = L.first;
00155     const Use *RU = R.first;
00156     if (LU == RU)
00157       return false;
00158 
00159     auto LID = OM.lookup(LU->getUser()).first;
00160     auto RID = OM.lookup(RU->getUser()).first;
00161 
00162     // Global values are processed in reverse order.
00163     //
00164     // Moreover, initializers of GlobalValues are set *after* all the globals
00165     // have been read (despite having earlier IDs).  Rather than awkwardly
00166     // modeling this behaviour here, orderModule() has assigned IDs to
00167     // initializers of GlobalValues before GlobalValues themselves.
00168     if (OM.isGlobalValue(LID) && OM.isGlobalValue(RID))
00169       return LID < RID;
00170 
00171     // If ID is 4, then expect: 7 6 5 1 2 3.
00172     if (LID < RID) {
00173       if (RID <= ID)
00174         if (!IsGlobalValue) // GlobalValue uses don't get reversed.
00175           return true;
00176       return false;
00177     }
00178     if (RID < LID) {
00179       if (LID <= ID)
00180         if (!IsGlobalValue) // GlobalValue uses don't get reversed.
00181           return false;
00182       return true;
00183     }
00184 
00185     // LID and RID are equal, so we have different operands of the same user.
00186     // Assume operands are added in order for all instructions.
00187     if (LID <= ID)
00188       if (!IsGlobalValue) // GlobalValue uses don't get reversed.
00189         return LU->getOperandNo() < RU->getOperandNo();
00190     return LU->getOperandNo() > RU->getOperandNo();
00191   });
00192 
00193   if (std::is_sorted(
00194           List.begin(), List.end(),
00195           [](const Entry &L, const Entry &R) { return L.second < R.second; }))
00196     // Order is already correct.
00197     return;
00198 
00199   // Store the shuffle.
00200   Stack.emplace_back(V, F, List.size());
00201   assert(List.size() == Stack.back().Shuffle.size() && "Wrong size");
00202   for (size_t I = 0, E = List.size(); I != E; ++I)
00203     Stack.back().Shuffle[I] = List[I].second;
00204 }
00205 
00206 static void predictValueUseListOrder(const Value *V, const Function *F,
00207                                      OrderMap &OM, UseListOrderStack &Stack) {
00208   auto &IDPair = OM[V];
00209   assert(IDPair.first && "Unmapped value");
00210   if (IDPair.second)
00211     // Already predicted.
00212     return;
00213 
00214   // Do the actual prediction.
00215   IDPair.second = true;
00216   if (!V->use_empty() && std::next(V->use_begin()) != V->use_end())
00217     predictValueUseListOrderImpl(V, F, IDPair.first, OM, Stack);
00218 
00219   // Recursive descent into constants.
00220   if (const Constant *C = dyn_cast<Constant>(V))
00221     if (C->getNumOperands()) // Visit GlobalValues.
00222       for (const Value *Op : C->operands())
00223         if (isa<Constant>(Op)) // Visit GlobalValues.
00224           predictValueUseListOrder(Op, F, OM, Stack);
00225 }
00226 
00227 static UseListOrderStack predictUseListOrder(const Module &M) {
00228   OrderMap OM = orderModule(M);
00229 
00230   // Use-list orders need to be serialized after all the users have been added
00231   // to a value, or else the shuffles will be incomplete.  Store them per
00232   // function in a stack.
00233   //
00234   // Aside from function order, the order of values doesn't matter much here.
00235   UseListOrderStack Stack;
00236 
00237   // We want to visit the functions backward now so we can list function-local
00238   // constants in the last Function they're used in.  Module-level constants
00239   // have already been visited above.
00240   for (auto I = M.rbegin(), E = M.rend(); I != E; ++I) {
00241     const Function &F = *I;
00242     if (F.isDeclaration())
00243       continue;
00244     for (const BasicBlock &BB : F)
00245       predictValueUseListOrder(&BB, &F, OM, Stack);
00246     for (const Argument &A : F.args())
00247       predictValueUseListOrder(&A, &F, OM, Stack);
00248     for (const BasicBlock &BB : F)
00249       for (const Instruction &I : BB)
00250         for (const Value *Op : I.operands())
00251           if (isa<Constant>(*Op) || isa<InlineAsm>(*Op)) // Visit GlobalValues.
00252             predictValueUseListOrder(Op, &F, OM, Stack);
00253     for (const BasicBlock &BB : F)
00254       for (const Instruction &I : BB)
00255         predictValueUseListOrder(&I, &F, OM, Stack);
00256   }
00257 
00258   // Visit globals last, since the module-level use-list block will be seen
00259   // before the function bodies are processed.
00260   for (const GlobalVariable &G : M.globals())
00261     predictValueUseListOrder(&G, nullptr, OM, Stack);
00262   for (const Function &F : M)
00263     predictValueUseListOrder(&F, nullptr, OM, Stack);
00264   for (const GlobalAlias &A : M.aliases())
00265     predictValueUseListOrder(&A, nullptr, OM, Stack);
00266   for (const GlobalVariable &G : M.globals())
00267     if (G.hasInitializer())
00268       predictValueUseListOrder(G.getInitializer(), nullptr, OM, Stack);
00269   for (const GlobalAlias &A : M.aliases())
00270     predictValueUseListOrder(A.getAliasee(), nullptr, OM, Stack);
00271   for (const Function &F : M) {
00272     if (F.hasPrefixData())
00273       predictValueUseListOrder(F.getPrefixData(), nullptr, OM, Stack);
00274     if (F.hasPrologueData())
00275       predictValueUseListOrder(F.getPrologueData(), nullptr, OM, Stack);
00276   }
00277 
00278   return Stack;
00279 }
00280 
00281 static bool isIntOrIntVectorValue(const std::pair<const Value*, unsigned> &V) {
00282   return V.first->getType()->isIntOrIntVectorTy();
00283 }
00284 
00285 ValueEnumerator::ValueEnumerator(const Module &M) {
00286   if (shouldPreserveBitcodeUseListOrder())
00287     UseListOrders = predictUseListOrder(M);
00288 
00289   // Enumerate the global variables.
00290   for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
00291        I != E; ++I)
00292     EnumerateValue(I);
00293 
00294   // Enumerate the functions.
00295   for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I) {
00296     EnumerateValue(I);
00297     EnumerateAttributes(cast<Function>(I)->getAttributes());
00298   }
00299 
00300   // Enumerate the aliases.
00301   for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
00302        I != E; ++I)
00303     EnumerateValue(I);
00304 
00305   // Remember what is the cutoff between globalvalue's and other constants.
00306   unsigned FirstConstant = Values.size();
00307 
00308   // Enumerate the global variable initializers.
00309   for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
00310        I != E; ++I)
00311     if (I->hasInitializer())
00312       EnumerateValue(I->getInitializer());
00313 
00314   // Enumerate the aliasees.
00315   for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
00316        I != E; ++I)
00317     EnumerateValue(I->getAliasee());
00318 
00319   // Enumerate the prefix data constants.
00320   for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
00321     if (I->hasPrefixData())
00322       EnumerateValue(I->getPrefixData());
00323 
00324   // Enumerate the prologue data constants.
00325   for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
00326     if (I->hasPrologueData())
00327       EnumerateValue(I->getPrologueData());
00328 
00329   // Enumerate the metadata type.
00330   //
00331   // TODO: Move this to ValueEnumerator::EnumerateOperandType() once bitcode
00332   // only encodes the metadata type when it's used as a value.
00333   EnumerateType(Type::getMetadataTy(M.getContext()));
00334 
00335   // Insert constants and metadata that are named at module level into the slot
00336   // pool so that the module symbol table can refer to them...
00337   EnumerateValueSymbolTable(M.getValueSymbolTable());
00338   EnumerateNamedMetadata(M);
00339 
00340   SmallVector<std::pair<unsigned, MDNode *>, 8> MDs;
00341 
00342   // Enumerate types used by function bodies and argument lists.
00343   for (const Function &F : M) {
00344     for (const Argument &A : F.args())
00345       EnumerateType(A.getType());
00346 
00347     for (const BasicBlock &BB : F)
00348       for (const Instruction &I : BB) {
00349         for (const Use &Op : I.operands()) {
00350           auto *MD = dyn_cast<MetadataAsValue>(&Op);
00351           if (!MD) {
00352             EnumerateOperandType(Op);
00353             continue;
00354           }
00355 
00356           // Local metadata is enumerated during function-incorporation.
00357           if (isa<LocalAsMetadata>(MD->getMetadata()))
00358             continue;
00359 
00360           EnumerateMetadata(MD->getMetadata());
00361         }
00362         EnumerateType(I.getType());
00363         if (const CallInst *CI = dyn_cast<CallInst>(&I))
00364           EnumerateAttributes(CI->getAttributes());
00365         else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I))
00366           EnumerateAttributes(II->getAttributes());
00367 
00368         // Enumerate metadata attached with this instruction.
00369         MDs.clear();
00370         I.getAllMetadataOtherThanDebugLoc(MDs);
00371         for (unsigned i = 0, e = MDs.size(); i != e; ++i)
00372           EnumerateMetadata(MDs[i].second);
00373 
00374         if (!I.getDebugLoc().isUnknown()) {
00375           MDNode *Scope, *IA;
00376           I.getDebugLoc().getScopeAndInlinedAt(Scope, IA, I.getContext());
00377           if (Scope) EnumerateMetadata(Scope);
00378           if (IA) EnumerateMetadata(IA);
00379         }
00380       }
00381   }
00382 
00383   // Optimize constant ordering.
00384   OptimizeConstants(FirstConstant, Values.size());
00385 }
00386 
00387 unsigned ValueEnumerator::getInstructionID(const Instruction *Inst) const {
00388   InstructionMapType::const_iterator I = InstructionMap.find(Inst);
00389   assert(I != InstructionMap.end() && "Instruction is not mapped!");
00390   return I->second;
00391 }
00392 
00393 unsigned ValueEnumerator::getComdatID(const Comdat *C) const {
00394   unsigned ComdatID = Comdats.idFor(C);
00395   assert(ComdatID && "Comdat not found!");
00396   return ComdatID;
00397 }
00398 
00399 void ValueEnumerator::setInstructionID(const Instruction *I) {
00400   InstructionMap[I] = InstructionCount++;
00401 }
00402 
00403 unsigned ValueEnumerator::getValueID(const Value *V) const {
00404   if (auto *MD = dyn_cast<MetadataAsValue>(V))
00405     return getMetadataID(MD->getMetadata());
00406 
00407   ValueMapType::const_iterator I = ValueMap.find(V);
00408   assert(I != ValueMap.end() && "Value not in slotcalculator!");
00409   return I->second-1;
00410 }
00411 
00412 unsigned ValueEnumerator::getMetadataID(const Metadata *MD) const {
00413   auto I = MDValueMap.find(MD);
00414   assert(I != MDValueMap.end() && "Metadata not in slotcalculator!");
00415   return I->second - 1;
00416 }
00417 
00418 void ValueEnumerator::dump() const {
00419   print(dbgs(), ValueMap, "Default");
00420   dbgs() << '\n';
00421   print(dbgs(), MDValueMap, "MetaData");
00422   dbgs() << '\n';
00423 }
00424 
00425 void ValueEnumerator::print(raw_ostream &OS, const ValueMapType &Map,
00426                             const char *Name) const {
00427 
00428   OS << "Map Name: " << Name << "\n";
00429   OS << "Size: " << Map.size() << "\n";
00430   for (ValueMapType::const_iterator I = Map.begin(),
00431          E = Map.end(); I != E; ++I) {
00432 
00433     const Value *V = I->first;
00434     if (V->hasName())
00435       OS << "Value: " << V->getName();
00436     else
00437       OS << "Value: [null]\n";
00438     V->dump();
00439 
00440     OS << " Uses(" << std::distance(V->use_begin(),V->use_end()) << "):";
00441     for (const Use &U : V->uses()) {
00442       if (&U != &*V->use_begin())
00443         OS << ",";
00444       if(U->hasName())
00445         OS << " " << U->getName();
00446       else
00447         OS << " [null]";
00448 
00449     }
00450     OS <<  "\n\n";
00451   }
00452 }
00453 
00454 void ValueEnumerator::print(raw_ostream &OS, const MetadataMapType &Map,
00455                             const char *Name) const {
00456 
00457   OS << "Map Name: " << Name << "\n";
00458   OS << "Size: " << Map.size() << "\n";
00459   for (auto I = Map.begin(), E = Map.end(); I != E; ++I) {
00460     const Metadata *MD = I->first;
00461     OS << "Metadata: slot = " << I->second << "\n";
00462     MD->print(OS);
00463   }
00464 }
00465 
00466 /// OptimizeConstants - Reorder constant pool for denser encoding.
00467 void ValueEnumerator::OptimizeConstants(unsigned CstStart, unsigned CstEnd) {
00468   if (CstStart == CstEnd || CstStart+1 == CstEnd) return;
00469 
00470   if (shouldPreserveBitcodeUseListOrder())
00471     // Optimizing constants makes the use-list order difficult to predict.
00472     // Disable it for now when trying to preserve the order.
00473     return;
00474 
00475   std::stable_sort(Values.begin() + CstStart, Values.begin() + CstEnd,
00476                    [this](const std::pair<const Value *, unsigned> &LHS,
00477                           const std::pair<const Value *, unsigned> &RHS) {
00478     // Sort by plane.
00479     if (LHS.first->getType() != RHS.first->getType())
00480       return getTypeID(LHS.first->getType()) < getTypeID(RHS.first->getType());
00481     // Then by frequency.
00482     return LHS.second > RHS.second;
00483   });
00484 
00485   // Ensure that integer and vector of integer constants are at the start of the
00486   // constant pool.  This is important so that GEP structure indices come before
00487   // gep constant exprs.
00488   std::partition(Values.begin()+CstStart, Values.begin()+CstEnd,
00489                  isIntOrIntVectorValue);
00490 
00491   // Rebuild the modified portion of ValueMap.
00492   for (; CstStart != CstEnd; ++CstStart)
00493     ValueMap[Values[CstStart].first] = CstStart+1;
00494 }
00495 
00496 
00497 /// EnumerateValueSymbolTable - Insert all of the values in the specified symbol
00498 /// table into the values table.
00499 void ValueEnumerator::EnumerateValueSymbolTable(const ValueSymbolTable &VST) {
00500   for (ValueSymbolTable::const_iterator VI = VST.begin(), VE = VST.end();
00501        VI != VE; ++VI)
00502     EnumerateValue(VI->getValue());
00503 }
00504 
00505 /// Insert all of the values referenced by named metadata in the specified
00506 /// module.
00507 void ValueEnumerator::EnumerateNamedMetadata(const Module &M) {
00508   for (Module::const_named_metadata_iterator I = M.named_metadata_begin(),
00509                                              E = M.named_metadata_end();
00510        I != E; ++I)
00511     EnumerateNamedMDNode(I);
00512 }
00513 
00514 void ValueEnumerator::EnumerateNamedMDNode(const NamedMDNode *MD) {
00515   for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i)
00516     EnumerateMetadata(MD->getOperand(i));
00517 }
00518 
00519 /// EnumerateMDNodeOperands - Enumerate all non-function-local values
00520 /// and types referenced by the given MDNode.
00521 void ValueEnumerator::EnumerateMDNodeOperands(const MDNode *N) {
00522   for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
00523     Metadata *MD = N->getOperand(i);
00524     if (!MD)
00525       continue;
00526     assert(!isa<LocalAsMetadata>(MD) && "MDNodes cannot be function-local");
00527     EnumerateMetadata(MD);
00528   }
00529 }
00530 
00531 void ValueEnumerator::EnumerateMetadata(const Metadata *MD) {
00532   assert(
00533       (isa<MDNode>(MD) || isa<MDString>(MD) || isa<ConstantAsMetadata>(MD)) &&
00534       "Invalid metadata kind");
00535 
00536   // Insert a dummy ID to block the co-recursive call to
00537   // EnumerateMDNodeOperands() from re-visiting MD in a cyclic graph.
00538   //
00539   // Return early if there's already an ID.
00540   if (!MDValueMap.insert(std::make_pair(MD, 0)).second)
00541     return;
00542 
00543   // Visit operands first to minimize RAUW.
00544   if (auto *N = dyn_cast<MDNode>(MD))
00545     EnumerateMDNodeOperands(N);
00546   else if (auto *C = dyn_cast<ConstantAsMetadata>(MD))
00547     EnumerateValue(C->getValue());
00548 
00549   // Replace the dummy ID inserted above with the correct one.  MDValueMap may
00550   // have changed by inserting operands, so we need a fresh lookup here.
00551   MDs.push_back(MD);
00552   MDValueMap[MD] = MDs.size();
00553 }
00554 
00555 /// EnumerateFunctionLocalMetadataa - Incorporate function-local metadata
00556 /// information reachable from the metadata.
00557 void ValueEnumerator::EnumerateFunctionLocalMetadata(
00558     const LocalAsMetadata *Local) {
00559   // Check to see if it's already in!
00560   unsigned &MDValueID = MDValueMap[Local];
00561   if (MDValueID)
00562     return;
00563 
00564   MDs.push_back(Local);
00565   MDValueID = MDs.size();
00566 
00567   EnumerateValue(Local->getValue());
00568 
00569   // Also, collect all function-local metadata for easy access.
00570   FunctionLocalMDs.push_back(Local);
00571 }
00572 
00573 void ValueEnumerator::EnumerateValue(const Value *V) {
00574   assert(!V->getType()->isVoidTy() && "Can't insert void values!");
00575   assert(!isa<MetadataAsValue>(V) && "EnumerateValue doesn't handle Metadata!");
00576 
00577   // Check to see if it's already in!
00578   unsigned &ValueID = ValueMap[V];
00579   if (ValueID) {
00580     // Increment use count.
00581     Values[ValueID-1].second++;
00582     return;
00583   }
00584 
00585   if (auto *GO = dyn_cast<GlobalObject>(V))
00586     if (const Comdat *C = GO->getComdat())
00587       Comdats.insert(C);
00588 
00589   // Enumerate the type of this value.
00590   EnumerateType(V->getType());
00591 
00592   if (const Constant *C = dyn_cast<Constant>(V)) {
00593     if (isa<GlobalValue>(C)) {
00594       // Initializers for globals are handled explicitly elsewhere.
00595     } else if (C->getNumOperands()) {
00596       // If a constant has operands, enumerate them.  This makes sure that if a
00597       // constant has uses (for example an array of const ints), that they are
00598       // inserted also.
00599 
00600       // We prefer to enumerate them with values before we enumerate the user
00601       // itself.  This makes it more likely that we can avoid forward references
00602       // in the reader.  We know that there can be no cycles in the constants
00603       // graph that don't go through a global variable.
00604       for (User::const_op_iterator I = C->op_begin(), E = C->op_end();
00605            I != E; ++I)
00606         if (!isa<BasicBlock>(*I)) // Don't enumerate BB operand to BlockAddress.
00607           EnumerateValue(*I);
00608 
00609       // Finally, add the value.  Doing this could make the ValueID reference be
00610       // dangling, don't reuse it.
00611       Values.push_back(std::make_pair(V, 1U));
00612       ValueMap[V] = Values.size();
00613       return;
00614     }
00615   }
00616 
00617   // Add the value.
00618   Values.push_back(std::make_pair(V, 1U));
00619   ValueID = Values.size();
00620 }
00621 
00622 
00623 void ValueEnumerator::EnumerateType(Type *Ty) {
00624   unsigned *TypeID = &TypeMap[Ty];
00625 
00626   // We've already seen this type.
00627   if (*TypeID)
00628     return;
00629 
00630   // If it is a non-anonymous struct, mark the type as being visited so that we
00631   // don't recursively visit it.  This is safe because we allow forward
00632   // references of these in the bitcode reader.
00633   if (StructType *STy = dyn_cast<StructType>(Ty))
00634     if (!STy->isLiteral())
00635       *TypeID = ~0U;
00636 
00637   // Enumerate all of the subtypes before we enumerate this type.  This ensures
00638   // that the type will be enumerated in an order that can be directly built.
00639   for (Type *SubTy : Ty->subtypes())
00640     EnumerateType(SubTy);
00641 
00642   // Refresh the TypeID pointer in case the table rehashed.
00643   TypeID = &TypeMap[Ty];
00644 
00645   // Check to see if we got the pointer another way.  This can happen when
00646   // enumerating recursive types that hit the base case deeper than they start.
00647   //
00648   // If this is actually a struct that we are treating as forward ref'able,
00649   // then emit the definition now that all of its contents are available.
00650   if (*TypeID && *TypeID != ~0U)
00651     return;
00652 
00653   // Add this type now that its contents are all happily enumerated.
00654   Types.push_back(Ty);
00655 
00656   *TypeID = Types.size();
00657 }
00658 
00659 // Enumerate the types for the specified value.  If the value is a constant,
00660 // walk through it, enumerating the types of the constant.
00661 void ValueEnumerator::EnumerateOperandType(const Value *V) {
00662   EnumerateType(V->getType());
00663 
00664   if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
00665     assert(!isa<LocalAsMetadata>(MD->getMetadata()) &&
00666            "Function-local metadata should be left for later");
00667 
00668     EnumerateMetadata(MD->getMetadata());
00669     return;
00670   }
00671 
00672   const Constant *C = dyn_cast<Constant>(V);
00673   if (!C)
00674     return;
00675 
00676   // If this constant is already enumerated, ignore it, we know its type must
00677   // be enumerated.
00678   if (ValueMap.count(C))
00679     return;
00680 
00681   // This constant may have operands, make sure to enumerate the types in
00682   // them.
00683   for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) {
00684     const Value *Op = C->getOperand(i);
00685 
00686     // Don't enumerate basic blocks here, this happens as operands to
00687     // blockaddress.
00688     if (isa<BasicBlock>(Op))
00689       continue;
00690 
00691     EnumerateOperandType(Op);
00692   }
00693 }
00694 
00695 void ValueEnumerator::EnumerateAttributes(AttributeSet PAL) {
00696   if (PAL.isEmpty()) return;  // null is always 0.
00697 
00698   // Do a lookup.
00699   unsigned &Entry = AttributeMap[PAL];
00700   if (Entry == 0) {
00701     // Never saw this before, add it.
00702     Attribute.push_back(PAL);
00703     Entry = Attribute.size();
00704   }
00705 
00706   // Do lookups for all attribute groups.
00707   for (unsigned i = 0, e = PAL.getNumSlots(); i != e; ++i) {
00708     AttributeSet AS = PAL.getSlotAttributes(i);
00709     unsigned &Entry = AttributeGroupMap[AS];
00710     if (Entry == 0) {
00711       AttributeGroups.push_back(AS);
00712       Entry = AttributeGroups.size();
00713     }
00714   }
00715 }
00716 
00717 void ValueEnumerator::incorporateFunction(const Function &F) {
00718   InstructionCount = 0;
00719   NumModuleValues = Values.size();
00720   NumModuleMDs = MDs.size();
00721 
00722   // Adding function arguments to the value table.
00723   for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
00724        I != E; ++I)
00725     EnumerateValue(I);
00726 
00727   FirstFuncConstantID = Values.size();
00728 
00729   // Add all function-level constants to the value table.
00730   for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
00731     for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I)
00732       for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
00733            OI != E; ++OI) {
00734         if ((isa<Constant>(*OI) && !isa<GlobalValue>(*OI)) ||
00735             isa<InlineAsm>(*OI))
00736           EnumerateValue(*OI);
00737       }
00738     BasicBlocks.push_back(BB);
00739     ValueMap[BB] = BasicBlocks.size();
00740   }
00741 
00742   // Optimize the constant layout.
00743   OptimizeConstants(FirstFuncConstantID, Values.size());
00744 
00745   // Add the function's parameter attributes so they are available for use in
00746   // the function's instruction.
00747   EnumerateAttributes(F.getAttributes());
00748 
00749   FirstInstID = Values.size();
00750 
00751   SmallVector<LocalAsMetadata *, 8> FnLocalMDVector;
00752   // Add all of the instructions.
00753   for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
00754     for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
00755       for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
00756            OI != E; ++OI) {
00757         if (auto *MD = dyn_cast<MetadataAsValue>(&*OI))
00758           if (auto *Local = dyn_cast<LocalAsMetadata>(MD->getMetadata()))
00759             // Enumerate metadata after the instructions they might refer to.
00760             FnLocalMDVector.push_back(Local);
00761       }
00762 
00763       if (!I->getType()->isVoidTy())
00764         EnumerateValue(I);
00765     }
00766   }
00767 
00768   // Add all of the function-local metadata.
00769   for (unsigned i = 0, e = FnLocalMDVector.size(); i != e; ++i)
00770     EnumerateFunctionLocalMetadata(FnLocalMDVector[i]);
00771 }
00772 
00773 void ValueEnumerator::purgeFunction() {
00774   /// Remove purged values from the ValueMap.
00775   for (unsigned i = NumModuleValues, e = Values.size(); i != e; ++i)
00776     ValueMap.erase(Values[i].first);
00777   for (unsigned i = NumModuleMDs, e = MDs.size(); i != e; ++i)
00778     MDValueMap.erase(MDs[i]);
00779   for (unsigned i = 0, e = BasicBlocks.size(); i != e; ++i)
00780     ValueMap.erase(BasicBlocks[i]);
00781 
00782   Values.resize(NumModuleValues);
00783   MDs.resize(NumModuleMDs);
00784   BasicBlocks.clear();
00785   FunctionLocalMDs.clear();
00786 }
00787 
00788 static void IncorporateFunctionInfoGlobalBBIDs(const Function *F,
00789                                  DenseMap<const BasicBlock*, unsigned> &IDMap) {
00790   unsigned Counter = 0;
00791   for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
00792     IDMap[BB] = ++Counter;
00793 }
00794 
00795 /// getGlobalBasicBlockID - This returns the function-specific ID for the
00796 /// specified basic block.  This is relatively expensive information, so it
00797 /// should only be used by rare constructs such as address-of-label.
00798 unsigned ValueEnumerator::getGlobalBasicBlockID(const BasicBlock *BB) const {
00799   unsigned &Idx = GlobalBasicBlockIDs[BB];
00800   if (Idx != 0)
00801     return Idx-1;
00802 
00803   IncorporateFunctionInfoGlobalBBIDs(BB->getParent(), GlobalBasicBlockIDs);
00804   return getGlobalBasicBlockID(BB);
00805 }