17#include "llvm/Config/llvm-config.h"
56 DenseMap<const Value *, std::pair<unsigned, bool>> IDs;
57 unsigned LastGlobalConstantID = 0;
58 unsigned LastGlobalValueID = 0;
62 bool isGlobalConstant(
unsigned ID)
const {
63 return ID <= LastGlobalConstantID;
66 bool isGlobalValue(
unsigned ID)
const {
67 return ID <= LastGlobalValueID && !isGlobalConstant(
ID);
70 unsigned size()
const {
return IDs.
size(); }
73 std::pair<unsigned, bool>
lookup(
const Value *V)
const {
77 void index(
const Value *V) {
79 unsigned ID = IDs.
size() + 1;
87 if (OM.lookup(V).first)
92 for (
const Value *
Op :
C->operands())
96 if (CE->getOpcode() == Instruction::ShuffleVector)
97 orderValue(CE->getShuffleMaskForBitcode(), OM);
117 if (
G.hasInitializer())
127 for (
const Use &U :
F.operands())
137 auto orderConstantValue = [&OM](
const Value *V) {
143 if (
F.isDeclaration())
148 for (
const Value *V :
I.operands()) {
150 if (
const auto *VAM =
152 orderConstantValue(VAM->getValue());
153 }
else if (
const auto *AL =
155 for (
const auto *VAM : AL->getArgs())
156 orderConstantValue(VAM->getValue());
163 OM.LastGlobalConstantID = OM.size();
183 OM.LastGlobalValueID = OM.size();
187 if (
F.isDeclaration())
199 for (
const Value *
Op :
I.operands())
204 orderValue(SVI->getShuffleMaskForBitcode(), OM);
206 for (
const auto &Case :
SI->cases())
221 unsigned ID,
const OrderMap &OM,
224 using Entry = std::pair<const Use *, unsigned>;
228 if (OM.lookup(U.getUser()).first)
229 List.
push_back(std::make_pair(&U, List.size()));
235 bool IsGlobalValue = OM.isGlobalValue(
ID);
236 llvm::sort(List, [&](
const Entry &L,
const Entry &R) {
237 const Use *LU = L.first;
238 const Use *RU = R.first;
242 auto LID = OM.lookup(LU->getUser()).first;
243 auto RID = OM.lookup(RU->getUser()).first;
251 if (OM.isGlobalValue(LID) && OM.isGlobalValue(RID)) {
253 return LU->getOperandNo() > RU->getOperandNo();
275 return LU->getOperandNo() < RU->getOperandNo();
276 return LU->getOperandNo() > RU->getOperandNo();
284 Stack.emplace_back(V,
F, List.size());
285 assert(List.size() == Stack.back().Shuffle.size() &&
"Wrong size");
286 for (
size_t I = 0,
E = List.size();
I !=
E; ++
I)
287 Stack.back().Shuffle[
I] = List[
I].second;
292 auto &IDPair = OM[V];
293 assert(IDPair.first &&
"Unmapped value");
299 IDPair.second =
true;
300 if (!V->use_empty() && std::next(V->use_begin()) != V->use_end())
305 if (
C->getNumOperands()) {
306 for (
const Value *
Op :
C->operands())
310 if (CE->getOpcode() == Instruction::ShuffleVector)
331 if (
F.isDeclaration())
340 for (
const Value *
Op :
I.operands())
368 if (
G.hasInitializer())
375 for (
const Use &U :
F.operands())
384 : DebugInfo(DebugInfo) {
402 EnumerateAttributes(
F.getAttributes());
413 EnumerateValue(&GIF);
419 if (GV.hasInitializer())
420 EnumerateValue(GV.getInitializer());
423 if (GV.hasAttributes())
424 EnumerateAttributes(GV.getAttributesAsList(AttributeList::FunctionIndex));
429 EnumerateValue(GA.getAliasee());
433 EnumerateValue(GIF.getResolver());
437 for (
const Use &U :
F.operands())
438 EnumerateValue(U.get());
448 EnumerateValueSymbolTable(M.getValueSymbolTable());
449 EnumerateNamedMetadata(M);
454 GV.getAllMetadata(MDs);
455 for (
const auto &
I : MDs)
459 EnumerateMetadata(
nullptr,
I.second);
469 F.getAllMetadata(MDs);
470 for (
const auto &
I : MDs)
471 EnumerateMetadata(
F.isDeclaration() ?
nullptr : &
F,
I.second);
476 for (
const Use &
Op :
I.operands()) {
479 EnumerateOperandType(
Op);
489 for (
auto *VAM : AL->getArgs())
491 EnumerateMetadata(&
F, VAM);
495 EnumerateMetadata(&
F, MD->getMetadata());
505 EnumerateAttributes(
Call->getAttributes());
511 I.getAllMetadataOtherThanDebugLoc(MDs);
512 for (
unsigned i = 0, e = MDs.size(); i != e; ++i)
513 EnumerateMetadata(&
F, MDs[i].second);
519 EnumerateMetadata(&
F,
Op);
529 assert(
I != InstructionMap.
end() &&
"Instruction is not mapped!");
534 unsigned ComdatID = Comdats.
idFor(
C);
535 assert(ComdatID &&
"Comdat not found!");
540 InstructionMap[
I] = InstructionCount++;
548 assert(
I != ValueMap.end() &&
"Value not in slotcalculator!");
549 return I->second - 1;
552#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
562 const char *Name)
const {
563 OS <<
"Map Name: " <<
Name <<
"\n";
564 OS <<
"Size: " <<
Map.size() <<
"\n";
565 for (
const auto &
I : Map) {
568 OS <<
"Value: " <<
V->getName() <<
'\n';
570 OS <<
"Value: [null]\n";
574 if (
V->hasUseList()) {
575 OS <<
" Uses(" <<
V->getNumUses() <<
"):";
576 for (
const Use &U :
V->uses()) {
577 if (&U != &*
V->use_begin())
580 OS <<
" " <<
U->getName();
592 const char *Name)
const {
593 OS <<
"Map Name: " <<
Name <<
"\n";
594 OS <<
"Size: " <<
Map.size() <<
"\n";
595 for (
const auto &
I : Map) {
597 OS <<
"Metadata: slot = " <<
I.second.ID <<
"\n";
598 OS <<
"Metadata: function = " <<
I.second.F <<
"\n";
606void ValueEnumerator::EnumerateValueSymbolTable(
const ValueSymbolTable &VST) {
616void ValueEnumerator::EnumerateNamedMetadata(
const Module &M) {
617 for (
const auto &
I :
M.named_metadata())
618 EnumerateNamedMDNode(&
I);
621void ValueEnumerator::EnumerateNamedMDNode(
const NamedMDNode *MD) {
623 EnumerateMetadata(
nullptr, MD->
getOperand(i));
626unsigned ValueEnumerator::getMetadataFunctionID(
const Function *
F)
const {
630void ValueEnumerator::EnumerateMetadata(
const Function *
F,
const Metadata *MD) {
631 EnumerateMetadata(getMetadataFunctionID(
F), MD);
634void ValueEnumerator::EnumerateFunctionLocalMetadata(
635 const Function &
F,
const LocalAsMetadata *
Local) {
636 EnumerateFunctionLocalMetadata(getMetadataFunctionID(&
F),
Local);
639void ValueEnumerator::EnumerateFunctionLocalListMetadata(
640 const Function &
F,
const DIArgList *ArgList) {
641 EnumerateFunctionLocalListMetadata(getMetadataFunctionID(&
F), ArgList);
644void ValueEnumerator::dropFunctionFromMetadata(
645 MetadataMapType::value_type &FirstMD) {
648 auto &
Entry = MD.second;
664 while (!Worklist.
empty())
668 auto MD = MetadataMap.find(
Op);
669 if (MD != MetadataMap.end())
674void ValueEnumerator::EnumerateMetadata(
unsigned F,
const Metadata *MD) {
686 if (
const MDNode *
N = enumerateMetadataImpl(
F, MD))
687 Worklist.
push_back(std::make_pair(
N,
N->op_begin()));
689 while (!Worklist.
empty()) {
690 const MDNode *
N = Worklist.
back().first;
695 Worklist.
back().second,
N->op_end(),
696 [&](
const Metadata *MD) { return enumerateMetadataImpl(F, MD); });
697 if (
I !=
N->op_end()) {
699 Worklist.
back().second = ++
I;
702 if (
Op->isDistinct() && !
N->isDistinct())
709 if (
const Metadata *ExtraMD = DebugInfo.MDExtra.lookup(
N)) {
710 if (enumerateMetadataImpl(
F, ExtraMD)) {
712 Worklist.
push_back(std::make_pair(ExtraN, ExtraN->op_begin()));
721 MetadataMap[
N].ID = MDs.size();
725 if (Worklist.
empty() || Worklist.
back().first->isDistinct()) {
726 for (
const MDNode *
N : DelayedDistinctNodes)
727 Worklist.
push_back(std::make_pair(
N,
N->op_begin()));
728 DelayedDistinctNodes.clear();
733const MDNode *ValueEnumerator::enumerateMetadataImpl(
unsigned F,
742 "Invalid metadata kind");
744 auto Insertion = MetadataMap.insert(std::make_pair(MD, MDIndex(
F)));
745 MDIndex &
Entry = Insertion.first->second;
746 if (!Insertion.second) {
748 if (
Entry.hasDifferentFunction(
F))
749 dropFunctionFromMetadata(*Insertion.first);
759 Entry.ID = MDs.size();
763 EnumerateValue(
C->getValue());
770void ValueEnumerator::EnumerateFunctionLocalMetadata(
771 unsigned F,
const LocalAsMetadata *
Local) {
772 assert(
F &&
"Expected a function");
781 MDs.push_back(
Local);
783 Index.ID = MDs.size();
785 EnumerateValue(
Local->getValue());
790void ValueEnumerator::EnumerateFunctionLocalListMetadata(
791 unsigned F,
const DIArgList *ArgList) {
792 assert(
F &&
"Expected a function");
795 MDIndex &
Index = MetadataMap[ArgList];
801 for (ValueAsMetadata *VAM : ArgList->
getArgs()) {
803 assert(MetadataMap.count(VAM) &&
804 "LocalAsMetadata should be enumerated before DIArgList");
806 "Expected LocalAsMetadata in the same function");
809 "Expected LocalAsMetadata or ConstantAsMetadata");
810 assert(ValueMap.count(VAM->getValue()) &&
811 "Constant should be enumerated beforeDIArgList");
812 EnumerateMetadata(
F, VAM);
816 MDs.push_back(ArgList);
818 Index.ID = MDs.size();
834 return N->isDistinct() ? 2 : 3;
837void ValueEnumerator::organizeMetadata() {
838 assert(MetadataMap.size() == MDs.size() &&
839 "Metadata map and vector out of sync");
847 Order.
reserve(MetadataMap.size());
864 std::vector<const Metadata *> OldMDs;
866 MDs.reserve(OldMDs.size());
867 for (
unsigned I = 0,
E = Order.
size();
I !=
E && !Order[
I].F; ++
I) {
868 auto *MD = Order[
I].get(OldMDs);
870 MetadataMap[MD].ID =
I + 1;
876 if (MDs.size() == Order.
size())
881 FunctionMDs.reserve(OldMDs.size());
883 for (
unsigned I = MDs.size(),
E = Order.
size(),
ID = MDs.size();
I !=
E;
885 unsigned F = Order[
I].F;
888 }
else if (PrevF !=
F) {
889 R.Last = FunctionMDs.size();
891 R.First = FunctionMDs.size();
897 auto *MD = Order[
I].get(OldMDs);
899 MetadataMap[MD].ID = ++
ID;
903 R.Last = FunctionMDs.size();
904 FunctionMDInfo[PrevF] =
R;
908 return DebugInfo.getDXILFunction(
F);
913 return DebugInfo.getDXILInstruction(
I);
917 return DebugInfo.getDXILMetadata(M);
928void ValueEnumerator::incorporateFunctionMetadata(
const Function &
F) {
929 NumModuleMDs = MDs.size();
932 NumMDStrings = R.NumStrings;
933 MDs.insert(MDs.end(), FunctionMDs.begin() + R.First,
934 FunctionMDs.begin() + R.Last);
937void ValueEnumerator::EnumerateValue(
const Value *V) {
938 assert(!V->getType()->isVoidTy() &&
"Can't insert void values!");
946 Values[ValueID - 1].second++;
951 if (
const Comdat *
C = GO->getComdat())
960 }
else if (
C->getNumOperands()) {
974 if (
CE->getOpcode() == Instruction::ShuffleVector)
975 EnumerateValue(
CE->getShuffleMaskForBitcode());
982 Values.push_back(std::make_pair(V, 1U));
983 ValueMap[
V] = Values.size();
989 Values.push_back(std::make_pair(V, 1U));
990 ValueID = Values.size();
994 unsigned *
TypeID = &TypeMap[Ty];
1004 if (!STy->isLiteral())
1024 Types.push_back(Ty);
1031void ValueEnumerator::EnumerateOperandType(
const Value *V) {
1042 if (ValueMap.count(
C))
1047 for (
const Value *
Op :
C->operands()) {
1053 EnumerateOperandType(
Op);
1056 if (
CE->getOpcode() == Instruction::ShuffleVector)
1057 EnumerateOperandType(
CE->getShuffleMaskForBitcode());
1058 if (
CE->getOpcode() == Instruction::GetElementPtr)
1063void ValueEnumerator::EnumerateAttributes(AttributeList PAL) {
1068 unsigned &
Entry = AttributeListMap[PAL];
1071 AttributeLists.push_back(PAL);
1072 Entry = AttributeLists.size();
1076 for (
unsigned i : PAL.indexes()) {
1077 AttributeSet AS = PAL.getAttributes(i);
1081 unsigned &
Entry = AttributeGroupMap[Pair];
1083 AttributeGroups.push_back(Pair);
1084 Entry = AttributeGroups.size();
1087 if (Attr.isTypeAttribute())
1095 InstructionCount = 0;
1096 NumModuleValues = Values.size();
1100 incorporateFunctionMetadata(
F);
1103 for (
const auto &
I :
F.args()) {
1105 if (
I.hasAttribute(Attribute::ByVal))
1107 else if (
I.hasAttribute(Attribute::StructRet))
1109 else if (
I.hasAttribute(Attribute::ByRef))
1112 FirstFuncConstantID = Values.size();
1115 for (
const BasicBlock &BB :
F) {
1116 for (
const Instruction &OrigI : BB) {
1118 for (
const Use &OI :
I.operands()) {
1123 EnumerateValue(SVI->getShuffleMaskForBitcode());
1125 for (
const auto &Case :
SI->cases())
1126 EnumerateValue(Case.getCaseValue());
1129 BasicBlocks.push_back(&BB);
1130 ValueMap[&BB] = BasicBlocks.size();
1135 EnumerateAttributes(
F.getAttributes());
1137 FirstInstID = Values.size();
1142 for (
const BasicBlock &BB :
F) {
1143 for (
const Instruction &OrigI : BB) {
1145 for (
const Use &OI :
I.operands()) {
1152 for (ValueAsMetadata *VMD : ArgList->
getArgs()) {
1163 if (!
I.getType()->isVoidTy())
1169 for (
unsigned i = 0, e = FnLocalMDVector.
size(); i != e; ++i) {
1172 assert(ValueMap.count(FnLocalMDVector[i]->getValue()) &&
1173 "Missing value for metadata operand");
1174 EnumerateFunctionLocalMetadata(
F, FnLocalMDVector[i]);
1178 for (
const DIArgList *ArgList : ArgListMDVector)
1179 EnumerateFunctionLocalListMetadata(
F, ArgList);
1184 for (
unsigned i = NumModuleValues, e = Values.size(); i != e; ++i)
1185 ValueMap.erase(Values[i].first);
1186 for (
unsigned i = NumModuleMDs, e = MDs.size(); i != e; ++i)
1187 MetadataMap.erase(MDs[i]);
1188 for (
const BasicBlock *BB : BasicBlocks)
1191 Values.resize(NumModuleValues);
1192 MDs.resize(NumModuleMDs);
1193 BasicBlocks.clear();
1199 unsigned Counter = 0;
1201 IDMap[&BB] = ++Counter;
1208 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 OrderMap orderModule(const Module &M, ValueEnumerator &VE)
UseListOrderStack predictUseListOrder(const Module &M, ValueEnumerator &VE)
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, GsymDataExtractor &Data, uint64_t &Offset, uint64_t BaseAddr, uint64_t Addr, SourceLocations &SrcLocs, llvm::Error &Err)
A Lookup helper functions.
Machine Check Debug Module
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.
ArrayRef< ValueAsMetadata * > getArgs() const
ValueT lookup(const_arg_type_t< KeyT > Val) const
Return the entry for the specified key, or a default constructed value if no such entry exists.
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 insert(const T &Entry)
insert - Append entry to the vector if it doesn't already exist.
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
ValueEnumerator(const Module &M, bool ShouldPreserveUseListOrder)
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()
unsigned getValueID(const Value *V) const
std::pair< unsigned, AttributeSet > IndexAndAttrSet
Attribute groups as encoded in bitcode are almost AttributeSets, but they include the AttributeList i...
const Value & getDXILValue(const Value &V) const
const Instruction & getDXILInstruction(const Instruction &I) const
void setInstructionID(const Instruction *I)
void EnumerateType(Type *T)
unsigned getComdatID(const Comdat *C) const
uint64_t computeBitsRequiredForTypeIndices() const
unsigned getMetadataID(const Metadata *MD) const
const TypeList & getTypes() const
const Function & getDXILFunction(const Function &F) const
const Metadata * getDXILMetadata(const Metadata *M) const
void print(raw_ostream &OS, const ValueMapType &Map, const char *Name) const
void incorporateFunction(const Function &F)
incorporateFunction/purgeFunction - If you'd like to deal with a function, use these two methods to g...
unsigned getInstructionID(const Instruction *I) const
unsigned getGlobalBasicBlockID(const BasicBlock *BB) const
getGlobalBasicBlockID - This returns the function-specific ID for the specified basic block.
UseListOrderStack UseListOrders
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)
friend class Instruction
Iterator for Instructions in a `BasicBlock.
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
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:...