LLVM API Documentation
00001 //===-- TypeFinder.cpp - Implement the TypeFinder class -------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the TypeFinder class for the IR library. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "llvm/IR/TypeFinder.h" 00015 #include "llvm/ADT/SmallVector.h" 00016 #include "llvm/IR/BasicBlock.h" 00017 #include "llvm/IR/DerivedTypes.h" 00018 #include "llvm/IR/Function.h" 00019 #include "llvm/IR/Metadata.h" 00020 #include "llvm/IR/Module.h" 00021 using namespace llvm; 00022 00023 void TypeFinder::run(const Module &M, bool onlyNamed) { 00024 OnlyNamed = onlyNamed; 00025 00026 // Get types from global variables. 00027 for (Module::const_global_iterator I = M.global_begin(), 00028 E = M.global_end(); I != E; ++I) { 00029 incorporateType(I->getType()); 00030 if (I->hasInitializer()) 00031 incorporateValue(I->getInitializer()); 00032 } 00033 00034 // Get types from aliases. 00035 for (Module::const_alias_iterator I = M.alias_begin(), 00036 E = M.alias_end(); I != E; ++I) { 00037 incorporateType(I->getType()); 00038 if (const Value *Aliasee = I->getAliasee()) 00039 incorporateValue(Aliasee); 00040 } 00041 00042 // Get types from functions. 00043 SmallVector<std::pair<unsigned, MDNode*>, 4> MDForInst; 00044 for (Module::const_iterator FI = M.begin(), E = M.end(); FI != E; ++FI) { 00045 incorporateType(FI->getType()); 00046 00047 // First incorporate the arguments. 00048 for (Function::const_arg_iterator AI = FI->arg_begin(), 00049 AE = FI->arg_end(); AI != AE; ++AI) 00050 incorporateValue(AI); 00051 00052 for (Function::const_iterator BB = FI->begin(), E = FI->end(); 00053 BB != E;++BB) 00054 for (BasicBlock::const_iterator II = BB->begin(), 00055 E = BB->end(); II != E; ++II) { 00056 const Instruction &I = *II; 00057 00058 // Incorporate the type of the instruction. 00059 incorporateType(I.getType()); 00060 00061 // Incorporate non-instruction operand types. (We are incorporating all 00062 // instructions with this loop.) 00063 for (User::const_op_iterator OI = I.op_begin(), OE = I.op_end(); 00064 OI != OE; ++OI) 00065 if (!isa<Instruction>(OI)) 00066 incorporateValue(*OI); 00067 00068 // Incorporate types hiding in metadata. 00069 I.getAllMetadataOtherThanDebugLoc(MDForInst); 00070 for (unsigned i = 0, e = MDForInst.size(); i != e; ++i) 00071 incorporateMDNode(MDForInst[i].second); 00072 00073 MDForInst.clear(); 00074 } 00075 } 00076 00077 for (Module::const_named_metadata_iterator I = M.named_metadata_begin(), 00078 E = M.named_metadata_end(); I != E; ++I) { 00079 const NamedMDNode *NMD = I; 00080 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) 00081 incorporateMDNode(NMD->getOperand(i)); 00082 } 00083 } 00084 00085 void TypeFinder::clear() { 00086 VisitedConstants.clear(); 00087 VisitedTypes.clear(); 00088 StructTypes.clear(); 00089 } 00090 00091 /// incorporateType - This method adds the type to the list of used structures 00092 /// if it's not in there already. 00093 void TypeFinder::incorporateType(Type *Ty) { 00094 // Check to see if we're already visited this type. 00095 if (!VisitedTypes.insert(Ty).second) 00096 return; 00097 00098 // If this is a structure or opaque type, add a name for the type. 00099 if (StructType *STy = dyn_cast<StructType>(Ty)) 00100 if (!OnlyNamed || STy->hasName()) 00101 StructTypes.push_back(STy); 00102 00103 // Recursively walk all contained types. 00104 for (Type::subtype_iterator I = Ty->subtype_begin(), 00105 E = Ty->subtype_end(); I != E; ++I) 00106 incorporateType(*I); 00107 } 00108 00109 /// incorporateValue - This method is used to walk operand lists finding types 00110 /// hiding in constant expressions and other operands that won't be walked in 00111 /// other ways. GlobalValues, basic blocks, instructions, and inst operands are 00112 /// all explicitly enumerated. 00113 void TypeFinder::incorporateValue(const Value *V) { 00114 if (const MDNode *M = dyn_cast<MDNode>(V)) 00115 return incorporateMDNode(M); 00116 00117 if (!isa<Constant>(V) || isa<GlobalValue>(V)) return; 00118 00119 // Already visited? 00120 if (!VisitedConstants.insert(V).second) 00121 return; 00122 00123 // Check this type. 00124 incorporateType(V->getType()); 00125 00126 // If this is an instruction, we incorporate it separately. 00127 if (isa<Instruction>(V)) 00128 return; 00129 00130 // Look in operands for types. 00131 const User *U = cast<User>(V); 00132 for (Constant::const_op_iterator I = U->op_begin(), 00133 E = U->op_end(); I != E;++I) 00134 incorporateValue(*I); 00135 } 00136 00137 /// incorporateMDNode - This method is used to walk the operands of an MDNode to 00138 /// find types hiding within. 00139 void TypeFinder::incorporateMDNode(const MDNode *V) { 00140 // Already visited? 00141 if (!VisitedConstants.insert(V).second) 00142 return; 00143 00144 // Look in operands for types. 00145 for (unsigned i = 0, e = V->getNumOperands(); i != e; ++i) 00146 if (Value *Op = V->getOperand(i)) 00147 incorporateValue(Op); 00148 }