Module.cpp revision baf3c404409d5e47b13984a7f95bfbd6d1f2e79e
1//===-- Module.cpp - Implement the Module class ---------------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file implements the Module class for the VMCore library. 11// 12//===----------------------------------------------------------------------===// 13 14#include "llvm/Module.h" 15#include "llvm/InstrTypes.h" 16#include "llvm/Constants.h" 17#include "llvm/DerivedTypes.h" 18#include "llvm/LLVMContext.h" 19#include "llvm/ADT/STLExtras.h" 20#include "llvm/ADT/StringExtras.h" 21#include "llvm/Support/LeakDetector.h" 22#include "SymbolTableListTraitsImpl.h" 23#include "llvm/TypeSymbolTable.h" 24#include <algorithm> 25#include <cstdarg> 26#include <cstdlib> 27using namespace llvm; 28 29//===----------------------------------------------------------------------===// 30// Methods to implement the globals and functions lists. 31// 32 33GlobalVariable *ilist_traits<GlobalVariable>::createSentinel() { 34 GlobalVariable *Ret = new GlobalVariable(getGlobalContext(), Type::Int32Ty, 35 false, GlobalValue::ExternalLinkage); 36 // This should not be garbage monitored. 37 LeakDetector::removeGarbageObject(Ret); 38 return Ret; 39} 40GlobalAlias *ilist_traits<GlobalAlias>::createSentinel() { 41 GlobalAlias *Ret = new GlobalAlias(Type::Int32Ty, 42 GlobalValue::ExternalLinkage); 43 // This should not be garbage monitored. 44 LeakDetector::removeGarbageObject(Ret); 45 return Ret; 46} 47 48// Explicit instantiations of SymbolTableListTraits since some of the methods 49// are not in the public header file. 50template class SymbolTableListTraits<GlobalVariable, Module>; 51template class SymbolTableListTraits<Function, Module>; 52template class SymbolTableListTraits<GlobalAlias, Module>; 53 54//===----------------------------------------------------------------------===// 55// Primitive Module methods. 56// 57 58Module::Module(const StringRef &MID, LLVMContext& C) 59 : Context(C), ModuleID(MID), DataLayout("") { 60 ValSymTab = new ValueSymbolTable(); 61 TypeSymTab = new TypeSymbolTable(); 62} 63 64Module::~Module() { 65 dropAllReferences(); 66 GlobalList.clear(); 67 FunctionList.clear(); 68 AliasList.clear(); 69 LibraryList.clear(); 70 NamedMDList.clear(); 71 delete ValSymTab; 72 delete TypeSymTab; 73} 74 75/// Target endian information... 76Module::Endianness Module::getEndianness() const { 77 std::string temp = DataLayout; 78 Module::Endianness ret = AnyEndianness; 79 80 while (!temp.empty()) { 81 std::string token = getToken(temp, "-"); 82 83 if (token[0] == 'e') { 84 ret = LittleEndian; 85 } else if (token[0] == 'E') { 86 ret = BigEndian; 87 } 88 } 89 90 return ret; 91} 92 93/// Target Pointer Size information... 94Module::PointerSize Module::getPointerSize() const { 95 std::string temp = DataLayout; 96 Module::PointerSize ret = AnyPointerSize; 97 98 while (!temp.empty()) { 99 std::string token = getToken(temp, "-"); 100 char signal = getToken(token, ":")[0]; 101 102 if (signal == 'p') { 103 int size = atoi(getToken(token, ":").c_str()); 104 if (size == 32) 105 ret = Pointer32; 106 else if (size == 64) 107 ret = Pointer64; 108 } 109 } 110 111 return ret; 112} 113 114/// getNamedValue - Return the first global value in the module with 115/// the specified name, of arbitrary type. This method returns null 116/// if a global with the specified name is not found. 117GlobalValue *Module::getNamedValue(const StringRef &Name) const { 118 return cast_or_null<GlobalValue>(getValueSymbolTable().lookup(Name)); 119} 120 121//===----------------------------------------------------------------------===// 122// Methods for easy access to the functions in the module. 123// 124 125// getOrInsertFunction - Look up the specified function in the module symbol 126// table. If it does not exist, add a prototype for the function and return 127// it. This is nice because it allows most passes to get away with not handling 128// the symbol table directly for this common task. 129// 130Constant *Module::getOrInsertFunction(const StringRef &Name, 131 const FunctionType *Ty, 132 AttrListPtr AttributeList) { 133 // See if we have a definition for the specified function already. 134 GlobalValue *F = getNamedValue(Name); 135 if (F == 0) { 136 // Nope, add it 137 Function *New = Function::Create(Ty, GlobalVariable::ExternalLinkage, Name); 138 if (!New->isIntrinsic()) // Intrinsics get attrs set on construction 139 New->setAttributes(AttributeList); 140 FunctionList.push_back(New); 141 return New; // Return the new prototype. 142 } 143 144 // Okay, the function exists. Does it have externally visible linkage? 145 if (F->hasLocalLinkage()) { 146 // Clear the function's name. 147 F->setName(""); 148 // Retry, now there won't be a conflict. 149 Constant *NewF = getOrInsertFunction(Name, Ty); 150 F->setName(Name); 151 return NewF; 152 } 153 154 // If the function exists but has the wrong type, return a bitcast to the 155 // right type. 156 if (F->getType() != Context.getPointerTypeUnqual(Ty)) 157 return ConstantExpr::getBitCast(F, Context.getPointerTypeUnqual(Ty)); 158 159 // Otherwise, we just found the existing function or a prototype. 160 return F; 161} 162 163Constant *Module::getOrInsertTargetIntrinsic(const StringRef &Name, 164 const FunctionType *Ty, 165 AttrListPtr AttributeList) { 166 // See if we have a definition for the specified function already. 167 GlobalValue *F = getNamedValue(Name); 168 if (F == 0) { 169 // Nope, add it 170 Function *New = Function::Create(Ty, GlobalVariable::ExternalLinkage, Name); 171 New->setAttributes(AttributeList); 172 FunctionList.push_back(New); 173 return New; // Return the new prototype. 174 } 175 176 // Otherwise, we just found the existing function or a prototype. 177 return F; 178} 179 180Constant *Module::getOrInsertFunction(const StringRef &Name, 181 const FunctionType *Ty) { 182 AttrListPtr AttributeList = AttrListPtr::get((AttributeWithIndex *)0, 0); 183 return getOrInsertFunction(Name, Ty, AttributeList); 184} 185 186// getOrInsertFunction - Look up the specified function in the module symbol 187// table. If it does not exist, add a prototype for the function and return it. 188// This version of the method takes a null terminated list of function 189// arguments, which makes it easier for clients to use. 190// 191Constant *Module::getOrInsertFunction(const StringRef &Name, 192 AttrListPtr AttributeList, 193 const Type *RetTy, ...) { 194 va_list Args; 195 va_start(Args, RetTy); 196 197 // Build the list of argument types... 198 std::vector<const Type*> ArgTys; 199 while (const Type *ArgTy = va_arg(Args, const Type*)) 200 ArgTys.push_back(ArgTy); 201 202 va_end(Args); 203 204 // Build the function type and chain to the other getOrInsertFunction... 205 return getOrInsertFunction(Name, 206 Context.getFunctionType(RetTy, ArgTys, false), 207 AttributeList); 208} 209 210Constant *Module::getOrInsertFunction(const StringRef &Name, 211 const Type *RetTy, ...) { 212 va_list Args; 213 va_start(Args, RetTy); 214 215 // Build the list of argument types... 216 std::vector<const Type*> ArgTys; 217 while (const Type *ArgTy = va_arg(Args, const Type*)) 218 ArgTys.push_back(ArgTy); 219 220 va_end(Args); 221 222 // Build the function type and chain to the other getOrInsertFunction... 223 return getOrInsertFunction(Name, 224 Context.getFunctionType(RetTy, ArgTys, false), 225 AttrListPtr::get((AttributeWithIndex *)0, 0)); 226} 227 228// getFunction - Look up the specified function in the module symbol table. 229// If it does not exist, return null. 230// 231Function *Module::getFunction(const StringRef &Name) const { 232 return dyn_cast_or_null<Function>(getNamedValue(Name)); 233} 234 235//===----------------------------------------------------------------------===// 236// Methods for easy access to the global variables in the module. 237// 238 239/// getGlobalVariable - Look up the specified global variable in the module 240/// symbol table. If it does not exist, return null. The type argument 241/// should be the underlying type of the global, i.e., it should not have 242/// the top-level PointerType, which represents the address of the global. 243/// If AllowLocal is set to true, this function will return types that 244/// have an local. By default, these types are not returned. 245/// 246GlobalVariable *Module::getGlobalVariable(const StringRef &Name, 247 bool AllowLocal) const { 248 if (GlobalVariable *Result = 249 dyn_cast_or_null<GlobalVariable>(getNamedValue(Name))) 250 if (AllowLocal || !Result->hasLocalLinkage()) 251 return Result; 252 return 0; 253} 254 255/// getOrInsertGlobal - Look up the specified global in the module symbol table. 256/// 1. If it does not exist, add a declaration of the global and return it. 257/// 2. Else, the global exists but has the wrong type: return the function 258/// with a constantexpr cast to the right type. 259/// 3. Finally, if the existing global is the correct delclaration, return the 260/// existing global. 261Constant *Module::getOrInsertGlobal(const StringRef &Name, const Type *Ty) { 262 // See if we have a definition for the specified global already. 263 GlobalVariable *GV = dyn_cast_or_null<GlobalVariable>(getNamedValue(Name)); 264 if (GV == 0) { 265 // Nope, add it 266 GlobalVariable *New = 267 new GlobalVariable(*this, Ty, false, GlobalVariable::ExternalLinkage, 268 0, Name); 269 return New; // Return the new declaration. 270 } 271 272 // If the variable exists but has the wrong type, return a bitcast to the 273 // right type. 274 if (GV->getType() != Context.getPointerTypeUnqual(Ty)) 275 return ConstantExpr::getBitCast(GV, Context.getPointerTypeUnqual(Ty)); 276 277 // Otherwise, we just found the existing function or a prototype. 278 return GV; 279} 280 281//===----------------------------------------------------------------------===// 282// Methods for easy access to the global variables in the module. 283// 284 285// getNamedAlias - Look up the specified global in the module symbol table. 286// If it does not exist, return null. 287// 288GlobalAlias *Module::getNamedAlias(const StringRef &Name) const { 289 return dyn_cast_or_null<GlobalAlias>(getNamedValue(Name)); 290} 291 292/// getNamedMetadata - Return the first named MDNode in the module with the 293/// specified name. This method returns null if a MDNode with the specified 294/// name is not found. 295NamedMDNode *Module::getNamedMetadata(const StringRef &Name) const { 296 return dyn_cast_or_null<NamedMDNode>(getNamedValue(Name)); 297} 298 299//===----------------------------------------------------------------------===// 300// Methods for easy access to the types in the module. 301// 302 303 304// addTypeName - Insert an entry in the symbol table mapping Str to Type. If 305// there is already an entry for this name, true is returned and the symbol 306// table is not modified. 307// 308bool Module::addTypeName(const StringRef &Name, const Type *Ty) { 309 TypeSymbolTable &ST = getTypeSymbolTable(); 310 311 if (ST.lookup(Name)) return true; // Already in symtab... 312 313 // Not in symbol table? Set the name with the Symtab as an argument so the 314 // type knows what to update... 315 ST.insert(Name, Ty); 316 317 return false; 318} 319 320/// getTypeByName - Return the type with the specified name in this module, or 321/// null if there is none by that name. 322const Type *Module::getTypeByName(const StringRef &Name) const { 323 const TypeSymbolTable &ST = getTypeSymbolTable(); 324 return cast_or_null<Type>(ST.lookup(Name)); 325} 326 327// getTypeName - If there is at least one entry in the symbol table for the 328// specified type, return it. 329// 330std::string Module::getTypeName(const Type *Ty) const { 331 const TypeSymbolTable &ST = getTypeSymbolTable(); 332 333 TypeSymbolTable::const_iterator TI = ST.begin(); 334 TypeSymbolTable::const_iterator TE = ST.end(); 335 if ( TI == TE ) return ""; // No names for types 336 337 while (TI != TE && TI->second != Ty) 338 ++TI; 339 340 if (TI != TE) // Must have found an entry! 341 return TI->first; 342 return ""; // Must not have found anything... 343} 344 345//===----------------------------------------------------------------------===// 346// Other module related stuff. 347// 348 349 350// dropAllReferences() - This function causes all the subelementss to "let go" 351// of all references that they are maintaining. This allows one to 'delete' a 352// whole module at a time, even though there may be circular references... first 353// all references are dropped, and all use counts go to zero. Then everything 354// is deleted for real. Note that no operations are valid on an object that 355// has "dropped all references", except operator delete. 356// 357void Module::dropAllReferences() { 358 for(Module::iterator I = begin(), E = end(); I != E; ++I) 359 I->dropAllReferences(); 360 361 for(Module::global_iterator I = global_begin(), E = global_end(); I != E; ++I) 362 I->dropAllReferences(); 363 364 for(Module::alias_iterator I = alias_begin(), E = alias_end(); I != E; ++I) 365 I->dropAllReferences(); 366} 367 368void Module::addLibrary(const StringRef& Lib) { 369 for (Module::lib_iterator I = lib_begin(), E = lib_end(); I != E; ++I) 370 if (*I == Lib) 371 return; 372 LibraryList.push_back(Lib); 373} 374 375void Module::removeLibrary(const StringRef& Lib) { 376 LibraryListType::iterator I = LibraryList.begin(); 377 LibraryListType::iterator E = LibraryList.end(); 378 for (;I != E; ++I) 379 if (*I == Lib) { 380 LibraryList.erase(I); 381 return; 382 } 383} 384