LLVM API Documentation

TypeFinder.cpp
Go to the documentation of this file.
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     if (FI->hasPrefixData())
00048       incorporateValue(FI->getPrefixData());
00049 
00050     if (FI->hasPrologueData())
00051       incorporateValue(FI->getPrologueData());
00052 
00053     // First incorporate the arguments.
00054     for (Function::const_arg_iterator AI = FI->arg_begin(),
00055            AE = FI->arg_end(); AI != AE; ++AI)
00056       incorporateValue(AI);
00057 
00058     for (Function::const_iterator BB = FI->begin(), E = FI->end();
00059          BB != E;++BB)
00060       for (BasicBlock::const_iterator II = BB->begin(),
00061              E = BB->end(); II != E; ++II) {
00062         const Instruction &I = *II;
00063 
00064         // Incorporate the type of the instruction.
00065         incorporateType(I.getType());
00066 
00067         // Incorporate non-instruction operand types. (We are incorporating all
00068         // instructions with this loop.)
00069         for (User::const_op_iterator OI = I.op_begin(), OE = I.op_end();
00070              OI != OE; ++OI)
00071           if (!isa<Instruction>(OI))
00072             incorporateValue(*OI);
00073 
00074         // Incorporate types hiding in metadata.
00075         I.getAllMetadataOtherThanDebugLoc(MDForInst);
00076         for (unsigned i = 0, e = MDForInst.size(); i != e; ++i)
00077           incorporateMDNode(MDForInst[i].second);
00078 
00079         MDForInst.clear();
00080       }
00081   }
00082 
00083   for (Module::const_named_metadata_iterator I = M.named_metadata_begin(),
00084          E = M.named_metadata_end(); I != E; ++I) {
00085     const NamedMDNode *NMD = I;
00086     for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
00087       incorporateMDNode(NMD->getOperand(i));
00088   }
00089 }
00090 
00091 void TypeFinder::clear() {
00092   VisitedConstants.clear();
00093   VisitedTypes.clear();
00094   StructTypes.clear();
00095 }
00096 
00097 /// incorporateType - This method adds the type to the list of used structures
00098 /// if it's not in there already.
00099 void TypeFinder::incorporateType(Type *Ty) {
00100   // Check to see if we've already visited this type.
00101   if (!VisitedTypes.insert(Ty).second)
00102     return;
00103 
00104   SmallVector<Type *, 4> TypeWorklist;
00105   TypeWorklist.push_back(Ty);
00106   do {
00107     Ty = TypeWorklist.pop_back_val();
00108 
00109     // If this is a structure or opaque type, add a name for the type.
00110     if (StructType *STy = dyn_cast<StructType>(Ty))
00111       if (!OnlyNamed || STy->hasName())
00112         StructTypes.push_back(STy);
00113 
00114     // Add all unvisited subtypes to worklist for processing
00115     for (Type::subtype_reverse_iterator I = Ty->subtype_rbegin(),
00116                                         E = Ty->subtype_rend();
00117          I != E; ++I)
00118       if (VisitedTypes.insert(*I).second)
00119         TypeWorklist.push_back(*I);
00120   } while (!TypeWorklist.empty());
00121 }
00122 
00123 /// incorporateValue - This method is used to walk operand lists finding types
00124 /// hiding in constant expressions and other operands that won't be walked in
00125 /// other ways.  GlobalValues, basic blocks, instructions, and inst operands are
00126 /// all explicitly enumerated.
00127 void TypeFinder::incorporateValue(const Value *V) {
00128   if (const auto *M = dyn_cast<MetadataAsValue>(V)) {
00129     if (const auto *N = dyn_cast<MDNode>(M->getMetadata()))
00130       return incorporateMDNode(N);
00131     if (const auto *MDV = dyn_cast<ValueAsMetadata>(M->getMetadata()))
00132       return incorporateValue(MDV->getValue());
00133     return;
00134   }
00135 
00136   if (!isa<Constant>(V) || isa<GlobalValue>(V)) return;
00137 
00138   // Already visited?
00139   if (!VisitedConstants.insert(V).second)
00140     return;
00141 
00142   // Check this type.
00143   incorporateType(V->getType());
00144 
00145   // If this is an instruction, we incorporate it separately.
00146   if (isa<Instruction>(V))
00147     return;
00148 
00149   // Look in operands for types.
00150   const User *U = cast<User>(V);
00151   for (Constant::const_op_iterator I = U->op_begin(),
00152          E = U->op_end(); I != E;++I)
00153     incorporateValue(*I);
00154 }
00155 
00156 /// incorporateMDNode - This method is used to walk the operands of an MDNode to
00157 /// find types hiding within.
00158 void TypeFinder::incorporateMDNode(const MDNode *V) {
00159   // Already visited?
00160   if (!VisitedMetadata.insert(V).second)
00161     return;
00162 
00163   // Look in operands for types.
00164   for (unsigned i = 0, e = V->getNumOperands(); i != e; ++i) {
00165     Metadata *Op = V->getOperand(i);
00166     if (!Op)
00167       continue;
00168     if (auto *N = dyn_cast<MDNode>(Op)) {
00169       incorporateMDNode(N);
00170       continue;
00171     }
00172     if (auto *C = dyn_cast<ConstantAsMetadata>(Op)) {
00173       incorporateValue(C->getValue());
00174       continue;
00175     }
00176   }
00177 }