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);
156 std::sort(List.
begin(), List.
end(), [&](
const Entry &
L,
const Entry &R) {
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());
324 EnumerateValue(GA.getAliasee());
328 EnumerateValue(GIF.getResolver());
332 for (
const Use &U :
F.operands())
333 EnumerateValue(U.get());
343 EnumerateValueSymbolTable(M.getValueSymbolTable());
344 EnumerateNamedMetadata(M);
349 GV.getAllMetadata(MDs);
350 for (
const auto &
I : MDs)
354 EnumerateMetadata(
nullptr,
I.second);
360 EnumerateType(
A.getType());
364 F.getAllMetadata(MDs);
365 for (
const auto &
I : MDs)
366 EnumerateMetadata(
F.isDeclaration() ?
nullptr : &
F,
I.second);
370 for (
const Use &
Op :
I.operands()) {
373 EnumerateOperandType(
Op);
378 if (isa<LocalAsMetadata>(MD->getMetadata()))
381 EnumerateMetadata(&F, MD->getMetadata());
383 EnumerateType(
I.getType());
384 if (
const CallInst *CI = dyn_cast<CallInst>(&
I))
385 EnumerateAttributes(CI->getAttributes());
386 else if (
const InvokeInst *II = dyn_cast<InvokeInst>(&
I))
387 EnumerateAttributes(II->getAttributes());
391 I.getAllMetadataOtherThanDebugLoc(MDs);
392 for (
unsigned i = 0, e = MDs.size();
i != e; ++
i)
393 EnumerateMetadata(&F, MDs[
i].second);
399 EnumerateMetadata(&F,
Op);
404 OptimizeConstants(FirstConstant, Values.size());
412 assert(I != InstructionMap.
end() &&
"Instruction is not mapped!");
417 unsigned ComdatID = Comdats.
idFor(C);
418 assert(ComdatID &&
"Comdat not found!");
423 InstructionMap[
I] = InstructionCount++;
427 if (
auto *MD = dyn_cast<MetadataAsValue>(V))
443 const char *
Name)
const {
445 OS <<
"Map Name: " << Name <<
"\n";
446 OS <<
"Size: " << Map.
size() <<
"\n";
450 const Value *V =
I->first;
452 OS <<
"Value: " << V->
getName();
454 OS <<
"Value: [null]\n";
458 for (
const Use &U : V->
uses()) {
462 OS <<
" " << U->getName();
472 const char *
Name)
const {
474 OS <<
"Map Name: " << Name <<
"\n";
475 OS <<
"Size: " << Map.
size() <<
"\n";
478 OS <<
"Metadata: slot = " <<
I->second.ID <<
"\n";
479 OS <<
"Metadata: function = " <<
I->second.F <<
"\n";
486 void ValueEnumerator::OptimizeConstants(
unsigned CstStart,
unsigned CstEnd) {
487 if (CstStart == CstEnd || CstStart+1 == CstEnd)
return;
489 if (ShouldPreserveUseListOrder)
494 std::stable_sort(Values.begin() + CstStart, Values.begin() + CstEnd,
495 [
this](
const std::pair<const Value *, unsigned> &LHS,
496 const std::pair<const Value *, unsigned> &RHS) {
498 if (LHS.first->getType() != RHS.first->getType())
501 return LHS.second > RHS.second;
507 std::stable_partition(Values.begin() + CstStart, Values.begin() + CstEnd,
511 for (; CstStart != CstEnd; ++CstStart)
512 ValueMap[Values[CstStart].first] = CstStart+1;
518 void ValueEnumerator::EnumerateValueSymbolTable(
const ValueSymbolTable &VST) {
521 EnumerateValue(
VI->getValue());
526 void ValueEnumerator::EnumerateNamedMetadata(
const Module &M) {
528 EnumerateNamedMDNode(&
I);
531 void ValueEnumerator::EnumerateNamedMDNode(
const NamedMDNode *MD) {
536 unsigned ValueEnumerator::getMetadataFunctionID(
const Function *
F)
const {
541 EnumerateMetadata(getMetadataFunctionID(F), MD);
544 void ValueEnumerator::EnumerateFunctionLocalMetadata(
546 EnumerateFunctionLocalMetadata(getMetadataFunctionID(&F), Local);
549 void ValueEnumerator::dropFunctionFromMetadata(
550 MetadataMapType::value_type &FirstMD) {
553 auto &Entry = MD.second;
565 if (
auto *
N = dyn_cast<MDNode>(MD.first))
569 while (!Worklist.
empty())
573 auto MD = MetadataMap.
find(
Op);
574 if (MD != MetadataMap.
end())
579 void ValueEnumerator::EnumerateMetadata(
unsigned F,
const Metadata *MD) {
589 if (
const MDNode *
N = enumerateMetadataImpl(F, MD))
590 Worklist.
push_back(std::make_pair(
N,
N->op_begin()));
592 while (!Worklist.
empty()) {
599 [&](
const Metadata *MD) {
return enumerateMetadataImpl(F, MD); });
601 auto *
Op = cast<MDNode>(*I);
602 Worklist.
back().second = ++
I;
615 MetadataMap[
N].ID = MDs.size();
619 if (Worklist.
empty() || Worklist.
back().first->isDistinct()) {
620 for (
const MDNode *N : DelayedDistinctNodes)
622 DelayedDistinctNodes.clear();
627 const MDNode *ValueEnumerator::enumerateMetadataImpl(
unsigned F,
const Metadata *MD) {
632 (isa<MDNode>(MD) || isa<MDString>(MD) || isa<ConstantAsMetadata>(MD)) &&
633 "Invalid metadata kind");
635 auto Insertion = MetadataMap.
insert(std::make_pair(MD, MDIndex(F)));
636 MDIndex &Entry = Insertion.first->second;
637 if (!Insertion.second) {
639 if (Entry.hasDifferentFunction(F))
640 dropFunctionFromMetadata(*Insertion.first);
645 if (
auto *N = dyn_cast<MDNode>(MD))
650 Entry.ID = MDs.size();
653 if (
auto *
C = dyn_cast<ConstantAsMetadata>(MD))
654 EnumerateValue(
C->getValue());
661 void ValueEnumerator::EnumerateFunctionLocalMetadata(
663 assert(F &&
"Expected a function");
666 MDIndex &Index = MetadataMap[Local];
668 assert(Index.F == F &&
"Expected the same function");
672 MDs.push_back(Local);
674 Index.ID = MDs.size();
681 if (isa<MDString>(MD))
695 void ValueEnumerator::organizeMetadata() {
697 "Metadata map and vector out of sync");
715 std::sort(Order.
begin(), Order.
end(), [
this](MDIndex LHS, MDIndex RHS) {
722 std::vector<const Metadata *> OldMDs = std::move(MDs);
723 MDs.reserve(OldMDs.size());
724 for (
unsigned I = 0,
E = Order.
size(); I !=
E && !Order[
I].F; ++
I) {
725 auto *MD = Order[
I].get(OldMDs);
727 MetadataMap[MD].ID = I + 1;
728 if (isa<MDString>(MD))
733 if (MDs.size() == Order.
size())
738 FunctionMDs.reserve(OldMDs.size());
740 for (
unsigned I = MDs.size(),
E = Order.
size(),
ID = MDs.size(); I !=
E;
742 unsigned F = Order[
I].F;
745 }
else if (PrevF != F) {
746 R.Last = FunctionMDs.size();
748 R.First = FunctionMDs.size();
754 auto *MD = Order[
I].get(OldMDs);
755 FunctionMDs.push_back(MD);
756 MetadataMap[MD].ID = ++
ID;
757 if (isa<MDString>(MD))
760 R.Last = FunctionMDs.size();
761 FunctionMDInfo[PrevF] = R;
764 void ValueEnumerator::incorporateFunctionMetadata(
const Function &F) {
765 NumModuleMDs = MDs.size();
768 NumMDStrings = R.NumStrings;
769 MDs.insert(MDs.end(), FunctionMDs.begin() + R.First,
770 FunctionMDs.begin() + R.Last);
773 void ValueEnumerator::EnumerateValue(
const Value *V) {
775 assert(!isa<MetadataAsValue>(V) &&
"EnumerateValue doesn't handle Metadata!");
781 Values[ValueID-1].second++;
785 if (
auto *GO = dyn_cast<GlobalObject>(V))
786 if (
const Comdat *
C = GO->getComdat())
792 if (
const Constant *
C = dyn_cast<Constant>(V)) {
793 if (isa<GlobalValue>(
C)) {
795 }
else if (
C->getNumOperands()) {
806 if (!isa<BasicBlock>(*I))
811 Values.push_back(std::make_pair(V, 1U));
818 Values.push_back(std::make_pair(V, 1U));
819 ValueID = Values.size();
823 void ValueEnumerator::EnumerateType(
Type *Ty) {
824 unsigned *
TypeID = &TypeMap[Ty];
833 if (
StructType *STy = dyn_cast<StructType>(Ty))
834 if (!STy->isLiteral())
840 EnumerateType(SubTy);
843 TypeID = &TypeMap[Ty];
850 if (*TypeID && *TypeID != ~0U)
856 *TypeID = Types.size();
861 void ValueEnumerator::EnumerateOperandType(
const Value *V) {
864 assert(!isa<MetadataAsValue>(V) &&
"Unexpected metadata operand");
880 if (isa<BasicBlock>(
Op))
883 EnumerateOperandType(
Op);
887 void ValueEnumerator::EnumerateAttributes(
AttributeSet PAL) {
891 unsigned &Entry = AttributeMap[PAL];
901 unsigned &Entry = AttributeGroupMap[AS];
903 AttributeGroups.push_back(AS);
904 Entry = AttributeGroups.size();
910 InstructionCount = 0;
911 NumModuleValues = Values.size();
915 incorporateFunctionMetadata(F);
918 for (
const auto &I : F.
args())
921 FirstFuncConstantID = Values.size();
926 for (
const Use &OI : I.operands()) {
927 if ((isa<Constant>(OI) && !isa<GlobalValue>(OI)) || isa<InlineAsm>(OI))
930 BasicBlocks.push_back(&BB);
935 OptimizeConstants(FirstFuncConstantID, Values.size());
939 EnumerateAttributes(F.getAttributes());
941 FirstInstID = Values.size();
947 for (
const Use &OI : I.operands()) {
948 if (
auto *MD = dyn_cast<MetadataAsValue>(&OI))
949 if (
auto *Local = dyn_cast<LocalAsMetadata>(MD->getMetadata()))
954 if (!I.getType()->isVoidTy())
960 for (
unsigned i = 0, e = FnLocalMDVector.
size();
i != e; ++
i) {
964 "Missing value for metadata operand");
965 EnumerateFunctionLocalMetadata(F, FnLocalMDVector[
i]);
971 for (
unsigned i = NumModuleValues, e = Values.size();
i != e; ++
i)
973 for (
unsigned i = NumModuleMDs, e = MDs.size(); i != e; ++
i)
974 MetadataMap.
erase(MDs[i]);
975 for (
unsigned i = 0, e = BasicBlocks.size(); i != e; ++
i)
978 Values.resize(NumModuleValues);
979 MDs.resize(NumModuleMDs);
995 unsigned &Idx = GlobalBasicBlockIDs[BB];
void push_back(const T &Elt)
unsigned Log2_32_Ceil(uint32_t Value)
Log2_32_Ceil - This function returns the ceil log base 2 of the specified value, 32 if the value is z...
Tracking metadata reference owned by Metadata.
This class provides a symbol table of name/value pairs.
iterator_range< use_iterator > uses()
static unsigned getMetadataTypeOrder(const Metadata *MD)
LLVM Argument representation.
iterator begin()
Get an iterator that from the beginning of the symbol table.
ValueT lookup(const KeyT &Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
#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
Class to represent struct types.
A Use represents the edge between a Value definition and its users.
std::vector< UseListOrder > UseListOrderStack
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
iterator find(const KeyT &Val)
iterator end()
Get an iterator to the end of the symbol table.
static GCRegistry::Add< CoreCLRGC > E("coreclr","CoreCLR-compatible GC")
unsigned getNumSlots() const
Return the number of slots used in this attribute list.
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...
AttributeSet getSlotAttributes(unsigned Slot) const
Return the attributes at the given slot.
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...
iterator_range< named_metadata_iterator > named_metadata()
MDNode * getOperand(unsigned i) const
static OrderMap orderModule(const Module &M)
static UseListOrderStack predictUseListOrder(const Module &M)
void dump() const
Support for debugging, callable in GDB: V->dump()
reverse_iterator rbegin()
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
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()
static GCRegistry::Add< ShadowStackGC > C("shadow-stack","Very portable GC for uncooperative code generators")
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
iterator find(const KeyT &Val)
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()
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...
static GCRegistry::Add< ErlangGC > A("erlang","erlang-compatible garbage collector")
bool isEmpty() const
Return true if there are no attributes.
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()