Path: blob/main/contrib/llvm-project/llvm/lib/Bitcode/Writer/ValueEnumerator.cpp
35291 views
//===- ValueEnumerator.cpp - Number values and types for bitcode writer ---===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file implements the ValueEnumerator class.9//10//===----------------------------------------------------------------------===//1112#include "ValueEnumerator.h"13#include "llvm/ADT/SmallVector.h"14#include "llvm/Config/llvm-config.h"15#include "llvm/IR/Argument.h"16#include "llvm/IR/BasicBlock.h"17#include "llvm/IR/Constant.h"18#include "llvm/IR/DebugInfoMetadata.h"19#include "llvm/IR/DerivedTypes.h"20#include "llvm/IR/Function.h"21#include "llvm/IR/GlobalAlias.h"22#include "llvm/IR/GlobalIFunc.h"23#include "llvm/IR/GlobalObject.h"24#include "llvm/IR/GlobalValue.h"25#include "llvm/IR/GlobalVariable.h"26#include "llvm/IR/Instruction.h"27#include "llvm/IR/Instructions.h"28#include "llvm/IR/Metadata.h"29#include "llvm/IR/Module.h"30#include "llvm/IR/Operator.h"31#include "llvm/IR/Type.h"32#include "llvm/IR/Use.h"33#include "llvm/IR/User.h"34#include "llvm/IR/Value.h"35#include "llvm/IR/ValueSymbolTable.h"36#include "llvm/Support/Casting.h"37#include "llvm/Support/Compiler.h"38#include "llvm/Support/Debug.h"39#include "llvm/Support/MathExtras.h"40#include "llvm/Support/raw_ostream.h"41#include <algorithm>42#include <cstddef>43#include <iterator>44#include <tuple>4546using namespace llvm;4748namespace {4950struct OrderMap {51DenseMap<const Value *, std::pair<unsigned, bool>> IDs;52unsigned LastGlobalValueID = 0;5354OrderMap() = default;5556bool isGlobalValue(unsigned ID) const {57return ID <= LastGlobalValueID;58}5960unsigned size() const { return IDs.size(); }61std::pair<unsigned, bool> &operator[](const Value *V) { return IDs[V]; }6263std::pair<unsigned, bool> lookup(const Value *V) const {64return IDs.lookup(V);65}6667void index(const Value *V) {68// Explicitly sequence get-size and insert-value operations to avoid UB.69unsigned ID = IDs.size() + 1;70IDs[V].first = ID;71}72};7374} // end anonymous namespace7576static void orderValue(const Value *V, OrderMap &OM) {77if (OM.lookup(V).first)78return;7980if (const Constant *C = dyn_cast<Constant>(V)) {81if (C->getNumOperands()) {82for (const Value *Op : C->operands())83if (!isa<BasicBlock>(Op) && !isa<GlobalValue>(Op))84orderValue(Op, OM);85if (auto *CE = dyn_cast<ConstantExpr>(C))86if (CE->getOpcode() == Instruction::ShuffleVector)87orderValue(CE->getShuffleMaskForBitcode(), OM);88}89}9091// Note: we cannot cache this lookup above, since inserting into the map92// changes the map's size, and thus affects the other IDs.93OM.index(V);94}9596static OrderMap orderModule(const Module &M) {97// This needs to match the order used by ValueEnumerator::ValueEnumerator()98// and ValueEnumerator::incorporateFunction().99OrderMap OM;100101// Initializers of GlobalValues are processed in102// BitcodeReader::ResolveGlobalAndAliasInits(). Match the order there rather103// than ValueEnumerator, and match the code in predictValueUseListOrderImpl()104// by giving IDs in reverse order.105//106// Since GlobalValues never reference each other directly (just through107// initializers), their relative IDs only matter for determining order of108// uses in their initializers.109for (const GlobalVariable &G : reverse(M.globals()))110orderValue(&G, OM);111for (const GlobalAlias &A : reverse(M.aliases()))112orderValue(&A, OM);113for (const GlobalIFunc &I : reverse(M.ifuncs()))114orderValue(&I, OM);115for (const Function &F : reverse(M))116orderValue(&F, OM);117OM.LastGlobalValueID = OM.size();118119auto orderConstantValue = [&OM](const Value *V) {120if (isa<Constant>(V) || isa<InlineAsm>(V))121orderValue(V, OM);122};123124for (const Function &F : M) {125if (F.isDeclaration())126continue;127// Here we need to match the union of ValueEnumerator::incorporateFunction()128// and WriteFunction(). Basic blocks are implicitly declared before129// anything else (by declaring their size).130for (const BasicBlock &BB : F)131orderValue(&BB, OM);132133// Metadata used by instructions is decoded before the actual instructions,134// so visit any constants used by it beforehand.135for (const BasicBlock &BB : F)136for (const Instruction &I : BB) {137auto OrderConstantFromMetadata = [&](Metadata *MD) {138if (const auto *VAM = dyn_cast<ValueAsMetadata>(MD)) {139orderConstantValue(VAM->getValue());140} else if (const auto *AL = dyn_cast<DIArgList>(MD)) {141for (const auto *VAM : AL->getArgs())142orderConstantValue(VAM->getValue());143}144};145146for (DbgVariableRecord &DVR : filterDbgVars(I.getDbgRecordRange())) {147OrderConstantFromMetadata(DVR.getRawLocation());148if (DVR.isDbgAssign())149OrderConstantFromMetadata(DVR.getRawAddress());150}151152for (const Value *V : I.operands()) {153if (const auto *MAV = dyn_cast<MetadataAsValue>(V))154OrderConstantFromMetadata(MAV->getMetadata());155}156}157158for (const Argument &A : F.args())159orderValue(&A, OM);160for (const BasicBlock &BB : F)161for (const Instruction &I : BB) {162for (const Value *Op : I.operands())163orderConstantValue(Op);164if (auto *SVI = dyn_cast<ShuffleVectorInst>(&I))165orderValue(SVI->getShuffleMaskForBitcode(), OM);166orderValue(&I, OM);167}168}169return OM;170}171172static void predictValueUseListOrderImpl(const Value *V, const Function *F,173unsigned ID, const OrderMap &OM,174UseListOrderStack &Stack) {175// Predict use-list order for this one.176using Entry = std::pair<const Use *, unsigned>;177SmallVector<Entry, 64> List;178for (const Use &U : V->uses())179// Check if this user will be serialized.180if (OM.lookup(U.getUser()).first)181List.push_back(std::make_pair(&U, List.size()));182183if (List.size() < 2)184// We may have lost some users.185return;186187bool IsGlobalValue = OM.isGlobalValue(ID);188llvm::sort(List, [&](const Entry &L, const Entry &R) {189const Use *LU = L.first;190const Use *RU = R.first;191if (LU == RU)192return false;193194auto LID = OM.lookup(LU->getUser()).first;195auto RID = OM.lookup(RU->getUser()).first;196197// If ID is 4, then expect: 7 6 5 1 2 3.198if (LID < RID) {199if (RID <= ID)200if (!IsGlobalValue) // GlobalValue uses don't get reversed.201return true;202return false;203}204if (RID < LID) {205if (LID <= ID)206if (!IsGlobalValue) // GlobalValue uses don't get reversed.207return false;208return true;209}210211// LID and RID are equal, so we have different operands of the same user.212// Assume operands are added in order for all instructions.213if (LID <= ID)214if (!IsGlobalValue) // GlobalValue uses don't get reversed.215return LU->getOperandNo() < RU->getOperandNo();216return LU->getOperandNo() > RU->getOperandNo();217});218219if (llvm::is_sorted(List, llvm::less_second()))220// Order is already correct.221return;222223// Store the shuffle.224Stack.emplace_back(V, F, List.size());225assert(List.size() == Stack.back().Shuffle.size() && "Wrong size");226for (size_t I = 0, E = List.size(); I != E; ++I)227Stack.back().Shuffle[I] = List[I].second;228}229230static void predictValueUseListOrder(const Value *V, const Function *F,231OrderMap &OM, UseListOrderStack &Stack) {232auto &IDPair = OM[V];233assert(IDPair.first && "Unmapped value");234if (IDPair.second)235// Already predicted.236return;237238// Do the actual prediction.239IDPair.second = true;240if (!V->use_empty() && std::next(V->use_begin()) != V->use_end())241predictValueUseListOrderImpl(V, F, IDPair.first, OM, Stack);242243// Recursive descent into constants.244if (const Constant *C = dyn_cast<Constant>(V)) {245if (C->getNumOperands()) { // Visit GlobalValues.246for (const Value *Op : C->operands())247if (isa<Constant>(Op)) // Visit GlobalValues.248predictValueUseListOrder(Op, F, OM, Stack);249if (auto *CE = dyn_cast<ConstantExpr>(C))250if (CE->getOpcode() == Instruction::ShuffleVector)251predictValueUseListOrder(CE->getShuffleMaskForBitcode(), F, OM,252Stack);253}254}255}256257static UseListOrderStack predictUseListOrder(const Module &M) {258OrderMap OM = orderModule(M);259260// Use-list orders need to be serialized after all the users have been added261// to a value, or else the shuffles will be incomplete. Store them per262// function in a stack.263//264// Aside from function order, the order of values doesn't matter much here.265UseListOrderStack Stack;266267// We want to visit the functions backward now so we can list function-local268// constants in the last Function they're used in. Module-level constants269// have already been visited above.270for (const Function &F : llvm::reverse(M)) {271auto PredictValueOrderFromMetadata = [&](Metadata *MD) {272if (const auto *VAM = dyn_cast<ValueAsMetadata>(MD)) {273predictValueUseListOrder(VAM->getValue(), &F, OM, Stack);274} else if (const auto *AL = dyn_cast<DIArgList>(MD)) {275for (const auto *VAM : AL->getArgs())276predictValueUseListOrder(VAM->getValue(), &F, OM, Stack);277}278};279if (F.isDeclaration())280continue;281for (const BasicBlock &BB : F)282predictValueUseListOrder(&BB, &F, OM, Stack);283for (const Argument &A : F.args())284predictValueUseListOrder(&A, &F, OM, Stack);285for (const BasicBlock &BB : F) {286for (const Instruction &I : BB) {287for (DbgVariableRecord &DVR : filterDbgVars(I.getDbgRecordRange())) {288PredictValueOrderFromMetadata(DVR.getRawLocation());289if (DVR.isDbgAssign())290PredictValueOrderFromMetadata(DVR.getRawAddress());291}292for (const Value *Op : I.operands()) {293if (isa<Constant>(*Op) || isa<InlineAsm>(*Op)) // Visit GlobalValues.294predictValueUseListOrder(Op, &F, OM, Stack);295if (const auto *MAV = dyn_cast<MetadataAsValue>(Op))296PredictValueOrderFromMetadata(MAV->getMetadata());297}298if (auto *SVI = dyn_cast<ShuffleVectorInst>(&I))299predictValueUseListOrder(SVI->getShuffleMaskForBitcode(), &F, OM,300Stack);301predictValueUseListOrder(&I, &F, OM, Stack);302}303}304}305306// Visit globals last, since the module-level use-list block will be seen307// before the function bodies are processed.308for (const GlobalVariable &G : M.globals())309predictValueUseListOrder(&G, nullptr, OM, Stack);310for (const Function &F : M)311predictValueUseListOrder(&F, nullptr, OM, Stack);312for (const GlobalAlias &A : M.aliases())313predictValueUseListOrder(&A, nullptr, OM, Stack);314for (const GlobalIFunc &I : M.ifuncs())315predictValueUseListOrder(&I, nullptr, OM, Stack);316for (const GlobalVariable &G : M.globals())317if (G.hasInitializer())318predictValueUseListOrder(G.getInitializer(), nullptr, OM, Stack);319for (const GlobalAlias &A : M.aliases())320predictValueUseListOrder(A.getAliasee(), nullptr, OM, Stack);321for (const GlobalIFunc &I : M.ifuncs())322predictValueUseListOrder(I.getResolver(), nullptr, OM, Stack);323for (const Function &F : M) {324for (const Use &U : F.operands())325predictValueUseListOrder(U.get(), nullptr, OM, Stack);326}327328return Stack;329}330331static bool isIntOrIntVectorValue(const std::pair<const Value*, unsigned> &V) {332return V.first->getType()->isIntOrIntVectorTy();333}334335ValueEnumerator::ValueEnumerator(const Module &M,336bool ShouldPreserveUseListOrder)337: ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) {338if (ShouldPreserveUseListOrder)339UseListOrders = predictUseListOrder(M);340341// Enumerate the global variables.342for (const GlobalVariable &GV : M.globals()) {343EnumerateValue(&GV);344EnumerateType(GV.getValueType());345}346347// Enumerate the functions.348for (const Function & F : M) {349EnumerateValue(&F);350EnumerateType(F.getValueType());351EnumerateAttributes(F.getAttributes());352}353354// Enumerate the aliases.355for (const GlobalAlias &GA : M.aliases()) {356EnumerateValue(&GA);357EnumerateType(GA.getValueType());358}359360// Enumerate the ifuncs.361for (const GlobalIFunc &GIF : M.ifuncs()) {362EnumerateValue(&GIF);363EnumerateType(GIF.getValueType());364}365366// Remember what is the cutoff between globalvalue's and other constants.367unsigned FirstConstant = Values.size();368369// Enumerate the global variable initializers and attributes.370for (const GlobalVariable &GV : M.globals()) {371if (GV.hasInitializer())372EnumerateValue(GV.getInitializer());373if (GV.hasAttributes())374EnumerateAttributes(GV.getAttributesAsList(AttributeList::FunctionIndex));375}376377// Enumerate the aliasees.378for (const GlobalAlias &GA : M.aliases())379EnumerateValue(GA.getAliasee());380381// Enumerate the ifunc resolvers.382for (const GlobalIFunc &GIF : M.ifuncs())383EnumerateValue(GIF.getResolver());384385// Enumerate any optional Function data.386for (const Function &F : M)387for (const Use &U : F.operands())388EnumerateValue(U.get());389390// Enumerate the metadata type.391//392// TODO: Move this to ValueEnumerator::EnumerateOperandType() once bitcode393// only encodes the metadata type when it's used as a value.394EnumerateType(Type::getMetadataTy(M.getContext()));395396// Insert constants and metadata that are named at module level into the slot397// pool so that the module symbol table can refer to them...398EnumerateValueSymbolTable(M.getValueSymbolTable());399EnumerateNamedMetadata(M);400401SmallVector<std::pair<unsigned, MDNode *>, 8> MDs;402for (const GlobalVariable &GV : M.globals()) {403MDs.clear();404GV.getAllMetadata(MDs);405for (const auto &I : MDs)406// FIXME: Pass GV to EnumerateMetadata and arrange for the bitcode writer407// to write metadata to the global variable's own metadata block408// (PR28134).409EnumerateMetadata(nullptr, I.second);410}411412// Enumerate types used by function bodies and argument lists.413for (const Function &F : M) {414for (const Argument &A : F.args())415EnumerateType(A.getType());416417// Enumerate metadata attached to this function.418MDs.clear();419F.getAllMetadata(MDs);420for (const auto &I : MDs)421EnumerateMetadata(F.isDeclaration() ? nullptr : &F, I.second);422423for (const BasicBlock &BB : F)424for (const Instruction &I : BB) {425// Local metadata is enumerated during function-incorporation, but426// any ConstantAsMetadata arguments in a DIArgList should be examined427// now.428auto EnumerateNonLocalValuesFromMetadata = [&](Metadata *MD) {429assert(MD && "Metadata unexpectedly null");430if (const auto *AL = dyn_cast<DIArgList>(MD)) {431for (const auto *VAM : AL->getArgs()) {432if (isa<ConstantAsMetadata>(VAM))433EnumerateMetadata(&F, VAM);434}435return;436}437438if (!isa<LocalAsMetadata>(MD))439EnumerateMetadata(&F, MD);440};441442for (DbgRecord &DR : I.getDbgRecordRange()) {443if (DbgLabelRecord *DLR = dyn_cast<DbgLabelRecord>(&DR)) {444EnumerateMetadata(&F, DLR->getLabel());445EnumerateMetadata(&F, &*DLR->getDebugLoc());446continue;447}448// Enumerate non-local location metadata.449DbgVariableRecord &DVR = cast<DbgVariableRecord>(DR);450EnumerateNonLocalValuesFromMetadata(DVR.getRawLocation());451EnumerateMetadata(&F, DVR.getExpression());452EnumerateMetadata(&F, DVR.getVariable());453EnumerateMetadata(&F, &*DVR.getDebugLoc());454if (DVR.isDbgAssign()) {455EnumerateNonLocalValuesFromMetadata(DVR.getRawAddress());456EnumerateMetadata(&F, DVR.getAssignID());457EnumerateMetadata(&F, DVR.getAddressExpression());458}459}460for (const Use &Op : I.operands()) {461auto *MD = dyn_cast<MetadataAsValue>(&Op);462if (!MD) {463EnumerateOperandType(Op);464continue;465}466467EnumerateNonLocalValuesFromMetadata(MD->getMetadata());468}469if (auto *SVI = dyn_cast<ShuffleVectorInst>(&I))470EnumerateType(SVI->getShuffleMaskForBitcode()->getType());471if (auto *GEP = dyn_cast<GetElementPtrInst>(&I))472EnumerateType(GEP->getSourceElementType());473if (auto *AI = dyn_cast<AllocaInst>(&I))474EnumerateType(AI->getAllocatedType());475EnumerateType(I.getType());476if (const auto *Call = dyn_cast<CallBase>(&I)) {477EnumerateAttributes(Call->getAttributes());478EnumerateType(Call->getFunctionType());479}480481// Enumerate metadata attached with this instruction.482MDs.clear();483I.getAllMetadataOtherThanDebugLoc(MDs);484for (unsigned i = 0, e = MDs.size(); i != e; ++i)485EnumerateMetadata(&F, MDs[i].second);486487// Don't enumerate the location directly -- it has a special record488// type -- but enumerate its operands.489if (DILocation *L = I.getDebugLoc())490for (const Metadata *Op : L->operands())491EnumerateMetadata(&F, Op);492}493}494495// Optimize constant ordering.496OptimizeConstants(FirstConstant, Values.size());497498// Organize metadata ordering.499organizeMetadata();500}501502unsigned ValueEnumerator::getInstructionID(const Instruction *Inst) const {503InstructionMapType::const_iterator I = InstructionMap.find(Inst);504assert(I != InstructionMap.end() && "Instruction is not mapped!");505return I->second;506}507508unsigned ValueEnumerator::getComdatID(const Comdat *C) const {509unsigned ComdatID = Comdats.idFor(C);510assert(ComdatID && "Comdat not found!");511return ComdatID;512}513514void ValueEnumerator::setInstructionID(const Instruction *I) {515InstructionMap[I] = InstructionCount++;516}517518unsigned ValueEnumerator::getValueID(const Value *V) const {519if (auto *MD = dyn_cast<MetadataAsValue>(V))520return getMetadataID(MD->getMetadata());521522ValueMapType::const_iterator I = ValueMap.find(V);523assert(I != ValueMap.end() && "Value not in slotcalculator!");524return I->second-1;525}526527#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)528LLVM_DUMP_METHOD void ValueEnumerator::dump() const {529print(dbgs(), ValueMap, "Default");530dbgs() << '\n';531print(dbgs(), MetadataMap, "MetaData");532dbgs() << '\n';533}534#endif535536void ValueEnumerator::print(raw_ostream &OS, const ValueMapType &Map,537const char *Name) const {538OS << "Map Name: " << Name << "\n";539OS << "Size: " << Map.size() << "\n";540for (const auto &I : Map) {541const Value *V = I.first;542if (V->hasName())543OS << "Value: " << V->getName();544else545OS << "Value: [null]\n";546V->print(errs());547errs() << '\n';548549OS << " Uses(" << V->getNumUses() << "):";550for (const Use &U : V->uses()) {551if (&U != &*V->use_begin())552OS << ",";553if(U->hasName())554OS << " " << U->getName();555else556OS << " [null]";557558}559OS << "\n\n";560}561}562563void ValueEnumerator::print(raw_ostream &OS, const MetadataMapType &Map,564const char *Name) const {565OS << "Map Name: " << Name << "\n";566OS << "Size: " << Map.size() << "\n";567for (const auto &I : Map) {568const Metadata *MD = I.first;569OS << "Metadata: slot = " << I.second.ID << "\n";570OS << "Metadata: function = " << I.second.F << "\n";571MD->print(OS);572OS << "\n";573}574}575576/// OptimizeConstants - Reorder constant pool for denser encoding.577void ValueEnumerator::OptimizeConstants(unsigned CstStart, unsigned CstEnd) {578if (CstStart == CstEnd || CstStart+1 == CstEnd) return;579580if (ShouldPreserveUseListOrder)581// Optimizing constants makes the use-list order difficult to predict.582// Disable it for now when trying to preserve the order.583return;584585std::stable_sort(Values.begin() + CstStart, Values.begin() + CstEnd,586[this](const std::pair<const Value *, unsigned> &LHS,587const std::pair<const Value *, unsigned> &RHS) {588// Sort by plane.589if (LHS.first->getType() != RHS.first->getType())590return getTypeID(LHS.first->getType()) < getTypeID(RHS.first->getType());591// Then by frequency.592return LHS.second > RHS.second;593});594595// Ensure that integer and vector of integer constants are at the start of the596// constant pool. This is important so that GEP structure indices come before597// gep constant exprs.598std::stable_partition(Values.begin() + CstStart, Values.begin() + CstEnd,599isIntOrIntVectorValue);600601// Rebuild the modified portion of ValueMap.602for (; CstStart != CstEnd; ++CstStart)603ValueMap[Values[CstStart].first] = CstStart+1;604}605606/// EnumerateValueSymbolTable - Insert all of the values in the specified symbol607/// table into the values table.608void ValueEnumerator::EnumerateValueSymbolTable(const ValueSymbolTable &VST) {609for (ValueSymbolTable::const_iterator VI = VST.begin(), VE = VST.end();610VI != VE; ++VI)611EnumerateValue(VI->getValue());612}613614/// Insert all of the values referenced by named metadata in the specified615/// module.616void ValueEnumerator::EnumerateNamedMetadata(const Module &M) {617for (const auto &I : M.named_metadata())618EnumerateNamedMDNode(&I);619}620621void ValueEnumerator::EnumerateNamedMDNode(const NamedMDNode *MD) {622for (const MDNode *N : MD->operands())623EnumerateMetadata(nullptr, N);624}625626unsigned ValueEnumerator::getMetadataFunctionID(const Function *F) const {627return F ? getValueID(F) + 1 : 0;628}629630void ValueEnumerator::EnumerateMetadata(const Function *F, const Metadata *MD) {631EnumerateMetadata(getMetadataFunctionID(F), MD);632}633634void ValueEnumerator::EnumerateFunctionLocalMetadata(635const Function &F, const LocalAsMetadata *Local) {636EnumerateFunctionLocalMetadata(getMetadataFunctionID(&F), Local);637}638639void ValueEnumerator::EnumerateFunctionLocalListMetadata(640const Function &F, const DIArgList *ArgList) {641EnumerateFunctionLocalListMetadata(getMetadataFunctionID(&F), ArgList);642}643644void ValueEnumerator::dropFunctionFromMetadata(645MetadataMapType::value_type &FirstMD) {646SmallVector<const MDNode *, 64> Worklist;647auto push = [&Worklist](MetadataMapType::value_type &MD) {648auto &Entry = MD.second;649650// Nothing to do if this metadata isn't tagged.651if (!Entry.F)652return;653654// Drop the function tag.655Entry.F = 0;656657// If this is has an ID and is an MDNode, then its operands have entries as658// well. We need to drop the function from them too.659if (Entry.ID)660if (auto *N = dyn_cast<MDNode>(MD.first))661Worklist.push_back(N);662};663push(FirstMD);664while (!Worklist.empty())665for (const Metadata *Op : Worklist.pop_back_val()->operands()) {666if (!Op)667continue;668auto MD = MetadataMap.find(Op);669if (MD != MetadataMap.end())670push(*MD);671}672}673674void ValueEnumerator::EnumerateMetadata(unsigned F, const Metadata *MD) {675// It's vital for reader efficiency that uniqued subgraphs are done in676// post-order; it's expensive when their operands have forward references.677// If a distinct node is referenced from a uniqued node, it'll be delayed678// until the uniqued subgraph has been completely traversed.679SmallVector<const MDNode *, 32> DelayedDistinctNodes;680681// Start by enumerating MD, and then work through its transitive operands in682// post-order. This requires a depth-first search.683SmallVector<std::pair<const MDNode *, MDNode::op_iterator>, 32> Worklist;684if (const MDNode *N = enumerateMetadataImpl(F, MD))685Worklist.push_back(std::make_pair(N, N->op_begin()));686687while (!Worklist.empty()) {688const MDNode *N = Worklist.back().first;689690// Enumerate operands until we hit a new node. We need to traverse these691// nodes' operands before visiting the rest of N's operands.692MDNode::op_iterator I = std::find_if(693Worklist.back().second, N->op_end(),694[&](const Metadata *MD) { return enumerateMetadataImpl(F, MD); });695if (I != N->op_end()) {696auto *Op = cast<MDNode>(*I);697Worklist.back().second = ++I;698699// Delay traversing Op if it's a distinct node and N is uniqued.700if (Op->isDistinct() && !N->isDistinct())701DelayedDistinctNodes.push_back(Op);702else703Worklist.push_back(std::make_pair(Op, Op->op_begin()));704continue;705}706707// All the operands have been visited. Now assign an ID.708Worklist.pop_back();709MDs.push_back(N);710MetadataMap[N].ID = MDs.size();711712// Flush out any delayed distinct nodes; these are all the distinct nodes713// that are leaves in last uniqued subgraph.714if (Worklist.empty() || Worklist.back().first->isDistinct()) {715for (const MDNode *N : DelayedDistinctNodes)716Worklist.push_back(std::make_pair(N, N->op_begin()));717DelayedDistinctNodes.clear();718}719}720}721722const MDNode *ValueEnumerator::enumerateMetadataImpl(unsigned F, const Metadata *MD) {723if (!MD)724return nullptr;725726assert(727(isa<MDNode>(MD) || isa<MDString>(MD) || isa<ConstantAsMetadata>(MD)) &&728"Invalid metadata kind");729730auto Insertion = MetadataMap.insert(std::make_pair(MD, MDIndex(F)));731MDIndex &Entry = Insertion.first->second;732if (!Insertion.second) {733// Already mapped. If F doesn't match the function tag, drop it.734if (Entry.hasDifferentFunction(F))735dropFunctionFromMetadata(*Insertion.first);736return nullptr;737}738739// Don't assign IDs to metadata nodes.740if (auto *N = dyn_cast<MDNode>(MD))741return N;742743// Save the metadata.744MDs.push_back(MD);745Entry.ID = MDs.size();746747// Enumerate the constant, if any.748if (auto *C = dyn_cast<ConstantAsMetadata>(MD))749EnumerateValue(C->getValue());750751return nullptr;752}753754/// EnumerateFunctionLocalMetadata - Incorporate function-local metadata755/// information reachable from the metadata.756void ValueEnumerator::EnumerateFunctionLocalMetadata(757unsigned F, const LocalAsMetadata *Local) {758assert(F && "Expected a function");759760// Check to see if it's already in!761MDIndex &Index = MetadataMap[Local];762if (Index.ID) {763assert(Index.F == F && "Expected the same function");764return;765}766767MDs.push_back(Local);768Index.F = F;769Index.ID = MDs.size();770771EnumerateValue(Local->getValue());772}773774/// EnumerateFunctionLocalListMetadata - Incorporate function-local metadata775/// information reachable from the metadata.776void ValueEnumerator::EnumerateFunctionLocalListMetadata(777unsigned F, const DIArgList *ArgList) {778assert(F && "Expected a function");779780// Check to see if it's already in!781MDIndex &Index = MetadataMap[ArgList];782if (Index.ID) {783assert(Index.F == F && "Expected the same function");784return;785}786787for (ValueAsMetadata *VAM : ArgList->getArgs()) {788if (isa<LocalAsMetadata>(VAM)) {789assert(MetadataMap.count(VAM) &&790"LocalAsMetadata should be enumerated before DIArgList");791assert(MetadataMap[VAM].F == F &&792"Expected LocalAsMetadata in the same function");793} else {794assert(isa<ConstantAsMetadata>(VAM) &&795"Expected LocalAsMetadata or ConstantAsMetadata");796assert(ValueMap.count(VAM->getValue()) &&797"Constant should be enumerated beforeDIArgList");798EnumerateMetadata(F, VAM);799}800}801802MDs.push_back(ArgList);803Index.F = F;804Index.ID = MDs.size();805}806807static unsigned getMetadataTypeOrder(const Metadata *MD) {808// Strings are emitted in bulk and must come first.809if (isa<MDString>(MD))810return 0;811812// ConstantAsMetadata doesn't reference anything. We may as well shuffle it813// to the front since we can detect it.814auto *N = dyn_cast<MDNode>(MD);815if (!N)816return 1;817818// The reader is fast forward references for distinct node operands, but slow819// when uniqued operands are unresolved.820return N->isDistinct() ? 2 : 3;821}822823void ValueEnumerator::organizeMetadata() {824assert(MetadataMap.size() == MDs.size() &&825"Metadata map and vector out of sync");826827if (MDs.empty())828return;829830// Copy out the index information from MetadataMap in order to choose a new831// order.832SmallVector<MDIndex, 64> Order;833Order.reserve(MetadataMap.size());834for (const Metadata *MD : MDs)835Order.push_back(MetadataMap.lookup(MD));836837// Partition:838// - by function, then839// - by isa<MDString>840// and then sort by the original/current ID. Since the IDs are guaranteed to841// be unique, the result of llvm::sort will be deterministic. There's no need842// for std::stable_sort.843llvm::sort(Order, [this](MDIndex LHS, MDIndex RHS) {844return std::make_tuple(LHS.F, getMetadataTypeOrder(LHS.get(MDs)), LHS.ID) <845std::make_tuple(RHS.F, getMetadataTypeOrder(RHS.get(MDs)), RHS.ID);846});847848// Rebuild MDs, index the metadata ranges for each function in FunctionMDs,849// and fix up MetadataMap.850std::vector<const Metadata *> OldMDs;851MDs.swap(OldMDs);852MDs.reserve(OldMDs.size());853for (unsigned I = 0, E = Order.size(); I != E && !Order[I].F; ++I) {854auto *MD = Order[I].get(OldMDs);855MDs.push_back(MD);856MetadataMap[MD].ID = I + 1;857if (isa<MDString>(MD))858++NumMDStrings;859}860861// Return early if there's nothing for the functions.862if (MDs.size() == Order.size())863return;864865// Build the function metadata ranges.866MDRange R;867FunctionMDs.reserve(OldMDs.size());868unsigned PrevF = 0;869for (unsigned I = MDs.size(), E = Order.size(), ID = MDs.size(); I != E;870++I) {871unsigned F = Order[I].F;872if (!PrevF) {873PrevF = F;874} else if (PrevF != F) {875R.Last = FunctionMDs.size();876std::swap(R, FunctionMDInfo[PrevF]);877R.First = FunctionMDs.size();878879ID = MDs.size();880PrevF = F;881}882883auto *MD = Order[I].get(OldMDs);884FunctionMDs.push_back(MD);885MetadataMap[MD].ID = ++ID;886if (isa<MDString>(MD))887++R.NumStrings;888}889R.Last = FunctionMDs.size();890FunctionMDInfo[PrevF] = R;891}892893void ValueEnumerator::incorporateFunctionMetadata(const Function &F) {894NumModuleMDs = MDs.size();895896auto R = FunctionMDInfo.lookup(getValueID(&F) + 1);897NumMDStrings = R.NumStrings;898MDs.insert(MDs.end(), FunctionMDs.begin() + R.First,899FunctionMDs.begin() + R.Last);900}901902void ValueEnumerator::EnumerateValue(const Value *V) {903assert(!V->getType()->isVoidTy() && "Can't insert void values!");904assert(!isa<MetadataAsValue>(V) && "EnumerateValue doesn't handle Metadata!");905906// Check to see if it's already in!907unsigned &ValueID = ValueMap[V];908if (ValueID) {909// Increment use count.910Values[ValueID-1].second++;911return;912}913914if (auto *GO = dyn_cast<GlobalObject>(V))915if (const Comdat *C = GO->getComdat())916Comdats.insert(C);917918// Enumerate the type of this value.919EnumerateType(V->getType());920921if (const Constant *C = dyn_cast<Constant>(V)) {922if (isa<GlobalValue>(C)) {923// Initializers for globals are handled explicitly elsewhere.924} else if (C->getNumOperands()) {925// If a constant has operands, enumerate them. This makes sure that if a926// constant has uses (for example an array of const ints), that they are927// inserted also.928929// We prefer to enumerate them with values before we enumerate the user930// itself. This makes it more likely that we can avoid forward references931// in the reader. We know that there can be no cycles in the constants932// graph that don't go through a global variable.933for (const Use &U : C->operands())934if (!isa<BasicBlock>(U)) // Don't enumerate BB operand to BlockAddress.935EnumerateValue(U);936if (auto *CE = dyn_cast<ConstantExpr>(C)) {937if (CE->getOpcode() == Instruction::ShuffleVector)938EnumerateValue(CE->getShuffleMaskForBitcode());939if (auto *GEP = dyn_cast<GEPOperator>(CE))940EnumerateType(GEP->getSourceElementType());941}942943// Finally, add the value. Doing this could make the ValueID reference be944// dangling, don't reuse it.945Values.push_back(std::make_pair(V, 1U));946ValueMap[V] = Values.size();947return;948}949}950951// Add the value.952Values.push_back(std::make_pair(V, 1U));953ValueID = Values.size();954}955956957void ValueEnumerator::EnumerateType(Type *Ty) {958unsigned *TypeID = &TypeMap[Ty];959960// We've already seen this type.961if (*TypeID)962return;963964// If it is a non-anonymous struct, mark the type as being visited so that we965// don't recursively visit it. This is safe because we allow forward966// references of these in the bitcode reader.967if (StructType *STy = dyn_cast<StructType>(Ty))968if (!STy->isLiteral())969*TypeID = ~0U;970971// Enumerate all of the subtypes before we enumerate this type. This ensures972// that the type will be enumerated in an order that can be directly built.973for (Type *SubTy : Ty->subtypes())974EnumerateType(SubTy);975976// Refresh the TypeID pointer in case the table rehashed.977TypeID = &TypeMap[Ty];978979// Check to see if we got the pointer another way. This can happen when980// enumerating recursive types that hit the base case deeper than they start.981//982// If this is actually a struct that we are treating as forward ref'able,983// then emit the definition now that all of its contents are available.984if (*TypeID && *TypeID != ~0U)985return;986987// Add this type now that its contents are all happily enumerated.988Types.push_back(Ty);989990*TypeID = Types.size();991}992993// Enumerate the types for the specified value. If the value is a constant,994// walk through it, enumerating the types of the constant.995void ValueEnumerator::EnumerateOperandType(const Value *V) {996EnumerateType(V->getType());997998assert(!isa<MetadataAsValue>(V) && "Unexpected metadata operand");9991000const Constant *C = dyn_cast<Constant>(V);1001if (!C)1002return;10031004// If this constant is already enumerated, ignore it, we know its type must1005// be enumerated.1006if (ValueMap.count(C))1007return;10081009// This constant may have operands, make sure to enumerate the types in1010// them.1011for (const Value *Op : C->operands()) {1012// Don't enumerate basic blocks here, this happens as operands to1013// blockaddress.1014if (isa<BasicBlock>(Op))1015continue;10161017EnumerateOperandType(Op);1018}1019if (auto *CE = dyn_cast<ConstantExpr>(C)) {1020if (CE->getOpcode() == Instruction::ShuffleVector)1021EnumerateOperandType(CE->getShuffleMaskForBitcode());1022if (CE->getOpcode() == Instruction::GetElementPtr)1023EnumerateType(cast<GEPOperator>(CE)->getSourceElementType());1024}1025}10261027void ValueEnumerator::EnumerateAttributes(AttributeList PAL) {1028if (PAL.isEmpty()) return; // null is always 0.10291030// Do a lookup.1031unsigned &Entry = AttributeListMap[PAL];1032if (Entry == 0) {1033// Never saw this before, add it.1034AttributeLists.push_back(PAL);1035Entry = AttributeLists.size();1036}10371038// Do lookups for all attribute groups.1039for (unsigned i : PAL.indexes()) {1040AttributeSet AS = PAL.getAttributes(i);1041if (!AS.hasAttributes())1042continue;1043IndexAndAttrSet Pair = {i, AS};1044unsigned &Entry = AttributeGroupMap[Pair];1045if (Entry == 0) {1046AttributeGroups.push_back(Pair);1047Entry = AttributeGroups.size();10481049for (Attribute Attr : AS) {1050if (Attr.isTypeAttribute())1051EnumerateType(Attr.getValueAsType());1052}1053}1054}1055}10561057void ValueEnumerator::incorporateFunction(const Function &F) {1058InstructionCount = 0;1059NumModuleValues = Values.size();10601061// Add global metadata to the function block. This doesn't include1062// LocalAsMetadata.1063incorporateFunctionMetadata(F);10641065// Adding function arguments to the value table.1066for (const auto &I : F.args()) {1067EnumerateValue(&I);1068if (I.hasAttribute(Attribute::ByVal))1069EnumerateType(I.getParamByValType());1070else if (I.hasAttribute(Attribute::StructRet))1071EnumerateType(I.getParamStructRetType());1072else if (I.hasAttribute(Attribute::ByRef))1073EnumerateType(I.getParamByRefType());1074}1075FirstFuncConstantID = Values.size();10761077// Add all function-level constants to the value table.1078for (const BasicBlock &BB : F) {1079for (const Instruction &I : BB) {1080for (const Use &OI : I.operands()) {1081if ((isa<Constant>(OI) && !isa<GlobalValue>(OI)) || isa<InlineAsm>(OI))1082EnumerateValue(OI);1083}1084if (auto *SVI = dyn_cast<ShuffleVectorInst>(&I))1085EnumerateValue(SVI->getShuffleMaskForBitcode());1086}1087BasicBlocks.push_back(&BB);1088ValueMap[&BB] = BasicBlocks.size();1089}10901091// Optimize the constant layout.1092OptimizeConstants(FirstFuncConstantID, Values.size());10931094// Add the function's parameter attributes so they are available for use in1095// the function's instruction.1096EnumerateAttributes(F.getAttributes());10971098FirstInstID = Values.size();10991100SmallVector<LocalAsMetadata *, 8> FnLocalMDVector;1101SmallVector<DIArgList *, 8> ArgListMDVector;11021103auto AddFnLocalMetadata = [&](Metadata *MD) {1104if (!MD)1105return;1106if (auto *Local = dyn_cast<LocalAsMetadata>(MD)) {1107// Enumerate metadata after the instructions they might refer to.1108FnLocalMDVector.push_back(Local);1109} else if (auto *ArgList = dyn_cast<DIArgList>(MD)) {1110ArgListMDVector.push_back(ArgList);1111for (ValueAsMetadata *VMD : ArgList->getArgs()) {1112if (auto *Local = dyn_cast<LocalAsMetadata>(VMD)) {1113// Enumerate metadata after the instructions they might refer1114// to.1115FnLocalMDVector.push_back(Local);1116}1117}1118}1119};11201121// Add all of the instructions.1122for (const BasicBlock &BB : F) {1123for (const Instruction &I : BB) {1124for (const Use &OI : I.operands()) {1125if (auto *MD = dyn_cast<MetadataAsValue>(&OI))1126AddFnLocalMetadata(MD->getMetadata());1127}1128/// RemoveDIs: Add non-instruction function-local metadata uses.1129for (DbgVariableRecord &DVR : filterDbgVars(I.getDbgRecordRange())) {1130assert(DVR.getRawLocation() &&1131"DbgVariableRecord location unexpectedly null");1132AddFnLocalMetadata(DVR.getRawLocation());1133if (DVR.isDbgAssign()) {1134assert(DVR.getRawAddress() &&1135"DbgVariableRecord location unexpectedly null");1136AddFnLocalMetadata(DVR.getRawAddress());1137}1138}1139if (!I.getType()->isVoidTy())1140EnumerateValue(&I);1141}1142}11431144// Add all of the function-local metadata.1145for (const LocalAsMetadata *Local : FnLocalMDVector) {1146// At this point, every local values have been incorporated, we shouldn't1147// have a metadata operand that references a value that hasn't been seen.1148assert(ValueMap.count(Local->getValue()) &&1149"Missing value for metadata operand");1150EnumerateFunctionLocalMetadata(F, Local);1151}1152// DIArgList entries must come after function-local metadata, as it is not1153// possible to forward-reference them.1154for (const DIArgList *ArgList : ArgListMDVector)1155EnumerateFunctionLocalListMetadata(F, ArgList);1156}11571158void ValueEnumerator::purgeFunction() {1159/// Remove purged values from the ValueMap.1160for (const auto &V : llvm::drop_begin(Values, NumModuleValues))1161ValueMap.erase(V.first);1162for (const Metadata *MD : llvm::drop_begin(MDs, NumModuleMDs))1163MetadataMap.erase(MD);1164for (const BasicBlock *BB : BasicBlocks)1165ValueMap.erase(BB);11661167Values.resize(NumModuleValues);1168MDs.resize(NumModuleMDs);1169BasicBlocks.clear();1170NumMDStrings = 0;1171}11721173static void IncorporateFunctionInfoGlobalBBIDs(const Function *F,1174DenseMap<const BasicBlock*, unsigned> &IDMap) {1175unsigned Counter = 0;1176for (const BasicBlock &BB : *F)1177IDMap[&BB] = ++Counter;1178}11791180/// getGlobalBasicBlockID - This returns the function-specific ID for the1181/// specified basic block. This is relatively expensive information, so it1182/// should only be used by rare constructs such as address-of-label.1183unsigned ValueEnumerator::getGlobalBasicBlockID(const BasicBlock *BB) const {1184unsigned &Idx = GlobalBasicBlockIDs[BB];1185if (Idx != 0)1186return Idx-1;11871188IncorporateFunctionInfoGlobalBBIDs(BB->getParent(), GlobalBasicBlockIDs);1189return getGlobalBasicBlockID(BB);1190}11911192uint64_t ValueEnumerator::computeBitsRequiredForTypeIndices() const {1193return Log2_32_Ceil(getTypes().size() + 1);1194}119511961197