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 (
F.hasPrefixData())
91 if (!isa<GlobalValue>(
F.getPrefixData()))
93 if (
F.hasPrologueData())
94 if (!isa<GlobalValue>(
F.getPrologueData()))
96 if (
F.hasPersonalityFn())
97 if (!isa<GlobalValue>(
F.getPersonalityFn()))
100 OM.LastGlobalConstantID = OM.size();
116 OM.LastGlobalValueID = OM.size();
119 if (
F.isDeclaration())
130 for (
const Value *Op :
I.operands())
131 if ((isa<Constant>(*Op) && !isa<GlobalValue>(*Op)) ||
142 unsigned ID,
const OrderMap &OM,
145 typedef std::pair<const Use *, unsigned> Entry;
149 if (OM.lookup(U.getUser()).first)
156 bool IsGlobalValue = OM.isGlobalValue(ID);
157 std::sort(List.
begin(), List.
end(), [&](
const Entry &L,
const Entry &R) {
158 const Use *LU = L.first;
159 const Use *RU = R.first;
163 auto LID = OM.lookup(LU->getUser()).first;
164 auto RID = OM.lookup(RU->getUser()).first;
172 if (OM.isGlobalValue(LID) && OM.isGlobalValue(RID))
193 return LU->getOperandNo() < RU->getOperandNo();
194 return LU->getOperandNo() > RU->getOperandNo();
199 [](
const Entry &L,
const Entry &R) {
return L.second < R.second; }))
204 Stack.emplace_back(V, F, List.
size());
205 assert(List.
size() == Stack.back().Shuffle.size() &&
"Wrong size");
206 for (
size_t I = 0, E = List.
size();
I != E; ++
I)
207 Stack.back().Shuffle[
I] = List[
I].second;
212 auto &IDPair = OM[V];
213 assert(IDPair.first &&
"Unmapped value");
219 IDPair.second =
true;
224 if (
const Constant *C = dyn_cast<Constant>(V))
225 if (C->getNumOperands())
226 for (
const Value *Op : C->operands())
227 if (isa<Constant>(Op))
254 for (
const Value *Op :
I.operands())
255 if (isa<Constant>(*Op) || isa<InlineAsm>(*Op))
271 if (
G.hasInitializer())
276 if (
F.hasPrefixData())
278 if (
F.hasPrologueData())
280 if (
F.hasPersonalityFn())
288 return V.first->getType()->isIntOrIntVectorTy();
291 ValueEnumerator::ValueEnumerator(
const Module &M,
292 bool ShouldPreserveUseListOrder)
294 ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) {
295 if (ShouldPreserveUseListOrder)
305 EnumerateAttributes(
F.getAttributes());
313 unsigned FirstConstant = Values.size();
317 if (GV.hasInitializer())
318 EnumerateValue(GV.getInitializer());
322 EnumerateValue(GA.getAliasee());
326 if (
F.hasPrefixData())
327 EnumerateValue(
F.getPrefixData());
331 if (
F.hasPrologueData())
332 EnumerateValue(
F.getPrologueData());
336 if (
I->hasPersonalityFn())
337 EnumerateValue(
I->getPersonalityFn());
347 EnumerateValueSymbolTable(M.getValueSymbolTable());
348 EnumerateNamedMetadata(M);
355 EnumerateType(
A.getType());
358 F.getAllMetadata(MDs);
359 for (
const auto &
I : MDs)
360 EnumerateMetadata(
I.second);
364 for (
const Use &Op :
I.operands()) {
367 EnumerateOperandType(Op);
372 if (isa<LocalAsMetadata>(MD->getMetadata()))
375 EnumerateMetadata(MD->getMetadata());
377 EnumerateType(
I.getType());
378 if (
const CallInst *CI = dyn_cast<CallInst>(&
I))
379 EnumerateAttributes(CI->getAttributes());
380 else if (
const InvokeInst *II = dyn_cast<InvokeInst>(&
I))
381 EnumerateAttributes(II->getAttributes());
385 I.getAllMetadataOtherThanDebugLoc(MDs);
386 for (
unsigned i = 0, e = MDs.size(); i != e; ++i)
387 EnumerateMetadata(MDs[i].second);
392 EnumerateMDNodeOperands(L);
397 OptimizeConstants(FirstConstant, Values.size());
402 assert(I != InstructionMap.
end() &&
"Instruction is not mapped!");
407 unsigned ComdatID = Comdats.
idFor(C);
408 assert(ComdatID &&
"Comdat not found!");
413 InstructionMap[
I] = InstructionCount++;
417 if (
auto *MD = dyn_cast<MetadataAsValue>(V))
421 assert(I !=
ValueMap.
end() &&
"Value not in slotcalculator!");
433 const char *
Name)
const {
435 OS <<
"Map Name: " << Name <<
"\n";
436 OS <<
"Size: " << Map.
size() <<
"\n";
438 E = Map.
end();
I != E; ++
I) {
440 const Value *V =
I->first;
442 OS <<
"Value: " << V->
getName();
444 OS <<
"Value: [null]\n";
448 for (
const Use &U : V->
uses()) {
452 OS <<
" " << U->getName();
462 const char *
Name)
const {
464 OS <<
"Map Name: " << Name <<
"\n";
465 OS <<
"Size: " << Map.
size() <<
"\n";
466 for (
auto I = Map.
begin(), E = Map.
end();
I != E; ++
I) {
468 OS <<
"Metadata: slot = " <<
I->second <<
"\n";
474 void ValueEnumerator::OptimizeConstants(
unsigned CstStart,
unsigned CstEnd) {
475 if (CstStart == CstEnd || CstStart+1 == CstEnd)
return;
477 if (ShouldPreserveUseListOrder)
482 std::stable_sort(Values.begin() + CstStart, Values.begin() + CstEnd,
483 [
this](
const std::pair<const Value *, unsigned> &LHS,
484 const std::pair<const Value *, unsigned> &RHS) {
486 if (LHS.first->getType() != RHS.first->getType())
489 return LHS.second > RHS.second;
495 std::partition(Values.begin()+CstStart, Values.begin()+CstEnd,
499 for (; CstStart != CstEnd; ++CstStart)
500 ValueMap[Values[CstStart].first] = CstStart+1;
506 void ValueEnumerator::EnumerateValueSymbolTable(
const ValueSymbolTable &VST) {
509 EnumerateValue(
VI->getValue());
514 void ValueEnumerator::EnumerateNamedMetadata(
const Module &M) {
518 EnumerateNamedMDNode(
I);
521 void ValueEnumerator::EnumerateNamedMDNode(
const NamedMDNode *MD) {
528 void ValueEnumerator::EnumerateMDNodeOperands(
const MDNode *
N) {
533 assert(!isa<LocalAsMetadata>(MD) &&
"MDNodes cannot be function-local");
534 EnumerateMetadata(MD);
538 void ValueEnumerator::EnumerateMetadata(
const Metadata *MD) {
540 (isa<MDNode>(MD) || isa<MDString>(MD) || isa<ConstantAsMetadata>(MD)) &&
541 "Invalid metadata kind");
547 if (!MDValueMap.
insert(std::make_pair(MD, 0)).second)
551 if (
auto *N = dyn_cast<MDNode>(MD))
552 EnumerateMDNodeOperands(N);
553 else if (
auto *
C = dyn_cast<ConstantAsMetadata>(MD))
554 EnumerateValue(
C->getValue());
556 HasMDString |= isa<MDString>(MD);
557 HasDILocation |= isa<DILocation>(MD);
558 HasGenericDINode |= isa<GenericDINode>(MD);
563 MDValueMap[MD] = MDs.size();
568 void ValueEnumerator::EnumerateFunctionLocalMetadata(
571 unsigned &MDValueID = MDValueMap[
Local];
575 MDs.push_back(Local);
576 MDValueID = MDs.size();
581 FunctionLocalMDs.push_back(Local);
584 void ValueEnumerator::EnumerateValue(
const Value *V) {
586 assert(!isa<MetadataAsValue>(V) &&
"EnumerateValue doesn't handle Metadata!");
592 Values[ValueID-1].second++;
596 if (
auto *GO = dyn_cast<GlobalObject>(V))
597 if (
const Comdat *
C = GO->getComdat())
603 if (
const Constant *
C = dyn_cast<Constant>(V)) {
604 if (isa<GlobalValue>(
C)) {
606 }
else if (
C->getNumOperands()) {
617 if (!isa<BasicBlock>(*
I))
622 Values.push_back(std::make_pair(V, 1U));
629 Values.push_back(std::make_pair(V, 1U));
630 ValueID = Values.size();
634 void ValueEnumerator::EnumerateType(
Type *Ty) {
635 unsigned *
TypeID = &TypeMap[Ty];
644 if (
StructType *STy = dyn_cast<StructType>(Ty))
645 if (!STy->isLiteral())
651 EnumerateType(SubTy);
654 TypeID = &TypeMap[Ty];
661 if (*TypeID && *TypeID != ~0U)
667 *TypeID = Types.size();
672 void ValueEnumerator::EnumerateOperandType(
const Value *V) {
675 if (
auto *MD = dyn_cast<MetadataAsValue>(V)) {
676 assert(!isa<LocalAsMetadata>(MD->getMetadata()) &&
677 "Function-local metadata should be left for later");
679 EnumerateMetadata(MD->getMetadata());
697 if (isa<BasicBlock>(Op))
700 EnumerateOperandType(Op);
704 void ValueEnumerator::EnumerateAttributes(
AttributeSet PAL) {
708 unsigned &Entry = AttributeMap[PAL];
716 for (
unsigned i = 0, e = PAL.
getNumSlots(); i != e; ++i) {
718 unsigned &Entry = AttributeGroupMap[AS];
720 AttributeGroups.push_back(AS);
721 Entry = AttributeGroups.size();
727 InstructionCount = 0;
728 NumModuleValues = Values.size();
729 NumModuleMDs = MDs.size();
736 FirstFuncConstantID = Values.size();
743 if ((isa<Constant>(*OI) && !isa<GlobalValue>(*OI)) ||
747 BasicBlocks.push_back(BB);
752 OptimizeConstants(FirstFuncConstantID, Values.size());
758 FirstInstID = Values.size();
766 if (
auto *MD = dyn_cast<MetadataAsValue>(&*OI))
767 if (
auto *Local = dyn_cast<LocalAsMetadata>(MD->getMetadata()))
772 if (!
I->getType()->isVoidTy())
778 for (
unsigned i = 0, e = FnLocalMDVector.
size(); i != e; ++i)
779 EnumerateFunctionLocalMetadata(FnLocalMDVector[i]);
784 for (
unsigned i = NumModuleValues, e = Values.size(); i != e; ++i)
786 for (
unsigned i = NumModuleMDs, e = MDs.size(); i != e; ++i)
787 MDValueMap.
erase(MDs[i]);
788 for (
unsigned i = 0, e = BasicBlocks.size(); i != e; ++i)
791 Values.resize(NumModuleValues);
792 MDs.resize(NumModuleMDs);
794 FunctionLocalMDs.clear();
799 unsigned Counter = 0;
801 IDMap[BB] = ++Counter;
808 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...
This class provides a symbol table of name/value pairs.
iterator_range< use_iterator > uses()
LLVM Argument representation.
iterator begin()
Get an iterator that from the beginning of the symbol table.
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. ...
unsigned getNumOperands() const
Return number of MDNode operands.
size_type count(const KeyT &Val) const
Return 1 if the specified key is in the map, 0 otherwise.
CallInst - This class represents a function call, abstracting a target machine's calling convention...
named_metadata_iterator named_metadata_end()
void setInstructionID(const Instruction *I)
const Function * getParent() const
Return the enclosing method, or null if none.
static Type * getMetadataTy(LLVMContext &C)
static void predictValueUseListOrderImpl(const Value *V, const Function *F, unsigned ID, const OrderMap &OM, UseListOrderStack &Stack)
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
StructType - 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.
uint64_t computeBitsRequiredForTypeIndicies() const
unsigned idFor(const T &Entry) const
idFor - return the ID for an existing entry.
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.
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.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
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()
const MDOperand & getOperand(unsigned I) const
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.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
AttributeSet getAttributes() const
Return the attribute list for this Function.
unsigned getMetadataID(const Metadata *MD) const
UseListOrderStack UseListOrders
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
static const uint16_t * lookup(unsigned opcode, unsigned domain)
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...
iterator find(const KeyT &Val)
static void IncorporateFunctionInfoGlobalBBIDs(const Function *F, DenseMap< const BasicBlock *, unsigned > &IDMap)
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...
InvokeInst - Invoke instruction.
iterator_range< global_iterator > globals()
bool isEmpty() const
Return true if there are no attributes.
named_metadata_iterator named_metadata_begin()
bool erase(const KeyT &Val)
ArrayRef< Type * > subtypes() const
bool isVoidTy() const
isVoidTy - Return true if this is 'void'.
iterator_range< alias_iterator > aliases()