Path: blob/main/contrib/llvm-project/llvm/lib/Target/DirectX/DXILWriter/DXILValueEnumerator.h
35294 views
//===- DirectX/DXILWriter/ValueEnumerator.h - Number values -----*- C++ -*-===//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 class gives values and types Unique ID's.9// Forked from lib/Bitcode/Writer10//11//===----------------------------------------------------------------------===//1213#ifndef LLVM_DXILWRITER_VALUEENUMERATOR_H14#define LLVM_DXILWRITER_VALUEENUMERATOR_H1516#include "llvm/ADT/ArrayRef.h"17#include "llvm/ADT/DenseMap.h"18#include "llvm/ADT/UniqueVector.h"19#include "llvm/IR/Attributes.h"20#include "llvm/IR/UseListOrder.h"21#include <cassert>22#include <cstdint>23#include <utility>24#include <vector>2526namespace llvm {2728class BasicBlock;29class Comdat;30class DIArgList;31class Function;32class Instruction;33class LocalAsMetadata;34class MDNode;35class Metadata;36class Module;37class NamedMDNode;38class raw_ostream;39class Type;40class Value;41class ValueSymbolTable;4243namespace dxil {4445class ValueEnumerator {46public:47using TypeList = std::vector<Type *>;4849// For each value, we remember its Value* and occurrence frequency.50using ValueList = std::vector<std::pair<const Value *, unsigned>>;5152/// Attribute groups as encoded in bitcode are almost AttributeSets, but they53/// include the AttributeList index, so we have to track that in our map.54using IndexAndAttrSet = std::pair<unsigned, AttributeSet>;5556UseListOrderStack UseListOrders;5758private:59using TypeMapType = DenseMap<Type *, unsigned>;60TypeMapType TypeMap;61TypeList Types;6263using ValueMapType = DenseMap<const Value *, unsigned>;64ValueMapType ValueMap;65ValueList Values;6667using ComdatSetType = UniqueVector<const Comdat *>;68ComdatSetType Comdats;6970std::vector<const Metadata *> MDs;71std::vector<const Metadata *> FunctionMDs;7273/// Index of information about a piece of metadata.74struct MDIndex {75unsigned F = 0; ///< The ID of the function for this metadata, if any.76unsigned ID = 0; ///< The implicit ID of this metadata in bitcode.7778MDIndex() = default;79explicit MDIndex(unsigned F) : F(F) {}8081/// Check if this has a function tag, and it's different from NewF.82bool hasDifferentFunction(unsigned NewF) const { return F && F != NewF; }8384/// Fetch the MD this references out of the given metadata array.85const Metadata *get(ArrayRef<const Metadata *> MDs) const {86assert(ID && "Expected non-zero ID");87assert(ID <= MDs.size() && "Expected valid ID");88return MDs[ID - 1];89}90};9192using MetadataMapType = DenseMap<const Metadata *, MDIndex>;93MetadataMapType MetadataMap;9495/// Range of metadata IDs, as a half-open range.96struct MDRange {97unsigned First = 0;98unsigned Last = 0;99100/// Number of strings in the prefix of the metadata range.101unsigned NumStrings = 0;102103MDRange() = default;104explicit MDRange(unsigned First) : First(First) {}105};106SmallDenseMap<unsigned, MDRange, 1> FunctionMDInfo;107108using AttributeGroupMapType = DenseMap<IndexAndAttrSet, unsigned>;109AttributeGroupMapType AttributeGroupMap;110std::vector<IndexAndAttrSet> AttributeGroups;111112using AttributeListMapType = DenseMap<AttributeList, unsigned>;113AttributeListMapType AttributeListMap;114std::vector<AttributeList> AttributeLists;115116/// GlobalBasicBlockIDs - This map memoizes the basic block ID's referenced by117/// the "getGlobalBasicBlockID" method.118mutable DenseMap<const BasicBlock *, unsigned> GlobalBasicBlockIDs;119120using InstructionMapType = DenseMap<const Instruction *, unsigned>;121InstructionMapType InstructionMap;122unsigned InstructionCount;123124/// BasicBlocks - This contains all the basic blocks for the currently125/// incorporated function. Their reverse mapping is stored in ValueMap.126std::vector<const BasicBlock *> BasicBlocks;127128/// When a function is incorporated, this is the size of the Values list129/// before incorporation.130unsigned NumModuleValues;131132/// When a function is incorporated, this is the size of the Metadatas list133/// before incorporation.134unsigned NumModuleMDs = 0;135unsigned NumMDStrings = 0;136137unsigned FirstFuncConstantID;138unsigned FirstInstID;139140public:141ValueEnumerator(const Module &M, Type *PrefixType);142ValueEnumerator(const ValueEnumerator &) = delete;143ValueEnumerator &operator=(const ValueEnumerator &) = delete;144145void dump() const;146void print(raw_ostream &OS, const ValueMapType &Map, const char *Name) const;147void print(raw_ostream &OS, const MetadataMapType &Map,148const char *Name) const;149150unsigned getValueID(const Value *V) const;151152unsigned getMetadataID(const Metadata *MD) const {153auto ID = getMetadataOrNullID(MD);154assert(ID != 0 && "Metadata not in slotcalculator!");155return ID - 1;156}157158unsigned getMetadataOrNullID(const Metadata *MD) const {159return MetadataMap.lookup(MD).ID;160}161162unsigned numMDs() const { return MDs.size(); }163164unsigned getTypeID(Type *T) const {165TypeMapType::const_iterator I = TypeMap.find(T);166assert(I != TypeMap.end() && "Type not in ValueEnumerator!");167return I->second - 1;168}169170unsigned getInstructionID(const Instruction *I) const;171void setInstructionID(const Instruction *I);172173unsigned getAttributeListID(AttributeList PAL) const {174if (PAL.isEmpty())175return 0; // Null maps to zero.176AttributeListMapType::const_iterator I = AttributeListMap.find(PAL);177assert(I != AttributeListMap.end() && "Attribute not in ValueEnumerator!");178return I->second;179}180181unsigned getAttributeGroupID(IndexAndAttrSet Group) const {182if (!Group.second.hasAttributes())183return 0; // Null maps to zero.184AttributeGroupMapType::const_iterator I = AttributeGroupMap.find(Group);185assert(I != AttributeGroupMap.end() && "Attribute not in ValueEnumerator!");186return I->second;187}188189/// getFunctionConstantRange - Return the range of values that corresponds to190/// function-local constants.191void getFunctionConstantRange(unsigned &Start, unsigned &End) const {192Start = FirstFuncConstantID;193End = FirstInstID;194}195196const ValueList &getValues() const { return Values; }197198/// Check whether the current block has any metadata to emit.199bool hasMDs() const { return NumModuleMDs < MDs.size(); }200201/// Get the MDString metadata for this block.202ArrayRef<const Metadata *> getMDStrings() const {203return ArrayRef(MDs).slice(NumModuleMDs, NumMDStrings);204}205206/// Get the non-MDString metadata for this block.207ArrayRef<const Metadata *> getNonMDStrings() const {208return ArrayRef(MDs).slice(NumModuleMDs).slice(NumMDStrings);209}210211const TypeList &getTypes() const { return Types; }212213const std::vector<const BasicBlock *> &getBasicBlocks() const {214return BasicBlocks;215}216217const std::vector<AttributeList> &getAttributeLists() const {218return AttributeLists;219}220221const std::vector<IndexAndAttrSet> &getAttributeGroups() const {222return AttributeGroups;223}224225const ComdatSetType &getComdats() const { return Comdats; }226unsigned getComdatID(const Comdat *C) const;227228/// getGlobalBasicBlockID - This returns the function-specific ID for the229/// specified basic block. This is relatively expensive information, so it230/// should only be used by rare constructs such as address-of-label.231unsigned getGlobalBasicBlockID(const BasicBlock *BB) const;232233/// incorporateFunction/purgeFunction - If you'd like to deal with a function,234/// use these two methods to get its data into the ValueEnumerator!235void incorporateFunction(const Function &F);236237void purgeFunction();238uint64_t computeBitsRequiredForTypeIndices() const;239240void EnumerateType(Type *T);241242private:243244/// Reorder the reachable metadata.245///246/// This is not just an optimization, but is mandatory for emitting MDString247/// correctly.248void organizeMetadata();249250/// Drop the function tag from the transitive operands of the given node.251void dropFunctionFromMetadata(MetadataMapType::value_type &FirstMD);252253/// Incorporate the function metadata.254///255/// This should be called before enumerating LocalAsMetadata for the256/// function.257void incorporateFunctionMetadata(const Function &F);258259/// Enumerate a single instance of metadata with the given function tag.260///261/// If \c MD has already been enumerated, check that \c F matches its262/// function tag. If not, call \a dropFunctionFromMetadata().263///264/// Otherwise, mark \c MD as visited. Assign it an ID, or just return it if265/// it's an \a MDNode.266const MDNode *enumerateMetadataImpl(unsigned F, const Metadata *MD);267268unsigned getMetadataFunctionID(const Function *F) const;269270/// Enumerate reachable metadata in (almost) post-order.271///272/// Enumerate all the metadata reachable from MD. We want to minimize the273/// cost of reading bitcode records, and so the primary consideration is that274/// operands of uniqued nodes are resolved before the nodes are read. This275/// avoids re-uniquing them on the context and factors away RAUW support.276///277/// This algorithm guarantees that subgraphs of uniqued nodes are in278/// post-order. Distinct subgraphs reachable only from a single uniqued node279/// will be in post-order.280///281/// \note The relative order of a distinct and uniqued node is irrelevant.282/// \a organizeMetadata() will later partition distinct nodes ahead of283/// uniqued ones.284///{285void EnumerateMetadata(const Function *F, const Metadata *MD);286void EnumerateMetadata(unsigned F, const Metadata *MD);287///}288289void EnumerateFunctionLocalMetadata(const Function &F,290const LocalAsMetadata *Local);291void EnumerateFunctionLocalMetadata(unsigned F, const LocalAsMetadata *Local);292void EnumerateFunctionLocalListMetadata(const Function &F,293const DIArgList *ArgList);294void EnumerateFunctionLocalListMetadata(unsigned F, const DIArgList *Arglist);295void EnumerateNamedMDNode(const NamedMDNode *NMD);296void EnumerateValue(const Value *V);297void EnumerateOperandType(const Value *V);298void EnumerateAttributes(AttributeList PAL);299300void EnumerateValueSymbolTable(const ValueSymbolTable &ST);301void EnumerateNamedMetadata(const Module &M);302};303304} // end namespace dxil305} // end namespace llvm306307#endif // LLVM_DXILWRITER_VALUEENUMERATOR_H308309310