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