32 unsigned LastGlobalConstantID;
33 unsigned LastGlobalValueID;
35 OrderMap() : LastGlobalConstantID(0), LastGlobalValueID(0) {}
37 bool isGlobalConstant(
unsigned ID)
const {
38 return ID <= LastGlobalConstantID;
40 bool isGlobalValue(
unsigned ID)
const {
41 return ID <= LastGlobalValueID && !isGlobalConstant(ID);
44 unsigned size()
const {
return IDs.size(); }
45 std::pair<unsigned, bool> &operator[](
const Value *V) {
return IDs[V]; }
46 std::pair<unsigned, bool>
lookup(
const Value *V)
const {
49 void index(
const Value *V) {
51 unsigned ID = IDs.size() + 1;
58 if (OM.lookup(V).first)
61 if (
const Constant *
C = dyn_cast<Constant>(V))
62 if (
C->getNumOperands() && !isa<GlobalValue>(
C))
63 for (
const Value *
Op :
C->operands())
64 if (!isa<BasicBlock>(
Op) && !isa<GlobalValue>(
Op))
83 if (
G.hasInitializer())
84 if (!isa<GlobalValue>(
G.getInitializer()))
87 if (!isa<GlobalValue>(
A.getAliasee()))
90 if (!isa<GlobalValue>(
I.getResolver()))
93 for (
const Use &U :
F.operands())
94 if (!isa<GlobalValue>(U.get()))
97 OM.LastGlobalConstantID = OM.size();
115 OM.LastGlobalValueID = OM.size();
118 if (
F.isDeclaration())
129 for (
const Value *
Op :
I.operands())
130 if ((isa<Constant>(*
Op) && !isa<GlobalValue>(*
Op)) ||
141 unsigned ID,
const OrderMap &OM,
144 typedef std::pair<const Use *, unsigned> Entry;
148 if (OM.lookup(U.getUser()).
first)
155 bool IsGlobalValue = OM.isGlobalValue(ID);
157 const Use *LU = L.first;
158 const Use *RU = R.first;
162 auto LID = OM.lookup(LU->getUser()).
first;
163 auto RID = OM.lookup(RU->getUser()).
first;
171 if (OM.isGlobalValue(LID) && OM.isGlobalValue(RID))
192 return LU->getOperandNo() < RU->getOperandNo();
193 return LU->getOperandNo() > RU->getOperandNo();
198 [](
const Entry &
L,
const Entry &R) {
return L.second < R.second; }))
203 Stack.emplace_back(V, F, List.
size());
204 assert(List.
size() == Stack.back().Shuffle.size() &&
"Wrong size");
205 for (
size_t I = 0,
E = List.
size();
I !=
E; ++
I)
206 Stack.back().Shuffle[
I] = List[
I].second;
211 auto &IDPair = OM[V];
212 assert(IDPair.first &&
"Unmapped value");
218 IDPair.second =
true;
223 if (
const Constant *
C = dyn_cast<Constant>(V))
224 if (
C->getNumOperands())
225 for (
const Value *
Op :
C->operands())
226 if (isa<Constant>(
Op))
253 for (
const Value *
Op :
I.operands())
254 if (isa<Constant>(*
Op) || isa<InlineAsm>(*Op))
272 if (
G.hasInitializer())
279 for (
const Use &U :
F.operands())
287 return V.first->getType()->isIntOrIntVectorTy();
290 ValueEnumerator::ValueEnumerator(
const Module &M,
291 bool ShouldPreserveUseListOrder)
292 : ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) {
293 if (ShouldPreserveUseListOrder)
303 EnumerateAttributes(
F.getAttributes());
312 EnumerateValue(&GIF);
315 unsigned FirstConstant = Values.size();
319 if (GV.hasInitializer())
320 EnumerateValue(GV.getInitializer());
321 if (GV.hasAttributes())
327 EnumerateValue(GA.getAliasee());
331 EnumerateValue(GIF.getResolver());
335 for (
const Use &U :
F.operands())
336 EnumerateValue(U.get());
346 EnumerateValueSymbolTable(M.getValueSymbolTable());
347 EnumerateNamedMetadata(M);
352 GV.getAllMetadata(MDs);
353 for (
const auto &
I : MDs)
357 EnumerateMetadata(
nullptr,
I.second);
363 EnumerateType(
A.getType());
367 F.getAllMetadata(MDs);
368 for (
const auto &
I : MDs)
369 EnumerateMetadata(
F.isDeclaration() ?
nullptr : &
F,
I.second);
373 for (
const Use &
Op :
I.operands()) {
376 EnumerateOperandType(
Op);
381 if (isa<LocalAsMetadata>(MD->getMetadata()))
384 EnumerateMetadata(&F, MD->getMetadata());
386 EnumerateType(
I.getType());
387 if (
const CallInst *CI = dyn_cast<CallInst>(&
I))
388 EnumerateAttributes(CI->getAttributes());
389 else if (
const InvokeInst *II = dyn_cast<InvokeInst>(&
I))
390 EnumerateAttributes(II->getAttributes());
394 I.getAllMetadataOtherThanDebugLoc(MDs);
395 for (
unsigned i = 0, e = MDs.size(); i != e; ++i)
396 EnumerateMetadata(&F, MDs[i].
second);
402 EnumerateMetadata(&F,
Op);
407 OptimizeConstants(FirstConstant, Values.size());
415 assert(I != InstructionMap.
end() &&
"Instruction is not mapped!");
420 unsigned ComdatID = Comdats.
idFor(C);
421 assert(ComdatID &&
"Comdat not found!");
426 InstructionMap[
I] = InstructionCount++;
430 if (
auto *MD = dyn_cast<MetadataAsValue>(V))
438 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
448 const char *
Name)
const {
450 OS <<
"Map Name: " << Name <<
"\n";
451 OS <<
"Size: " << Map.
size() <<
"\n";
455 const Value *V =
I->first;
457 OS <<
"Value: " << V->
getName();
459 OS <<
"Value: [null]\n";
464 for (
const Use &U : V->
uses()) {
468 OS <<
" " << U->getName();
478 const char *
Name)
const {
480 OS <<
"Map Name: " << Name <<
"\n";
481 OS <<
"Size: " << Map.
size() <<
"\n";
484 OS <<
"Metadata: slot = " <<
I->second.ID <<
"\n";
485 OS <<
"Metadata: function = " <<
I->second.F <<
"\n";
492 void ValueEnumerator::OptimizeConstants(
unsigned CstStart,
unsigned CstEnd) {
493 if (CstStart == CstEnd || CstStart+1 == CstEnd)
return;
495 if (ShouldPreserveUseListOrder)
500 std::stable_sort(Values.begin() + CstStart, Values.begin() + CstEnd,
501 [
this](
const std::pair<const Value *, unsigned> &LHS,
502 const std::pair<const Value *, unsigned> &RHS) {
504 if (LHS.first->getType() != RHS.first->getType())
507 return LHS.second > RHS.second;
513 std::stable_partition(Values.begin() + CstStart, Values.begin() + CstEnd,
517 for (; CstStart != CstEnd; ++CstStart)
524 void ValueEnumerator::EnumerateValueSymbolTable(
const ValueSymbolTable &VST) {
527 EnumerateValue(
VI->getValue());
532 void ValueEnumerator::EnumerateNamedMetadata(
const Module &M) {
534 EnumerateNamedMDNode(&
I);
537 void ValueEnumerator::EnumerateNamedMDNode(
const NamedMDNode *MD) {
539 EnumerateMetadata(
nullptr, MD->
getOperand(i));
542 unsigned ValueEnumerator::getMetadataFunctionID(
const Function *
F)
const {
547 EnumerateMetadata(getMetadataFunctionID(F), MD);
550 void ValueEnumerator::EnumerateFunctionLocalMetadata(
552 EnumerateFunctionLocalMetadata(getMetadataFunctionID(&F), Local);
555 void ValueEnumerator::dropFunctionFromMetadata(
556 MetadataMapType::value_type &FirstMD) {
559 auto &Entry = MD.second;
571 if (
auto *
N = dyn_cast<MDNode>(MD.first))
575 while (!Worklist.
empty())
579 auto MD = MetadataMap.
find(
Op);
580 if (MD != MetadataMap.
end())
585 void ValueEnumerator::EnumerateMetadata(
unsigned F,
const Metadata *MD) {
595 if (
const MDNode *
N = enumerateMetadataImpl(F, MD))
596 Worklist.
push_back(std::make_pair(
N,
N->op_begin()));
598 while (!Worklist.
empty()) {
605 [&](
const Metadata *MD) {
return enumerateMetadataImpl(F, MD); });
607 auto *
Op = cast<MDNode>(*I);
608 Worklist.
back().second = ++
I;
621 MetadataMap[
N].ID = MDs.size();
625 if (Worklist.
empty() || Worklist.
back().first->isDistinct()) {
626 for (
const MDNode *N : DelayedDistinctNodes)
628 DelayedDistinctNodes.clear();
633 const MDNode *ValueEnumerator::enumerateMetadataImpl(
unsigned F,
const Metadata *MD) {
638 (isa<MDNode>(MD) || isa<MDString>(MD) || isa<ConstantAsMetadata>(MD)) &&
639 "Invalid metadata kind");
641 auto Insertion = MetadataMap.
insert(std::make_pair(MD, MDIndex(F)));
642 MDIndex &Entry = Insertion.first->second;
643 if (!Insertion.second) {
645 if (Entry.hasDifferentFunction(F))
646 dropFunctionFromMetadata(*Insertion.first);
651 if (
auto *N = dyn_cast<MDNode>(MD))
656 Entry.ID = MDs.size();
659 if (
auto *
C = dyn_cast<ConstantAsMetadata>(MD))
660 EnumerateValue(
C->getValue());
667 void ValueEnumerator::EnumerateFunctionLocalMetadata(
669 assert(F &&
"Expected a function");
672 MDIndex &Index = MetadataMap[Local];
674 assert(Index.F == F &&
"Expected the same function");
678 MDs.push_back(Local);
680 Index.ID = MDs.size();
687 if (isa<MDString>(MD))
701 void ValueEnumerator::organizeMetadata() {
703 "Metadata map and vector out of sync");
728 std::vector<const Metadata *> OldMDs = std::move(MDs);
729 MDs.reserve(OldMDs.size());
730 for (
unsigned I = 0,
E = Order.
size(); I !=
E && !Order[
I].F; ++
I) {
731 auto *MD = Order[
I].get(OldMDs);
733 MetadataMap[MD].ID = I + 1;
734 if (isa<MDString>(MD))
739 if (MDs.size() == Order.
size())
744 FunctionMDs.reserve(OldMDs.size());
746 for (
unsigned I = MDs.size(),
E = Order.
size(),
ID = MDs.size(); I !=
E;
748 unsigned F = Order[
I].F;
751 }
else if (PrevF != F) {
752 R.Last = FunctionMDs.size();
754 R.First = FunctionMDs.size();
760 auto *MD = Order[
I].get(OldMDs);
761 FunctionMDs.push_back(MD);
762 MetadataMap[MD].ID = ++
ID;
763 if (isa<MDString>(MD))
766 R.Last = FunctionMDs.size();
767 FunctionMDInfo[PrevF] = R;
770 void ValueEnumerator::incorporateFunctionMetadata(
const Function &F) {
771 NumModuleMDs = MDs.size();
774 NumMDStrings = R.NumStrings;
775 MDs.insert(MDs.end(), FunctionMDs.begin() + R.First,
776 FunctionMDs.begin() + R.Last);
779 void ValueEnumerator::EnumerateValue(
const Value *V) {
781 assert(!isa<MetadataAsValue>(V) &&
"EnumerateValue doesn't handle Metadata!");
787 Values[ValueID-1].second++;
791 if (
auto *GO = dyn_cast<GlobalObject>(V))
792 if (
const Comdat *
C = GO->getComdat())
798 if (
const Constant *
C = dyn_cast<Constant>(V)) {
799 if (isa<GlobalValue>(
C)) {
801 }
else if (
C->getNumOperands()) {
812 if (!isa<BasicBlock>(*I))
817 Values.push_back(std::make_pair(V, 1U));
824 Values.push_back(std::make_pair(V, 1U));
825 ValueID = Values.size();
829 void ValueEnumerator::EnumerateType(
Type *Ty) {
830 unsigned *
TypeID = &TypeMap[Ty];
839 if (
StructType *STy = dyn_cast<StructType>(Ty))
840 if (!STy->isLiteral())
846 EnumerateType(SubTy);
849 TypeID = &TypeMap[Ty];
856 if (*TypeID && *TypeID != ~0U)
862 *TypeID = Types.size();
867 void ValueEnumerator::EnumerateOperandType(
const Value *V) {
870 assert(!isa<MetadataAsValue>(V) &&
"Unexpected metadata operand");
886 if (isa<BasicBlock>(
Op))
889 EnumerateOperandType(
Op);
893 void ValueEnumerator::EnumerateAttributes(
AttributeList PAL) {
897 unsigned &Entry = AttributeListMap[PAL];
900 AttributeLists.push_back(PAL);
901 Entry = AttributeLists.size();
910 unsigned &Entry = AttributeGroupMap[Pair];
912 AttributeGroups.push_back(Pair);
913 Entry = AttributeGroups.size();
919 InstructionCount = 0;
920 NumModuleValues = Values.size();
924 incorporateFunctionMetadata(F);
927 for (
const auto &I : F.
args())
930 FirstFuncConstantID = Values.size();
935 for (
const Use &OI : I.operands()) {
936 if ((isa<Constant>(OI) && !isa<GlobalValue>(OI)) || isa<InlineAsm>(OI))
939 BasicBlocks.push_back(&BB);
944 OptimizeConstants(FirstFuncConstantID, Values.size());
948 EnumerateAttributes(F.getAttributes());
950 FirstInstID = Values.size();
956 for (
const Use &OI : I.operands()) {
957 if (
auto *MD = dyn_cast<MetadataAsValue>(&OI))
958 if (
auto *Local = dyn_cast<LocalAsMetadata>(MD->getMetadata()))
963 if (!I.getType()->isVoidTy())
969 for (
unsigned i = 0, e = FnLocalMDVector.
size(); i != e; ++i) {
973 "Missing value for metadata operand");
974 EnumerateFunctionLocalMetadata(F, FnLocalMDVector[i]);
980 for (
unsigned i = NumModuleValues, e = Values.size(); i != e; ++i)
982 for (
unsigned i = NumModuleMDs, e = MDs.size(); i != e; ++i)
983 MetadataMap.
erase(MDs[i]);
984 for (
unsigned i = 0, e = BasicBlocks.size(); i != e; ++i)
987 Values.resize(NumModuleValues);
988 MDs.resize(NumModuleMDs);
1004 unsigned &Idx = GlobalBasicBlockIDs[BB];
void push_back(const T &Elt)
unsigned Log2_32_Ceil(uint32_t Value)
Return the ceil log base 2 of the specified value, 32 if the value is zero.
Tracking metadata reference owned by Metadata.
This class provides a symbol table of name/value pairs.
iterator_range< use_iterator > uses()
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
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...
static unsigned getMetadataTypeOrder(const Metadata *MD)
This class represents an incoming formal argument to a Function.
iterator begin()
Get an iterator that from the beginning of the symbol table.
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
static void predictValueUseListOrder(const Value *V, const Function *F, OrderMap &OM, UseListOrderStack &Stack)
A Module instance is used to store all the information related to an LLVM module. ...
size_type count(const KeyT &Val) const
Return 1 if the specified key is in the map, 0 otherwise.
This class represents a function call, abstracting a target machine's calling convention.
void setInstructionID(const Instruction *I)
op_iterator op_begin() const
const Function * getParent() const
Return the enclosing method, or null if none.
static Type * getMetadataTy(LLVMContext &C)
void reserve(size_type N)
static void predictValueUseListOrderImpl(const Value *V, const Function *F, unsigned ID, const OrderMap &OM, UseListOrderStack &Stack)
op_iterator op_end() const
StringRef getName() const
Return a constant reference to the value's name.
void incorporateFunction(const Function &F)
incorporateFunction/purgeFunction - If you'd like to deal with a function, use these two methods to g...
static bool isIntOrIntVectorValue(const std::pair< const Value *, unsigned > &V)
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
unsigned getComdatID(const Comdat *C) const
void print(raw_ostream &O, bool IsForDebug=false) const
Implement operator<< on Value.
Class to represent struct types.
A Use represents the edge between a Value definition and its users.
TypeID
Definitions of all of the base types for the Type system.
static const uint16_t * lookup(unsigned opcode, unsigned domain, ArrayRef< uint16_t[3]> Table)
uint64_t computeBitsRequiredForTypeIndicies() const
unsigned idFor(const T &Entry) const
idFor - return the ID for an existing entry.
LLVM_NODISCARD bool empty() const
const TypeList & getTypes() const
unsigned getInstructionID(const Instruction *I) const
unsigned getValueID(const Value *V) const
detail::DenseMapPair< const Metadata *, MDIndex > value_type
iterator find(const KeyT &Val)
AttributeSet getAttributes(unsigned Index) const
The attributes for the specified index are returned.
iterator end()
Get an iterator to the end of the symbol table.
iterator find(const_arg_type_t< KeyT > Val)
bool erase(const KeyT &Val)
unsigned getTypeID(Type *T) const
LLVM Basic Block Representation.
The instances of the Type class are immutable: once they are created, they are never changed...
unsigned index_end() const
This is an important base class in LLVM.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
This file contains the declarations for the subclasses of Constant, which represent the different fla...
std::vector< UseListOrder > UseListOrderStack
iterator_range< named_metadata_iterator > named_metadata()
MDNode * getOperand(unsigned i) const
static OrderMap orderModule(const Module &M)
static UseListOrderStack predictUseListOrder(const Module &M)
unsigned index_begin() const
Use these to iterate over the valid attribute indices.
reverse_iterator rbegin()
std::pair< unsigned, AttributeSet > IndexAndAttrSet
Attribute groups as encoded in bitcode are almost AttributeSets, but they include the AttributeList i...
bool isEmpty() const
Return true if there are no attributes.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
bool hasAttributes() const
Return true if attributes exists in this set.
Module.h This file contains the declarations for the Module class.
Type * getType() const
All values are typed, get the type of this value.
LLVM_NODISCARD T pop_back_val()
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
unsigned getMetadataID(const Metadata *MD) const
UseListOrderStack UseListOrders
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
static void orderValue(const Value *V, OrderMap &OM)
void print(raw_ostream &OS, const ValueMapType &Map, const char *Name) const
unsigned insert(const T &Entry)
insert - Append entry to the vector if it doesn't already exist.
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
op_range operands() const
iterator_range< ifunc_iterator > ifuncs()
static void IncorporateFunctionInfoGlobalBBIDs(const Function *F, DenseMap< const BasicBlock *, unsigned > &IDMap)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
unsigned getGlobalBasicBlockID(const BasicBlock *BB) const
getGlobalBasicBlockID - This returns the function-specific ID for the specified basic block...
LLVM Value Representation.
unsigned getNumOperands() const
This class implements an extremely fast bulk output stream that can only output to a stream...
iterator_range< global_iterator > globals()
void sort(Policy policy, RandomAccessIterator Start, RandomAccessIterator End, const Comparator &Comp=Comparator())
auto find_if(R &&Range, UnaryPredicate P) -> decltype(std::begin(Range))
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly...
iterator_range< arg_iterator > args()
bool erase(const KeyT &Val)
ArrayRef< Type * > subtypes() const
bool isVoidTy() const
Return true if this is 'void'.
iterator_range< alias_iterator > aliases()