2003-10-13 03:32:08 +00:00
|
|
|
//===-- Module.cpp - Implement the Module class ---------------------------===//
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-06 20:29:01 +00:00
|
|
|
//
|
2013-01-02 09:10:48 +00:00
|
|
|
// This file implements the Module class for the IR library.
|
2001-06-06 20:29:01 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/Module.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "SymbolTableListTraitsImpl.h"
|
|
|
|
#include "llvm/ADT/DenseSet.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
2014-03-06 03:50:29 +00:00
|
|
|
#include "llvm/IR/GVMaterializer.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/InstrTypes.h"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
Ask the module for its the identified types.
When lazy reading a module, the types used in a function will not be visible to
a TypeFinder until the body is read.
This patch fixes that by asking the module for its identified struct types.
If a materializer is present, the module asks it. If not, it uses a TypeFinder.
This fixes pr21374.
I will be the first to say that this is ugly, but it was the best I could find.
Some of the options I looked at:
* Asking the LLVMContext. This could be made to work for gold, but not currently
for ld64. ld64 will load multiple modules into a single context before merging
them. This causes us to see types from future merges. Unfortunately,
MappedTypes is not just a cache when it comes to opaque types. Once the
mapping has been made, we have to remember it for as long as the key may
be used. This would mean moving MappedTypes to the Linker class and having
to drop the Linker::LinkModules static methods, which are visible from C.
* Adding an option to ignore function bodies in the TypeFinder. This would
fix the PR by picking the worst result. It would work, but unfortunately
we are currently quite dependent on the upfront type merging. I will
try to reduce our dependency, but it is not clear that we will be able
to get rid of it for now.
The only clean solution I could think of is making the Module own the types.
This would have other advantages, but it is a much bigger change. I will
propose it, but it is nice to have this fixed while that is discussed.
With the gold plugin, this patch takes the number of types in the LTO clang
binary from 52817 to 49669.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@223215 91177308-0d34-0410-b5e6-96231b3b80d8
2014-12-03 07:18:23 +00:00
|
|
|
#include "llvm/IR/TypeFinder.h"
|
2014-04-17 22:33:50 +00:00
|
|
|
#include "llvm/Support/Dwarf.h"
|
2014-06-25 15:21:42 +00:00
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
#include "llvm/Support/RandomNumberGenerator.h"
|
2002-06-25 16:13:24 +00:00
|
|
|
#include <algorithm>
|
2003-08-31 00:19:28 +00:00
|
|
|
#include <cstdarg>
|
2006-05-18 02:10:31 +00:00
|
|
|
#include <cstdlib>
|
2003-11-21 20:23:48 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2003-12-31 08:43:01 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2004-04-21 18:27:56 +00:00
|
|
|
// Methods to implement the globals and functions lists.
|
2003-12-31 08:43:01 +00:00
|
|
|
//
|
|
|
|
|
2002-06-25 16:13:24 +00:00
|
|
|
// Explicit instantiations of SymbolTableListTraits since some of the methods
|
2006-01-24 04:13:11 +00:00
|
|
|
// are not in the public header file.
|
2009-12-19 00:55:12 +00:00
|
|
|
template class llvm::SymbolTableListTraits<Function, Module>;
|
2011-08-13 01:04:44 +00:00
|
|
|
template class llvm::SymbolTableListTraits<GlobalVariable, Module>;
|
2009-12-19 00:55:12 +00:00
|
|
|
template class llvm::SymbolTableListTraits<GlobalAlias, Module>;
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2003-12-31 08:43:01 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Primitive Module methods.
|
|
|
|
//
|
2001-10-13 06:58:40 +00:00
|
|
|
|
2014-02-25 20:01:08 +00:00
|
|
|
Module::Module(StringRef MID, LLVMContext &C)
|
2014-12-17 18:12:10 +00:00
|
|
|
: Context(C), Materializer(), ModuleID(MID), DL("") {
|
2007-02-05 20:47:22 +00:00
|
|
|
ValSymTab = new ValueSymbolTable();
|
2010-07-21 23:38:33 +00:00
|
|
|
NamedMDSymTab = new StringMap<NamedMDNode *>();
|
2010-09-08 18:03:32 +00:00
|
|
|
Context.addModule(this);
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Module::~Module() {
|
2010-09-08 18:03:32 +00:00
|
|
|
Context.removeModule(this);
|
2001-06-06 20:29:01 +00:00
|
|
|
dropAllReferences();
|
2002-06-25 16:13:24 +00:00
|
|
|
GlobalList.clear();
|
|
|
|
FunctionList.clear();
|
2007-04-25 14:27:10 +00:00
|
|
|
AliasList.clear();
|
2009-07-29 17:16:17 +00:00
|
|
|
NamedMDList.clear();
|
2007-01-06 07:24:44 +00:00
|
|
|
delete ValSymTab;
|
2010-07-21 23:38:33 +00:00
|
|
|
delete static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab);
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
2014-12-17 18:12:10 +00:00
|
|
|
RandomNumberGenerator *Module::createRNG(const Pass* P) const {
|
|
|
|
SmallString<32> Salt(P->getPassName());
|
|
|
|
|
|
|
|
// This RNG is guaranteed to produce the same random stream only
|
|
|
|
// when the Module ID and thus the input filename is the same. This
|
|
|
|
// might be problematic if the input filename extension changes
|
|
|
|
// (e.g. from .c to .bc or .ll).
|
|
|
|
//
|
|
|
|
// We could store this salt in NamedMetadata, but this would make
|
|
|
|
// the parameter non-const. This would unfortunately make this
|
|
|
|
// interface unusable by any Machine passes, since they only have a
|
|
|
|
// const reference to their IR Module. Alternatively we can always
|
|
|
|
// store salt metadata from the Module constructor.
|
|
|
|
Salt += sys::path::filename(getModuleIdentifier());
|
|
|
|
|
|
|
|
return new RandomNumberGenerator(Salt);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-06 22:04:43 +00: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 10:58:06 +00:00
|
|
|
GlobalValue *Module::getNamedValue(StringRef Name) const {
|
2009-03-06 22:04:43 +00:00
|
|
|
return cast_or_null<GlobalValue>(getValueSymbolTable().lookup(Name));
|
|
|
|
}
|
|
|
|
|
2009-12-29 09:01:33 +00: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 08:43:01 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Methods for easy access to the functions in the module.
|
|
|
|
//
|
|
|
|
|
2007-02-05 20:47:22 +00: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 10:58:06 +00:00
|
|
|
Constant *Module::getOrInsertFunction(StringRef Name,
|
2011-07-18 04:54:35 +00:00
|
|
|
FunctionType *Ty,
|
2012-12-07 23:16:57 +00:00
|
|
|
AttributeSet AttributeList) {
|
2007-02-05 20:47:22 +00:00
|
|
|
// See if we have a definition for the specified function already.
|
2009-03-06 22:04:43 +00:00
|
|
|
GlobalValue *F = getNamedValue(Name);
|
2014-04-09 06:08:46 +00:00
|
|
|
if (!F) {
|
2007-02-05 20:47:22 +00:00
|
|
|
// Nope, add it
|
2008-04-06 20:25:17 +00:00
|
|
|
Function *New = Function::Create(Ty, GlobalVariable::ExternalLinkage, Name);
|
2009-01-04 22:54:40 +00:00
|
|
|
if (!New->isIntrinsic()) // Intrinsics get attrs set on construction
|
|
|
|
New->setAttributes(AttributeList);
|
2002-03-29 03:44:18 +00:00
|
|
|
FunctionList.push_back(New);
|
2007-01-07 08:09:25 +00:00
|
|
|
return New; // Return the new prototype.
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the function exists but has the wrong type, return a bitcast to the
|
|
|
|
// right type.
|
2009-07-29 22:17:13 +00:00
|
|
|
if (F->getType() != PointerType::getUnqual(Ty))
|
|
|
|
return ConstantExpr::getBitCast(F, PointerType::getUnqual(Ty));
|
2012-04-23 00:23:33 +00:00
|
|
|
|
2007-01-07 08:09:25 +00:00
|
|
|
// Otherwise, we just found the existing function or a prototype.
|
2012-04-23 00:23:33 +00:00
|
|
|
return F;
|
2002-03-29 03:44:18 +00:00
|
|
|
}
|
|
|
|
|
2009-11-06 10:58:06 +00:00
|
|
|
Constant *Module::getOrInsertFunction(StringRef Name,
|
2011-07-18 04:54:35 +00:00
|
|
|
FunctionType *Ty) {
|
2012-12-07 23:16:57 +00:00
|
|
|
return getOrInsertFunction(Name, Ty, AttributeSet());
|
2009-01-04 22:54:40 +00:00
|
|
|
}
|
|
|
|
|
2003-08-31 00:19:28 +00: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 10:58:06 +00:00
|
|
|
Constant *Module::getOrInsertFunction(StringRef Name,
|
2012-12-07 23:16:57 +00:00
|
|
|
AttributeSet AttributeList,
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *RetTy, ...) {
|
2003-08-31 00:19:28 +00:00
|
|
|
va_list Args;
|
|
|
|
va_start(Args, RetTy);
|
|
|
|
|
|
|
|
// Build the list of argument types...
|
2011-07-12 14:06:48 +00:00
|
|
|
std::vector<Type*> ArgTys;
|
|
|
|
while (Type *ArgTy = va_arg(Args, Type*))
|
2003-08-31 00:19:28 +00:00
|
|
|
ArgTys.push_back(ArgTy);
|
|
|
|
|
|
|
|
va_end(Args);
|
|
|
|
|
|
|
|
// Build the function type and chain to the other getOrInsertFunction...
|
2009-07-08 23:50:31 +00:00
|
|
|
return getOrInsertFunction(Name,
|
2009-07-29 22:17:13 +00:00
|
|
|
FunctionType::get(RetTy, ArgTys, false),
|
2009-01-04 22:54:40 +00:00
|
|
|
AttributeList);
|
2003-08-31 00:19:28 +00:00
|
|
|
}
|
|
|
|
|
2009-11-06 10:58:06 +00:00
|
|
|
Constant *Module::getOrInsertFunction(StringRef Name,
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *RetTy, ...) {
|
2009-01-04 22:54:40 +00:00
|
|
|
va_list Args;
|
|
|
|
va_start(Args, RetTy);
|
|
|
|
|
|
|
|
// Build the list of argument types...
|
2011-07-12 14:06:48 +00:00
|
|
|
std::vector<Type*> ArgTys;
|
|
|
|
while (Type *ArgTy = va_arg(Args, Type*))
|
2009-01-04 22:54:40 +00:00
|
|
|
ArgTys.push_back(ArgTy);
|
|
|
|
|
|
|
|
va_end(Args);
|
|
|
|
|
|
|
|
// Build the function type and chain to the other getOrInsertFunction...
|
2012-04-23 00:23:33 +00:00
|
|
|
return getOrInsertFunction(Name,
|
2009-07-29 22:17:13 +00:00
|
|
|
FunctionType::get(RetTy, ArgTys, false),
|
2012-12-07 23:16:57 +00:00
|
|
|
AttributeSet());
|
2009-01-04 22:54:40 +00:00
|
|
|
}
|
2003-08-31 00:19:28 +00:00
|
|
|
|
2002-03-29 03:44:18 +00:00
|
|
|
// getFunction - Look up the specified function in the module symbol table.
|
|
|
|
// If it does not exist, return null.
|
|
|
|
//
|
2009-11-06 10:58:06 +00:00
|
|
|
Function *Module::getFunction(StringRef Name) const {
|
2009-03-06 22:04:43 +00:00
|
|
|
return dyn_cast_or_null<Function>(getNamedValue(Name));
|
2008-06-27 21:09:10 +00:00
|
|
|
}
|
|
|
|
|
2003-12-31 08:43:01 +00: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 05:30:21 +00: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-15 20:18:42 +00: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 08:43:01 +00:00
|
|
|
///
|
2013-07-25 02:50:08 +00:00
|
|
|
GlobalVariable *Module::getGlobalVariable(StringRef Name, bool AllowLocal) {
|
2012-04-23 00:23:33 +00:00
|
|
|
if (GlobalVariable *Result =
|
2009-03-06 22:04:43 +00:00
|
|
|
dyn_cast_or_null<GlobalVariable>(getNamedValue(Name)))
|
|
|
|
if (AllowLocal || !Result->hasLocalLinkage())
|
2003-12-31 08:43:01 +00:00
|
|
|
return Result;
|
2014-04-09 06:08:46 +00:00
|
|
|
return nullptr;
|
2003-12-31 08:43:01 +00:00
|
|
|
}
|
|
|
|
|
2008-11-05 23:42:27 +00: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.
|
2013-09-28 01:08:00 +00:00
|
|
|
/// 3. Finally, if the existing global is the correct declaration, return the
|
2008-11-05 23:42:27 +00:00
|
|
|
/// existing global.
|
2011-07-18 04:54:35 +00:00
|
|
|
Constant *Module::getOrInsertGlobal(StringRef Name, Type *Ty) {
|
2008-11-04 22:51:24 +00:00
|
|
|
// See if we have a definition for the specified global already.
|
2009-03-06 22:04:43 +00:00
|
|
|
GlobalVariable *GV = dyn_cast_or_null<GlobalVariable>(getNamedValue(Name));
|
2014-04-09 06:08:46 +00:00
|
|
|
if (!GV) {
|
2008-11-04 22:51:24 +00:00
|
|
|
// Nope, add it
|
|
|
|
GlobalVariable *New =
|
2009-07-08 19:03:57 +00:00
|
|
|
new GlobalVariable(*this, Ty, false, GlobalVariable::ExternalLinkage,
|
2014-04-09 06:08:46 +00:00
|
|
|
nullptr, Name);
|
2009-07-08 19:03:57 +00:00
|
|
|
return New; // Return the new declaration.
|
2008-11-04 22:51:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the variable exists but has the wrong type, return a bitcast to the
|
|
|
|
// right type.
|
2013-09-30 21:23:03 +00:00
|
|
|
Type *GVTy = GV->getType();
|
|
|
|
PointerType *PTy = PointerType::get(Ty, GVTy->getPointerAddressSpace());
|
2013-09-30 23:31:50 +00:00
|
|
|
if (GVTy != PTy)
|
2013-09-30 21:23:03 +00:00
|
|
|
return ConstantExpr::getBitCast(GV, PTy);
|
2012-04-23 00:23:33 +00:00
|
|
|
|
2008-11-04 22:51:24 +00:00
|
|
|
// Otherwise, we just found the existing function or a prototype.
|
|
|
|
return GV;
|
|
|
|
}
|
|
|
|
|
2007-04-25 14:27:10 +00: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 10:58:06 +00:00
|
|
|
GlobalAlias *Module::getNamedAlias(StringRef Name) const {
|
2009-03-06 22:04:43 +00:00
|
|
|
return dyn_cast_or_null<GlobalAlias>(getNamedValue(Name));
|
2007-04-25 14:27:10 +00:00
|
|
|
}
|
|
|
|
|
2009-07-30 23:59:04 +00:00
|
|
|
/// getNamedMetadata - Return the first NamedMDNode in the module with the
|
2012-04-23 00:23:33 +00:00
|
|
|
/// specified name. This method returns null if a NamedMDNode with the
|
2010-06-19 05:33:57 +00:00
|
|
|
/// specified name is not found.
|
2010-06-22 01:19:38 +00:00
|
|
|
NamedMDNode *Module::getNamedMetadata(const Twine &Name) const {
|
2010-06-16 00:53:55 +00:00
|
|
|
SmallString<256> NameData;
|
|
|
|
StringRef NameRef = Name.toStringRef(NameData);
|
2010-07-21 23:38:33 +00:00
|
|
|
return static_cast<StringMap<NamedMDNode*> *>(NamedMDSymTab)->lookup(NameRef);
|
2010-06-16 00:53:55 +00:00
|
|
|
}
|
|
|
|
|
2012-04-23 00:23:33 +00:00
|
|
|
/// getOrInsertNamedMetadata - Return the first named MDNode in the module
|
|
|
|
/// with the specified name. This method returns a new NamedMDNode if a
|
2009-07-30 23:59:04 +00:00
|
|
|
/// NamedMDNode with the specified name is not found.
|
2009-11-06 10:58:06 +00:00
|
|
|
NamedMDNode *Module::getOrInsertNamedMetadata(StringRef Name) {
|
2010-07-21 23:38:33 +00:00
|
|
|
NamedMDNode *&NMD =
|
|
|
|
(*static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab))[Name];
|
|
|
|
if (!NMD) {
|
|
|
|
NMD = new NamedMDNode(Name);
|
|
|
|
NMD->setParent(this);
|
|
|
|
NamedMDList.push_back(NMD);
|
|
|
|
}
|
2009-07-30 23:59:04 +00:00
|
|
|
return NMD;
|
|
|
|
}
|
|
|
|
|
2012-02-11 11:38:06 +00:00
|
|
|
/// eraseNamedMetadata - Remove the given NamedMDNode from this module and
|
|
|
|
/// delete it.
|
2010-07-21 23:38:33 +00:00
|
|
|
void Module::eraseNamedMetadata(NamedMDNode *NMD) {
|
|
|
|
static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab)->erase(NMD->getName());
|
|
|
|
NamedMDList.erase(NMD);
|
|
|
|
}
|
|
|
|
|
IR: Split Metadata from Value
Split `Metadata` away from the `Value` class hierarchy, as part of
PR21532. Assembly and bitcode changes are in the wings, but this is the
bulk of the change for the IR C++ API.
I have a follow-up patch prepared for `clang`. If this breaks other
sub-projects, I apologize in advance :(. Help me compile it on Darwin
I'll try to fix it. FWIW, the errors should be easy to fix, so it may
be simpler to just fix it yourself.
This breaks the build for all metadata-related code that's out-of-tree.
Rest assured the transition is mechanical and the compiler should catch
almost all of the problems.
Here's a quick guide for updating your code:
- `Metadata` is the root of a class hierarchy with three main classes:
`MDNode`, `MDString`, and `ValueAsMetadata`. It is distinct from
the `Value` class hierarchy. It is typeless -- i.e., instances do
*not* have a `Type`.
- `MDNode`'s operands are all `Metadata *` (instead of `Value *`).
- `TrackingVH<MDNode>` and `WeakVH` referring to metadata can be
replaced with `TrackingMDNodeRef` and `TrackingMDRef`, respectively.
If you're referring solely to resolved `MDNode`s -- post graph
construction -- just use `MDNode*`.
- `MDNode` (and the rest of `Metadata`) have only limited support for
`replaceAllUsesWith()`.
As long as an `MDNode` is pointing at a forward declaration -- the
result of `MDNode::getTemporary()` -- it maintains a side map of its
uses and can RAUW itself. Once the forward declarations are fully
resolved RAUW support is dropped on the ground. This means that
uniquing collisions on changing operands cause nodes to become
"distinct". (This already happened fairly commonly, whenever an
operand went to null.)
If you're constructing complex (non self-reference) `MDNode` cycles,
you need to call `MDNode::resolveCycles()` on each node (or on a
top-level node that somehow references all of the nodes). Also,
don't do that. Metadata cycles (and the RAUW machinery needed to
construct them) are expensive.
- An `MDNode` can only refer to a `Constant` through a bridge called
`ConstantAsMetadata` (one of the subclasses of `ValueAsMetadata`).
As a side effect, accessing an operand of an `MDNode` that is known
to be, e.g., `ConstantInt`, takes three steps: first, cast from
`Metadata` to `ConstantAsMetadata`; second, extract the `Constant`;
third, cast down to `ConstantInt`.
The eventual goal is to introduce `MDInt`/`MDFloat`/etc. and have
metadata schema owners transition away from using `Constant`s when
the type isn't important (and they don't care about referring to
`GlobalValue`s).
In the meantime, I've added transitional API to the `mdconst`
namespace that matches semantics with the old code, in order to
avoid adding the error-prone three-step equivalent to every call
site. If your old code was:
MDNode *N = foo();
bar(isa <ConstantInt>(N->getOperand(0)));
baz(cast <ConstantInt>(N->getOperand(1)));
bak(cast_or_null <ConstantInt>(N->getOperand(2)));
bat(dyn_cast <ConstantInt>(N->getOperand(3)));
bay(dyn_cast_or_null<ConstantInt>(N->getOperand(4)));
you can trivially match its semantics with:
MDNode *N = foo();
bar(mdconst::hasa <ConstantInt>(N->getOperand(0)));
baz(mdconst::extract <ConstantInt>(N->getOperand(1)));
bak(mdconst::extract_or_null <ConstantInt>(N->getOperand(2)));
bat(mdconst::dyn_extract <ConstantInt>(N->getOperand(3)));
bay(mdconst::dyn_extract_or_null<ConstantInt>(N->getOperand(4)));
and when you transition your metadata schema to `MDInt`:
MDNode *N = foo();
bar(isa <MDInt>(N->getOperand(0)));
baz(cast <MDInt>(N->getOperand(1)));
bak(cast_or_null <MDInt>(N->getOperand(2)));
bat(dyn_cast <MDInt>(N->getOperand(3)));
bay(dyn_cast_or_null<MDInt>(N->getOperand(4)));
- A `CallInst` -- specifically, intrinsic instructions -- can refer to
metadata through a bridge called `MetadataAsValue`. This is a
subclass of `Value` where `getType()->isMetadataTy()`.
`MetadataAsValue` is the *only* class that can legally refer to a
`LocalAsMetadata`, which is a bridged form of non-`Constant` values
like `Argument` and `Instruction`. It can also refer to any other
`Metadata` subclass.
(I'll break all your testcases in a follow-up commit, when I propagate
this change to assembly.)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@223802 91177308-0d34-0410-b5e6-96231b3b80d8
2014-12-09 18:38:53 +00:00
|
|
|
bool Module::isValidModFlagBehavior(Metadata *MD, ModFlagBehavior &MFB) {
|
2015-02-11 09:13:06 +00:00
|
|
|
if (ConstantInt *Behavior = mdconst::dyn_extract_or_null<ConstantInt>(MD)) {
|
2014-09-08 19:16:28 +00:00
|
|
|
uint64_t Val = Behavior->getLimitedValue();
|
|
|
|
if (Val >= ModFlagBehaviorFirstVal && Val <= ModFlagBehaviorLastVal) {
|
|
|
|
MFB = static_cast<ModFlagBehavior>(Val);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-02-15 22:34:20 +00:00
|
|
|
/// getModuleFlagsMetadata - Returns the module flags in the provided vector.
|
|
|
|
void Module::
|
|
|
|
getModuleFlagsMetadata(SmallVectorImpl<ModuleFlagEntry> &Flags) const {
|
|
|
|
const NamedMDNode *ModFlags = getModuleFlagsMetadata();
|
|
|
|
if (!ModFlags) return;
|
|
|
|
|
2014-11-11 21:30:22 +00:00
|
|
|
for (const MDNode *Flag : ModFlags->operands()) {
|
2014-09-08 19:16:28 +00:00
|
|
|
ModFlagBehavior MFB;
|
|
|
|
if (Flag->getNumOperands() >= 3 &&
|
|
|
|
isValidModFlagBehavior(Flag->getOperand(0), MFB) &&
|
2015-02-11 09:13:06 +00:00
|
|
|
dyn_cast_or_null<MDString>(Flag->getOperand(1))) {
|
2013-12-02 21:29:56 +00:00
|
|
|
// Check the operands of the MDNode before accessing the operands.
|
|
|
|
// The verifier will actually catch these failures.
|
|
|
|
MDString *Key = cast<MDString>(Flag->getOperand(1));
|
IR: Split Metadata from Value
Split `Metadata` away from the `Value` class hierarchy, as part of
PR21532. Assembly and bitcode changes are in the wings, but this is the
bulk of the change for the IR C++ API.
I have a follow-up patch prepared for `clang`. If this breaks other
sub-projects, I apologize in advance :(. Help me compile it on Darwin
I'll try to fix it. FWIW, the errors should be easy to fix, so it may
be simpler to just fix it yourself.
This breaks the build for all metadata-related code that's out-of-tree.
Rest assured the transition is mechanical and the compiler should catch
almost all of the problems.
Here's a quick guide for updating your code:
- `Metadata` is the root of a class hierarchy with three main classes:
`MDNode`, `MDString`, and `ValueAsMetadata`. It is distinct from
the `Value` class hierarchy. It is typeless -- i.e., instances do
*not* have a `Type`.
- `MDNode`'s operands are all `Metadata *` (instead of `Value *`).
- `TrackingVH<MDNode>` and `WeakVH` referring to metadata can be
replaced with `TrackingMDNodeRef` and `TrackingMDRef`, respectively.
If you're referring solely to resolved `MDNode`s -- post graph
construction -- just use `MDNode*`.
- `MDNode` (and the rest of `Metadata`) have only limited support for
`replaceAllUsesWith()`.
As long as an `MDNode` is pointing at a forward declaration -- the
result of `MDNode::getTemporary()` -- it maintains a side map of its
uses and can RAUW itself. Once the forward declarations are fully
resolved RAUW support is dropped on the ground. This means that
uniquing collisions on changing operands cause nodes to become
"distinct". (This already happened fairly commonly, whenever an
operand went to null.)
If you're constructing complex (non self-reference) `MDNode` cycles,
you need to call `MDNode::resolveCycles()` on each node (or on a
top-level node that somehow references all of the nodes). Also,
don't do that. Metadata cycles (and the RAUW machinery needed to
construct them) are expensive.
- An `MDNode` can only refer to a `Constant` through a bridge called
`ConstantAsMetadata` (one of the subclasses of `ValueAsMetadata`).
As a side effect, accessing an operand of an `MDNode` that is known
to be, e.g., `ConstantInt`, takes three steps: first, cast from
`Metadata` to `ConstantAsMetadata`; second, extract the `Constant`;
third, cast down to `ConstantInt`.
The eventual goal is to introduce `MDInt`/`MDFloat`/etc. and have
metadata schema owners transition away from using `Constant`s when
the type isn't important (and they don't care about referring to
`GlobalValue`s).
In the meantime, I've added transitional API to the `mdconst`
namespace that matches semantics with the old code, in order to
avoid adding the error-prone three-step equivalent to every call
site. If your old code was:
MDNode *N = foo();
bar(isa <ConstantInt>(N->getOperand(0)));
baz(cast <ConstantInt>(N->getOperand(1)));
bak(cast_or_null <ConstantInt>(N->getOperand(2)));
bat(dyn_cast <ConstantInt>(N->getOperand(3)));
bay(dyn_cast_or_null<ConstantInt>(N->getOperand(4)));
you can trivially match its semantics with:
MDNode *N = foo();
bar(mdconst::hasa <ConstantInt>(N->getOperand(0)));
baz(mdconst::extract <ConstantInt>(N->getOperand(1)));
bak(mdconst::extract_or_null <ConstantInt>(N->getOperand(2)));
bat(mdconst::dyn_extract <ConstantInt>(N->getOperand(3)));
bay(mdconst::dyn_extract_or_null<ConstantInt>(N->getOperand(4)));
and when you transition your metadata schema to `MDInt`:
MDNode *N = foo();
bar(isa <MDInt>(N->getOperand(0)));
baz(cast <MDInt>(N->getOperand(1)));
bak(cast_or_null <MDInt>(N->getOperand(2)));
bat(dyn_cast <MDInt>(N->getOperand(3)));
bay(dyn_cast_or_null<MDInt>(N->getOperand(4)));
- A `CallInst` -- specifically, intrinsic instructions -- can refer to
metadata through a bridge called `MetadataAsValue`. This is a
subclass of `Value` where `getType()->isMetadataTy()`.
`MetadataAsValue` is the *only* class that can legally refer to a
`LocalAsMetadata`, which is a bridged form of non-`Constant` values
like `Argument` and `Instruction`. It can also refer to any other
`Metadata` subclass.
(I'll break all your testcases in a follow-up commit, when I propagate
this change to assembly.)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@223802 91177308-0d34-0410-b5e6-96231b3b80d8
2014-12-09 18:38:53 +00:00
|
|
|
Metadata *Val = Flag->getOperand(2);
|
2014-09-08 19:16:28 +00:00
|
|
|
Flags.push_back(ModuleFlagEntry(MFB, Key, Val));
|
2013-12-02 21:29:56 +00:00
|
|
|
}
|
2012-02-15 22:34:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-16 23:21:16 +00:00
|
|
|
/// Return the corresponding value if Key appears in module flags, otherwise
|
|
|
|
/// return null.
|
IR: Split Metadata from Value
Split `Metadata` away from the `Value` class hierarchy, as part of
PR21532. Assembly and bitcode changes are in the wings, but this is the
bulk of the change for the IR C++ API.
I have a follow-up patch prepared for `clang`. If this breaks other
sub-projects, I apologize in advance :(. Help me compile it on Darwin
I'll try to fix it. FWIW, the errors should be easy to fix, so it may
be simpler to just fix it yourself.
This breaks the build for all metadata-related code that's out-of-tree.
Rest assured the transition is mechanical and the compiler should catch
almost all of the problems.
Here's a quick guide for updating your code:
- `Metadata` is the root of a class hierarchy with three main classes:
`MDNode`, `MDString`, and `ValueAsMetadata`. It is distinct from
the `Value` class hierarchy. It is typeless -- i.e., instances do
*not* have a `Type`.
- `MDNode`'s operands are all `Metadata *` (instead of `Value *`).
- `TrackingVH<MDNode>` and `WeakVH` referring to metadata can be
replaced with `TrackingMDNodeRef` and `TrackingMDRef`, respectively.
If you're referring solely to resolved `MDNode`s -- post graph
construction -- just use `MDNode*`.
- `MDNode` (and the rest of `Metadata`) have only limited support for
`replaceAllUsesWith()`.
As long as an `MDNode` is pointing at a forward declaration -- the
result of `MDNode::getTemporary()` -- it maintains a side map of its
uses and can RAUW itself. Once the forward declarations are fully
resolved RAUW support is dropped on the ground. This means that
uniquing collisions on changing operands cause nodes to become
"distinct". (This already happened fairly commonly, whenever an
operand went to null.)
If you're constructing complex (non self-reference) `MDNode` cycles,
you need to call `MDNode::resolveCycles()` on each node (or on a
top-level node that somehow references all of the nodes). Also,
don't do that. Metadata cycles (and the RAUW machinery needed to
construct them) are expensive.
- An `MDNode` can only refer to a `Constant` through a bridge called
`ConstantAsMetadata` (one of the subclasses of `ValueAsMetadata`).
As a side effect, accessing an operand of an `MDNode` that is known
to be, e.g., `ConstantInt`, takes three steps: first, cast from
`Metadata` to `ConstantAsMetadata`; second, extract the `Constant`;
third, cast down to `ConstantInt`.
The eventual goal is to introduce `MDInt`/`MDFloat`/etc. and have
metadata schema owners transition away from using `Constant`s when
the type isn't important (and they don't care about referring to
`GlobalValue`s).
In the meantime, I've added transitional API to the `mdconst`
namespace that matches semantics with the old code, in order to
avoid adding the error-prone three-step equivalent to every call
site. If your old code was:
MDNode *N = foo();
bar(isa <ConstantInt>(N->getOperand(0)));
baz(cast <ConstantInt>(N->getOperand(1)));
bak(cast_or_null <ConstantInt>(N->getOperand(2)));
bat(dyn_cast <ConstantInt>(N->getOperand(3)));
bay(dyn_cast_or_null<ConstantInt>(N->getOperand(4)));
you can trivially match its semantics with:
MDNode *N = foo();
bar(mdconst::hasa <ConstantInt>(N->getOperand(0)));
baz(mdconst::extract <ConstantInt>(N->getOperand(1)));
bak(mdconst::extract_or_null <ConstantInt>(N->getOperand(2)));
bat(mdconst::dyn_extract <ConstantInt>(N->getOperand(3)));
bay(mdconst::dyn_extract_or_null<ConstantInt>(N->getOperand(4)));
and when you transition your metadata schema to `MDInt`:
MDNode *N = foo();
bar(isa <MDInt>(N->getOperand(0)));
baz(cast <MDInt>(N->getOperand(1)));
bak(cast_or_null <MDInt>(N->getOperand(2)));
bat(dyn_cast <MDInt>(N->getOperand(3)));
bay(dyn_cast_or_null<MDInt>(N->getOperand(4)));
- A `CallInst` -- specifically, intrinsic instructions -- can refer to
metadata through a bridge called `MetadataAsValue`. This is a
subclass of `Value` where `getType()->isMetadataTy()`.
`MetadataAsValue` is the *only* class that can legally refer to a
`LocalAsMetadata`, which is a bridged form of non-`Constant` values
like `Argument` and `Instruction`. It can also refer to any other
`Metadata` subclass.
(I'll break all your testcases in a follow-up commit, when I propagate
this change to assembly.)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@223802 91177308-0d34-0410-b5e6-96231b3b80d8
2014-12-09 18:38:53 +00:00
|
|
|
Metadata *Module::getModuleFlag(StringRef Key) const {
|
2013-07-16 23:21:16 +00:00
|
|
|
SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags;
|
|
|
|
getModuleFlagsMetadata(ModuleFlags);
|
2014-03-10 15:03:06 +00:00
|
|
|
for (const ModuleFlagEntry &MFE : ModuleFlags) {
|
2013-07-16 23:21:16 +00:00
|
|
|
if (Key == MFE.Key->getString())
|
|
|
|
return MFE.Val;
|
|
|
|
}
|
2014-04-09 06:08:46 +00:00
|
|
|
return nullptr;
|
2013-07-16 23:21:16 +00:00
|
|
|
}
|
|
|
|
|
2012-02-11 11:38:06 +00: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 10:28:10 +00:00
|
|
|
void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key,
|
IR: Split Metadata from Value
Split `Metadata` away from the `Value` class hierarchy, as part of
PR21532. Assembly and bitcode changes are in the wings, but this is the
bulk of the change for the IR C++ API.
I have a follow-up patch prepared for `clang`. If this breaks other
sub-projects, I apologize in advance :(. Help me compile it on Darwin
I'll try to fix it. FWIW, the errors should be easy to fix, so it may
be simpler to just fix it yourself.
This breaks the build for all metadata-related code that's out-of-tree.
Rest assured the transition is mechanical and the compiler should catch
almost all of the problems.
Here's a quick guide for updating your code:
- `Metadata` is the root of a class hierarchy with three main classes:
`MDNode`, `MDString`, and `ValueAsMetadata`. It is distinct from
the `Value` class hierarchy. It is typeless -- i.e., instances do
*not* have a `Type`.
- `MDNode`'s operands are all `Metadata *` (instead of `Value *`).
- `TrackingVH<MDNode>` and `WeakVH` referring to metadata can be
replaced with `TrackingMDNodeRef` and `TrackingMDRef`, respectively.
If you're referring solely to resolved `MDNode`s -- post graph
construction -- just use `MDNode*`.
- `MDNode` (and the rest of `Metadata`) have only limited support for
`replaceAllUsesWith()`.
As long as an `MDNode` is pointing at a forward declaration -- the
result of `MDNode::getTemporary()` -- it maintains a side map of its
uses and can RAUW itself. Once the forward declarations are fully
resolved RAUW support is dropped on the ground. This means that
uniquing collisions on changing operands cause nodes to become
"distinct". (This already happened fairly commonly, whenever an
operand went to null.)
If you're constructing complex (non self-reference) `MDNode` cycles,
you need to call `MDNode::resolveCycles()` on each node (or on a
top-level node that somehow references all of the nodes). Also,
don't do that. Metadata cycles (and the RAUW machinery needed to
construct them) are expensive.
- An `MDNode` can only refer to a `Constant` through a bridge called
`ConstantAsMetadata` (one of the subclasses of `ValueAsMetadata`).
As a side effect, accessing an operand of an `MDNode` that is known
to be, e.g., `ConstantInt`, takes three steps: first, cast from
`Metadata` to `ConstantAsMetadata`; second, extract the `Constant`;
third, cast down to `ConstantInt`.
The eventual goal is to introduce `MDInt`/`MDFloat`/etc. and have
metadata schema owners transition away from using `Constant`s when
the type isn't important (and they don't care about referring to
`GlobalValue`s).
In the meantime, I've added transitional API to the `mdconst`
namespace that matches semantics with the old code, in order to
avoid adding the error-prone three-step equivalent to every call
site. If your old code was:
MDNode *N = foo();
bar(isa <ConstantInt>(N->getOperand(0)));
baz(cast <ConstantInt>(N->getOperand(1)));
bak(cast_or_null <ConstantInt>(N->getOperand(2)));
bat(dyn_cast <ConstantInt>(N->getOperand(3)));
bay(dyn_cast_or_null<ConstantInt>(N->getOperand(4)));
you can trivially match its semantics with:
MDNode *N = foo();
bar(mdconst::hasa <ConstantInt>(N->getOperand(0)));
baz(mdconst::extract <ConstantInt>(N->getOperand(1)));
bak(mdconst::extract_or_null <ConstantInt>(N->getOperand(2)));
bat(mdconst::dyn_extract <ConstantInt>(N->getOperand(3)));
bay(mdconst::dyn_extract_or_null<ConstantInt>(N->getOperand(4)));
and when you transition your metadata schema to `MDInt`:
MDNode *N = foo();
bar(isa <MDInt>(N->getOperand(0)));
baz(cast <MDInt>(N->getOperand(1)));
bak(cast_or_null <MDInt>(N->getOperand(2)));
bat(dyn_cast <MDInt>(N->getOperand(3)));
bay(dyn_cast_or_null<MDInt>(N->getOperand(4)));
- A `CallInst` -- specifically, intrinsic instructions -- can refer to
metadata through a bridge called `MetadataAsValue`. This is a
subclass of `Value` where `getType()->isMetadataTy()`.
`MetadataAsValue` is the *only* class that can legally refer to a
`LocalAsMetadata`, which is a bridged form of non-`Constant` values
like `Argument` and `Instruction`. It can also refer to any other
`Metadata` subclass.
(I'll break all your testcases in a follow-up commit, when I propagate
this change to assembly.)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@223802 91177308-0d34-0410-b5e6-96231b3b80d8
2014-12-09 18:38:53 +00:00
|
|
|
Metadata *Val) {
|
2012-02-11 11:38:06 +00:00
|
|
|
Type *Int32Ty = Type::getInt32Ty(Context);
|
IR: Split Metadata from Value
Split `Metadata` away from the `Value` class hierarchy, as part of
PR21532. Assembly and bitcode changes are in the wings, but this is the
bulk of the change for the IR C++ API.
I have a follow-up patch prepared for `clang`. If this breaks other
sub-projects, I apologize in advance :(. Help me compile it on Darwin
I'll try to fix it. FWIW, the errors should be easy to fix, so it may
be simpler to just fix it yourself.
This breaks the build for all metadata-related code that's out-of-tree.
Rest assured the transition is mechanical and the compiler should catch
almost all of the problems.
Here's a quick guide for updating your code:
- `Metadata` is the root of a class hierarchy with three main classes:
`MDNode`, `MDString`, and `ValueAsMetadata`. It is distinct from
the `Value` class hierarchy. It is typeless -- i.e., instances do
*not* have a `Type`.
- `MDNode`'s operands are all `Metadata *` (instead of `Value *`).
- `TrackingVH<MDNode>` and `WeakVH` referring to metadata can be
replaced with `TrackingMDNodeRef` and `TrackingMDRef`, respectively.
If you're referring solely to resolved `MDNode`s -- post graph
construction -- just use `MDNode*`.
- `MDNode` (and the rest of `Metadata`) have only limited support for
`replaceAllUsesWith()`.
As long as an `MDNode` is pointing at a forward declaration -- the
result of `MDNode::getTemporary()` -- it maintains a side map of its
uses and can RAUW itself. Once the forward declarations are fully
resolved RAUW support is dropped on the ground. This means that
uniquing collisions on changing operands cause nodes to become
"distinct". (This already happened fairly commonly, whenever an
operand went to null.)
If you're constructing complex (non self-reference) `MDNode` cycles,
you need to call `MDNode::resolveCycles()` on each node (or on a
top-level node that somehow references all of the nodes). Also,
don't do that. Metadata cycles (and the RAUW machinery needed to
construct them) are expensive.
- An `MDNode` can only refer to a `Constant` through a bridge called
`ConstantAsMetadata` (one of the subclasses of `ValueAsMetadata`).
As a side effect, accessing an operand of an `MDNode` that is known
to be, e.g., `ConstantInt`, takes three steps: first, cast from
`Metadata` to `ConstantAsMetadata`; second, extract the `Constant`;
third, cast down to `ConstantInt`.
The eventual goal is to introduce `MDInt`/`MDFloat`/etc. and have
metadata schema owners transition away from using `Constant`s when
the type isn't important (and they don't care about referring to
`GlobalValue`s).
In the meantime, I've added transitional API to the `mdconst`
namespace that matches semantics with the old code, in order to
avoid adding the error-prone three-step equivalent to every call
site. If your old code was:
MDNode *N = foo();
bar(isa <ConstantInt>(N->getOperand(0)));
baz(cast <ConstantInt>(N->getOperand(1)));
bak(cast_or_null <ConstantInt>(N->getOperand(2)));
bat(dyn_cast <ConstantInt>(N->getOperand(3)));
bay(dyn_cast_or_null<ConstantInt>(N->getOperand(4)));
you can trivially match its semantics with:
MDNode *N = foo();
bar(mdconst::hasa <ConstantInt>(N->getOperand(0)));
baz(mdconst::extract <ConstantInt>(N->getOperand(1)));
bak(mdconst::extract_or_null <ConstantInt>(N->getOperand(2)));
bat(mdconst::dyn_extract <ConstantInt>(N->getOperand(3)));
bay(mdconst::dyn_extract_or_null<ConstantInt>(N->getOperand(4)));
and when you transition your metadata schema to `MDInt`:
MDNode *N = foo();
bar(isa <MDInt>(N->getOperand(0)));
baz(cast <MDInt>(N->getOperand(1)));
bak(cast_or_null <MDInt>(N->getOperand(2)));
bat(dyn_cast <MDInt>(N->getOperand(3)));
bay(dyn_cast_or_null<MDInt>(N->getOperand(4)));
- A `CallInst` -- specifically, intrinsic instructions -- can refer to
metadata through a bridge called `MetadataAsValue`. This is a
subclass of `Value` where `getType()->isMetadataTy()`.
`MetadataAsValue` is the *only* class that can legally refer to a
`LocalAsMetadata`, which is a bridged form of non-`Constant` values
like `Argument` and `Instruction`. It can also refer to any other
`Metadata` subclass.
(I'll break all your testcases in a follow-up commit, when I propagate
this change to assembly.)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@223802 91177308-0d34-0410-b5e6-96231b3b80d8
2014-12-09 18:38:53 +00:00
|
|
|
Metadata *Ops[3] = {
|
|
|
|
ConstantAsMetadata::get(ConstantInt::get(Int32Ty, Behavior)),
|
|
|
|
MDString::get(Context, Key), Val};
|
2012-02-11 11:38:06 +00:00
|
|
|
getOrInsertModuleFlagsMetadata()->addOperand(MDNode::get(Context, Ops));
|
|
|
|
}
|
IR: Split Metadata from Value
Split `Metadata` away from the `Value` class hierarchy, as part of
PR21532. Assembly and bitcode changes are in the wings, but this is the
bulk of the change for the IR C++ API.
I have a follow-up patch prepared for `clang`. If this breaks other
sub-projects, I apologize in advance :(. Help me compile it on Darwin
I'll try to fix it. FWIW, the errors should be easy to fix, so it may
be simpler to just fix it yourself.
This breaks the build for all metadata-related code that's out-of-tree.
Rest assured the transition is mechanical and the compiler should catch
almost all of the problems.
Here's a quick guide for updating your code:
- `Metadata` is the root of a class hierarchy with three main classes:
`MDNode`, `MDString`, and `ValueAsMetadata`. It is distinct from
the `Value` class hierarchy. It is typeless -- i.e., instances do
*not* have a `Type`.
- `MDNode`'s operands are all `Metadata *` (instead of `Value *`).
- `TrackingVH<MDNode>` and `WeakVH` referring to metadata can be
replaced with `TrackingMDNodeRef` and `TrackingMDRef`, respectively.
If you're referring solely to resolved `MDNode`s -- post graph
construction -- just use `MDNode*`.
- `MDNode` (and the rest of `Metadata`) have only limited support for
`replaceAllUsesWith()`.
As long as an `MDNode` is pointing at a forward declaration -- the
result of `MDNode::getTemporary()` -- it maintains a side map of its
uses and can RAUW itself. Once the forward declarations are fully
resolved RAUW support is dropped on the ground. This means that
uniquing collisions on changing operands cause nodes to become
"distinct". (This already happened fairly commonly, whenever an
operand went to null.)
If you're constructing complex (non self-reference) `MDNode` cycles,
you need to call `MDNode::resolveCycles()` on each node (or on a
top-level node that somehow references all of the nodes). Also,
don't do that. Metadata cycles (and the RAUW machinery needed to
construct them) are expensive.
- An `MDNode` can only refer to a `Constant` through a bridge called
`ConstantAsMetadata` (one of the subclasses of `ValueAsMetadata`).
As a side effect, accessing an operand of an `MDNode` that is known
to be, e.g., `ConstantInt`, takes three steps: first, cast from
`Metadata` to `ConstantAsMetadata`; second, extract the `Constant`;
third, cast down to `ConstantInt`.
The eventual goal is to introduce `MDInt`/`MDFloat`/etc. and have
metadata schema owners transition away from using `Constant`s when
the type isn't important (and they don't care about referring to
`GlobalValue`s).
In the meantime, I've added transitional API to the `mdconst`
namespace that matches semantics with the old code, in order to
avoid adding the error-prone three-step equivalent to every call
site. If your old code was:
MDNode *N = foo();
bar(isa <ConstantInt>(N->getOperand(0)));
baz(cast <ConstantInt>(N->getOperand(1)));
bak(cast_or_null <ConstantInt>(N->getOperand(2)));
bat(dyn_cast <ConstantInt>(N->getOperand(3)));
bay(dyn_cast_or_null<ConstantInt>(N->getOperand(4)));
you can trivially match its semantics with:
MDNode *N = foo();
bar(mdconst::hasa <ConstantInt>(N->getOperand(0)));
baz(mdconst::extract <ConstantInt>(N->getOperand(1)));
bak(mdconst::extract_or_null <ConstantInt>(N->getOperand(2)));
bat(mdconst::dyn_extract <ConstantInt>(N->getOperand(3)));
bay(mdconst::dyn_extract_or_null<ConstantInt>(N->getOperand(4)));
and when you transition your metadata schema to `MDInt`:
MDNode *N = foo();
bar(isa <MDInt>(N->getOperand(0)));
baz(cast <MDInt>(N->getOperand(1)));
bak(cast_or_null <MDInt>(N->getOperand(2)));
bat(dyn_cast <MDInt>(N->getOperand(3)));
bay(dyn_cast_or_null<MDInt>(N->getOperand(4)));
- A `CallInst` -- specifically, intrinsic instructions -- can refer to
metadata through a bridge called `MetadataAsValue`. This is a
subclass of `Value` where `getType()->isMetadataTy()`.
`MetadataAsValue` is the *only* class that can legally refer to a
`LocalAsMetadata`, which is a bridged form of non-`Constant` values
like `Argument` and `Instruction`. It can also refer to any other
`Metadata` subclass.
(I'll break all your testcases in a follow-up commit, when I propagate
this change to assembly.)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@223802 91177308-0d34-0410-b5e6-96231b3b80d8
2014-12-09 18:38:53 +00:00
|
|
|
void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key,
|
|
|
|
Constant *Val) {
|
|
|
|
addModuleFlag(Behavior, Key, ConstantAsMetadata::get(Val));
|
|
|
|
}
|
2012-02-16 10:28:10 +00:00
|
|
|
void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key,
|
2012-02-11 11:38:06 +00: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!");
|
IR: Split Metadata from Value
Split `Metadata` away from the `Value` class hierarchy, as part of
PR21532. Assembly and bitcode changes are in the wings, but this is the
bulk of the change for the IR C++ API.
I have a follow-up patch prepared for `clang`. If this breaks other
sub-projects, I apologize in advance :(. Help me compile it on Darwin
I'll try to fix it. FWIW, the errors should be easy to fix, so it may
be simpler to just fix it yourself.
This breaks the build for all metadata-related code that's out-of-tree.
Rest assured the transition is mechanical and the compiler should catch
almost all of the problems.
Here's a quick guide for updating your code:
- `Metadata` is the root of a class hierarchy with three main classes:
`MDNode`, `MDString`, and `ValueAsMetadata`. It is distinct from
the `Value` class hierarchy. It is typeless -- i.e., instances do
*not* have a `Type`.
- `MDNode`'s operands are all `Metadata *` (instead of `Value *`).
- `TrackingVH<MDNode>` and `WeakVH` referring to metadata can be
replaced with `TrackingMDNodeRef` and `TrackingMDRef`, respectively.
If you're referring solely to resolved `MDNode`s -- post graph
construction -- just use `MDNode*`.
- `MDNode` (and the rest of `Metadata`) have only limited support for
`replaceAllUsesWith()`.
As long as an `MDNode` is pointing at a forward declaration -- the
result of `MDNode::getTemporary()` -- it maintains a side map of its
uses and can RAUW itself. Once the forward declarations are fully
resolved RAUW support is dropped on the ground. This means that
uniquing collisions on changing operands cause nodes to become
"distinct". (This already happened fairly commonly, whenever an
operand went to null.)
If you're constructing complex (non self-reference) `MDNode` cycles,
you need to call `MDNode::resolveCycles()` on each node (or on a
top-level node that somehow references all of the nodes). Also,
don't do that. Metadata cycles (and the RAUW machinery needed to
construct them) are expensive.
- An `MDNode` can only refer to a `Constant` through a bridge called
`ConstantAsMetadata` (one of the subclasses of `ValueAsMetadata`).
As a side effect, accessing an operand of an `MDNode` that is known
to be, e.g., `ConstantInt`, takes three steps: first, cast from
`Metadata` to `ConstantAsMetadata`; second, extract the `Constant`;
third, cast down to `ConstantInt`.
The eventual goal is to introduce `MDInt`/`MDFloat`/etc. and have
metadata schema owners transition away from using `Constant`s when
the type isn't important (and they don't care about referring to
`GlobalValue`s).
In the meantime, I've added transitional API to the `mdconst`
namespace that matches semantics with the old code, in order to
avoid adding the error-prone three-step equivalent to every call
site. If your old code was:
MDNode *N = foo();
bar(isa <ConstantInt>(N->getOperand(0)));
baz(cast <ConstantInt>(N->getOperand(1)));
bak(cast_or_null <ConstantInt>(N->getOperand(2)));
bat(dyn_cast <ConstantInt>(N->getOperand(3)));
bay(dyn_cast_or_null<ConstantInt>(N->getOperand(4)));
you can trivially match its semantics with:
MDNode *N = foo();
bar(mdconst::hasa <ConstantInt>(N->getOperand(0)));
baz(mdconst::extract <ConstantInt>(N->getOperand(1)));
bak(mdconst::extract_or_null <ConstantInt>(N->getOperand(2)));
bat(mdconst::dyn_extract <ConstantInt>(N->getOperand(3)));
bay(mdconst::dyn_extract_or_null<ConstantInt>(N->getOperand(4)));
and when you transition your metadata schema to `MDInt`:
MDNode *N = foo();
bar(isa <MDInt>(N->getOperand(0)));
baz(cast <MDInt>(N->getOperand(1)));
bak(cast_or_null <MDInt>(N->getOperand(2)));
bat(dyn_cast <MDInt>(N->getOperand(3)));
bay(dyn_cast_or_null<MDInt>(N->getOperand(4)));
- A `CallInst` -- specifically, intrinsic instructions -- can refer to
metadata through a bridge called `MetadataAsValue`. This is a
subclass of `Value` where `getType()->isMetadataTy()`.
`MetadataAsValue` is the *only* class that can legally refer to a
`LocalAsMetadata`, which is a bridged form of non-`Constant` values
like `Argument` and `Instruction`. It can also refer to any other
`Metadata` subclass.
(I'll break all your testcases in a follow-up commit, when I propagate
this change to assembly.)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@223802 91177308-0d34-0410-b5e6-96231b3b80d8
2014-12-09 18:38:53 +00:00
|
|
|
assert(mdconst::hasa<ConstantInt>(Node->getOperand(0)) &&
|
2012-02-11 11:38:06 +00:00
|
|
|
isa<MDString>(Node->getOperand(1)) &&
|
|
|
|
"Invalid operand types for module flag!");
|
|
|
|
getOrInsertModuleFlagsMetadata()->addOperand(Node);
|
|
|
|
}
|
2002-04-13 18:58:33 +00:00
|
|
|
|
2014-02-25 20:01:08 +00:00
|
|
|
void Module::setDataLayout(StringRef Desc) {
|
2014-02-25 22:23:04 +00:00
|
|
|
DL.reset(Desc);
|
|
|
|
|
2014-02-25 20:01:08 +00:00
|
|
|
if (Desc.empty()) {
|
|
|
|
DataLayoutStr = "";
|
|
|
|
} else {
|
|
|
|
DataLayoutStr = DL.getStringRepresentation();
|
2014-02-25 22:23:04 +00:00
|
|
|
// DataLayoutStr is now equivalent to Desc, but since the representation
|
|
|
|
// is not unique, they may not be identical.
|
2014-02-25 20:01:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Module::setDataLayout(const DataLayout *Other) {
|
|
|
|
if (!Other) {
|
|
|
|
DataLayoutStr = "";
|
2014-02-25 22:23:04 +00:00
|
|
|
DL.reset("");
|
2014-02-25 20:01:08 +00:00
|
|
|
} else {
|
|
|
|
DL = *Other;
|
|
|
|
DataLayoutStr = DL.getStringRepresentation();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const DataLayout *Module::getDataLayout() const {
|
|
|
|
if (DataLayoutStr.empty())
|
2014-04-09 06:08:46 +00:00
|
|
|
return nullptr;
|
2014-02-25 20:01:08 +00:00
|
|
|
return &DL;
|
|
|
|
}
|
|
|
|
|
2010-01-27 20:34:15 +00: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::isDematerializable(const GlobalValue *GV) const {
|
|
|
|
if (Materializer)
|
|
|
|
return Materializer->isDematerializable(GV);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-10-24 22:50:48 +00:00
|
|
|
std::error_code Module::materialize(GlobalValue *GV) {
|
2013-11-05 19:36:34 +00:00
|
|
|
if (!Materializer)
|
2014-10-24 22:50:48 +00:00
|
|
|
return std::error_code();
|
|
|
|
|
|
|
|
return Materializer->materialize(GV);
|
2010-01-27 20:34:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Module::Dematerialize(GlobalValue *GV) {
|
|
|
|
if (Materializer)
|
|
|
|
return Materializer->Dematerialize(GV);
|
|
|
|
}
|
|
|
|
|
2014-06-13 02:24:39 +00:00
|
|
|
std::error_code Module::materializeAll() {
|
2010-01-27 20:34:15 +00:00
|
|
|
if (!Materializer)
|
2014-06-13 02:24:39 +00:00
|
|
|
return std::error_code();
|
2014-01-14 23:02:01 +00:00
|
|
|
return Materializer->MaterializeModule(this);
|
2010-01-27 20:34:15 +00:00
|
|
|
}
|
|
|
|
|
2014-08-26 21:49:01 +00:00
|
|
|
std::error_code Module::materializeAllPermanently() {
|
2014-06-13 02:24:39 +00:00
|
|
|
if (std::error_code EC = materializeAll())
|
2014-01-14 23:51:27 +00:00
|
|
|
return EC;
|
|
|
|
|
2010-01-27 20:34:15 +00:00
|
|
|
Materializer.reset();
|
2014-06-13 02:24:39 +00:00
|
|
|
return std::error_code();
|
2010-01-27 20:34:15 +00:00
|
|
|
}
|
|
|
|
|
2003-12-31 08:43:01 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Other module related stuff.
|
|
|
|
//
|
|
|
|
|
Ask the module for its the identified types.
When lazy reading a module, the types used in a function will not be visible to
a TypeFinder until the body is read.
This patch fixes that by asking the module for its identified struct types.
If a materializer is present, the module asks it. If not, it uses a TypeFinder.
This fixes pr21374.
I will be the first to say that this is ugly, but it was the best I could find.
Some of the options I looked at:
* Asking the LLVMContext. This could be made to work for gold, but not currently
for ld64. ld64 will load multiple modules into a single context before merging
them. This causes us to see types from future merges. Unfortunately,
MappedTypes is not just a cache when it comes to opaque types. Once the
mapping has been made, we have to remember it for as long as the key may
be used. This would mean moving MappedTypes to the Linker class and having
to drop the Linker::LinkModules static methods, which are visible from C.
* Adding an option to ignore function bodies in the TypeFinder. This would
fix the PR by picking the worst result. It would work, but unfortunately
we are currently quite dependent on the upfront type merging. I will
try to reduce our dependency, but it is not clear that we will be able
to get rid of it for now.
The only clean solution I could think of is making the Module own the types.
This would have other advantages, but it is a much bigger change. I will
propose it, but it is nice to have this fixed while that is discussed.
With the gold plugin, this patch takes the number of types in the LTO clang
binary from 52817 to 49669.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@223215 91177308-0d34-0410-b5e6-96231b3b80d8
2014-12-03 07:18:23 +00:00
|
|
|
std::vector<StructType *> Module::getIdentifiedStructTypes() const {
|
|
|
|
// If we have a materializer, it is possible that some unread function
|
|
|
|
// uses a type that is currently not visible to a TypeFinder, so ask
|
|
|
|
// the materializer which types it created.
|
|
|
|
if (Materializer)
|
|
|
|
return Materializer->getIdentifiedStructTypes();
|
|
|
|
|
|
|
|
std::vector<StructType *> Ret;
|
|
|
|
TypeFinder SrcStructTypes;
|
|
|
|
SrcStructTypes.run(*this, true);
|
|
|
|
Ret.assign(SrcStructTypes.begin(), SrcStructTypes.end());
|
|
|
|
return Ret;
|
|
|
|
}
|
2003-12-31 08:43:01 +00:00
|
|
|
|
2012-04-16 23:54:31 +00:00
|
|
|
// dropAllReferences() - This function causes all the subelements to "let go"
|
2002-08-17 23:32:47 +00: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-10 17:54:14 +00:00
|
|
|
// is deleted for real. Note that no operations are valid on an object that
|
2002-08-17 23:32:47 +00:00
|
|
|
// has "dropped all references", except operator delete.
|
2001-06-06 20:29:01 +00:00
|
|
|
//
|
|
|
|
void Module::dropAllReferences() {
|
2014-07-03 16:12:55 +00:00
|
|
|
for (Function &F : *this)
|
|
|
|
F.dropAllReferences();
|
2001-10-13 06:58:40 +00:00
|
|
|
|
2014-07-03 16:12:55 +00:00
|
|
|
for (GlobalVariable &GV : globals())
|
|
|
|
GV.dropAllReferences();
|
2007-04-28 13:45:00 +00:00
|
|
|
|
2014-07-03 16:12:55 +00:00
|
|
|
for (GlobalAlias &GA : aliases())
|
|
|
|
GA.dropAllReferences();
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
2014-04-17 22:33:50 +00:00
|
|
|
|
|
|
|
unsigned Module::getDwarfVersion() const {
|
IR: Split Metadata from Value
Split `Metadata` away from the `Value` class hierarchy, as part of
PR21532. Assembly and bitcode changes are in the wings, but this is the
bulk of the change for the IR C++ API.
I have a follow-up patch prepared for `clang`. If this breaks other
sub-projects, I apologize in advance :(. Help me compile it on Darwin
I'll try to fix it. FWIW, the errors should be easy to fix, so it may
be simpler to just fix it yourself.
This breaks the build for all metadata-related code that's out-of-tree.
Rest assured the transition is mechanical and the compiler should catch
almost all of the problems.
Here's a quick guide for updating your code:
- `Metadata` is the root of a class hierarchy with three main classes:
`MDNode`, `MDString`, and `ValueAsMetadata`. It is distinct from
the `Value` class hierarchy. It is typeless -- i.e., instances do
*not* have a `Type`.
- `MDNode`'s operands are all `Metadata *` (instead of `Value *`).
- `TrackingVH<MDNode>` and `WeakVH` referring to metadata can be
replaced with `TrackingMDNodeRef` and `TrackingMDRef`, respectively.
If you're referring solely to resolved `MDNode`s -- post graph
construction -- just use `MDNode*`.
- `MDNode` (and the rest of `Metadata`) have only limited support for
`replaceAllUsesWith()`.
As long as an `MDNode` is pointing at a forward declaration -- the
result of `MDNode::getTemporary()` -- it maintains a side map of its
uses and can RAUW itself. Once the forward declarations are fully
resolved RAUW support is dropped on the ground. This means that
uniquing collisions on changing operands cause nodes to become
"distinct". (This already happened fairly commonly, whenever an
operand went to null.)
If you're constructing complex (non self-reference) `MDNode` cycles,
you need to call `MDNode::resolveCycles()` on each node (or on a
top-level node that somehow references all of the nodes). Also,
don't do that. Metadata cycles (and the RAUW machinery needed to
construct them) are expensive.
- An `MDNode` can only refer to a `Constant` through a bridge called
`ConstantAsMetadata` (one of the subclasses of `ValueAsMetadata`).
As a side effect, accessing an operand of an `MDNode` that is known
to be, e.g., `ConstantInt`, takes three steps: first, cast from
`Metadata` to `ConstantAsMetadata`; second, extract the `Constant`;
third, cast down to `ConstantInt`.
The eventual goal is to introduce `MDInt`/`MDFloat`/etc. and have
metadata schema owners transition away from using `Constant`s when
the type isn't important (and they don't care about referring to
`GlobalValue`s).
In the meantime, I've added transitional API to the `mdconst`
namespace that matches semantics with the old code, in order to
avoid adding the error-prone three-step equivalent to every call
site. If your old code was:
MDNode *N = foo();
bar(isa <ConstantInt>(N->getOperand(0)));
baz(cast <ConstantInt>(N->getOperand(1)));
bak(cast_or_null <ConstantInt>(N->getOperand(2)));
bat(dyn_cast <ConstantInt>(N->getOperand(3)));
bay(dyn_cast_or_null<ConstantInt>(N->getOperand(4)));
you can trivially match its semantics with:
MDNode *N = foo();
bar(mdconst::hasa <ConstantInt>(N->getOperand(0)));
baz(mdconst::extract <ConstantInt>(N->getOperand(1)));
bak(mdconst::extract_or_null <ConstantInt>(N->getOperand(2)));
bat(mdconst::dyn_extract <ConstantInt>(N->getOperand(3)));
bay(mdconst::dyn_extract_or_null<ConstantInt>(N->getOperand(4)));
and when you transition your metadata schema to `MDInt`:
MDNode *N = foo();
bar(isa <MDInt>(N->getOperand(0)));
baz(cast <MDInt>(N->getOperand(1)));
bak(cast_or_null <MDInt>(N->getOperand(2)));
bat(dyn_cast <MDInt>(N->getOperand(3)));
bay(dyn_cast_or_null<MDInt>(N->getOperand(4)));
- A `CallInst` -- specifically, intrinsic instructions -- can refer to
metadata through a bridge called `MetadataAsValue`. This is a
subclass of `Value` where `getType()->isMetadataTy()`.
`MetadataAsValue` is the *only* class that can legally refer to a
`LocalAsMetadata`, which is a bridged form of non-`Constant` values
like `Argument` and `Instruction`. It can also refer to any other
`Metadata` subclass.
(I'll break all your testcases in a follow-up commit, when I propagate
this change to assembly.)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@223802 91177308-0d34-0410-b5e6-96231b3b80d8
2014-12-09 18:38:53 +00:00
|
|
|
auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("Dwarf Version"));
|
2014-04-17 22:33:50 +00:00
|
|
|
if (!Val)
|
|
|
|
return dwarf::DWARF_VERSION;
|
IR: Split Metadata from Value
Split `Metadata` away from the `Value` class hierarchy, as part of
PR21532. Assembly and bitcode changes are in the wings, but this is the
bulk of the change for the IR C++ API.
I have a follow-up patch prepared for `clang`. If this breaks other
sub-projects, I apologize in advance :(. Help me compile it on Darwin
I'll try to fix it. FWIW, the errors should be easy to fix, so it may
be simpler to just fix it yourself.
This breaks the build for all metadata-related code that's out-of-tree.
Rest assured the transition is mechanical and the compiler should catch
almost all of the problems.
Here's a quick guide for updating your code:
- `Metadata` is the root of a class hierarchy with three main classes:
`MDNode`, `MDString`, and `ValueAsMetadata`. It is distinct from
the `Value` class hierarchy. It is typeless -- i.e., instances do
*not* have a `Type`.
- `MDNode`'s operands are all `Metadata *` (instead of `Value *`).
- `TrackingVH<MDNode>` and `WeakVH` referring to metadata can be
replaced with `TrackingMDNodeRef` and `TrackingMDRef`, respectively.
If you're referring solely to resolved `MDNode`s -- post graph
construction -- just use `MDNode*`.
- `MDNode` (and the rest of `Metadata`) have only limited support for
`replaceAllUsesWith()`.
As long as an `MDNode` is pointing at a forward declaration -- the
result of `MDNode::getTemporary()` -- it maintains a side map of its
uses and can RAUW itself. Once the forward declarations are fully
resolved RAUW support is dropped on the ground. This means that
uniquing collisions on changing operands cause nodes to become
"distinct". (This already happened fairly commonly, whenever an
operand went to null.)
If you're constructing complex (non self-reference) `MDNode` cycles,
you need to call `MDNode::resolveCycles()` on each node (or on a
top-level node that somehow references all of the nodes). Also,
don't do that. Metadata cycles (and the RAUW machinery needed to
construct them) are expensive.
- An `MDNode` can only refer to a `Constant` through a bridge called
`ConstantAsMetadata` (one of the subclasses of `ValueAsMetadata`).
As a side effect, accessing an operand of an `MDNode` that is known
to be, e.g., `ConstantInt`, takes three steps: first, cast from
`Metadata` to `ConstantAsMetadata`; second, extract the `Constant`;
third, cast down to `ConstantInt`.
The eventual goal is to introduce `MDInt`/`MDFloat`/etc. and have
metadata schema owners transition away from using `Constant`s when
the type isn't important (and they don't care about referring to
`GlobalValue`s).
In the meantime, I've added transitional API to the `mdconst`
namespace that matches semantics with the old code, in order to
avoid adding the error-prone three-step equivalent to every call
site. If your old code was:
MDNode *N = foo();
bar(isa <ConstantInt>(N->getOperand(0)));
baz(cast <ConstantInt>(N->getOperand(1)));
bak(cast_or_null <ConstantInt>(N->getOperand(2)));
bat(dyn_cast <ConstantInt>(N->getOperand(3)));
bay(dyn_cast_or_null<ConstantInt>(N->getOperand(4)));
you can trivially match its semantics with:
MDNode *N = foo();
bar(mdconst::hasa <ConstantInt>(N->getOperand(0)));
baz(mdconst::extract <ConstantInt>(N->getOperand(1)));
bak(mdconst::extract_or_null <ConstantInt>(N->getOperand(2)));
bat(mdconst::dyn_extract <ConstantInt>(N->getOperand(3)));
bay(mdconst::dyn_extract_or_null<ConstantInt>(N->getOperand(4)));
and when you transition your metadata schema to `MDInt`:
MDNode *N = foo();
bar(isa <MDInt>(N->getOperand(0)));
baz(cast <MDInt>(N->getOperand(1)));
bak(cast_or_null <MDInt>(N->getOperand(2)));
bat(dyn_cast <MDInt>(N->getOperand(3)));
bay(dyn_cast_or_null<MDInt>(N->getOperand(4)));
- A `CallInst` -- specifically, intrinsic instructions -- can refer to
metadata through a bridge called `MetadataAsValue`. This is a
subclass of `Value` where `getType()->isMetadataTy()`.
`MetadataAsValue` is the *only* class that can legally refer to a
`LocalAsMetadata`, which is a bridged form of non-`Constant` values
like `Argument` and `Instruction`. It can also refer to any other
`Metadata` subclass.
(I'll break all your testcases in a follow-up commit, when I propagate
this change to assembly.)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@223802 91177308-0d34-0410-b5e6-96231b3b80d8
2014-12-09 18:38:53 +00:00
|
|
|
return cast<ConstantInt>(Val->getValue())->getZExtValue();
|
2014-04-17 22:33:50 +00:00
|
|
|
}
|
2014-06-27 18:19:56 +00:00
|
|
|
|
|
|
|
Comdat *Module::getOrInsertComdat(StringRef Name) {
|
2014-11-19 05:49:42 +00:00
|
|
|
auto &Entry = *ComdatSymTab.insert(std::make_pair(Name, Comdat())).first;
|
2014-06-27 18:19:56 +00:00
|
|
|
Entry.second.Name = &Entry;
|
|
|
|
return &Entry.second;
|
|
|
|
}
|
2014-11-07 04:46:10 +00:00
|
|
|
|
|
|
|
PICLevel::Level Module::getPICLevel() const {
|
IR: Split Metadata from Value
Split `Metadata` away from the `Value` class hierarchy, as part of
PR21532. Assembly and bitcode changes are in the wings, but this is the
bulk of the change for the IR C++ API.
I have a follow-up patch prepared for `clang`. If this breaks other
sub-projects, I apologize in advance :(. Help me compile it on Darwin
I'll try to fix it. FWIW, the errors should be easy to fix, so it may
be simpler to just fix it yourself.
This breaks the build for all metadata-related code that's out-of-tree.
Rest assured the transition is mechanical and the compiler should catch
almost all of the problems.
Here's a quick guide for updating your code:
- `Metadata` is the root of a class hierarchy with three main classes:
`MDNode`, `MDString`, and `ValueAsMetadata`. It is distinct from
the `Value` class hierarchy. It is typeless -- i.e., instances do
*not* have a `Type`.
- `MDNode`'s operands are all `Metadata *` (instead of `Value *`).
- `TrackingVH<MDNode>` and `WeakVH` referring to metadata can be
replaced with `TrackingMDNodeRef` and `TrackingMDRef`, respectively.
If you're referring solely to resolved `MDNode`s -- post graph
construction -- just use `MDNode*`.
- `MDNode` (and the rest of `Metadata`) have only limited support for
`replaceAllUsesWith()`.
As long as an `MDNode` is pointing at a forward declaration -- the
result of `MDNode::getTemporary()` -- it maintains a side map of its
uses and can RAUW itself. Once the forward declarations are fully
resolved RAUW support is dropped on the ground. This means that
uniquing collisions on changing operands cause nodes to become
"distinct". (This already happened fairly commonly, whenever an
operand went to null.)
If you're constructing complex (non self-reference) `MDNode` cycles,
you need to call `MDNode::resolveCycles()` on each node (or on a
top-level node that somehow references all of the nodes). Also,
don't do that. Metadata cycles (and the RAUW machinery needed to
construct them) are expensive.
- An `MDNode` can only refer to a `Constant` through a bridge called
`ConstantAsMetadata` (one of the subclasses of `ValueAsMetadata`).
As a side effect, accessing an operand of an `MDNode` that is known
to be, e.g., `ConstantInt`, takes three steps: first, cast from
`Metadata` to `ConstantAsMetadata`; second, extract the `Constant`;
third, cast down to `ConstantInt`.
The eventual goal is to introduce `MDInt`/`MDFloat`/etc. and have
metadata schema owners transition away from using `Constant`s when
the type isn't important (and they don't care about referring to
`GlobalValue`s).
In the meantime, I've added transitional API to the `mdconst`
namespace that matches semantics with the old code, in order to
avoid adding the error-prone three-step equivalent to every call
site. If your old code was:
MDNode *N = foo();
bar(isa <ConstantInt>(N->getOperand(0)));
baz(cast <ConstantInt>(N->getOperand(1)));
bak(cast_or_null <ConstantInt>(N->getOperand(2)));
bat(dyn_cast <ConstantInt>(N->getOperand(3)));
bay(dyn_cast_or_null<ConstantInt>(N->getOperand(4)));
you can trivially match its semantics with:
MDNode *N = foo();
bar(mdconst::hasa <ConstantInt>(N->getOperand(0)));
baz(mdconst::extract <ConstantInt>(N->getOperand(1)));
bak(mdconst::extract_or_null <ConstantInt>(N->getOperand(2)));
bat(mdconst::dyn_extract <ConstantInt>(N->getOperand(3)));
bay(mdconst::dyn_extract_or_null<ConstantInt>(N->getOperand(4)));
and when you transition your metadata schema to `MDInt`:
MDNode *N = foo();
bar(isa <MDInt>(N->getOperand(0)));
baz(cast <MDInt>(N->getOperand(1)));
bak(cast_or_null <MDInt>(N->getOperand(2)));
bat(dyn_cast <MDInt>(N->getOperand(3)));
bay(dyn_cast_or_null<MDInt>(N->getOperand(4)));
- A `CallInst` -- specifically, intrinsic instructions -- can refer to
metadata through a bridge called `MetadataAsValue`. This is a
subclass of `Value` where `getType()->isMetadataTy()`.
`MetadataAsValue` is the *only* class that can legally refer to a
`LocalAsMetadata`, which is a bridged form of non-`Constant` values
like `Argument` and `Instruction`. It can also refer to any other
`Metadata` subclass.
(I'll break all your testcases in a follow-up commit, when I propagate
this change to assembly.)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@223802 91177308-0d34-0410-b5e6-96231b3b80d8
2014-12-09 18:38:53 +00:00
|
|
|
auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("PIC Level"));
|
2014-11-07 04:46:10 +00:00
|
|
|
|
|
|
|
if (Val == NULL)
|
|
|
|
return PICLevel::Default;
|
|
|
|
|
IR: Split Metadata from Value
Split `Metadata` away from the `Value` class hierarchy, as part of
PR21532. Assembly and bitcode changes are in the wings, but this is the
bulk of the change for the IR C++ API.
I have a follow-up patch prepared for `clang`. If this breaks other
sub-projects, I apologize in advance :(. Help me compile it on Darwin
I'll try to fix it. FWIW, the errors should be easy to fix, so it may
be simpler to just fix it yourself.
This breaks the build for all metadata-related code that's out-of-tree.
Rest assured the transition is mechanical and the compiler should catch
almost all of the problems.
Here's a quick guide for updating your code:
- `Metadata` is the root of a class hierarchy with three main classes:
`MDNode`, `MDString`, and `ValueAsMetadata`. It is distinct from
the `Value` class hierarchy. It is typeless -- i.e., instances do
*not* have a `Type`.
- `MDNode`'s operands are all `Metadata *` (instead of `Value *`).
- `TrackingVH<MDNode>` and `WeakVH` referring to metadata can be
replaced with `TrackingMDNodeRef` and `TrackingMDRef`, respectively.
If you're referring solely to resolved `MDNode`s -- post graph
construction -- just use `MDNode*`.
- `MDNode` (and the rest of `Metadata`) have only limited support for
`replaceAllUsesWith()`.
As long as an `MDNode` is pointing at a forward declaration -- the
result of `MDNode::getTemporary()` -- it maintains a side map of its
uses and can RAUW itself. Once the forward declarations are fully
resolved RAUW support is dropped on the ground. This means that
uniquing collisions on changing operands cause nodes to become
"distinct". (This already happened fairly commonly, whenever an
operand went to null.)
If you're constructing complex (non self-reference) `MDNode` cycles,
you need to call `MDNode::resolveCycles()` on each node (or on a
top-level node that somehow references all of the nodes). Also,
don't do that. Metadata cycles (and the RAUW machinery needed to
construct them) are expensive.
- An `MDNode` can only refer to a `Constant` through a bridge called
`ConstantAsMetadata` (one of the subclasses of `ValueAsMetadata`).
As a side effect, accessing an operand of an `MDNode` that is known
to be, e.g., `ConstantInt`, takes three steps: first, cast from
`Metadata` to `ConstantAsMetadata`; second, extract the `Constant`;
third, cast down to `ConstantInt`.
The eventual goal is to introduce `MDInt`/`MDFloat`/etc. and have
metadata schema owners transition away from using `Constant`s when
the type isn't important (and they don't care about referring to
`GlobalValue`s).
In the meantime, I've added transitional API to the `mdconst`
namespace that matches semantics with the old code, in order to
avoid adding the error-prone three-step equivalent to every call
site. If your old code was:
MDNode *N = foo();
bar(isa <ConstantInt>(N->getOperand(0)));
baz(cast <ConstantInt>(N->getOperand(1)));
bak(cast_or_null <ConstantInt>(N->getOperand(2)));
bat(dyn_cast <ConstantInt>(N->getOperand(3)));
bay(dyn_cast_or_null<ConstantInt>(N->getOperand(4)));
you can trivially match its semantics with:
MDNode *N = foo();
bar(mdconst::hasa <ConstantInt>(N->getOperand(0)));
baz(mdconst::extract <ConstantInt>(N->getOperand(1)));
bak(mdconst::extract_or_null <ConstantInt>(N->getOperand(2)));
bat(mdconst::dyn_extract <ConstantInt>(N->getOperand(3)));
bay(mdconst::dyn_extract_or_null<ConstantInt>(N->getOperand(4)));
and when you transition your metadata schema to `MDInt`:
MDNode *N = foo();
bar(isa <MDInt>(N->getOperand(0)));
baz(cast <MDInt>(N->getOperand(1)));
bak(cast_or_null <MDInt>(N->getOperand(2)));
bat(dyn_cast <MDInt>(N->getOperand(3)));
bay(dyn_cast_or_null<MDInt>(N->getOperand(4)));
- A `CallInst` -- specifically, intrinsic instructions -- can refer to
metadata through a bridge called `MetadataAsValue`. This is a
subclass of `Value` where `getType()->isMetadataTy()`.
`MetadataAsValue` is the *only* class that can legally refer to a
`LocalAsMetadata`, which is a bridged form of non-`Constant` values
like `Argument` and `Instruction`. It can also refer to any other
`Metadata` subclass.
(I'll break all your testcases in a follow-up commit, when I propagate
this change to assembly.)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@223802 91177308-0d34-0410-b5e6-96231b3b80d8
2014-12-09 18:38:53 +00:00
|
|
|
return static_cast<PICLevel::Level>(
|
|
|
|
cast<ConstantInt>(Val->getValue())->getZExtValue());
|
2014-11-07 04:46:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Module::setPICLevel(PICLevel::Level PL) {
|
|
|
|
addModuleFlag(ModFlagBehavior::Error, "PIC Level", PL);
|
|
|
|
}
|