2003-10-13 11:32:08 +08:00
|
|
|
//===-- Module.cpp - Implement the Module class ---------------------------===//
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-07 04:29:01 +08:00
|
|
|
//
|
|
|
|
// This file implements the Module class for the VMCore library.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Module.h"
|
2001-06-30 12:35:40 +08:00
|
|
|
#include "llvm/InstrTypes.h"
|
2002-04-29 03:55:58 +08:00
|
|
|
#include "llvm/Constants.h"
|
2002-03-29 11:44:18 +08:00
|
|
|
#include "llvm/DerivedTypes.h"
|
2010-01-28 04:34:15 +08:00
|
|
|
#include "llvm/GVMaterializer.h"
|
2009-07-02 00:58:40 +08:00
|
|
|
#include "llvm/LLVMContext.h"
|
Land the long talked about "type system rewrite" patch. This
patch brings numerous advantages to LLVM. One way to look at it
is through diffstat:
109 files changed, 3005 insertions(+), 5906 deletions(-)
Removing almost 3K lines of code is a good thing. Other advantages
include:
1. Value::getType() is a simple load that can be CSE'd, not a mutating
union-find operation.
2. Types a uniqued and never move once created, defining away PATypeHolder.
3. Structs can be "named" now, and their name is part of the identity that
uniques them. This means that the compiler doesn't merge them structurally
which makes the IR much less confusing.
4. Now that there is no way to get a cycle in a type graph without a named
struct type, "upreferences" go away.
5. Type refinement is completely gone, which should make LTO much MUCH faster
in some common cases with C++ code.
6. Types are now generally immutable, so we can use "Type *" instead
"const Type *" everywhere.
Downsides of this patch are that it removes some functions from the C API,
so people using those will have to upgrade to (not yet added) new API.
"LLVM 3.0" is the right time to do this.
There are still some cleanups pending after this, this patch is large enough
as-is.
llvm-svn: 134829
2011-07-10 01:41:24 +08:00
|
|
|
#include "llvm/ADT/DenseSet.h"
|
2010-06-16 08:53:55 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2004-09-02 06:55:40 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2006-05-18 10:10:31 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2004-09-02 06:55:40 +08:00
|
|
|
#include "llvm/Support/LeakDetector.h"
|
2002-06-26 00:13:24 +08:00
|
|
|
#include "SymbolTableListTraitsImpl.h"
|
|
|
|
#include <algorithm>
|
2003-08-31 08:19:28 +08:00
|
|
|
#include <cstdarg>
|
2006-05-18 10:10:31 +08:00
|
|
|
#include <cstdlib>
|
2003-11-22 04:23:48 +08:00
|
|
|
using namespace llvm;
|
2003-11-12 06:41:34 +08:00
|
|
|
|
2003-12-31 16:43:01 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2004-04-22 02:27:56 +08:00
|
|
|
// Methods to implement the globals and functions lists.
|
2003-12-31 16:43:01 +08:00
|
|
|
//
|
|
|
|
|
2002-06-26 00:13:24 +08:00
|
|
|
// Explicit instantiations of SymbolTableListTraits since some of the methods
|
2006-01-24 12:13:11 +08:00
|
|
|
// are not in the public header file.
|
2009-12-19 08:55:12 +08:00
|
|
|
template class llvm::SymbolTableListTraits<Function, Module>;
|
2011-08-13 09:04:44 +08:00
|
|
|
template class llvm::SymbolTableListTraits<GlobalVariable, Module>;
|
2009-12-19 08:55:12 +08:00
|
|
|
template class llvm::SymbolTableListTraits<GlobalAlias, Module>;
|
2001-06-07 04:29:01 +08:00
|
|
|
|
2003-12-31 16:43:01 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Primitive Module methods.
|
|
|
|
//
|
2001-10-13 14:58:40 +08:00
|
|
|
|
2009-11-06 18:58:06 +08:00
|
|
|
Module::Module(StringRef MID, LLVMContext& C)
|
2010-08-04 09:39:08 +08:00
|
|
|
: Context(C), Materializer(NULL), ModuleID(MID) {
|
2007-02-06 04:47:22 +08:00
|
|
|
ValSymTab = new ValueSymbolTable();
|
2010-07-22 07:38:33 +08:00
|
|
|
NamedMDSymTab = new StringMap<NamedMDNode *>();
|
2010-09-09 02:03:32 +08:00
|
|
|
Context.addModule(this);
|
2001-06-07 04:29:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Module::~Module() {
|
2010-09-09 02:03:32 +08:00
|
|
|
Context.removeModule(this);
|
2001-06-07 04:29:01 +08:00
|
|
|
dropAllReferences();
|
2002-06-26 00:13:24 +08:00
|
|
|
GlobalList.clear();
|
|
|
|
FunctionList.clear();
|
2007-04-25 22:27:10 +08:00
|
|
|
AliasList.clear();
|
2004-07-26 02:08:57 +08:00
|
|
|
LibraryList.clear();
|
2009-07-30 01:16:17 +08:00
|
|
|
NamedMDList.clear();
|
2007-01-06 15:24:44 +08:00
|
|
|
delete ValSymTab;
|
2010-07-22 07:38:33 +08:00
|
|
|
delete static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab);
|
2001-06-07 04:29:01 +08:00
|
|
|
}
|
|
|
|
|
Land the long talked about "type system rewrite" patch. This
patch brings numerous advantages to LLVM. One way to look at it
is through diffstat:
109 files changed, 3005 insertions(+), 5906 deletions(-)
Removing almost 3K lines of code is a good thing. Other advantages
include:
1. Value::getType() is a simple load that can be CSE'd, not a mutating
union-find operation.
2. Types a uniqued and never move once created, defining away PATypeHolder.
3. Structs can be "named" now, and their name is part of the identity that
uniques them. This means that the compiler doesn't merge them structurally
which makes the IR much less confusing.
4. Now that there is no way to get a cycle in a type graph without a named
struct type, "upreferences" go away.
5. Type refinement is completely gone, which should make LTO much MUCH faster
in some common cases with C++ code.
6. Types are now generally immutable, so we can use "Type *" instead
"const Type *" everywhere.
Downsides of this patch are that it removes some functions from the C API,
so people using those will have to upgrade to (not yet added) new API.
"LLVM 3.0" is the right time to do this.
There are still some cleanups pending after this, this patch is large enough
as-is.
llvm-svn: 134829
2011-07-10 01:41:24 +08:00
|
|
|
/// Target endian information.
|
2006-05-18 10:10:31 +08:00
|
|
|
Module::Endianness Module::getEndianness() const {
|
2010-01-12 02:03:24 +08:00
|
|
|
StringRef temp = DataLayout;
|
2006-05-18 13:46:08 +08:00
|
|
|
Module::Endianness ret = AnyEndianness;
|
2012-04-23 08:23:33 +08:00
|
|
|
|
2006-05-18 13:46:08 +08:00
|
|
|
while (!temp.empty()) {
|
2011-07-21 14:21:31 +08:00
|
|
|
std::pair<StringRef, StringRef> P = getToken(temp, "-");
|
2012-04-23 08:23:33 +08:00
|
|
|
|
2011-07-21 14:21:31 +08:00
|
|
|
StringRef token = P.first;
|
|
|
|
temp = P.second;
|
2012-04-23 08:23:33 +08:00
|
|
|
|
2006-05-18 10:10:31 +08:00
|
|
|
if (token[0] == 'e') {
|
2006-05-18 13:46:08 +08:00
|
|
|
ret = LittleEndian;
|
2006-05-18 10:10:31 +08:00
|
|
|
} else if (token[0] == 'E') {
|
2006-05-18 13:46:08 +08:00
|
|
|
ret = BigEndian;
|
2006-05-18 10:10:31 +08:00
|
|
|
}
|
|
|
|
}
|
2012-04-23 08:23:33 +08:00
|
|
|
|
2006-05-18 13:46:08 +08:00
|
|
|
return ret;
|
2006-05-18 10:10:31 +08:00
|
|
|
}
|
|
|
|
|
2011-07-21 14:21:31 +08:00
|
|
|
/// Target Pointer Size information.
|
2006-05-18 10:10:31 +08:00
|
|
|
Module::PointerSize Module::getPointerSize() const {
|
2010-01-12 02:03:24 +08:00
|
|
|
StringRef temp = DataLayout;
|
2006-05-18 13:46:08 +08:00
|
|
|
Module::PointerSize ret = AnyPointerSize;
|
2012-04-23 08:23:33 +08:00
|
|
|
|
2006-05-18 13:46:08 +08:00
|
|
|
while (!temp.empty()) {
|
2011-07-21 14:21:31 +08:00
|
|
|
std::pair<StringRef, StringRef> TmpP = getToken(temp, "-");
|
|
|
|
temp = TmpP.second;
|
|
|
|
TmpP = getToken(TmpP.first, ":");
|
|
|
|
StringRef token = TmpP.second, signalToken = TmpP.first;
|
2012-04-23 08:23:33 +08:00
|
|
|
|
2010-01-12 02:03:24 +08:00
|
|
|
if (signalToken[0] == 'p') {
|
|
|
|
int size = 0;
|
|
|
|
getToken(token, ":").first.getAsInteger(10, size);
|
2006-05-18 10:10:31 +08:00
|
|
|
if (size == 32)
|
2006-05-18 13:46:08 +08:00
|
|
|
ret = Pointer32;
|
2006-05-18 10:10:31 +08:00
|
|
|
else if (size == 64)
|
2006-05-18 13:46:08 +08:00
|
|
|
ret = Pointer64;
|
2006-05-18 10:10:31 +08:00
|
|
|
}
|
|
|
|
}
|
2012-04-23 08:23:33 +08:00
|
|
|
|
2006-05-18 13:46:08 +08:00
|
|
|
return ret;
|
2006-05-18 10:10:31 +08:00
|
|
|
}
|
|
|
|
|
2009-03-07 06:04:43 +08:00
|
|
|
/// getNamedValue - Return the first global value in the module with
|
|
|
|
/// the specified name, of arbitrary type. This method returns null
|
|
|
|
/// if a global with the specified name is not found.
|
2009-11-06 18:58:06 +08:00
|
|
|
GlobalValue *Module::getNamedValue(StringRef Name) const {
|
2009-03-07 06:04:43 +08:00
|
|
|
return cast_or_null<GlobalValue>(getValueSymbolTable().lookup(Name));
|
|
|
|
}
|
|
|
|
|
2009-12-29 17:01:33 +08:00
|
|
|
/// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
|
|
|
|
/// This ID is uniqued across modules in the current LLVMContext.
|
|
|
|
unsigned Module::getMDKindID(StringRef Name) const {
|
|
|
|
return Context.getMDKindID(Name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getMDKindNames - Populate client supplied SmallVector with the name for
|
|
|
|
/// custom metadata IDs registered in this LLVMContext. ID #0 is not used,
|
|
|
|
/// so it is filled in as an empty string.
|
|
|
|
void Module::getMDKindNames(SmallVectorImpl<StringRef> &Result) const {
|
|
|
|
return Context.getMDKindNames(Result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-12-31 16:43:01 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Methods for easy access to the functions in the module.
|
|
|
|
//
|
|
|
|
|
2007-02-06 04:47:22 +08:00
|
|
|
// 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.
|
|
|
|
//
|
2009-11-06 18:58:06 +08:00
|
|
|
Constant *Module::getOrInsertFunction(StringRef Name,
|
2011-07-18 12:54:35 +08:00
|
|
|
FunctionType *Ty,
|
2009-01-05 06:54:40 +08:00
|
|
|
AttrListPtr AttributeList) {
|
2007-02-06 04:47:22 +08:00
|
|
|
// See if we have a definition for the specified function already.
|
2009-03-07 06:04:43 +08:00
|
|
|
GlobalValue *F = getNamedValue(Name);
|
2007-01-07 16:09:25 +08:00
|
|
|
if (F == 0) {
|
2007-02-06 04:47:22 +08:00
|
|
|
// Nope, add it
|
2008-04-07 04:25:17 +08:00
|
|
|
Function *New = Function::Create(Ty, GlobalVariable::ExternalLinkage, Name);
|
2009-01-05 06:54:40 +08:00
|
|
|
if (!New->isIntrinsic()) // Intrinsics get attrs set on construction
|
|
|
|
New->setAttributes(AttributeList);
|
2002-03-29 11:44:18 +08:00
|
|
|
FunctionList.push_back(New);
|
2007-01-07 16:09:25 +08:00
|
|
|
return New; // Return the new prototype.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Okay, the function exists. Does it have externally visible linkage?
|
2009-01-16 04:18:42 +08:00
|
|
|
if (F->hasLocalLinkage()) {
|
2008-06-28 05:25:24 +08:00
|
|
|
// Clear the function's name.
|
|
|
|
F->setName("");
|
2007-01-07 16:09:25 +08:00
|
|
|
// Retry, now there won't be a conflict.
|
2008-06-28 05:25:24 +08:00
|
|
|
Constant *NewF = getOrInsertFunction(Name, Ty);
|
2009-07-25 14:02:13 +08:00
|
|
|
F->setName(Name);
|
2008-06-28 05:25:24 +08:00
|
|
|
return NewF;
|
2002-03-29 11:44:18 +08:00
|
|
|
}
|
2007-01-07 16:09:25 +08:00
|
|
|
|
|
|
|
// If the function exists but has the wrong type, return a bitcast to the
|
|
|
|
// right type.
|
2009-07-30 06:17:13 +08:00
|
|
|
if (F->getType() != PointerType::getUnqual(Ty))
|
|
|
|
return ConstantExpr::getBitCast(F, PointerType::getUnqual(Ty));
|
2012-04-23 08:23:33 +08:00
|
|
|
|
2007-01-07 16:09:25 +08:00
|
|
|
// Otherwise, we just found the existing function or a prototype.
|
2012-04-23 08:23:33 +08:00
|
|
|
return F;
|
2002-03-29 11:44:18 +08:00
|
|
|
}
|
|
|
|
|
2009-11-06 18:58:06 +08:00
|
|
|
Constant *Module::getOrInsertTargetIntrinsic(StringRef Name,
|
2011-07-18 12:54:35 +08:00
|
|
|
FunctionType *Ty,
|
2009-02-05 09:49:45 +08:00
|
|
|
AttrListPtr AttributeList) {
|
|
|
|
// See if we have a definition for the specified function already.
|
2009-03-07 06:04:43 +08:00
|
|
|
GlobalValue *F = getNamedValue(Name);
|
2009-02-05 09:49:45 +08:00
|
|
|
if (F == 0) {
|
|
|
|
// Nope, add it
|
|
|
|
Function *New = Function::Create(Ty, GlobalVariable::ExternalLinkage, Name);
|
|
|
|
New->setAttributes(AttributeList);
|
|
|
|
FunctionList.push_back(New);
|
|
|
|
return New; // Return the new prototype.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we just found the existing function or a prototype.
|
2012-04-23 08:23:33 +08:00
|
|
|
return F;
|
2009-02-05 09:49:45 +08:00
|
|
|
}
|
|
|
|
|
2009-11-06 18:58:06 +08:00
|
|
|
Constant *Module::getOrInsertFunction(StringRef Name,
|
2011-07-18 12:54:35 +08:00
|
|
|
FunctionType *Ty) {
|
2012-05-28 09:37:08 +08:00
|
|
|
return getOrInsertFunction(Name, Ty, AttrListPtr());
|
2009-01-05 06:54:40 +08:00
|
|
|
}
|
|
|
|
|
2003-08-31 08:19:28 +08:00
|
|
|
// 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 version of the method takes a null terminated list of function
|
|
|
|
// arguments, which makes it easier for clients to use.
|
|
|
|
//
|
2009-11-06 18:58:06 +08:00
|
|
|
Constant *Module::getOrInsertFunction(StringRef Name,
|
2009-01-05 06:54:40 +08:00
|
|
|
AttrListPtr AttributeList,
|
2011-07-18 12:54:35 +08:00
|
|
|
Type *RetTy, ...) {
|
2003-08-31 08:19:28 +08:00
|
|
|
va_list Args;
|
|
|
|
va_start(Args, RetTy);
|
|
|
|
|
|
|
|
// Build the list of argument types...
|
2011-07-12 22:06:48 +08:00
|
|
|
std::vector<Type*> ArgTys;
|
|
|
|
while (Type *ArgTy = va_arg(Args, Type*))
|
2003-08-31 08:19:28 +08:00
|
|
|
ArgTys.push_back(ArgTy);
|
|
|
|
|
|
|
|
va_end(Args);
|
|
|
|
|
|
|
|
// Build the function type and chain to the other getOrInsertFunction...
|
2009-07-09 07:50:31 +08:00
|
|
|
return getOrInsertFunction(Name,
|
2009-07-30 06:17:13 +08:00
|
|
|
FunctionType::get(RetTy, ArgTys, false),
|
2009-01-05 06:54:40 +08:00
|
|
|
AttributeList);
|
2003-08-31 08:19:28 +08:00
|
|
|
}
|
|
|
|
|
2009-11-06 18:58:06 +08:00
|
|
|
Constant *Module::getOrInsertFunction(StringRef Name,
|
2011-07-18 12:54:35 +08:00
|
|
|
Type *RetTy, ...) {
|
2009-01-05 06:54:40 +08:00
|
|
|
va_list Args;
|
|
|
|
va_start(Args, RetTy);
|
|
|
|
|
|
|
|
// Build the list of argument types...
|
2011-07-12 22:06:48 +08:00
|
|
|
std::vector<Type*> ArgTys;
|
|
|
|
while (Type *ArgTy = va_arg(Args, Type*))
|
2009-01-05 06:54:40 +08:00
|
|
|
ArgTys.push_back(ArgTy);
|
|
|
|
|
|
|
|
va_end(Args);
|
|
|
|
|
|
|
|
// Build the function type and chain to the other getOrInsertFunction...
|
2012-04-23 08:23:33 +08:00
|
|
|
return getOrInsertFunction(Name,
|
2009-07-30 06:17:13 +08:00
|
|
|
FunctionType::get(RetTy, ArgTys, false),
|
2012-05-28 09:37:08 +08:00
|
|
|
AttrListPtr());
|
2009-01-05 06:54:40 +08:00
|
|
|
}
|
2003-08-31 08:19:28 +08:00
|
|
|
|
2002-03-29 11:44:18 +08:00
|
|
|
// getFunction - Look up the specified function in the module symbol table.
|
|
|
|
// If it does not exist, return null.
|
|
|
|
//
|
2009-11-06 18:58:06 +08:00
|
|
|
Function *Module::getFunction(StringRef Name) const {
|
2009-03-07 06:04:43 +08:00
|
|
|
return dyn_cast_or_null<Function>(getNamedValue(Name));
|
2008-06-28 05:09:10 +08:00
|
|
|
}
|
|
|
|
|
2003-12-31 16:43:01 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Methods for easy access to the global variables in the module.
|
|
|
|
//
|
|
|
|
|
|
|
|
/// getGlobalVariable - Look up the specified global variable in the module
|
2005-12-05 13:30:21 +08:00
|
|
|
/// symbol table. If it does not exist, return null. The type argument
|
|
|
|
/// should be the underlying type of the global, i.e., it should not have
|
|
|
|
/// the top-level PointerType, which represents the address of the global.
|
2009-01-16 04:18:42 +08:00
|
|
|
/// If AllowLocal is set to true, this function will return types that
|
|
|
|
/// have an local. By default, these types are not returned.
|
2003-12-31 16:43:01 +08:00
|
|
|
///
|
2009-11-06 18:58:06 +08:00
|
|
|
GlobalVariable *Module::getGlobalVariable(StringRef Name,
|
2009-01-16 04:18:42 +08:00
|
|
|
bool AllowLocal) const {
|
2012-04-23 08:23:33 +08:00
|
|
|
if (GlobalVariable *Result =
|
2009-03-07 06:04:43 +08:00
|
|
|
dyn_cast_or_null<GlobalVariable>(getNamedValue(Name)))
|
|
|
|
if (AllowLocal || !Result->hasLocalLinkage())
|
2003-12-31 16:43:01 +08:00
|
|
|
return Result;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-11-06 07:42:27 +08:00
|
|
|
/// getOrInsertGlobal - Look up the specified global in the module symbol table.
|
|
|
|
/// 1. If it does not exist, add a declaration of the global and return it.
|
|
|
|
/// 2. Else, the global exists but has the wrong type: return the function
|
|
|
|
/// with a constantexpr cast to the right type.
|
|
|
|
/// 3. Finally, if the existing global is the correct delclaration, return the
|
|
|
|
/// existing global.
|
2011-07-18 12:54:35 +08:00
|
|
|
Constant *Module::getOrInsertGlobal(StringRef Name, Type *Ty) {
|
2008-11-05 06:51:24 +08:00
|
|
|
// See if we have a definition for the specified global already.
|
2009-03-07 06:04:43 +08:00
|
|
|
GlobalVariable *GV = dyn_cast_or_null<GlobalVariable>(getNamedValue(Name));
|
2008-11-05 06:51:24 +08:00
|
|
|
if (GV == 0) {
|
|
|
|
// Nope, add it
|
|
|
|
GlobalVariable *New =
|
2009-07-09 03:03:57 +08:00
|
|
|
new GlobalVariable(*this, Ty, false, GlobalVariable::ExternalLinkage,
|
|
|
|
0, Name);
|
|
|
|
return New; // Return the new declaration.
|
2008-11-05 06:51:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the variable exists but has the wrong type, return a bitcast to the
|
|
|
|
// right type.
|
2009-07-30 06:17:13 +08:00
|
|
|
if (GV->getType() != PointerType::getUnqual(Ty))
|
|
|
|
return ConstantExpr::getBitCast(GV, PointerType::getUnqual(Ty));
|
2012-04-23 08:23:33 +08:00
|
|
|
|
2008-11-05 06:51:24 +08:00
|
|
|
// Otherwise, we just found the existing function or a prototype.
|
|
|
|
return GV;
|
|
|
|
}
|
|
|
|
|
2007-04-25 22:27:10 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// 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.
|
|
|
|
//
|
2009-11-06 18:58:06 +08:00
|
|
|
GlobalAlias *Module::getNamedAlias(StringRef Name) const {
|
2009-03-07 06:04:43 +08:00
|
|
|
return dyn_cast_or_null<GlobalAlias>(getNamedValue(Name));
|
2007-04-25 22:27:10 +08:00
|
|
|
}
|
|
|
|
|
2009-07-31 07:59:04 +08:00
|
|
|
/// getNamedMetadata - Return the first NamedMDNode in the module with the
|
2012-04-23 08:23:33 +08:00
|
|
|
/// specified name. This method returns null if a NamedMDNode with the
|
2010-06-19 13:33:57 +08:00
|
|
|
/// specified name is not found.
|
2010-06-22 09:19:38 +08:00
|
|
|
NamedMDNode *Module::getNamedMetadata(const Twine &Name) const {
|
2010-06-16 08:53:55 +08:00
|
|
|
SmallString<256> NameData;
|
|
|
|
StringRef NameRef = Name.toStringRef(NameData);
|
2010-07-22 07:38:33 +08:00
|
|
|
return static_cast<StringMap<NamedMDNode*> *>(NamedMDSymTab)->lookup(NameRef);
|
2010-06-16 08:53:55 +08:00
|
|
|
}
|
|
|
|
|
2012-04-23 08:23:33 +08:00
|
|
|
/// getOrInsertNamedMetadata - Return the first named MDNode in the module
|
|
|
|
/// with the specified name. This method returns a new NamedMDNode if a
|
2009-07-31 07:59:04 +08:00
|
|
|
/// NamedMDNode with the specified name is not found.
|
2009-11-06 18:58:06 +08:00
|
|
|
NamedMDNode *Module::getOrInsertNamedMetadata(StringRef Name) {
|
2010-07-22 07:38:33 +08:00
|
|
|
NamedMDNode *&NMD =
|
|
|
|
(*static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab))[Name];
|
|
|
|
if (!NMD) {
|
|
|
|
NMD = new NamedMDNode(Name);
|
|
|
|
NMD->setParent(this);
|
|
|
|
NamedMDList.push_back(NMD);
|
|
|
|
}
|
2009-07-31 07:59:04 +08:00
|
|
|
return NMD;
|
|
|
|
}
|
|
|
|
|
2012-02-11 19:38:06 +08:00
|
|
|
/// eraseNamedMetadata - Remove the given NamedMDNode from this module and
|
|
|
|
/// delete it.
|
2010-07-22 07:38:33 +08:00
|
|
|
void Module::eraseNamedMetadata(NamedMDNode *NMD) {
|
|
|
|
static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab)->erase(NMD->getName());
|
|
|
|
NamedMDList.erase(NMD);
|
|
|
|
}
|
|
|
|
|
2012-02-16 06:34:20 +08:00
|
|
|
/// getModuleFlagsMetadata - Returns the module flags in the provided vector.
|
|
|
|
void Module::
|
|
|
|
getModuleFlagsMetadata(SmallVectorImpl<ModuleFlagEntry> &Flags) const {
|
|
|
|
const NamedMDNode *ModFlags = getModuleFlagsMetadata();
|
|
|
|
if (!ModFlags) return;
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = ModFlags->getNumOperands(); i != e; ++i) {
|
|
|
|
MDNode *Flag = ModFlags->getOperand(i);
|
|
|
|
ConstantInt *Behavior = cast<ConstantInt>(Flag->getOperand(0));
|
|
|
|
MDString *Key = cast<MDString>(Flag->getOperand(1));
|
|
|
|
Value *Val = Flag->getOperand(2);
|
2012-02-16 18:28:10 +08:00
|
|
|
Flags.push_back(ModuleFlagEntry(ModFlagBehavior(Behavior->getZExtValue()),
|
2012-02-16 07:27:50 +08:00
|
|
|
Key, Val));
|
2012-02-16 06:34:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-11 19:38:06 +08:00
|
|
|
/// getModuleFlagsMetadata - Returns the NamedMDNode in the module that
|
|
|
|
/// represents module-level flags. This method returns null if there are no
|
|
|
|
/// module-level flags.
|
|
|
|
NamedMDNode *Module::getModuleFlagsMetadata() const {
|
|
|
|
return getNamedMetadata("llvm.module.flags");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getOrInsertModuleFlagsMetadata - Returns the NamedMDNode in the module that
|
|
|
|
/// represents module-level flags. If module-level flags aren't found, it
|
|
|
|
/// creates the named metadata that contains them.
|
|
|
|
NamedMDNode *Module::getOrInsertModuleFlagsMetadata() {
|
|
|
|
return getOrInsertNamedMetadata("llvm.module.flags");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// addModuleFlag - Add a module-level flag to the module-level flags
|
|
|
|
/// metadata. It will create the module-level flags named metadata if it doesn't
|
|
|
|
/// already exist.
|
2012-02-16 18:28:10 +08:00
|
|
|
void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key,
|
2012-02-11 19:38:06 +08:00
|
|
|
Value *Val) {
|
|
|
|
Type *Int32Ty = Type::getInt32Ty(Context);
|
|
|
|
Value *Ops[3] = {
|
|
|
|
ConstantInt::get(Int32Ty, Behavior), MDString::get(Context, Key), Val
|
|
|
|
};
|
|
|
|
getOrInsertModuleFlagsMetadata()->addOperand(MDNode::get(Context, Ops));
|
|
|
|
}
|
2012-02-16 18:28:10 +08:00
|
|
|
void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key,
|
2012-02-11 19:38:06 +08:00
|
|
|
uint32_t Val) {
|
|
|
|
Type *Int32Ty = Type::getInt32Ty(Context);
|
|
|
|
addModuleFlag(Behavior, Key, ConstantInt::get(Int32Ty, Val));
|
|
|
|
}
|
|
|
|
void Module::addModuleFlag(MDNode *Node) {
|
|
|
|
assert(Node->getNumOperands() == 3 &&
|
|
|
|
"Invalid number of operands for module flag!");
|
|
|
|
assert(isa<ConstantInt>(Node->getOperand(0)) &&
|
|
|
|
isa<MDString>(Node->getOperand(1)) &&
|
|
|
|
"Invalid operand types for module flag!");
|
|
|
|
getOrInsertModuleFlagsMetadata()->addOperand(Node);
|
|
|
|
}
|
2002-04-14 02:58:33 +08:00
|
|
|
|
2010-01-28 04:34:15 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Methods to control the materialization of GlobalValues in the Module.
|
|
|
|
//
|
|
|
|
void Module::setMaterializer(GVMaterializer *GVM) {
|
|
|
|
assert(!Materializer &&
|
|
|
|
"Module already has a GVMaterializer. Call MaterializeAllPermanently"
|
|
|
|
" to clear it out before setting another one.");
|
|
|
|
Materializer.reset(GVM);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Module::isMaterializable(const GlobalValue *GV) const {
|
|
|
|
if (Materializer)
|
|
|
|
return Materializer->isMaterializable(GV);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Module::isDematerializable(const GlobalValue *GV) const {
|
|
|
|
if (Materializer)
|
|
|
|
return Materializer->isDematerializable(GV);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Module::Materialize(GlobalValue *GV, std::string *ErrInfo) {
|
|
|
|
if (Materializer)
|
|
|
|
return Materializer->Materialize(GV, ErrInfo);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Module::Dematerialize(GlobalValue *GV) {
|
|
|
|
if (Materializer)
|
|
|
|
return Materializer->Dematerialize(GV);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Module::MaterializeAll(std::string *ErrInfo) {
|
|
|
|
if (!Materializer)
|
|
|
|
return false;
|
|
|
|
return Materializer->MaterializeModule(this, ErrInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Module::MaterializeAllPermanently(std::string *ErrInfo) {
|
|
|
|
if (MaterializeAll(ErrInfo))
|
|
|
|
return true;
|
|
|
|
Materializer.reset();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2003-12-31 16:43:01 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Other module related stuff.
|
|
|
|
//
|
|
|
|
|
|
|
|
|
2012-04-17 07:54:31 +08:00
|
|
|
// dropAllReferences() - This function causes all the subelements to "let go"
|
2002-08-18 07:32:47 +08:00
|
|
|
// of all references that they are maintaining. This allows one to 'delete' a
|
|
|
|
// whole module at a time, even though there may be circular references... first
|
|
|
|
// all references are dropped, and all use counts go to zero. Then everything
|
2003-10-11 01:54:14 +08:00
|
|
|
// is deleted for real. Note that no operations are valid on an object that
|
2002-08-18 07:32:47 +08:00
|
|
|
// has "dropped all references", except operator delete.
|
2001-06-07 04:29:01 +08:00
|
|
|
//
|
|
|
|
void Module::dropAllReferences() {
|
2002-06-26 00:13:24 +08:00
|
|
|
for(Module::iterator I = begin(), E = end(); I != E; ++I)
|
|
|
|
I->dropAllReferences();
|
2001-10-13 14:58:40 +08:00
|
|
|
|
2005-03-15 12:54:21 +08:00
|
|
|
for(Module::global_iterator I = global_begin(), E = global_end(); I != E; ++I)
|
2002-06-26 00:13:24 +08:00
|
|
|
I->dropAllReferences();
|
2007-04-28 21:45:00 +08:00
|
|
|
|
|
|
|
for(Module::alias_iterator I = alias_begin(), E = alias_end(); I != E; ++I)
|
|
|
|
I->dropAllReferences();
|
2001-06-07 04:29:01 +08:00
|
|
|
}
|
2001-06-30 12:35:40 +08:00
|
|
|
|
2009-11-06 18:58:06 +08:00
|
|
|
void Module::addLibrary(StringRef Lib) {
|
2007-02-04 08:40:42 +08:00
|
|
|
for (Module::lib_iterator I = lib_begin(), E = lib_end(); I != E; ++I)
|
|
|
|
if (*I == Lib)
|
|
|
|
return;
|
|
|
|
LibraryList.push_back(Lib);
|
|
|
|
}
|
|
|
|
|
2009-11-06 18:58:06 +08:00
|
|
|
void Module::removeLibrary(StringRef Lib) {
|
2007-02-04 08:40:42 +08:00
|
|
|
LibraryListType::iterator I = LibraryList.begin();
|
|
|
|
LibraryListType::iterator E = LibraryList.end();
|
|
|
|
for (;I != E; ++I)
|
|
|
|
if (*I == Lib) {
|
|
|
|
LibraryList.erase(I);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|