Path: blob/main/contrib/llvm-project/llvm/lib/Target/DirectX/DXILWriter/DXILValueEnumerator.cpp
35294 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// Forked from lib/Bitcode/Writer10//11//===----------------------------------------------------------------------===//1213#include "DXILValueEnumerator.h"14#include "llvm/ADT/SmallVector.h"15#include "llvm/Config/llvm-config.h"16#include "llvm/IR/Argument.h"17#include "llvm/IR/BasicBlock.h"18#include "llvm/IR/Constant.h"19#include "llvm/IR/DebugInfoMetadata.h"20#include "llvm/IR/DerivedTypes.h"21#include "llvm/IR/Function.h"22#include "llvm/IR/GlobalAlias.h"23#include "llvm/IR/GlobalIFunc.h"24#include "llvm/IR/GlobalObject.h"25#include "llvm/IR/GlobalValue.h"26#include "llvm/IR/GlobalVariable.h"27#include "llvm/IR/Instruction.h"28#include "llvm/IR/Instructions.h"29#include "llvm/IR/Metadata.h"30#include "llvm/IR/Module.h"31#include "llvm/IR/Operator.h"32#include "llvm/IR/Type.h"33#include "llvm/IR/TypedPointerType.h"34#include "llvm/IR/Use.h"35#include "llvm/IR/User.h"36#include "llvm/IR/Value.h"37#include "llvm/IR/ValueSymbolTable.h"38#include "llvm/Support/Casting.h"39#include "llvm/Support/Compiler.h"40#include "llvm/Support/Debug.h"41#include "llvm/Support/MathExtras.h"42#include "llvm/Support/raw_ostream.h"43#include <algorithm>44#include <cstddef>45#include <iterator>46#include <tuple>4748using namespace llvm;49using namespace llvm::dxil;5051namespace {5253struct OrderMap {54DenseMap<const Value *, std::pair<unsigned, bool>> IDs;55unsigned LastGlobalConstantID = 0;56unsigned LastGlobalValueID = 0;5758OrderMap() = default;5960bool isGlobalConstant(unsigned ID) const {61return ID <= LastGlobalConstantID;62}6364bool isGlobalValue(unsigned ID) const {65return ID <= LastGlobalValueID && !isGlobalConstant(ID);66}6768unsigned size() const { return IDs.size(); }69std::pair<unsigned, bool> &operator[](const Value *V) { return IDs[V]; }7071std::pair<unsigned, bool> lookup(const Value *V) const {72return IDs.lookup(V);73}7475void index(const Value *V) {76// Explicitly sequence get-size and insert-value operations to avoid UB.77unsigned ID = IDs.size() + 1;78IDs[V].first = ID;79}80};8182} // end anonymous namespace8384static void orderValue(const Value *V, OrderMap &OM) {85if (OM.lookup(V).first)86return;8788if (const Constant *C = dyn_cast<Constant>(V)) {89if (C->getNumOperands() && !isa<GlobalValue>(C)) {90for (const Value *Op : C->operands())91if (!isa<BasicBlock>(Op) && !isa<GlobalValue>(Op))92orderValue(Op, OM);93if (auto *CE = dyn_cast<ConstantExpr>(C))94if (CE->getOpcode() == Instruction::ShuffleVector)95orderValue(CE->getShuffleMaskForBitcode(), OM);96}97}9899// Note: we cannot cache this lookup above, since inserting into the map100// changes the map's size, and thus affects the other IDs.101OM.index(V);102}103104static OrderMap orderModule(const Module &M) {105// This needs to match the order used by ValueEnumerator::ValueEnumerator()106// and ValueEnumerator::incorporateFunction().107OrderMap OM;108109// In the reader, initializers of GlobalValues are set *after* all the110// globals have been read. Rather than awkwardly modeling this behaviour111// directly in predictValueUseListOrderImpl(), just assign IDs to112// initializers of GlobalValues before GlobalValues themselves to model this113// implicitly.114for (const GlobalVariable &G : M.globals())115if (G.hasInitializer())116if (!isa<GlobalValue>(G.getInitializer()))117orderValue(G.getInitializer(), OM);118for (const GlobalAlias &A : M.aliases())119if (!isa<GlobalValue>(A.getAliasee()))120orderValue(A.getAliasee(), OM);121for (const GlobalIFunc &I : M.ifuncs())122if (!isa<GlobalValue>(I.getResolver()))123orderValue(I.getResolver(), OM);124for (const Function &F : M) {125for (const Use &U : F.operands())126if (!isa<GlobalValue>(U.get()))127orderValue(U.get(), OM);128}129130// As constants used in metadata operands are emitted as module-level131// constants, we must order them before other operands. Also, we must order132// these before global values, as these will be read before setting the133// global values' initializers. The latter matters for constants which have134// uses towards other constants that are used as initializers.135auto orderConstantValue = [&OM](const Value *V) {136if ((isa<Constant>(V) && !isa<GlobalValue>(V)) || isa<InlineAsm>(V))137orderValue(V, OM);138};139for (const Function &F : M) {140if (F.isDeclaration())141continue;142for (const BasicBlock &BB : F)143for (const Instruction &I : BB)144for (const Value *V : I.operands()) {145if (const auto *MAV = dyn_cast<MetadataAsValue>(V)) {146if (const auto *VAM =147dyn_cast<ValueAsMetadata>(MAV->getMetadata())) {148orderConstantValue(VAM->getValue());149} else if (const auto *AL =150dyn_cast<DIArgList>(MAV->getMetadata())) {151for (const auto *VAM : AL->getArgs())152orderConstantValue(VAM->getValue());153}154}155}156}157OM.LastGlobalConstantID = OM.size();158159// Initializers of GlobalValues are processed in160// BitcodeReader::ResolveGlobalAndAliasInits(). Match the order there rather161// than ValueEnumerator, and match the code in predictValueUseListOrderImpl()162// by giving IDs in reverse order.163//164// Since GlobalValues never reference each other directly (just through165// initializers), their relative IDs only matter for determining order of166// uses in their initializers.167for (const Function &F : M)168orderValue(&F, OM);169for (const GlobalAlias &A : M.aliases())170orderValue(&A, OM);171for (const GlobalIFunc &I : M.ifuncs())172orderValue(&I, OM);173for (const GlobalVariable &G : M.globals())174orderValue(&G, OM);175OM.LastGlobalValueID = OM.size();176177for (const Function &F : M) {178if (F.isDeclaration())179continue;180// Here we need to match the union of ValueEnumerator::incorporateFunction()181// and WriteFunction(). Basic blocks are implicitly declared before182// anything else (by declaring their size).183for (const BasicBlock &BB : F)184orderValue(&BB, OM);185for (const Argument &A : F.args())186orderValue(&A, OM);187for (const BasicBlock &BB : F)188for (const Instruction &I : BB) {189for (const Value *Op : I.operands())190if ((isa<Constant>(*Op) && !isa<GlobalValue>(*Op)) ||191isa<InlineAsm>(*Op))192orderValue(Op, OM);193if (auto *SVI = dyn_cast<ShuffleVectorInst>(&I))194orderValue(SVI->getShuffleMaskForBitcode(), OM);195}196for (const BasicBlock &BB : F)197for (const Instruction &I : BB)198orderValue(&I, OM);199}200return OM;201}202203static void predictValueUseListOrderImpl(const Value *V, const Function *F,204unsigned ID, const OrderMap &OM,205UseListOrderStack &Stack) {206// Predict use-list order for this one.207using Entry = std::pair<const Use *, unsigned>;208SmallVector<Entry, 64> List;209for (const Use &U : V->uses())210// Check if this user will be serialized.211if (OM.lookup(U.getUser()).first)212List.push_back(std::make_pair(&U, List.size()));213214if (List.size() < 2)215// We may have lost some users.216return;217218bool IsGlobalValue = OM.isGlobalValue(ID);219llvm::sort(List, [&](const Entry &L, const Entry &R) {220const Use *LU = L.first;221const Use *RU = R.first;222if (LU == RU)223return false;224225auto LID = OM.lookup(LU->getUser()).first;226auto RID = OM.lookup(RU->getUser()).first;227228// Global values are processed in reverse order.229//230// Moreover, initializers of GlobalValues are set *after* all the globals231// have been read (despite having earlier IDs). Rather than awkwardly232// modeling this behaviour here, orderModule() has assigned IDs to233// initializers of GlobalValues before GlobalValues themselves.234if (OM.isGlobalValue(LID) && OM.isGlobalValue(RID)) {235if (LID == RID)236return LU->getOperandNo() > RU->getOperandNo();237return LID < RID;238}239240// If ID is 4, then expect: 7 6 5 1 2 3.241if (LID < RID) {242if (RID <= ID)243if (!IsGlobalValue) // GlobalValue uses don't get reversed.244return true;245return false;246}247if (RID < LID) {248if (LID <= ID)249if (!IsGlobalValue) // GlobalValue uses don't get reversed.250return false;251return true;252}253254// LID and RID are equal, so we have different operands of the same user.255// Assume operands are added in order for all instructions.256if (LID <= ID)257if (!IsGlobalValue) // GlobalValue uses don't get reversed.258return LU->getOperandNo() < RU->getOperandNo();259return LU->getOperandNo() > RU->getOperandNo();260});261262if (llvm::is_sorted(List, llvm::less_second()))263// Order is already correct.264return;265266// Store the shuffle.267Stack.emplace_back(V, F, List.size());268assert(List.size() == Stack.back().Shuffle.size() && "Wrong size");269for (size_t I = 0, E = List.size(); I != E; ++I)270Stack.back().Shuffle[I] = List[I].second;271}272273static void predictValueUseListOrder(const Value *V, const Function *F,274OrderMap &OM, UseListOrderStack &Stack) {275auto &IDPair = OM[V];276assert(IDPair.first && "Unmapped value");277if (IDPair.second)278// Already predicted.279return;280281// Do the actual prediction.282IDPair.second = true;283if (!V->use_empty() && std::next(V->use_begin()) != V->use_end())284predictValueUseListOrderImpl(V, F, IDPair.first, OM, Stack);285286// Recursive descent into constants.287if (const Constant *C = dyn_cast<Constant>(V)) {288if (C->getNumOperands()) { // Visit GlobalValues.289for (const Value *Op : C->operands())290if (isa<Constant>(Op)) // Visit GlobalValues.291predictValueUseListOrder(Op, F, OM, Stack);292if (auto *CE = dyn_cast<ConstantExpr>(C))293if (CE->getOpcode() == Instruction::ShuffleVector)294predictValueUseListOrder(CE->getShuffleMaskForBitcode(), F, OM,295Stack);296}297}298}299300static UseListOrderStack predictUseListOrder(const Module &M) {301OrderMap OM = orderModule(M);302303// Use-list orders need to be serialized after all the users have been added304// to a value, or else the shuffles will be incomplete. Store them per305// function in a stack.306//307// Aside from function order, the order of values doesn't matter much here.308UseListOrderStack Stack;309310// We want to visit the functions backward now so we can list function-local311// constants in the last Function they're used in. Module-level constants312// have already been visited above.313for (const Function &F : llvm::reverse(M)) {314if (F.isDeclaration())315continue;316for (const BasicBlock &BB : F)317predictValueUseListOrder(&BB, &F, OM, Stack);318for (const Argument &A : F.args())319predictValueUseListOrder(&A, &F, OM, Stack);320for (const BasicBlock &BB : F)321for (const Instruction &I : BB) {322for (const Value *Op : I.operands())323if (isa<Constant>(*Op) || isa<InlineAsm>(*Op)) // Visit GlobalValues.324predictValueUseListOrder(Op, &F, OM, Stack);325if (auto *SVI = dyn_cast<ShuffleVectorInst>(&I))326predictValueUseListOrder(SVI->getShuffleMaskForBitcode(), &F, OM,327Stack);328}329for (const BasicBlock &BB : F)330for (const Instruction &I : BB)331predictValueUseListOrder(&I, &F, OM, Stack);332}333334// Visit globals last, since the module-level use-list block will be seen335// before the function bodies are processed.336for (const GlobalVariable &G : M.globals())337predictValueUseListOrder(&G, nullptr, OM, Stack);338for (const Function &F : M)339predictValueUseListOrder(&F, nullptr, OM, Stack);340for (const GlobalAlias &A : M.aliases())341predictValueUseListOrder(&A, nullptr, OM, Stack);342for (const GlobalIFunc &I : M.ifuncs())343predictValueUseListOrder(&I, nullptr, OM, Stack);344for (const GlobalVariable &G : M.globals())345if (G.hasInitializer())346predictValueUseListOrder(G.getInitializer(), nullptr, OM, Stack);347for (const GlobalAlias &A : M.aliases())348predictValueUseListOrder(A.getAliasee(), nullptr, OM, Stack);349for (const GlobalIFunc &I : M.ifuncs())350predictValueUseListOrder(I.getResolver(), nullptr, OM, Stack);351for (const Function &F : M) {352for (const Use &U : F.operands())353predictValueUseListOrder(U.get(), nullptr, OM, Stack);354}355356return Stack;357}358359ValueEnumerator::ValueEnumerator(const Module &M, Type *PrefixType) {360EnumerateType(PrefixType);361362UseListOrders = predictUseListOrder(M);363364// Enumerate the global variables.365for (const GlobalVariable &GV : M.globals()) {366EnumerateValue(&GV);367EnumerateType(GV.getValueType());368}369370// Enumerate the functions.371for (const Function &F : M) {372EnumerateValue(&F);373EnumerateType(F.getValueType());374EnumerateType(375TypedPointerType::get(F.getFunctionType(), F.getAddressSpace()));376EnumerateAttributes(F.getAttributes());377}378379// Enumerate the aliases.380for (const GlobalAlias &GA : M.aliases()) {381EnumerateValue(&GA);382EnumerateType(GA.getValueType());383}384385// Enumerate the ifuncs.386for (const GlobalIFunc &GIF : M.ifuncs()) {387EnumerateValue(&GIF);388EnumerateType(GIF.getValueType());389}390391// Enumerate the global variable initializers and attributes.392for (const GlobalVariable &GV : M.globals()) {393if (GV.hasInitializer())394EnumerateValue(GV.getInitializer());395EnumerateType(396TypedPointerType::get(GV.getValueType(), GV.getAddressSpace()));397if (GV.hasAttributes())398EnumerateAttributes(GV.getAttributesAsList(AttributeList::FunctionIndex));399}400401// Enumerate the aliasees.402for (const GlobalAlias &GA : M.aliases())403EnumerateValue(GA.getAliasee());404405// Enumerate the ifunc resolvers.406for (const GlobalIFunc &GIF : M.ifuncs())407EnumerateValue(GIF.getResolver());408409// Enumerate any optional Function data.410for (const Function &F : M)411for (const Use &U : F.operands())412EnumerateValue(U.get());413414// Enumerate the metadata type.415//416// TODO: Move this to ValueEnumerator::EnumerateOperandType() once bitcode417// only encodes the metadata type when it's used as a value.418EnumerateType(Type::getMetadataTy(M.getContext()));419420// Insert constants and metadata that are named at module level into the slot421// pool so that the module symbol table can refer to them...422EnumerateValueSymbolTable(M.getValueSymbolTable());423EnumerateNamedMetadata(M);424425SmallVector<std::pair<unsigned, MDNode *>, 8> MDs;426for (const GlobalVariable &GV : M.globals()) {427MDs.clear();428GV.getAllMetadata(MDs);429for (const auto &I : MDs)430// FIXME: Pass GV to EnumerateMetadata and arrange for the bitcode writer431// to write metadata to the global variable's own metadata block432// (PR28134).433EnumerateMetadata(nullptr, I.second);434}435436// Enumerate types used by function bodies and argument lists.437for (const Function &F : M) {438for (const Argument &A : F.args())439EnumerateType(A.getType());440441// Enumerate metadata attached to this function.442MDs.clear();443F.getAllMetadata(MDs);444for (const auto &I : MDs)445EnumerateMetadata(F.isDeclaration() ? nullptr : &F, I.second);446447for (const BasicBlock &BB : F)448for (const Instruction &I : BB) {449for (const Use &Op : I.operands()) {450auto *MD = dyn_cast<MetadataAsValue>(&Op);451if (!MD) {452EnumerateOperandType(Op);453continue;454}455456// Local metadata is enumerated during function-incorporation, but457// any ConstantAsMetadata arguments in a DIArgList should be examined458// now.459if (isa<LocalAsMetadata>(MD->getMetadata()))460continue;461if (auto *AL = dyn_cast<DIArgList>(MD->getMetadata())) {462for (auto *VAM : AL->getArgs())463if (isa<ConstantAsMetadata>(VAM))464EnumerateMetadata(&F, VAM);465continue;466}467468EnumerateMetadata(&F, MD->getMetadata());469}470if (auto *SVI = dyn_cast<ShuffleVectorInst>(&I))471EnumerateType(SVI->getShuffleMaskForBitcode()->getType());472if (auto *GEP = dyn_cast<GetElementPtrInst>(&I))473EnumerateType(GEP->getSourceElementType());474if (auto *AI = dyn_cast<AllocaInst>(&I))475EnumerateType(AI->getAllocatedType());476EnumerateType(I.getType());477if (const auto *Call = dyn_cast<CallBase>(&I)) {478EnumerateAttributes(Call->getAttributes());479EnumerateType(Call->getFunctionType());480}481482// Enumerate metadata attached with this instruction.483MDs.clear();484I.getAllMetadataOtherThanDebugLoc(MDs);485for (unsigned i = 0, e = MDs.size(); i != e; ++i)486EnumerateMetadata(&F, MDs[i].second);487488// Don't enumerate the location directly -- it has a special record489// type -- but enumerate its operands.490if (DILocation *L = I.getDebugLoc())491for (const Metadata *Op : L->operands())492EnumerateMetadata(&F, Op);493}494}495496// Organize metadata ordering.497organizeMetadata();498}499500unsigned ValueEnumerator::getInstructionID(const Instruction *Inst) const {501InstructionMapType::const_iterator I = InstructionMap.find(Inst);502assert(I != InstructionMap.end() && "Instruction is not mapped!");503return I->second;504}505506unsigned ValueEnumerator::getComdatID(const Comdat *C) const {507unsigned ComdatID = Comdats.idFor(C);508assert(ComdatID && "Comdat not found!");509return ComdatID;510}511512void ValueEnumerator::setInstructionID(const Instruction *I) {513InstructionMap[I] = InstructionCount++;514}515516unsigned ValueEnumerator::getValueID(const Value *V) const {517if (auto *MD = dyn_cast<MetadataAsValue>(V))518return getMetadataID(MD->getMetadata());519520ValueMapType::const_iterator I = ValueMap.find(V);521assert(I != ValueMap.end() && "Value not in slotcalculator!");522return I->second - 1;523}524525#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)526LLVM_DUMP_METHOD void ValueEnumerator::dump() const {527print(dbgs(), ValueMap, "Default");528dbgs() << '\n';529print(dbgs(), MetadataMap, "MetaData");530dbgs() << '\n';531}532#endif533534void ValueEnumerator::print(raw_ostream &OS, const ValueMapType &Map,535const char *Name) const {536OS << "Map Name: " << Name << "\n";537OS << "Size: " << Map.size() << "\n";538for (const auto &I : Map) {539const Value *V = I.first;540if (V->hasName())541OS << "Value: " << V->getName();542else543OS << "Value: [null]\n";544V->print(errs());545errs() << '\n';546547OS << " Uses(" << V->getNumUses() << "):";548for (const Use &U : V->uses()) {549if (&U != &*V->use_begin())550OS << ",";551if (U->hasName())552OS << " " << U->getName();553else554OS << " [null]";555}556OS << "\n\n";557}558}559560void ValueEnumerator::print(raw_ostream &OS, const MetadataMapType &Map,561const char *Name) const {562OS << "Map Name: " << Name << "\n";563OS << "Size: " << Map.size() << "\n";564for (const auto &I : Map) {565const Metadata *MD = I.first;566OS << "Metadata: slot = " << I.second.ID << "\n";567OS << "Metadata: function = " << I.second.F << "\n";568MD->print(OS);569OS << "\n";570}571}572573/// EnumerateValueSymbolTable - Insert all of the values in the specified symbol574/// table into the values table.575void ValueEnumerator::EnumerateValueSymbolTable(const ValueSymbolTable &VST) {576for (ValueSymbolTable::const_iterator VI = VST.begin(), VE = VST.end();577VI != VE; ++VI)578EnumerateValue(VI->getValue());579}580581/// Insert all of the values referenced by named metadata in the specified582/// module.583void ValueEnumerator::EnumerateNamedMetadata(const Module &M) {584for (const auto &I : M.named_metadata())585EnumerateNamedMDNode(&I);586}587588void ValueEnumerator::EnumerateNamedMDNode(const NamedMDNode *MD) {589for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i)590EnumerateMetadata(nullptr, MD->getOperand(i));591}592593unsigned ValueEnumerator::getMetadataFunctionID(const Function *F) const {594return F ? getValueID(F) + 1 : 0;595}596597void ValueEnumerator::EnumerateMetadata(const Function *F, const Metadata *MD) {598EnumerateMetadata(getMetadataFunctionID(F), MD);599}600601void ValueEnumerator::EnumerateFunctionLocalMetadata(602const Function &F, const LocalAsMetadata *Local) {603EnumerateFunctionLocalMetadata(getMetadataFunctionID(&F), Local);604}605606void ValueEnumerator::EnumerateFunctionLocalListMetadata(607const Function &F, const DIArgList *ArgList) {608EnumerateFunctionLocalListMetadata(getMetadataFunctionID(&F), ArgList);609}610611void ValueEnumerator::dropFunctionFromMetadata(612MetadataMapType::value_type &FirstMD) {613SmallVector<const MDNode *, 64> Worklist;614auto push = [&Worklist](MetadataMapType::value_type &MD) {615auto &Entry = MD.second;616617// Nothing to do if this metadata isn't tagged.618if (!Entry.F)619return;620621// Drop the function tag.622Entry.F = 0;623624// If this is has an ID and is an MDNode, then its operands have entries as625// well. We need to drop the function from them too.626if (Entry.ID)627if (auto *N = dyn_cast<MDNode>(MD.first))628Worklist.push_back(N);629};630push(FirstMD);631while (!Worklist.empty())632for (const Metadata *Op : Worklist.pop_back_val()->operands()) {633if (!Op)634continue;635auto MD = MetadataMap.find(Op);636if (MD != MetadataMap.end())637push(*MD);638}639}640641void ValueEnumerator::EnumerateMetadata(unsigned F, const Metadata *MD) {642// It's vital for reader efficiency that uniqued subgraphs are done in643// post-order; it's expensive when their operands have forward references.644// If a distinct node is referenced from a uniqued node, it'll be delayed645// until the uniqued subgraph has been completely traversed.646SmallVector<const MDNode *, 32> DelayedDistinctNodes;647648// Start by enumerating MD, and then work through its transitive operands in649// post-order. This requires a depth-first search.650SmallVector<std::pair<const MDNode *, MDNode::op_iterator>, 32> Worklist;651if (const MDNode *N = enumerateMetadataImpl(F, MD))652Worklist.push_back(std::make_pair(N, N->op_begin()));653654while (!Worklist.empty()) {655const MDNode *N = Worklist.back().first;656657// Enumerate operands until we hit a new node. We need to traverse these658// nodes' operands before visiting the rest of N's operands.659MDNode::op_iterator I = std::find_if(660Worklist.back().second, N->op_end(),661[&](const Metadata *MD) { return enumerateMetadataImpl(F, MD); });662if (I != N->op_end()) {663auto *Op = cast<MDNode>(*I);664Worklist.back().second = ++I;665666// Delay traversing Op if it's a distinct node and N is uniqued.667if (Op->isDistinct() && !N->isDistinct())668DelayedDistinctNodes.push_back(Op);669else670Worklist.push_back(std::make_pair(Op, Op->op_begin()));671continue;672}673674// All the operands have been visited. Now assign an ID.675Worklist.pop_back();676MDs.push_back(N);677MetadataMap[N].ID = MDs.size();678679// Flush out any delayed distinct nodes; these are all the distinct nodes680// that are leaves in last uniqued subgraph.681if (Worklist.empty() || Worklist.back().first->isDistinct()) {682for (const MDNode *N : DelayedDistinctNodes)683Worklist.push_back(std::make_pair(N, N->op_begin()));684DelayedDistinctNodes.clear();685}686}687}688689const MDNode *ValueEnumerator::enumerateMetadataImpl(unsigned F,690const Metadata *MD) {691if (!MD)692return nullptr;693694assert(695(isa<MDNode>(MD) || isa<MDString>(MD) || isa<ConstantAsMetadata>(MD)) &&696"Invalid metadata kind");697698auto Insertion = MetadataMap.insert(std::make_pair(MD, MDIndex(F)));699MDIndex &Entry = Insertion.first->second;700if (!Insertion.second) {701// Already mapped. If F doesn't match the function tag, drop it.702if (Entry.hasDifferentFunction(F))703dropFunctionFromMetadata(*Insertion.first);704return nullptr;705}706707// Don't assign IDs to metadata nodes.708if (auto *N = dyn_cast<MDNode>(MD))709return N;710711// Save the metadata.712MDs.push_back(MD);713Entry.ID = MDs.size();714715// Enumerate the constant, if any.716if (auto *C = dyn_cast<ConstantAsMetadata>(MD))717EnumerateValue(C->getValue());718719return nullptr;720}721722/// EnumerateFunctionLocalMetadata - Incorporate function-local metadata723/// information reachable from the metadata.724void ValueEnumerator::EnumerateFunctionLocalMetadata(725unsigned F, const LocalAsMetadata *Local) {726assert(F && "Expected a function");727728// Check to see if it's already in!729MDIndex &Index = MetadataMap[Local];730if (Index.ID) {731assert(Index.F == F && "Expected the same function");732return;733}734735MDs.push_back(Local);736Index.F = F;737Index.ID = MDs.size();738739EnumerateValue(Local->getValue());740}741742/// EnumerateFunctionLocalListMetadata - Incorporate function-local metadata743/// information reachable from the metadata.744void ValueEnumerator::EnumerateFunctionLocalListMetadata(745unsigned F, const DIArgList *ArgList) {746assert(F && "Expected a function");747748// Check to see if it's already in!749MDIndex &Index = MetadataMap[ArgList];750if (Index.ID) {751assert(Index.F == F && "Expected the same function");752return;753}754755for (ValueAsMetadata *VAM : ArgList->getArgs()) {756if (isa<LocalAsMetadata>(VAM)) {757assert(MetadataMap.count(VAM) &&758"LocalAsMetadata should be enumerated before DIArgList");759assert(MetadataMap[VAM].F == F &&760"Expected LocalAsMetadata in the same function");761} else {762assert(isa<ConstantAsMetadata>(VAM) &&763"Expected LocalAsMetadata or ConstantAsMetadata");764assert(ValueMap.count(VAM->getValue()) &&765"Constant should be enumerated beforeDIArgList");766EnumerateMetadata(F, VAM);767}768}769770MDs.push_back(ArgList);771Index.F = F;772Index.ID = MDs.size();773}774775static unsigned getMetadataTypeOrder(const Metadata *MD) {776// Strings are emitted in bulk and must come first.777if (isa<MDString>(MD))778return 0;779780// ConstantAsMetadata doesn't reference anything. We may as well shuffle it781// to the front since we can detect it.782auto *N = dyn_cast<MDNode>(MD);783if (!N)784return 1;785786// The reader is fast forward references for distinct node operands, but slow787// when uniqued operands are unresolved.788return N->isDistinct() ? 2 : 3;789}790791void ValueEnumerator::organizeMetadata() {792assert(MetadataMap.size() == MDs.size() &&793"Metadata map and vector out of sync");794795if (MDs.empty())796return;797798// Copy out the index information from MetadataMap in order to choose a new799// order.800SmallVector<MDIndex, 64> Order;801Order.reserve(MetadataMap.size());802for (const Metadata *MD : MDs)803Order.push_back(MetadataMap.lookup(MD));804805// Partition:806// - by function, then807// - by isa<MDString>808// and then sort by the original/current ID. Since the IDs are guaranteed to809// be unique, the result of llvm::sort will be deterministic. There's no need810// for std::stable_sort.811llvm::sort(Order, [this](MDIndex LHS, MDIndex RHS) {812return std::make_tuple(LHS.F, getMetadataTypeOrder(LHS.get(MDs)), LHS.ID) <813std::make_tuple(RHS.F, getMetadataTypeOrder(RHS.get(MDs)), RHS.ID);814});815816// Rebuild MDs, index the metadata ranges for each function in FunctionMDs,817// and fix up MetadataMap.818std::vector<const Metadata *> OldMDs;819MDs.swap(OldMDs);820MDs.reserve(OldMDs.size());821for (unsigned I = 0, E = Order.size(); I != E && !Order[I].F; ++I) {822auto *MD = Order[I].get(OldMDs);823MDs.push_back(MD);824MetadataMap[MD].ID = I + 1;825if (isa<MDString>(MD))826++NumMDStrings;827}828829// Return early if there's nothing for the functions.830if (MDs.size() == Order.size())831return;832833// Build the function metadata ranges.834MDRange R;835FunctionMDs.reserve(OldMDs.size());836unsigned PrevF = 0;837for (unsigned I = MDs.size(), E = Order.size(), ID = MDs.size(); I != E;838++I) {839unsigned F = Order[I].F;840if (!PrevF) {841PrevF = F;842} else if (PrevF != F) {843R.Last = FunctionMDs.size();844std::swap(R, FunctionMDInfo[PrevF]);845R.First = FunctionMDs.size();846847ID = MDs.size();848PrevF = F;849}850851auto *MD = Order[I].get(OldMDs);852FunctionMDs.push_back(MD);853MetadataMap[MD].ID = ++ID;854if (isa<MDString>(MD))855++R.NumStrings;856}857R.Last = FunctionMDs.size();858FunctionMDInfo[PrevF] = R;859}860861void ValueEnumerator::incorporateFunctionMetadata(const Function &F) {862NumModuleMDs = MDs.size();863864auto R = FunctionMDInfo.lookup(getValueID(&F) + 1);865NumMDStrings = R.NumStrings;866MDs.insert(MDs.end(), FunctionMDs.begin() + R.First,867FunctionMDs.begin() + R.Last);868}869870void ValueEnumerator::EnumerateValue(const Value *V) {871assert(!V->getType()->isVoidTy() && "Can't insert void values!");872assert(!isa<MetadataAsValue>(V) && "EnumerateValue doesn't handle Metadata!");873874// Check to see if it's already in!875unsigned &ValueID = ValueMap[V];876if (ValueID) {877// Increment use count.878Values[ValueID - 1].second++;879return;880}881882if (auto *GO = dyn_cast<GlobalObject>(V))883if (const Comdat *C = GO->getComdat())884Comdats.insert(C);885886// Enumerate the type of this value.887EnumerateType(V->getType());888889if (const Constant *C = dyn_cast<Constant>(V)) {890if (isa<GlobalValue>(C)) {891// Initializers for globals are handled explicitly elsewhere.892} else if (C->getNumOperands()) {893// If a constant has operands, enumerate them. This makes sure that if a894// constant has uses (for example an array of const ints), that they are895// inserted also.896897// We prefer to enumerate them with values before we enumerate the user898// itself. This makes it more likely that we can avoid forward references899// in the reader. We know that there can be no cycles in the constants900// graph that don't go through a global variable.901for (User::const_op_iterator I = C->op_begin(), E = C->op_end(); I != E;902++I)903if (!isa<BasicBlock>(*I)) // Don't enumerate BB operand to BlockAddress.904EnumerateValue(*I);905if (auto *CE = dyn_cast<ConstantExpr>(C)) {906if (CE->getOpcode() == Instruction::ShuffleVector)907EnumerateValue(CE->getShuffleMaskForBitcode());908if (auto *GEP = dyn_cast<GEPOperator>(CE))909EnumerateType(GEP->getSourceElementType());910}911912// Finally, add the value. Doing this could make the ValueID reference be913// dangling, don't reuse it.914Values.push_back(std::make_pair(V, 1U));915ValueMap[V] = Values.size();916return;917}918}919920// Add the value.921Values.push_back(std::make_pair(V, 1U));922ValueID = Values.size();923}924925void ValueEnumerator::EnumerateType(Type *Ty) {926unsigned *TypeID = &TypeMap[Ty];927928// We've already seen this type.929if (*TypeID)930return;931932// If it is a non-anonymous struct, mark the type as being visited so that we933// don't recursively visit it. This is safe because we allow forward934// references of these in the bitcode reader.935if (StructType *STy = dyn_cast<StructType>(Ty))936if (!STy->isLiteral())937*TypeID = ~0U;938939// Enumerate all of the subtypes before we enumerate this type. This ensures940// that the type will be enumerated in an order that can be directly built.941for (Type *SubTy : Ty->subtypes())942EnumerateType(SubTy);943944// Refresh the TypeID pointer in case the table rehashed.945TypeID = &TypeMap[Ty];946947// Check to see if we got the pointer another way. This can happen when948// enumerating recursive types that hit the base case deeper than they start.949//950// If this is actually a struct that we are treating as forward ref'able,951// then emit the definition now that all of its contents are available.952if (*TypeID && *TypeID != ~0U)953return;954955// Add this type now that its contents are all happily enumerated.956Types.push_back(Ty);957958*TypeID = Types.size();959}960961// Enumerate the types for the specified value. If the value is a constant,962// walk through it, enumerating the types of the constant.963void ValueEnumerator::EnumerateOperandType(const Value *V) {964EnumerateType(V->getType());965966assert(!isa<MetadataAsValue>(V) && "Unexpected metadata operand");967968const Constant *C = dyn_cast<Constant>(V);969if (!C)970return;971972// If this constant is already enumerated, ignore it, we know its type must973// be enumerated.974if (ValueMap.count(C))975return;976977// This constant may have operands, make sure to enumerate the types in978// them.979for (const Value *Op : C->operands()) {980// Don't enumerate basic blocks here, this happens as operands to981// blockaddress.982if (isa<BasicBlock>(Op))983continue;984985EnumerateOperandType(Op);986}987if (auto *CE = dyn_cast<ConstantExpr>(C)) {988if (CE->getOpcode() == Instruction::ShuffleVector)989EnumerateOperandType(CE->getShuffleMaskForBitcode());990if (CE->getOpcode() == Instruction::GetElementPtr)991EnumerateType(cast<GEPOperator>(CE)->getSourceElementType());992}993}994995void ValueEnumerator::EnumerateAttributes(AttributeList PAL) {996if (PAL.isEmpty())997return; // null is always 0.998999// Do a lookup.1000unsigned &Entry = AttributeListMap[PAL];1001if (Entry == 0) {1002// Never saw this before, add it.1003AttributeLists.push_back(PAL);1004Entry = AttributeLists.size();1005}10061007// Do lookups for all attribute groups.1008for (unsigned i : PAL.indexes()) {1009AttributeSet AS = PAL.getAttributes(i);1010if (!AS.hasAttributes())1011continue;1012IndexAndAttrSet Pair = {i, AS};1013unsigned &Entry = AttributeGroupMap[Pair];1014if (Entry == 0) {1015AttributeGroups.push_back(Pair);1016Entry = AttributeGroups.size();10171018for (Attribute Attr : AS) {1019if (Attr.isTypeAttribute())1020EnumerateType(Attr.getValueAsType());1021}1022}1023}1024}10251026void ValueEnumerator::incorporateFunction(const Function &F) {1027InstructionCount = 0;1028NumModuleValues = Values.size();10291030// Add global metadata to the function block. This doesn't include1031// LocalAsMetadata.1032incorporateFunctionMetadata(F);10331034// Adding function arguments to the value table.1035for (const auto &I : F.args()) {1036EnumerateValue(&I);1037if (I.hasAttribute(Attribute::ByVal))1038EnumerateType(I.getParamByValType());1039else if (I.hasAttribute(Attribute::StructRet))1040EnumerateType(I.getParamStructRetType());1041else if (I.hasAttribute(Attribute::ByRef))1042EnumerateType(I.getParamByRefType());1043}1044FirstFuncConstantID = Values.size();10451046// Add all function-level constants to the value table.1047for (const BasicBlock &BB : F) {1048for (const Instruction &I : BB) {1049for (const Use &OI : I.operands()) {1050if ((isa<Constant>(OI) && !isa<GlobalValue>(OI)) || isa<InlineAsm>(OI))1051EnumerateValue(OI);1052}1053if (auto *SVI = dyn_cast<ShuffleVectorInst>(&I))1054EnumerateValue(SVI->getShuffleMaskForBitcode());1055}1056BasicBlocks.push_back(&BB);1057ValueMap[&BB] = BasicBlocks.size();1058}10591060// Add the function's parameter attributes so they are available for use in1061// the function's instruction.1062EnumerateAttributes(F.getAttributes());10631064FirstInstID = Values.size();10651066SmallVector<LocalAsMetadata *, 8> FnLocalMDVector;1067SmallVector<DIArgList *, 8> ArgListMDVector;1068// Add all of the instructions.1069for (const BasicBlock &BB : F) {1070for (const Instruction &I : BB) {1071for (const Use &OI : I.operands()) {1072if (auto *MD = dyn_cast<MetadataAsValue>(&OI)) {1073if (auto *Local = dyn_cast<LocalAsMetadata>(MD->getMetadata())) {1074// Enumerate metadata after the instructions they might refer to.1075FnLocalMDVector.push_back(Local);1076} else if (auto *ArgList = dyn_cast<DIArgList>(MD->getMetadata())) {1077ArgListMDVector.push_back(ArgList);1078for (ValueAsMetadata *VMD : ArgList->getArgs()) {1079if (auto *Local = dyn_cast<LocalAsMetadata>(VMD)) {1080// Enumerate metadata after the instructions they might refer1081// to.1082FnLocalMDVector.push_back(Local);1083}1084}1085}1086}1087}10881089if (!I.getType()->isVoidTy())1090EnumerateValue(&I);1091}1092}10931094// Add all of the function-local metadata.1095for (unsigned i = 0, e = FnLocalMDVector.size(); i != e; ++i) {1096// At this point, every local values have been incorporated, we shouldn't1097// have a metadata operand that references a value that hasn't been seen.1098assert(ValueMap.count(FnLocalMDVector[i]->getValue()) &&1099"Missing value for metadata operand");1100EnumerateFunctionLocalMetadata(F, FnLocalMDVector[i]);1101}1102// DIArgList entries must come after function-local metadata, as it is not1103// possible to forward-reference them.1104for (const DIArgList *ArgList : ArgListMDVector)1105EnumerateFunctionLocalListMetadata(F, ArgList);1106}11071108void ValueEnumerator::purgeFunction() {1109/// Remove purged values from the ValueMap.1110for (unsigned i = NumModuleValues, e = Values.size(); i != e; ++i)1111ValueMap.erase(Values[i].first);1112for (unsigned i = NumModuleMDs, e = MDs.size(); i != e; ++i)1113MetadataMap.erase(MDs[i]);1114for (const BasicBlock *BB : BasicBlocks)1115ValueMap.erase(BB);11161117Values.resize(NumModuleValues);1118MDs.resize(NumModuleMDs);1119BasicBlocks.clear();1120NumMDStrings = 0;1121}11221123static void IncorporateFunctionInfoGlobalBBIDs(1124const Function *F, DenseMap<const BasicBlock *, unsigned> &IDMap) {1125unsigned Counter = 0;1126for (const BasicBlock &BB : *F)1127IDMap[&BB] = ++Counter;1128}11291130/// getGlobalBasicBlockID - This returns the function-specific ID for the1131/// specified basic block. This is relatively expensive information, so it1132/// should only be used by rare constructs such as address-of-label.1133unsigned ValueEnumerator::getGlobalBasicBlockID(const BasicBlock *BB) const {1134unsigned &Idx = GlobalBasicBlockIDs[BB];1135if (Idx != 0)1136return Idx - 1;11371138IncorporateFunctionInfoGlobalBBIDs(BB->getParent(), GlobalBasicBlockIDs);1139return getGlobalBasicBlockID(BB);1140}11411142uint64_t ValueEnumerator::computeBitsRequiredForTypeIndices() const {1143return Log2_32_Ceil(getTypes().size() + 1);1144}114511461147