X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=lib%2FVMCore%2FModule.cpp;h=78f4eb82dcf0c49d0c022956646130525a85a6e0;hb=67ae13575900e8efd056672987249fd0adbf5e73;hp=3c67191e09c2d570563dc6142e8db9a645384968;hpb=cf84d867619b68be17bb1c12bd9831fdd73a88e4;p=oota-llvm.git diff --git a/lib/VMCore/Module.cpp b/lib/VMCore/Module.cpp index 3c67191e09c..78f4eb82dcf 100644 --- a/lib/VMCore/Module.cpp +++ b/lib/VMCore/Module.cpp @@ -12,17 +12,17 @@ //===----------------------------------------------------------------------===// #include "llvm/Module.h" -#include "llvm/InstrTypes.h" +#include "SymbolTableListTraitsImpl.h" +#include "llvm/ADT/DenseSet.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/SmallString.h" +#include "llvm/ADT/StringExtras.h" #include "llvm/Constants.h" #include "llvm/DerivedTypes.h" #include "llvm/GVMaterializer.h" +#include "llvm/InstrTypes.h" #include "llvm/LLVMContext.h" -#include "llvm/ADT/DenseSet.h" -#include "llvm/ADT/SmallString.h" -#include "llvm/ADT/STLExtras.h" -#include "llvm/ADT/StringExtras.h" #include "llvm/Support/LeakDetector.h" -#include "SymbolTableListTraitsImpl.h" #include #include #include @@ -55,7 +55,6 @@ Module::~Module() { GlobalList.clear(); FunctionList.clear(); AliasList.clear(); - LibraryList.clear(); NamedMDList.clear(); delete ValSymTab; delete static_cast *>(NamedMDSymTab); @@ -65,20 +64,20 @@ Module::~Module() { Module::Endianness Module::getEndianness() const { StringRef temp = DataLayout; Module::Endianness ret = AnyEndianness; - + while (!temp.empty()) { std::pair P = getToken(temp, "-"); - + StringRef token = P.first; temp = P.second; - + if (token[0] == 'e') { ret = LittleEndian; } else if (token[0] == 'E') { ret = BigEndian; } } - + return ret; } @@ -86,13 +85,13 @@ Module::Endianness Module::getEndianness() const { Module::PointerSize Module::getPointerSize() const { StringRef temp = DataLayout; Module::PointerSize ret = AnyPointerSize; - + while (!temp.empty()) { std::pair TmpP = getToken(temp, "-"); temp = TmpP.second; TmpP = getToken(TmpP.first, ":"); StringRef token = TmpP.second, signalToken = TmpP.first; - + if (signalToken[0] == 'p') { int size = 0; getToken(token, ":").first.getAsInteger(10, size); @@ -102,7 +101,7 @@ Module::PointerSize Module::getPointerSize() const { ret = Pointer64; } } - + return ret; } @@ -138,7 +137,7 @@ void Module::getMDKindNames(SmallVectorImpl &Result) const { // Constant *Module::getOrInsertFunction(StringRef Name, FunctionType *Ty, - AttrListPtr AttributeList) { + AttributeSet AttributeList) { // See if we have a definition for the specified function already. GlobalValue *F = getNamedValue(Name); if (F == 0) { @@ -164,14 +163,14 @@ Constant *Module::getOrInsertFunction(StringRef Name, // right type. if (F->getType() != PointerType::getUnqual(Ty)) return ConstantExpr::getBitCast(F, PointerType::getUnqual(Ty)); - + // Otherwise, we just found the existing function or a prototype. - return F; + return F; } Constant *Module::getOrInsertTargetIntrinsic(StringRef Name, FunctionType *Ty, - AttrListPtr AttributeList) { + AttributeSet AttributeList) { // See if we have a definition for the specified function already. GlobalValue *F = getNamedValue(Name); if (F == 0) { @@ -183,13 +182,12 @@ Constant *Module::getOrInsertTargetIntrinsic(StringRef Name, } // Otherwise, we just found the existing function or a prototype. - return F; + return F; } Constant *Module::getOrInsertFunction(StringRef Name, FunctionType *Ty) { - AttrListPtr AttributeList = AttrListPtr::get((AttributeWithIndex *)0, 0); - return getOrInsertFunction(Name, Ty, AttributeList); + return getOrInsertFunction(Name, Ty, AttributeSet()); } // getOrInsertFunction - Look up the specified function in the module symbol @@ -198,7 +196,7 @@ Constant *Module::getOrInsertFunction(StringRef Name, // arguments, which makes it easier for clients to use. // Constant *Module::getOrInsertFunction(StringRef Name, - AttrListPtr AttributeList, + AttributeSet AttributeList, Type *RetTy, ...) { va_list Args; va_start(Args, RetTy); @@ -229,9 +227,9 @@ Constant *Module::getOrInsertFunction(StringRef Name, va_end(Args); // Build the function type and chain to the other getOrInsertFunction... - return getOrInsertFunction(Name, + return getOrInsertFunction(Name, FunctionType::get(RetTy, ArgTys, false), - AttrListPtr::get((AttributeWithIndex *)0, 0)); + AttributeSet()); } // getFunction - Look up the specified function in the module symbol table. @@ -254,7 +252,7 @@ Function *Module::getFunction(StringRef Name) const { /// GlobalVariable *Module::getGlobalVariable(StringRef Name, bool AllowLocal) const { - if (GlobalVariable *Result = + if (GlobalVariable *Result = dyn_cast_or_null(getNamedValue(Name))) if (AllowLocal || !Result->hasLocalLinkage()) return Result; @@ -282,7 +280,7 @@ Constant *Module::getOrInsertGlobal(StringRef Name, Type *Ty) { // right type. if (GV->getType() != PointerType::getUnqual(Ty)) return ConstantExpr::getBitCast(GV, PointerType::getUnqual(Ty)); - + // Otherwise, we just found the existing function or a prototype. return GV; } @@ -299,7 +297,7 @@ GlobalAlias *Module::getNamedAlias(StringRef Name) const { } /// getNamedMetadata - Return the first NamedMDNode in the module with the -/// specified name. This method returns null if a NamedMDNode with the +/// specified name. This method returns null if a NamedMDNode with the /// specified name is not found. NamedMDNode *Module::getNamedMetadata(const Twine &Name) const { SmallString<256> NameData; @@ -307,8 +305,8 @@ NamedMDNode *Module::getNamedMetadata(const Twine &Name) const { return static_cast *>(NamedMDSymTab)->lookup(NameRef); } -/// getOrInsertNamedMetadata - Return the first named MDNode in the module -/// with the specified name. This method returns a new NamedMDNode if a +/// getOrInsertNamedMetadata - Return the first named MDNode in the module +/// with the specified name. This method returns a new NamedMDNode if a /// NamedMDNode with the specified name is not found. NamedMDNode *Module::getOrInsertNamedMetadata(StringRef Name) { NamedMDNode *&NMD = @@ -451,145 +449,3 @@ void Module::dropAllReferences() { for(Module::alias_iterator I = alias_begin(), E = alias_end(); I != E; ++I) I->dropAllReferences(); } - -void Module::addLibrary(StringRef Lib) { - for (Module::lib_iterator I = lib_begin(), E = lib_end(); I != E; ++I) - if (*I == Lib) - return; - LibraryList.push_back(Lib); -} - -void Module::removeLibrary(StringRef Lib) { - LibraryListType::iterator I = LibraryList.begin(); - LibraryListType::iterator E = LibraryList.end(); - for (;I != E; ++I) - if (*I == Lib) { - LibraryList.erase(I); - return; - } -} - -//===----------------------------------------------------------------------===// -// Type finding functionality. -//===----------------------------------------------------------------------===// - -namespace { - /// TypeFinder - Walk over a module, identifying all of the types that are - /// used by the module. - class TypeFinder { - // To avoid walking constant expressions multiple times and other IR - // objects, we keep several helper maps. - DenseSet VisitedConstants; - DenseSet VisitedTypes; - - std::vector &StructTypes; - public: - TypeFinder(std::vector &structTypes) - : StructTypes(structTypes) {} - - void run(const Module &M) { - // Get types from global variables. - for (Module::const_global_iterator I = M.global_begin(), - E = M.global_end(); I != E; ++I) { - incorporateType(I->getType()); - if (I->hasInitializer()) - incorporateValue(I->getInitializer()); - } - - // Get types from aliases. - for (Module::const_alias_iterator I = M.alias_begin(), - E = M.alias_end(); I != E; ++I) { - incorporateType(I->getType()); - if (const Value *Aliasee = I->getAliasee()) - incorporateValue(Aliasee); - } - - SmallVector, 4> MDForInst; - - // Get types from functions. - for (Module::const_iterator FI = M.begin(), E = M.end(); FI != E; ++FI) { - incorporateType(FI->getType()); - - for (Function::const_iterator BB = FI->begin(), E = FI->end(); - BB != E;++BB) - for (BasicBlock::const_iterator II = BB->begin(), - E = BB->end(); II != E; ++II) { - const Instruction &I = *II; - // Incorporate the type of the instruction and all its operands. - incorporateType(I.getType()); - for (User::const_op_iterator OI = I.op_begin(), OE = I.op_end(); - OI != OE; ++OI) - incorporateValue(*OI); - - // Incorporate types hiding in metadata. - I.getAllMetadataOtherThanDebugLoc(MDForInst); - for (unsigned i = 0, e = MDForInst.size(); i != e; ++i) - incorporateMDNode(MDForInst[i].second); - MDForInst.clear(); - } - } - - for (Module::const_named_metadata_iterator I = M.named_metadata_begin(), - E = M.named_metadata_end(); I != E; ++I) { - const NamedMDNode *NMD = I; - for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) - incorporateMDNode(NMD->getOperand(i)); - } - } - - private: - void incorporateType(Type *Ty) { - // Check to see if we're already visited this type. - if (!VisitedTypes.insert(Ty).second) - return; - - // If this is a structure or opaque type, add a name for the type. - if (StructType *STy = dyn_cast(Ty)) - StructTypes.push_back(STy); - - // Recursively walk all contained types. - for (Type::subtype_iterator I = Ty->subtype_begin(), - E = Ty->subtype_end(); I != E; ++I) - incorporateType(*I); - } - - /// incorporateValue - This method is used to walk operand lists finding - /// types hiding in constant expressions and other operands that won't be - /// walked in other ways. GlobalValues, basic blocks, instructions, and - /// inst operands are all explicitly enumerated. - void incorporateValue(const Value *V) { - if (const MDNode *M = dyn_cast(V)) - return incorporateMDNode(M); - if (!isa(V) || isa(V)) return; - - // Already visited? - if (!VisitedConstants.insert(V).second) - return; - - // Check this type. - incorporateType(V->getType()); - - // Look in operands for types. - const User *U = cast(V); - for (Constant::const_op_iterator I = U->op_begin(), - E = U->op_end(); I != E;++I) - incorporateValue(*I); - } - - void incorporateMDNode(const MDNode *V) { - - // Already visited? - if (!VisitedConstants.insert(V).second) - return; - - // Look in operands for types. - for (unsigned i = 0, e = V->getNumOperands(); i != e; ++i) - if (Value *Op = V->getOperand(i)) - incorporateValue(Op); - } - }; -} // end anonymous namespace - -void Module::findUsedStructTypes(std::vector &StructTypes) const { - TypeFinder(StructTypes).run(*this); -}