15#include "llvm/Config/llvm-config.h"
52 DenseMap<const Value *, std::pair<unsigned, bool>> IDs;
53 unsigned LastGlobalValueID = 0;
57 bool isGlobalValue(
unsigned ID)
const {
58 return ID <= LastGlobalValueID;
61 unsigned size()
const {
return IDs.
size(); }
64 std::pair<unsigned, bool>
lookup(
const Value *V)
const {
68 void index(
const Value *V) {
70 unsigned ID = IDs.
size() + 1;
78 if (OM.lookup(V).first)
82 if (
C->getNumOperands()) {
83 for (
const Value *
Op :
C->operands())
87 if (CE->getOpcode() == Instruction::ShuffleVector)
88 orderValue(CE->getShuffleMaskForBitcode(), OM);
118 OM.LastGlobalValueID = OM.size();
120 auto orderConstantValue = [&OM](
const Value *V) {
126 if (
F.isDeclaration())
138 auto OrderConstantFromMetadata = [&](
Metadata *MD) {
140 orderConstantValue(VAM->getValue());
142 for (
const auto *VAM : AL->getArgs())
143 orderConstantValue(VAM->getValue());
148 OrderConstantFromMetadata(DVR.getRawLocation());
149 if (DVR.isDbgAssign())
150 OrderConstantFromMetadata(DVR.getRawAddress());
153 for (
const Value *V :
I.operands()) {
155 OrderConstantFromMetadata(MAV->getMetadata());
163 for (
const Value *
Op :
I.operands())
164 orderConstantValue(
Op);
166 orderValue(SVI->getShuffleMaskForBitcode(), OM);
174 unsigned ID,
const OrderMap &OM,
177 using Entry = std::pair<const Use *, unsigned>;
181 if (OM.lookup(U.getUser()).first)
182 List.
push_back(std::make_pair(&U, List.size()));
188 bool IsGlobalValue = OM.isGlobalValue(
ID);
189 llvm::sort(List, [&](
const Entry &L,
const Entry &R) {
190 const Use *LU = L.first;
191 const Use *RU = R.first;
195 auto LID = OM.lookup(LU->getUser()).first;
196 auto RID = OM.lookup(RU->getUser()).first;
216 return LU->getOperandNo() < RU->getOperandNo();
217 return LU->getOperandNo() > RU->getOperandNo();
225 Stack.emplace_back(V,
F, List.size());
226 assert(List.size() == Stack.back().Shuffle.size() &&
"Wrong size");
227 for (
size_t I = 0,
E = List.size();
I !=
E; ++
I)
228 Stack.back().Shuffle[
I] = List[
I].second;
233 if (!V->hasUseList())
236 auto &IDPair = OM[V];
237 assert(IDPair.first &&
"Unmapped value");
243 IDPair.second =
true;
244 if (!V->use_empty() && std::next(V->use_begin()) != V->use_end())
249 if (
C->getNumOperands()) {
250 for (
const Value *
Op :
C->operands())
254 if (CE->getOpcode() == Instruction::ShuffleVector)
275 auto PredictValueOrderFromMetadata = [&](
Metadata *MD) {
279 for (
const auto *VAM : AL->getArgs())
283 if (
F.isDeclaration())
292 PredictValueOrderFromMetadata(DVR.getRawLocation());
293 if (DVR.isDbgAssign())
294 PredictValueOrderFromMetadata(DVR.getRawAddress());
296 for (
const Value *
Op :
I.operands()) {
300 PredictValueOrderFromMetadata(MAV->getMetadata());
321 if (
G.hasInitializer())
328 for (
const Use &U :
F.operands())
336 return V.first->getType()->isIntOrIntVectorTy();
340 bool ShouldPreserveUseListOrder)
341 : ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) {
342 if (ShouldPreserveUseListOrder)
348 EnumerateType(GV.getValueType());
354 EnumerateType(
F.getValueType());
355 EnumerateAttributes(
F.getAttributes());
361 EnumerateType(GA.getValueType());
366 EnumerateValue(&GIF);
367 EnumerateType(GIF.getValueType());
371 unsigned FirstConstant = Values.size();
375 if (GV.hasInitializer())
376 EnumerateValue(GV.getInitializer());
377 if (GV.hasAttributes())
378 EnumerateAttributes(GV.getAttributesAsList(AttributeList::FunctionIndex));
383 EnumerateValue(GA.getAliasee());
387 EnumerateValue(GIF.getResolver());
391 for (
const Use &U :
F.operands())
392 EnumerateValue(U.get());
402 EnumerateValueSymbolTable(M.getValueSymbolTable());
403 EnumerateNamedMetadata(M);
408 GV.getAllMetadata(MDs);
409 for (const auto &I : MDs)
413 EnumerateMetadata(nullptr, I.second);
419 EnumerateType(
A.getType());
423 F.getAllMetadata(MDs);
424 for (
const auto &
I : MDs)
425 EnumerateMetadata(
F.isDeclaration() ?
nullptr : &
F,
I.second);
432 auto EnumerateNonLocalValuesFromMetadata = [&](
Metadata *MD) {
433 assert(MD &&
"Metadata unexpectedly null");
435 for (
const auto *VAM : AL->getArgs()) {
437 EnumerateMetadata(&
F, VAM);
443 EnumerateMetadata(&
F, MD);
448 EnumerateMetadata(&
F, DLR->getLabel());
449 EnumerateMetadata(&
F, &*DLR->getDebugLoc());
464 for (
const Use &
Op :
I.operands()) {
467 EnumerateOperandType(
Op);
471 EnumerateNonLocalValuesFromMetadata(MD->getMetadata());
474 EnumerateType(SVI->getShuffleMaskForBitcode()->getType());
476 EnumerateType(
GEP->getSourceElementType());
478 EnumerateType(AI->getAllocatedType());
479 EnumerateType(
I.getType());
481 EnumerateAttributes(
Call->getAttributes());
482 EnumerateType(
Call->getFunctionType());
487 I.getAllMetadataOtherThanDebugLoc(MDs);
488 for (
const auto &MD : MDs)
489 EnumerateMetadata(&
F, MD.second);
495 EnumerateMetadata(&
F,
Op);
500 GIF.getAllMetadata(MDs);
501 for (const auto &I : MDs)
502 EnumerateMetadata(nullptr, I.second);
506 OptimizeConstants(FirstConstant, Values.size());
514 assert(
I != InstructionMap.end() &&
"Instruction is not mapped!");
519 unsigned ComdatID = Comdats.idFor(
C);
520 assert(ComdatID &&
"Comdat not found!");
525 InstructionMap[
I] = InstructionCount++;
533 assert(
I != ValueMap.end() &&
"Value not in slotcalculator!");
537#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
547 const char *Name)
const {
548 OS <<
"Map Name: " << Name <<
"\n";
549 OS <<
"Size: " << Map.size() <<
"\n";
550 for (
const auto &
I : Map) {
553 OS <<
"Value: " << V->getName();
555 OS <<
"Value: [null]\n";
559 OS <<
" Uses(" << V->getNumUses() <<
"):";
560 for (
const Use &U : V->
uses()) {
561 if (&U != &*V->use_begin())
564 OS <<
" " << U->getName();
574 const char *Name)
const {
575 OS <<
"Map Name: " << Name <<
"\n";
576 OS <<
"Size: " << Map.size() <<
"\n";
577 for (
const auto &
I : Map) {
579 OS <<
"Metadata: slot = " <<
I.second.ID <<
"\n";
580 OS <<
"Metadata: function = " <<
I.second.F <<
"\n";
587void ValueEnumerator::OptimizeConstants(
unsigned CstStart,
unsigned CstEnd) {
588 if (CstStart == CstEnd || CstStart+1 == CstEnd)
return;
590 if (ShouldPreserveUseListOrder)
595 std::stable_sort(Values.begin() + CstStart, Values.begin() + CstEnd,
596 [
this](
const std::pair<const Value *, unsigned> &LHS,
597 const std::pair<const Value *, unsigned> &RHS) {
599 if (LHS.first->getType() != RHS.first->getType())
600 return getTypeID(LHS.first->getType()) < getTypeID(RHS.first->getType());
602 return LHS.second > RHS.second;
608 std::stable_partition(Values.begin() + CstStart, Values.begin() + CstEnd,
612 for (; CstStart != CstEnd; ++CstStart)
613 ValueMap[Values[CstStart].first] = CstStart+1;
618void ValueEnumerator::EnumerateValueSymbolTable(
const ValueSymbolTable &VST) {
621 EnumerateValue(VI->getValue());
626void ValueEnumerator::EnumerateNamedMetadata(
const Module &M) {
627 for (
const auto &
I :
M.named_metadata())
628 EnumerateNamedMDNode(&
I);
631void ValueEnumerator::EnumerateNamedMDNode(
const NamedMDNode *MD) {
633 EnumerateMetadata(
nullptr,
N);
636unsigned ValueEnumerator::getMetadataFunctionID(
const Function *
F)
const {
641 EnumerateMetadata(getMetadataFunctionID(
F), MD);
644void ValueEnumerator::EnumerateFunctionLocalMetadata(
646 EnumerateFunctionLocalMetadata(getMetadataFunctionID(&
F),
Local);
649void ValueEnumerator::EnumerateFunctionLocalListMetadata(
651 EnumerateFunctionLocalListMetadata(getMetadataFunctionID(&
F), ArgList);
654void ValueEnumerator::dropFunctionFromMetadata(
655 MetadataMapType::value_type &FirstMD) {
658 auto &
Entry = MD.second;
674 while (!Worklist.
empty())
678 auto MD = MetadataMap.find(
Op);
679 if (MD != MetadataMap.end())
684void ValueEnumerator::EnumerateMetadata(
unsigned F,
const Metadata *MD) {
694 if (
const MDNode *
N = enumerateMetadataImpl(
F, MD))
695 Worklist.
push_back(std::make_pair(
N,
N->op_begin()));
697 while (!Worklist.
empty()) {
698 const MDNode *
N = Worklist.
back().first;
703 Worklist.
back().second,
N->op_end(),
704 [&](
const Metadata *MD) { return enumerateMetadataImpl(F, MD); });
705 if (
I !=
N->op_end()) {
707 Worklist.
back().second = ++
I;
710 if (
Op->isDistinct() && !
N->isDistinct())
720 MetadataMap[
N].ID = MDs.size();
724 if (Worklist.
empty() || Worklist.
back().first->isDistinct()) {
725 for (
const MDNode *
N : DelayedDistinctNodes)
726 Worklist.
push_back(std::make_pair(
N,
N->op_begin()));
727 DelayedDistinctNodes.clear();
732const MDNode *ValueEnumerator::enumerateMetadataImpl(
unsigned F,
const Metadata *MD) {
738 "Invalid metadata kind");
740 auto Insertion = MetadataMap.insert(std::make_pair(MD, MDIndex(
F)));
741 MDIndex &
Entry = Insertion.first->second;
742 if (!Insertion.second) {
744 if (
Entry.hasDifferentFunction(
F))
745 dropFunctionFromMetadata(*Insertion.first);
755 Entry.ID = MDs.size();
759 EnumerateValue(
C->getValue());
766void ValueEnumerator::EnumerateFunctionLocalMetadata(
768 assert(
F &&
"Expected a function");
777 MDs.push_back(
Local);
779 Index.ID = MDs.size();
781 EnumerateValue(
Local->getValue());
786void ValueEnumerator::EnumerateFunctionLocalListMetadata(
788 assert(
F &&
"Expected a function");
791 MDIndex &
Index = MetadataMap[ArgList];
797 for (ValueAsMetadata *VAM : ArgList->
getArgs()) {
799 assert(MetadataMap.count(VAM) &&
800 "LocalAsMetadata should be enumerated before DIArgList");
802 "Expected LocalAsMetadata in the same function");
805 "Expected LocalAsMetadata or ConstantAsMetadata");
806 assert(ValueMap.count(VAM->getValue()) &&
807 "Constant should be enumerated beforeDIArgList");
808 EnumerateMetadata(
F, VAM);
812 MDs.push_back(ArgList);
814 Index.ID = MDs.size();
830 return N->isDistinct() ? 2 : 3;
833void ValueEnumerator::organizeMetadata() {
834 assert(MetadataMap.size() == MDs.size() &&
835 "Metadata map and vector out of sync");
843 Order.
reserve(MetadataMap.size());
860 std::vector<const Metadata *> OldMDs;
862 MDs.reserve(OldMDs.size());
863 for (
unsigned I = 0,
E = Order.
size();
I !=
E && !Order[
I].F; ++
I) {
864 auto *MD = Order[
I].get(OldMDs);
866 MetadataMap[MD].ID =
I + 1;
872 if (MDs.size() == Order.
size())
877 FunctionMDs.reserve(OldMDs.size());
879 for (
unsigned I = MDs.size(),
E = Order.
size(),
ID = MDs.size();
I !=
E;
881 unsigned F = Order[
I].F;
884 }
else if (PrevF !=
F) {
885 R.Last = FunctionMDs.size();
887 R.First = FunctionMDs.size();
893 auto *MD = Order[
I].get(OldMDs);
895 MetadataMap[MD].ID = ++
ID;
899 R.Last = FunctionMDs.size();
900 FunctionMDInfo[PrevF] =
R;
903void ValueEnumerator::incorporateFunctionMetadata(
const Function &
F) {
904 NumModuleMDs = MDs.size();
907 NumMDStrings =
R.NumStrings;
908 MDs.insert(MDs.end(), FunctionMDs.begin() +
R.First,
909 FunctionMDs.begin() +
R.Last);
912void ValueEnumerator::EnumerateValue(
const Value *V) {
913 assert(!
V->getType()->isVoidTy() &&
"Can't insert void values!");
917 unsigned &ValueID = ValueMap[
V];
920 Values[ValueID-1].second++;
925 if (
const Comdat *
C = GO->getComdat())
929 EnumerateType(
V->getType());
934 }
else if (
C->getNumOperands()) {
943 for (
const Use &U :
C->operands())
947 if (
CE->getOpcode() == Instruction::ShuffleVector)
948 EnumerateValue(
CE->getShuffleMaskForBitcode());
950 EnumerateType(
GEP->getSourceElementType());
955 Values.push_back(std::make_pair(V, 1U));
956 ValueMap[
V] = Values.size();
962 Values.push_back(std::make_pair(V, 1U));
963 ValueID = Values.size();
967void ValueEnumerator::EnumerateType(
Type *Ty) {
968 unsigned *
TypeID = &TypeMap[Ty];
978 if (!STy->isLiteral())
984 EnumerateType(SubTy);
1005void ValueEnumerator::EnumerateOperandType(
const Value *V) {
1006 EnumerateType(
V->getType());
1016 if (ValueMap.count(
C))
1021 for (
const Value *
Op :
C->operands()) {
1027 EnumerateOperandType(
Op);
1030 if (
CE->getOpcode() == Instruction::ShuffleVector)
1031 EnumerateOperandType(
CE->getShuffleMaskForBitcode());
1032 if (
CE->getOpcode() == Instruction::GetElementPtr)
1037void ValueEnumerator::EnumerateAttributes(AttributeList PAL) {
1038 if (PAL.isEmpty())
return;
1041 unsigned &
Entry = AttributeListMap[PAL];
1044 AttributeLists.push_back(PAL);
1045 Entry = AttributeLists.size();
1049 for (
unsigned i : PAL.indexes()) {
1050 AttributeSet AS = PAL.getAttributes(i);
1054 unsigned &
Entry = AttributeGroupMap[Pair];
1056 AttributeGroups.push_back(Pair);
1057 Entry = AttributeGroups.size();
1060 if (Attr.isTypeAttribute())
1061 EnumerateType(Attr.getValueAsType());
1068 InstructionCount = 0;
1069 NumModuleValues = Values.size();
1073 incorporateFunctionMetadata(
F);
1076 for (
const auto &
I :
F.args()) {
1078 if (
I.hasAttribute(Attribute::ByVal))
1079 EnumerateType(
I.getParamByValType());
1080 else if (
I.hasAttribute(Attribute::StructRet))
1081 EnumerateType(
I.getParamStructRetType());
1082 else if (
I.hasAttribute(Attribute::ByRef))
1083 EnumerateType(
I.getParamByRefType());
1085 FirstFuncConstantID = Values.size();
1090 for (
const Use &OI :
I.operands()) {
1095 EnumerateValue(SVI->getShuffleMaskForBitcode());
1097 BasicBlocks.push_back(&BB);
1098 ValueMap[&BB] = BasicBlocks.size();
1102 OptimizeConstants(FirstFuncConstantID, Values.size());
1106 EnumerateAttributes(
F.getAttributes());
1108 FirstInstID = Values.size();
1113 auto AddFnLocalMetadata = [&](
Metadata *MD) {
1134 for (
const Use &OI :
I.operands()) {
1136 AddFnLocalMetadata(MD->getMetadata());
1140 assert(DVR.getRawLocation() &&
1141 "DbgVariableRecord location unexpectedly null");
1142 AddFnLocalMetadata(DVR.getRawLocation());
1143 if (DVR.isDbgAssign()) {
1144 assert(DVR.getRawAddress() &&
1145 "DbgVariableRecord location unexpectedly null");
1146 AddFnLocalMetadata(DVR.getRawAddress());
1149 if (!
I.getType()->isVoidTy())
1159 "Missing value for metadata operand");
1160 EnumerateFunctionLocalMetadata(
F,
Local);
1164 for (
const DIArgList *ArgList : ArgListMDVector)
1165 EnumerateFunctionLocalListMetadata(
F, ArgList);
1171 ValueMap.erase(V.first);
1173 MetadataMap.erase(MD);
1177 Values.resize(NumModuleValues);
1178 MDs.resize(NumModuleMDs);
1179 BasicBlocks.clear();
1185 unsigned Counter = 0;
1187 IDMap[&BB] = ++Counter;
1194 unsigned &Idx = GlobalBasicBlockIDs[BB];
for(const MachineOperand &MO :llvm::drop_begin(OldMI.operands(), Desc.getNumOperands()))
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.
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 void predictValueUseListOrderImpl(const Value *V, const Function *F, unsigned ID, const OrderMap &OM, UseListOrderStack &Stack)
static unsigned getMetadataTypeOrder(const Metadata *MD)
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 void IncorporateFunctionInfoGlobalBBIDs(const Function *F, DenseMap< const BasicBlock *, unsigned > &IDMap)
static bool isIntOrIntVectorValue(const std::pair< const Value *, unsigned > &V)
static OrderMap orderModule(const Module &M)
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
Records a position in IR for a source label (DILabel).
Base class for non-instruction debug metadata records that have positions within IR.
DebugLoc getDebugLoc() const
Record of a variable value-assignment, aka a non instruction representation of the dbg....
LLVM_ABI DIAssignID * getAssignID() const
DIExpression * getExpression() const
DILocalVariable * getVariable() const
Metadata * getRawLocation() const
Returns the metadata operand for the first location description.
Metadata * getRawAddress() const
DIExpression * getAddressExpression() 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...
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.
iterator_range< op_iterator > operands()
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
A Use represents the edge between a Value definition and its users.
unsigned getMetadataID(const Metadata *MD) const
UseListOrderStack UseListOrders
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()
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.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
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.
static auto filterDbgVars(iterator_range< simple_ilist< DbgRecord >::iterator > R)
Filter the DbgRecord range to DbgVariableRecord types only and downcast.
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:...