35 OnlyNamed = onlyNamed;
38 for (
const auto &
G :
M.globals()) {
39 incorporateType(
G.getValueType());
40 if (
G.hasInitializer())
41 incorporateValue(
G.getInitializer());
45 for (
const auto &A :
M.aliases()) {
46 incorporateType(A.getValueType());
47 if (
const Value *Aliasee = A.getAliasee())
48 incorporateValue(Aliasee);
52 for (
const auto &GI :
M.ifuncs())
53 incorporateType(GI.getValueType());
58 incorporateType(FI.getFunctionType());
59 incorporateAttributes(FI.getAttributes());
61 for (
const Use &U : FI.operands())
62 incorporateValue(U.get());
65 for (
const auto &A : FI.args())
71 incorporateType(
I.getType());
75 for (
const auto &
O :
I.operands())
76 if (&*
O && !isa<Instruction>(&*
O))
77 incorporateValue(&*
O);
79 if (
auto *
GEP = dyn_cast<GetElementPtrInst>(&
I))
80 incorporateType(
GEP->getSourceElementType());
81 if (
auto *AI = dyn_cast<AllocaInst>(&
I))
82 incorporateType(AI->getAllocatedType());
83 if (
const auto *CB = dyn_cast<CallBase>(&
I))
84 incorporateAttributes(CB->getAttributes());
87 I.getAllMetadataOtherThanDebugLoc(MDForInst);
88 for (
const auto &MD : MDForInst)
89 incorporateMDNode(MD.second);
94 for (
const auto &NMD :
M.named_metadata())
95 for (
const auto *MDOp : NMD.operands())
96 incorporateMDNode(MDOp);
100 VisitedConstants.
clear();
101 VisitedTypes.clear();
107 void TypeFinder::incorporateType(
Type *Ty) {
109 if (!VisitedTypes.insert(Ty).second)
113 TypeWorklist.push_back(Ty);
118 if (
StructType *STy = dyn_cast<StructType>(Ty))
119 if (!OnlyNamed || STy->hasName())
120 StructTypes.push_back(STy);
124 if (VisitedTypes.insert(SubTy).second)
125 TypeWorklist.push_back(SubTy);
126 }
while (!TypeWorklist.empty());
133 void TypeFinder::incorporateValue(
const Value *V) {
134 if (
const auto *M = dyn_cast<MetadataAsValue>(V)) {
135 if (
const auto *
N = dyn_cast<MDNode>(
M->getMetadata()))
136 return incorporateMDNode(
N);
137 if (
const auto *MDV = dyn_cast<ValueAsMetadata>(
M->getMetadata()))
138 return incorporateValue(MDV->getValue());
142 if (!isa<Constant>(V) || isa<GlobalValue>(V))
return;
145 if (!VisitedConstants.
insert(V).second)
152 if (isa<Instruction>(V))
155 if (
auto *
GEP = dyn_cast<GEPOperator>(V))
156 incorporateType(
GEP->getSourceElementType());
159 const User *U = cast<User>(V);
161 incorporateValue(&*
I);
166 void TypeFinder::incorporateMDNode(
const MDNode *V) {
168 if (!VisitedMetadata.insert(V).second)
173 if (
const auto *
AL = dyn_cast<DIArgList>(V)) {
174 for (
auto *
Arg :
AL->getArgs())
175 incorporateValue(
Arg->getValue());
183 if (
auto *
N = dyn_cast<MDNode>(
Op)) {
184 incorporateMDNode(
N);
187 if (
auto *
C = dyn_cast<ConstantAsMetadata>(
Op)) {
188 incorporateValue(
C->getValue());
195 if (!VisitedAttributes.insert(
AL).second)
200 if (
A.isTypeAttribute())
201 incorporateType(
A.getValueAsType());