1 //===- TypeFinder.cpp - Implement the TypeFinder class --------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the TypeFinder class for the IR library.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/IR/TypeFinder.h"
14 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/IR/BasicBlock.h"
16 #include "llvm/IR/Constant.h"
17 #include "llvm/IR/DebugInfoMetadata.h"
18 #include "llvm/IR/DerivedTypes.h"
19 #include "llvm/IR/Function.h"
20 #include "llvm/IR/Instruction.h"
21 #include "llvm/IR/Metadata.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/IR/Type.h"
24 #include "llvm/IR/Use.h"
25 #include "llvm/IR/User.h"
26 #include "llvm/IR/Value.h"
27 #include "llvm/Support/Casting.h"
28 #include <utility>
29 
30 using namespace llvm;
31 
32 void TypeFinder::run(const Module &M, bool onlyNamed) {
33   OnlyNamed = onlyNamed;
34 
35   // Get types from global variables.
36   for (const auto &G : M.globals()) {
37     incorporateType(G.getType());
38     if (G.hasInitializer())
39       incorporateValue(G.getInitializer());
40   }
41 
42   // Get types from aliases.
43   for (const auto &A : M.aliases()) {
44     incorporateType(A.getType());
45     if (const Value *Aliasee = A.getAliasee())
46       incorporateValue(Aliasee);
47   }
48 
49   // Get types from functions.
50   SmallVector<std::pair<unsigned, MDNode *>, 4> MDForInst;
51   for (const Function &FI : M) {
52     incorporateType(FI.getType());
53 
54     for (const Use &U : FI.operands())
55       incorporateValue(U.get());
56 
57     // First incorporate the arguments.
58     for (const auto &A : FI.args())
59       incorporateValue(&A);
60 
61     for (const BasicBlock &BB : FI)
62       for (const Instruction &I : BB) {
63         // Incorporate the type of the instruction.
64         incorporateType(I.getType());
65 
66         // Incorporate non-instruction operand types. (We are incorporating all
67         // instructions with this loop.)
68         for (const auto &O : I.operands())
69           if (&*O && !isa<Instruction>(&*O))
70             incorporateValue(&*O);
71 
72         // Incorporate types hiding in metadata.
73         I.getAllMetadataOtherThanDebugLoc(MDForInst);
74         for (const auto &MD : MDForInst)
75           incorporateMDNode(MD.second);
76         MDForInst.clear();
77       }
78   }
79 
80   for (const auto &NMD : M.named_metadata())
81     for (const auto *MDOp : NMD.operands())
82       incorporateMDNode(MDOp);
83 }
84 
85 void TypeFinder::clear() {
86   VisitedConstants.clear();
87   VisitedTypes.clear();
88   StructTypes.clear();
89 }
90 
91 /// incorporateType - This method adds the type to the list of used structures
92 /// if it's not in there already.
93 void TypeFinder::incorporateType(Type *Ty) {
94   // Check to see if we've already visited this type.
95   if (!VisitedTypes.insert(Ty).second)
96     return;
97 
98   SmallVector<Type *, 4> TypeWorklist;
99   TypeWorklist.push_back(Ty);
100   do {
101     Ty = TypeWorklist.pop_back_val();
102 
103     // If this is a structure or opaque type, add a name for the type.
104     if (StructType *STy = dyn_cast<StructType>(Ty))
105       if (!OnlyNamed || STy->hasName())
106         StructTypes.push_back(STy);
107 
108     // Add all unvisited subtypes to worklist for processing
109     for (Type *SubTy : llvm::reverse(Ty->subtypes()))
110       if (VisitedTypes.insert(SubTy).second)
111         TypeWorklist.push_back(SubTy);
112   } while (!TypeWorklist.empty());
113 }
114 
115 /// incorporateValue - This method is used to walk operand lists finding types
116 /// hiding in constant expressions and other operands that won't be walked in
117 /// other ways.  GlobalValues, basic blocks, instructions, and inst operands are
118 /// all explicitly enumerated.
119 void TypeFinder::incorporateValue(const Value *V) {
120   if (const auto *M = dyn_cast<MetadataAsValue>(V)) {
121     if (const auto *N = dyn_cast<MDNode>(M->getMetadata()))
122       return incorporateMDNode(N);
123     if (const auto *MDV = dyn_cast<ValueAsMetadata>(M->getMetadata()))
124       return incorporateValue(MDV->getValue());
125     return;
126   }
127 
128   if (!isa<Constant>(V) || isa<GlobalValue>(V)) return;
129 
130   // Already visited?
131   if (!VisitedConstants.insert(V).second)
132     return;
133 
134   // Check this type.
135   incorporateType(V->getType());
136 
137   // If this is an instruction, we incorporate it separately.
138   if (isa<Instruction>(V))
139     return;
140 
141   // Look in operands for types.
142   const User *U = cast<User>(V);
143   for (const auto &I : U->operands())
144     incorporateValue(&*I);
145 }
146 
147 /// incorporateMDNode - This method is used to walk the operands of an MDNode to
148 /// find types hiding within.
149 void TypeFinder::incorporateMDNode(const MDNode *V) {
150   // Already visited?
151   if (!VisitedMetadata.insert(V).second)
152     return;
153 
154   // The arguments in DIArgList are not exposed as operands, so handle such
155   // nodes specifically here.
156   if (const auto *AL = dyn_cast<DIArgList>(V)) {
157     for (auto *Arg : AL->getArgs())
158       incorporateValue(Arg->getValue());
159     return;
160   }
161 
162   // Look in operands for types.
163   for (Metadata *Op : V->operands()) {
164     if (!Op)
165       continue;
166     if (auto *N = dyn_cast<MDNode>(Op)) {
167       incorporateMDNode(N);
168       continue;
169     }
170     if (auto *C = dyn_cast<ConstantAsMetadata>(Op)) {
171       incorporateValue(C->getValue());
172       continue;
173     }
174   }
175 }
176