16#include "llvm/Config/llvm-config.h"
55 DenseMap<const Value *, std::pair<unsigned, bool>> IDs;
56 unsigned LastGlobalConstantID = 0;
57 unsigned LastGlobalValueID = 0;
61 bool isGlobalConstant(
unsigned ID)
const {
62 return ID <= LastGlobalConstantID;
65 bool isGlobalValue(
unsigned ID)
const {
66 return ID <= LastGlobalValueID && !isGlobalConstant(
ID);
69 unsigned size()
const {
return IDs.
size(); }
72 std::pair<unsigned, bool>
lookup(
const Value *V)
const {
76 void index(
const Value *V) {
78 unsigned ID = IDs.
size() + 1;
86 if (OM.lookup(V).first)
91 for (
const Value *
Op :
C->operands())
95 if (CE->getOpcode() == Instruction::ShuffleVector)
96 orderValue(CE->getShuffleMaskForBitcode(), OM);
116 if (
G.hasInitializer())
126 for (
const Use &U :
F.operands())
136 auto orderConstantValue = [&OM](
const Value *V) {
141 if (
F.isDeclaration())
145 for (
const Value *V :
I.operands()) {
147 if (
const auto *VAM =
149 orderConstantValue(VAM->getValue());
150 }
else if (
const auto *AL =
152 for (
const auto *VAM : AL->getArgs())
153 orderConstantValue(VAM->getValue());
158 OM.LastGlobalConstantID = OM.size();
176 OM.LastGlobalValueID = OM.size();
179 if (
F.isDeclaration())
190 for (
const Value *
Op :
I.operands())
195 orderValue(SVI->getShuffleMaskForBitcode(), OM);
197 for (
const auto &Case :
SI->cases())
209 unsigned ID,
const OrderMap &OM,
212 using Entry = std::pair<const Use *, unsigned>;
216 if (OM.lookup(U.getUser()).first)
217 List.
push_back(std::make_pair(&U, List.size()));
223 bool IsGlobalValue = OM.isGlobalValue(
ID);
224 llvm::sort(List, [&](
const Entry &L,
const Entry &R) {
225 const Use *LU = L.first;
226 const Use *RU = R.first;
230 auto LID = OM.lookup(LU->getUser()).first;
231 auto RID = OM.lookup(RU->getUser()).first;
239 if (OM.isGlobalValue(LID) && OM.isGlobalValue(RID)) {
241 return LU->getOperandNo() > RU->getOperandNo();
263 return LU->getOperandNo() < RU->getOperandNo();
264 return LU->getOperandNo() > RU->getOperandNo();
272 Stack.emplace_back(V,
F, List.size());
273 assert(List.size() == Stack.back().Shuffle.size() &&
"Wrong size");
274 for (
size_t I = 0,
E = List.size();
I !=
E; ++
I)
275 Stack.back().Shuffle[
I] = List[
I].second;
280 auto &IDPair = OM[V];
281 assert(IDPair.first &&
"Unmapped value");
287 IDPair.second =
true;
288 if (!V->use_empty() && std::next(V->use_begin()) != V->use_end())
293 if (
C->getNumOperands()) {
294 for (
const Value *
Op :
C->operands())
298 if (CE->getOpcode() == Instruction::ShuffleVector)
319 if (
F.isDeclaration())
327 for (
const Value *
Op :
I.operands())
350 if (
G.hasInitializer())
357 for (
const Use &U :
F.operands())
381 EnumerateAttributes(
F.getAttributes());
392 EnumerateValue(&GIF);
398 if (GV.hasInitializer())
399 EnumerateValue(GV.getInitializer());
402 if (GV.hasAttributes())
403 EnumerateAttributes(GV.getAttributesAsList(AttributeList::FunctionIndex));
408 EnumerateValue(GA.getAliasee());
412 EnumerateValue(GIF.getResolver());
416 for (
const Use &U :
F.operands())
417 EnumerateValue(U.get());
427 EnumerateValueSymbolTable(M.getValueSymbolTable());
428 EnumerateNamedMetadata(M);
433 GV.getAllMetadata(MDs);
434 for (
const auto &
I : MDs)
438 EnumerateMetadata(
nullptr,
I.second);
448 F.getAllMetadata(MDs);
449 for (
const auto &
I : MDs)
450 EnumerateMetadata(
F.isDeclaration() ?
nullptr : &
F,
I.second);
454 for (
const Use &
Op :
I.operands()) {
457 EnumerateOperandType(
Op);
467 for (
auto *VAM : AL->getArgs())
469 EnumerateMetadata(&
F, VAM);
473 EnumerateMetadata(&
F, MD->getMetadata());
483 EnumerateAttributes(
Call->getAttributes());
489 I.getAllMetadataOtherThanDebugLoc(MDs);
490 for (
unsigned i = 0, e = MDs.size(); i != e; ++i)
491 EnumerateMetadata(&
F, MDs[i].second);
497 EnumerateMetadata(&
F,
Op);
507 assert(
I != InstructionMap.
end() &&
"Instruction is not mapped!");
512 unsigned ComdatID = Comdats.
idFor(
C);
513 assert(ComdatID &&
"Comdat not found!");
518 InstructionMap[
I] = InstructionCount++;
526 assert(
I != ValueMap.end() &&
"Value not in slotcalculator!");
527 return I->second - 1;
530#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
540 const char *Name)
const {
541 OS <<
"Map Name: " <<
Name <<
"\n";
542 OS <<
"Size: " <<
Map.size() <<
"\n";
543 for (
const auto &
I : Map) {
546 OS <<
"Value: " <<
V->getName();
548 OS <<
"Value: [null]\n";
552 OS <<
" Uses(" <<
V->getNumUses() <<
"):";
553 for (
const Use &U :
V->uses()) {
554 if (&U != &*
V->use_begin())
557 OS <<
" " <<
U->getName();
566 const char *Name)
const {
567 OS <<
"Map Name: " <<
Name <<
"\n";
568 OS <<
"Size: " <<
Map.size() <<
"\n";
569 for (
const auto &
I : Map) {
571 OS <<
"Metadata: slot = " <<
I.second.ID <<
"\n";
572 OS <<
"Metadata: function = " <<
I.second.F <<
"\n";
580void ValueEnumerator::EnumerateValueSymbolTable(
const ValueSymbolTable &VST) {
583 EnumerateValue(
VI->getValue());
588void ValueEnumerator::EnumerateNamedMetadata(
const Module &M) {
589 for (
const auto &
I :
M.named_metadata())
590 EnumerateNamedMDNode(&
I);
593void ValueEnumerator::EnumerateNamedMDNode(
const NamedMDNode *MD) {
595 EnumerateMetadata(
nullptr, MD->
getOperand(i));
598unsigned ValueEnumerator::getMetadataFunctionID(
const Function *
F)
const {
603 EnumerateMetadata(getMetadataFunctionID(
F), MD);
606void ValueEnumerator::EnumerateFunctionLocalMetadata(
608 EnumerateFunctionLocalMetadata(getMetadataFunctionID(&
F),
Local);
611void ValueEnumerator::EnumerateFunctionLocalListMetadata(
613 EnumerateFunctionLocalListMetadata(getMetadataFunctionID(&
F), ArgList);
616void ValueEnumerator::dropFunctionFromMetadata(
617 MetadataMapType::value_type &FirstMD) {
620 auto &
Entry = MD.second;
636 while (!Worklist.
empty())
640 auto MD = MetadataMap.find(
Op);
641 if (MD != MetadataMap.end())
646void ValueEnumerator::EnumerateMetadata(
unsigned F,
const Metadata *MD) {
656 if (
const MDNode *
N = enumerateMetadataImpl(
F, MD))
657 Worklist.
push_back(std::make_pair(
N,
N->op_begin()));
659 while (!Worklist.
empty()) {
660 const MDNode *
N = Worklist.
back().first;
665 Worklist.
back().second,
N->op_end(),
666 [&](
const Metadata *MD) { return enumerateMetadataImpl(F, MD); });
667 if (
I !=
N->op_end()) {
669 Worklist.
back().second = ++
I;
672 if (
Op->isDistinct() && !
N->isDistinct())
682 MetadataMap[
N].ID = MDs.size();
686 if (Worklist.
empty() || Worklist.
back().first->isDistinct()) {
687 for (
const MDNode *
N : DelayedDistinctNodes)
688 Worklist.
push_back(std::make_pair(
N,
N->op_begin()));
689 DelayedDistinctNodes.clear();
694const MDNode *ValueEnumerator::enumerateMetadataImpl(
unsigned F,
701 "Invalid metadata kind");
703 auto Insertion = MetadataMap.insert(std::make_pair(MD, MDIndex(
F)));
704 MDIndex &
Entry = Insertion.first->second;
705 if (!Insertion.second) {
707 if (
Entry.hasDifferentFunction(
F))
708 dropFunctionFromMetadata(*Insertion.first);
718 Entry.ID = MDs.size();
722 EnumerateValue(
C->getValue());
729void ValueEnumerator::EnumerateFunctionLocalMetadata(
731 assert(
F &&
"Expected a function");
740 MDs.push_back(
Local);
742 Index.ID = MDs.size();
744 EnumerateValue(
Local->getValue());
749void ValueEnumerator::EnumerateFunctionLocalListMetadata(
751 assert(
F &&
"Expected a function");
754 MDIndex &
Index = MetadataMap[ArgList];
760 for (ValueAsMetadata *VAM : ArgList->
getArgs()) {
762 assert(MetadataMap.count(VAM) &&
763 "LocalAsMetadata should be enumerated before DIArgList");
765 "Expected LocalAsMetadata in the same function");
768 "Expected LocalAsMetadata or ConstantAsMetadata");
769 assert(ValueMap.count(VAM->getValue()) &&
770 "Constant should be enumerated beforeDIArgList");
771 EnumerateMetadata(
F, VAM);
775 MDs.push_back(ArgList);
777 Index.ID = MDs.size();
793 return N->isDistinct() ? 2 : 3;
796void ValueEnumerator::organizeMetadata() {
797 assert(MetadataMap.size() == MDs.size() &&
798 "Metadata map and vector out of sync");
806 Order.
reserve(MetadataMap.size());
823 std::vector<const Metadata *> OldMDs;
825 MDs.reserve(OldMDs.size());
826 for (
unsigned I = 0,
E = Order.
size();
I !=
E && !Order[
I].F; ++
I) {
827 auto *MD = Order[
I].get(OldMDs);
829 MetadataMap[MD].ID =
I + 1;
835 if (MDs.size() == Order.
size())
840 FunctionMDs.reserve(OldMDs.size());
842 for (
unsigned I = MDs.size(),
E = Order.
size(),
ID = MDs.size();
I !=
E;
844 unsigned F = Order[
I].F;
847 }
else if (PrevF !=
F) {
848 R.Last = FunctionMDs.size();
850 R.First = FunctionMDs.size();
856 auto *MD = Order[
I].get(OldMDs);
858 MetadataMap[MD].ID = ++
ID;
862 R.Last = FunctionMDs.size();
863 FunctionMDInfo[PrevF] =
R;
866void ValueEnumerator::incorporateFunctionMetadata(
const Function &
F) {
867 NumModuleMDs = MDs.size();
870 NumMDStrings =
R.NumStrings;
871 MDs.insert(MDs.end(), FunctionMDs.begin() +
R.First,
872 FunctionMDs.begin() +
R.Last);
875void ValueEnumerator::EnumerateValue(
const Value *V) {
876 assert(!
V->getType()->isVoidTy() &&
"Can't insert void values!");
880 unsigned &ValueID = ValueMap[
V];
883 Values[ValueID - 1].second++;
888 if (
const Comdat *
C = GO->getComdat())
892 EnumerateType(
V->getType());
897 }
else if (
C->getNumOperands()) {
911 if (
CE->getOpcode() == Instruction::ShuffleVector)
912 EnumerateValue(
CE->getShuffleMaskForBitcode());
914 EnumerateType(
GEP->getSourceElementType());
919 Values.push_back(std::make_pair(V, 1U));
920 ValueMap[
V] = Values.size();
926 Values.push_back(std::make_pair(V, 1U));
927 ValueID = Values.size();
930void ValueEnumerator::EnumerateType(
Type *Ty) {
931 unsigned *
TypeID = &TypeMap[Ty];
941 if (!STy->isLiteral())
947 EnumerateType(SubTy);
968void ValueEnumerator::EnumerateOperandType(
const Value *V) {
969 EnumerateType(
V->getType());
979 if (ValueMap.count(
C))
984 for (
const Value *
Op :
C->operands()) {
990 EnumerateOperandType(
Op);
993 if (
CE->getOpcode() == Instruction::ShuffleVector)
994 EnumerateOperandType(
CE->getShuffleMaskForBitcode());
995 if (
CE->getOpcode() == Instruction::GetElementPtr)
1000void ValueEnumerator::EnumerateAttributes(AttributeList PAL) {
1005 unsigned &
Entry = AttributeListMap[PAL];
1008 AttributeLists.push_back(PAL);
1009 Entry = AttributeLists.size();
1013 for (
unsigned i : PAL.indexes()) {
1014 AttributeSet AS = PAL.getAttributes(i);
1018 unsigned &
Entry = AttributeGroupMap[Pair];
1020 AttributeGroups.push_back(Pair);
1021 Entry = AttributeGroups.size();
1024 if (Attr.isTypeAttribute())
1025 EnumerateType(Attr.getValueAsType());
1032 InstructionCount = 0;
1033 NumModuleValues = Values.size();
1037 incorporateFunctionMetadata(
F);
1040 for (
const auto &
I :
F.args()) {
1042 if (
I.hasAttribute(Attribute::ByVal))
1043 EnumerateType(
I.getParamByValType());
1044 else if (
I.hasAttribute(Attribute::StructRet))
1045 EnumerateType(
I.getParamStructRetType());
1046 else if (
I.hasAttribute(Attribute::ByRef))
1047 EnumerateType(
I.getParamByRefType());
1049 FirstFuncConstantID = Values.size();
1052 for (
const BasicBlock &BB :
F) {
1053 for (
const Instruction &
I : BB) {
1054 for (
const Use &OI :
I.operands()) {
1059 EnumerateValue(SVI->getShuffleMaskForBitcode());
1061 for (
const auto &Case :
SI->cases())
1062 EnumerateValue(Case.getCaseValue());
1065 BasicBlocks.push_back(&BB);
1066 ValueMap[&BB] = BasicBlocks.size();
1071 EnumerateAttributes(
F.getAttributes());
1073 FirstInstID = Values.size();
1078 for (
const BasicBlock &BB :
F) {
1079 for (
const Instruction &
I : BB) {
1080 for (
const Use &OI :
I.operands()) {
1087 for (ValueAsMetadata *VMD : ArgList->
getArgs()) {
1098 if (!
I.getType()->isVoidTy())
1104 for (
unsigned i = 0, e = FnLocalMDVector.
size(); i != e; ++i) {
1107 assert(ValueMap.count(FnLocalMDVector[i]->getValue()) &&
1108 "Missing value for metadata operand");
1109 EnumerateFunctionLocalMetadata(
F, FnLocalMDVector[i]);
1113 for (
const DIArgList *ArgList : ArgListMDVector)
1114 EnumerateFunctionLocalListMetadata(
F, ArgList);
1119 for (
unsigned i = NumModuleValues, e = Values.size(); i != e; ++i)
1120 ValueMap.erase(Values[i].first);
1121 for (
unsigned i = NumModuleMDs, e = MDs.size(); i != e; ++i)
1122 MetadataMap.erase(MDs[i]);
1123 for (
const BasicBlock *BB : BasicBlocks)
1126 Values.resize(NumModuleValues);
1127 MDs.resize(NumModuleMDs);
1128 BasicBlocks.clear();
1134 unsigned Counter = 0;
1136 IDMap[&BB] = ++Counter;
1143 unsigned &Idx = GlobalBasicBlockIDs[BB];
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MapVector< const Value *, unsigned > OrderMap
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
static void predictValueUseListOrderImpl(const Value *V, const Function *F, unsigned ID, const OrderMap &OM, UseListOrderStack &Stack)
static void orderValue(const Value *V, OrderMap &OM)
static void predictValueUseListOrder(const Value *V, const Function *F, OrderMap &OM, UseListOrderStack &Stack)
static UseListOrderStack predictUseListOrder(const Module &M)
static OrderMap orderModule(const Module &M)
This file contains the declaration of the GlobalIFunc class, which represents a single indirect funct...
Module.h This file contains the declarations for the Module class.
This defines the Use class.
static bool lookup(const GsymReader &GR, DataExtractor &Data, uint64_t &Offset, uint64_t BaseAddr, uint64_t Addr, SourceLocations &SrcLocs, llvm::Error &Err)
A Lookup helper functions.
This file defines the SmallVector class.
static unsigned getMetadataTypeOrder(const Metadata *MD)
static UseListOrderStack predictUseListOrder(const Module &M)
static void IncorporateFunctionInfoGlobalBBIDs(const Function *F, DenseMap< const BasicBlock *, unsigned > &IDMap)
This class represents an incoming formal argument to a Function.
bool hasAttributes() const
Return true if attributes exists in this set.
LLVM Basic Block Representation.
const Function * getParent() const
Return the enclosing method, or null if none.
This is an important base class in LLVM.
List of ValueAsMetadata, to be used as an argument to a dbg.value intrinsic.
ArrayRef< ValueAsMetadata * > getArgs() const
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
iterator find(const_arg_type_t< KeyT > Val)
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT, true > const_iterator
const MDOperand * op_iterator
unsigned & operator[](const const Value *&Key)
A Module instance is used to store all the information related to an LLVM module.
LLVM_ABI MDNode * getOperand(unsigned i) const
LLVM_ABI unsigned getNumOperands() const
void reserve(size_type N)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
The instances of the Type class are immutable: once they are created, they are never changed.
static LLVM_ABI Type * getMetadataTy(LLVMContext &C)
ArrayRef< Type * > subtypes() const
static LLVM_ABI TypedPointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
unsigned idFor(const T &Entry) const
idFor - return the ID for an existing entry.
A Use represents the edge between a Value definition and its users.
const Use * const_op_iterator
unsigned getMetadataID(const Metadata *MD) const
void print(raw_ostream &OS, const ValueMapType &Map, const char *Name) const
unsigned getInstructionID(const Instruction *I) const
void incorporateFunction(const Function &F)
incorporateFunction/purgeFunction - If you'd like to deal with a function, use these two methods to g...
ValueEnumerator(const Module &M, bool ShouldPreserveUseListOrder)
unsigned getComdatID(const Comdat *C) const
uint64_t computeBitsRequiredForTypeIndices() const
unsigned getValueID(const Value *V) const
unsigned getGlobalBasicBlockID(const BasicBlock *BB) const
getGlobalBasicBlockID - This returns the function-specific ID for the specified basic block.
void setInstructionID(const Instruction *I)
std::pair< unsigned, AttributeSet > IndexAndAttrSet
Attribute groups as encoded in bitcode are almost AttributeSets, but they include the AttributeList i...
const TypeList & getTypes() const
This class provides a symbol table of name/value pairs.
ValueMap::const_iterator const_iterator
A const_iterator over a ValueMap.
iterator end()
Get an iterator to the end of the symbol table.
iterator begin()
Get an iterator that from the beginning of the symbol table.
LLVM Value Representation.
iterator_range< use_iterator > uses()
void EnumerateType(Type *T)
UseListOrderStack UseListOrders
This class implements an extremely fast bulk output stream that can only output to a stream.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
@ C
The default llvm calling convention, compatible with C.
@ CE
Windows NT (Windows on ARM)
This is an optimization pass for GlobalISel generic memory operations.
unsigned Log2_32_Ceil(uint32_t Value)
Return the ceil log base 2 of the specified value, 32 if the value is zero.
FunctionAddr VTableAddr Value
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
auto reverse(ContainerTy &&C)
void sort(IteratorTy Start, IteratorTy End)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
bool is_sorted(R &&Range, Compare C)
Wrapper function around std::is_sorted to check if elements in a range R are sorted with respect to a...
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
std::vector< UseListOrder > UseListOrderStack
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
DWARFExpression::Operation Op
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Function object to check whether the second component of a container supported by std::get (like std:...