X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=lib%2FVMCore%2FModule.cpp;h=429cf1a4c69508066e17a9f8569a22ce7a21871e;hb=0b12ecf6ff6b5d3a144178257b6206f0c4788792;hp=efa6e6c7c090ba2cb5f9793beee354a8afb7bfff;hpb=6734b57d1bcd743491b627979f7801f94c97a970;p=oota-llvm.git diff --git a/lib/VMCore/Module.cpp b/lib/VMCore/Module.cpp index efa6e6c7c09..429cf1a4c69 100644 --- a/lib/VMCore/Module.cpp +++ b/lib/VMCore/Module.cpp @@ -2,8 +2,8 @@ // // The LLVM Compiler Infrastructure // -// This file was developed by the LLVM research group and is distributed under -// the University of Illinois Open Source License. See LICENSE.TXT for details. +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// // @@ -23,7 +23,6 @@ #include #include #include -#include using namespace llvm; //===----------------------------------------------------------------------===// @@ -32,9 +31,8 @@ using namespace llvm; Function *ilist_traits::createSentinel() { FunctionType *FTy = - FunctionType::get(Type::VoidTy, std::vector(), false, - std::vector() ); - Function *Ret = new Function(FTy, GlobalValue::ExternalLinkage); + FunctionType::get(Type::VoidTy, std::vector(), false); + Function *Ret = Function::Create(FTy, GlobalValue::ExternalLinkage); // This should not be garbage monitored. LeakDetector::removeGarbageObject(Ret); return Ret; @@ -46,6 +44,13 @@ GlobalVariable *ilist_traits::createSentinel() { LeakDetector::removeGarbageObject(Ret); return Ret; } +GlobalAlias *ilist_traits::createSentinel() { + GlobalAlias *Ret = new GlobalAlias(Type::Int32Ty, + GlobalValue::ExternalLinkage); + // This should not be garbage monitored. + LeakDetector::removeGarbageObject(Ret); + return Ret; +} iplist &ilist_traits::getList(Module *M) { return M->getFunctionList(); @@ -53,11 +58,15 @@ iplist &ilist_traits::getList(Module *M) { iplist &ilist_traits::getList(Module *M) { return M->getGlobalList(); } +iplist &ilist_traits::getList(Module *M) { + return M->getAliasList(); +} // Explicit instantiations of SymbolTableListTraits since some of the methods // are not in the public header file. -template class SymbolTableListTraits; -template class SymbolTableListTraits; +template class SymbolTableListTraits; +template class SymbolTableListTraits; +template class SymbolTableListTraits; //===----------------------------------------------------------------------===// // Primitive Module methods. @@ -65,20 +74,15 @@ template class SymbolTableListTraits; Module::Module(const std::string &MID) : ModuleID(MID), DataLayout("") { - FunctionList.setItemParent(this); - FunctionList.setParent(this); - GlobalList.setItemParent(this); - GlobalList.setParent(this); - ValSymTab = new SymbolTable(); + ValSymTab = new ValueSymbolTable(); TypeSymTab = new TypeSymbolTable(); } Module::~Module() { dropAllReferences(); GlobalList.clear(); - GlobalList.setParent(0); FunctionList.clear(); - FunctionList.setParent(0); + AliasList.clear(); LibraryList.clear(); delete ValSymTab; delete TypeSymTab; @@ -132,16 +136,20 @@ Module::PointerSize Module::getPointerSize() const { // Methods for easy access to the functions in the module. // +// getOrInsertFunction - Look up the specified function in the module symbol +// table. If it does not exist, add a prototype for the function and return +// it. This is nice because it allows most passes to get away with not handling +// the symbol table directly for this common task. +// Constant *Module::getOrInsertFunction(const std::string &Name, const FunctionType *Ty) { - SymbolTable &SymTab = getValueSymbolTable(); + ValueSymbolTable &SymTab = getValueSymbolTable(); - // See if we have a definitions for the specified function already. - Function *F = - dyn_cast_or_null(SymTab.lookup(PointerType::get(Ty), Name)); + // See if we have a definition for the specified function already. + GlobalValue *F = dyn_cast_or_null(SymTab.lookup(Name)); if (F == 0) { - // Nope, add it. - Function *New = new Function(Ty, GlobalVariable::ExternalLinkage, Name); + // Nope, add it + Function *New = Function::Create(Ty, GlobalVariable::ExternalLinkage, Name); FunctionList.push_back(New); return New; // Return the new prototype. } @@ -149,15 +157,15 @@ Constant *Module::getOrInsertFunction(const std::string &Name, // Okay, the function exists. Does it have externally visible linkage? if (F->hasInternalLinkage()) { // Rename the function. - F->setName(SymTab.getUniqueName(F->getType(), F->getName())); + F->setName(SymTab.getUniqueName(F->getName())); // Retry, now there won't be a conflict. return getOrInsertFunction(Name, Ty); } // If the function exists but has the wrong type, return a bitcast to the // right type. - if (F->getFunctionType() != Ty) - return ConstantExpr::getBitCast(F, PointerType::get(Ty)); + 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; @@ -188,73 +196,9 @@ Constant *Module::getOrInsertFunction(const std::string &Name, // getFunction - Look up the specified function in the module symbol table. // If it does not exist, return null. // -Function *Module::getFunction(const std::string &Name, const FunctionType *Ty) { - SymbolTable &SymTab = getValueSymbolTable(); - return cast_or_null(SymTab.lookup(PointerType::get(Ty), Name)); -} - - -/// getMainFunction - This function looks up main efficiently. This is such a -/// common case, that it is a method in Module. If main cannot be found, a -/// null pointer is returned. -/// -Function *Module::getMainFunction() { - std::vector Params; - - // int main(void)... - if (Function *F = getFunction("main", FunctionType::get(Type::Int32Ty, - Params, false))) - return F; - - // void main(void)... - if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy, - Params, false))) - return F; - - Params.push_back(Type::Int32Ty); - - // int main(int argc)... - if (Function *F = getFunction("main", FunctionType::get(Type::Int32Ty, - Params, false))) - return F; - - // void main(int argc)... - if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy, - Params, false))) - return F; - - for (unsigned i = 0; i != 2; ++i) { // Check argv and envp - Params.push_back(PointerType::get(PointerType::get(Type::Int8Ty))); - - // int main(int argc, char **argv)... - if (Function *F = getFunction("main", FunctionType::get(Type::Int32Ty, - Params, false))) - return F; - - // void main(int argc, char **argv)... - if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy, - Params, false))) - return F; - } - - // Ok, try to find main the hard way... - return getNamedFunction("main"); -} - -/// getNamedFunction - Return the first function in the module with the -/// specified name, of arbitrary type. This method returns null if a function -/// with the specified name is not found. -/// -Function *Module::getNamedFunction(const std::string &Name) const { - // Loop over all of the functions, looking for the function desired - const Function *Found = 0; - for (const_iterator I = begin(), E = end(); I != E; ++I) - if (I->getName() == Name) - if (I->isDeclaration()) - Found = I; - else - return const_cast(&(*I)); - return const_cast(Found); // Non-external function not found... +Function *Module::getFunction(const std::string &Name) const { + const ValueSymbolTable &SymTab = getValueSymbolTable(); + return dyn_cast_or_null(SymTab.lookup(Name)); } //===----------------------------------------------------------------------===// @@ -269,30 +213,26 @@ Function *Module::getNamedFunction(const std::string &Name) const { /// have InternalLinkage. By default, these types are not returned. /// GlobalVariable *Module::getGlobalVariable(const std::string &Name, - const Type *Ty, bool AllowInternal) { - if (Value *V = getValueSymbolTable().lookup(PointerType::get(Ty), Name)) { - GlobalVariable *Result = cast(V); - if (AllowInternal || !Result->hasInternalLinkage()) + bool AllowInternal) const { + if (Value *V = ValSymTab->lookup(Name)) { + GlobalVariable *Result = dyn_cast(V); + if (Result && (AllowInternal || !Result->hasInternalLinkage())) return Result; } return 0; } -/// getNamedGlobal - Return the first global variable in the module with the -/// specified name, of arbitrary type. This method returns null if a global -/// with the specified name is not found. -/// -GlobalVariable *Module::getNamedGlobal(const std::string &Name) const { - // FIXME: This would be much faster with a symbol table that doesn't - // discriminate based on type! - for (const_global_iterator I = global_begin(), E = global_end(); - I != E; ++I) - if (I->getName() == Name) - return const_cast(&(*I)); - return 0; -} - +//===----------------------------------------------------------------------===// +// Methods for easy access to the global variables in the module. +// +// getNamedAlias - Look up the specified global in the module symbol table. +// If it does not exist, return null. +// +GlobalAlias *Module::getNamedAlias(const std::string &Name) const { + const ValueSymbolTable &SymTab = getValueSymbolTable(); + return dyn_cast_or_null(SymTab.lookup(Name)); +} //===----------------------------------------------------------------------===// // Methods for easy access to the types in the module. @@ -358,6 +298,9 @@ void Module::dropAllReferences() { for(Module::global_iterator I = global_begin(), E = global_end(); I != E; ++I) I->dropAllReferences(); + + for(Module::alias_iterator I = alias_begin(), E = alias_end(); I != E; ++I) + I->dropAllReferences(); } void Module::addLibrary(const std::string& Lib) {