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