Use separate namespace for named metadata.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@92931 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Devang Patel 2010-01-07 19:39:36 +00:00
parent 3c37bb8dbe
commit 0386f01e06
9 changed files with 155 additions and 29 deletions

View File

@ -175,15 +175,16 @@ class NamedMDNode : public MetadataBase, public ilist_node<NamedMDNode> {
NamedMDNode(const NamedMDNode &); // DO NOT IMPLEMENT
std::string Name;
Module *Parent;
void *Operands; // SmallVector<WeakVH<MDNode>, 4>
void setParent(Module *M) { Parent = M; }
protected:
explicit NamedMDNode(LLVMContext &C, const Twine &N, MDNode*const *Vals,
explicit NamedMDNode(LLVMContext &C, StringRef N, MDNode*const *Vals,
unsigned NumVals, Module *M = 0);
public:
static NamedMDNode *Create(LLVMContext &C, const Twine &N,
static NamedMDNode *Create(LLVMContext &C, StringRef N,
MDNode *const *MDs,
unsigned NumMDs, Module *M = 0) {
return new NamedMDNode(C, N, MDs, NumMDs, M);
@ -213,7 +214,13 @@ public:
/// addOperand - Add metadata operand.
void addOperand(MDNode *M);
/// setName - Set the name of this named metadata.
void setName(StringRef Name);
/// getName - Return a constant reference to this named metadata's name.
StringRef getName() const;
/// Methods for support type inquiry through isa, cast, and dyn_cast:
static inline bool classof(const NamedMDNode *) { return true; }
static bool classof(const Value *V) {

View File

@ -26,6 +26,7 @@ namespace llvm {
class FunctionType;
class LLVMContext;
class MDSymbolTable;
template<> struct ilist_traits<Function>
: public SymbolTableListTraits<Function, Module> {
@ -131,19 +132,20 @@ public:
/// @name Member Variables
/// @{
private:
LLVMContext &Context; ///< The LLVMContext from which types and
///< constants are allocated.
GlobalListType GlobalList; ///< The Global Variables in the module
FunctionListType FunctionList; ///< The Functions in the module
AliasListType AliasList; ///< The Aliases in the module
LibraryListType LibraryList; ///< The Libraries needed by the module
NamedMDListType NamedMDList; ///< The named metadata in the module
std::string GlobalScopeAsm; ///< Inline Asm at global scope.
ValueSymbolTable *ValSymTab; ///< Symbol table for values
TypeSymbolTable *TypeSymTab; ///< Symbol table for types
std::string ModuleID; ///< Human readable identifier for the module
std::string TargetTriple; ///< Platform target triple Module compiled on
std::string DataLayout; ///< Target data description
LLVMContext &Context; ///< The LLVMContext from which types and
///< constants are allocated.
GlobalListType GlobalList; ///< The Global Variables in the module
FunctionListType FunctionList; ///< The Functions in the module
AliasListType AliasList; ///< The Aliases in the module
LibraryListType LibraryList; ///< The Libraries needed by the module
NamedMDListType NamedMDList; ///< The named metadata in the module
std::string GlobalScopeAsm; ///< Inline Asm at global scope.
ValueSymbolTable *ValSymTab; ///< Symbol table for values
TypeSymbolTable *TypeSymTab; ///< Symbol table for types
std::string ModuleID; ///< Human readable identifier for the module
std::string TargetTriple; ///< Platform target triple Module compiled on
std::string DataLayout; ///< Target data description
MDSymbolTable *NamedMDSymTab; ///< NamedMDNode names.
friend class Constant;
@ -322,6 +324,10 @@ public:
/// NamedMDNode with the specified name is not found.
NamedMDNode *getOrInsertNamedMetadata(StringRef Name);
/// addMDNodeName - Insert an entry in the NamedMDNode symbol table mapping
/// Name to NMD.
void addMDNodeName(StringRef Name, NamedMDNode *NMD);
/// @}
/// @name Type Accessors
/// @{
@ -379,6 +385,10 @@ public:
const TypeSymbolTable &getTypeSymbolTable() const { return *TypeSymTab; }
/// Get the Module's symbol table of types
TypeSymbolTable &getTypeSymbolTable() { return *TypeSymTab; }
/// Get the symbol table of named metadata
const MDSymbolTable &getMDSymbolTable() const { return *NamedMDSymTab; }
/// Get the Module's symbol table of named metadata
MDSymbolTable &getMDSymbolTable() { return *NamedMDSymTab; }
/// @}
/// @name Global Variable Iteration

View File

@ -26,7 +26,7 @@ namespace llvm {
class NamedMDNode;
class Module;
class StringRef;
/// This class provides a symbol table of name/value pairs. It is essentially
/// a std::map<std::string,Value*> but has a controlled interface provided by
/// LLVM as well as ensuring uniqueness of names.
@ -129,6 +129,84 @@ private:
/// @}
};
/// This class provides a symbol table of name/NamedMDNode pairs. It is
/// essentially a StringMap wrapper.
class MDSymbolTable {
/// @name Types
/// @{
public:
/// @brief A mapping of names to metadata
typedef StringMap<NamedMDNode*> MDMap;
/// @brief An iterator over a ValueMap.
typedef MDMap::iterator iterator;
/// @brief A const_iterator over a ValueMap.
typedef MDMap::const_iterator const_iterator;
/// @}
/// @name Constructors
/// @{
public:
MDSymbolTable() : mmap(0) {}
~MDSymbolTable();
/// @}
/// @name Accessors
/// @{
public:
/// This method finds the value with the given \p Name in the
/// the symbol table.
/// @returns the NamedMDNode associated with the \p Name
/// @brief Lookup a named Value.
NamedMDNode *lookup(StringRef Name) const { return mmap.lookup(Name); }
/// @returns true iff the symbol table is empty
/// @brief Determine if the symbol table is empty
inline bool empty() const { return mmap.empty(); }
/// @brief The number of name/type pairs is returned.
inline unsigned size() const { return unsigned(mmap.size()); }
/// @}
/// @name Iteration
/// @{
public:
/// @brief Get an iterator that from the beginning of the symbol table.
inline iterator begin() { return mmap.begin(); }
/// @brief Get a const_iterator that from the beginning of the symbol table.
inline const_iterator begin() const { return mmap.begin(); }
/// @brief Get an iterator to the end of the symbol table.
inline iterator end() { return mmap.end(); }
/// @brief Get a const_iterator to the end of the symbol table.
inline const_iterator end() const { return mmap.end(); }
/// @}
/// @name Mutators
/// @{
public:
/// insert - The method inserts a new entry into the stringmap.
void insert(StringRef Name, NamedMDNode *Node) {
(void) mmap.GetOrCreateValue(Name, Node);
}
/// This method removes a NamedMDNode from the symbol table.
void remove(StringRef Name) { mmap.erase(Name); }
/// @}
/// @name Internal Data
/// @{
private:
MDMap mmap; ///< The map that holds the symbol table.
/// @}
};
} // End llvm namespace
#endif

View File

@ -528,10 +528,9 @@ static void WriteModuleMetadata(const ValueEnumerator &VE,
}
// Write name.
std::string Str = NMD->getNameStr();
const char *StrBegin = Str.c_str();
for (unsigned i = 0, e = Str.length(); i != e; ++i)
Record.push_back(StrBegin[i]);
StringRef Str = NMD->getName();
for (unsigned i = 0, e = Str.size(); i != e; ++i)
Record.push_back(Str[i]);
Stream.EmitRecord(bitc::METADATA_NAME, Record, 0/*TODO*/);
Record.clear();

View File

@ -74,9 +74,10 @@ ValueEnumerator::ValueEnumerator(const Module *M) {
// Enumerate types used by the type symbol table.
EnumerateTypeSymbolTable(M->getTypeSymbolTable());
// Insert constants that are named at module level into the slot pool so that
// the module symbol table can refer to them...
// Insert constants and metadata that are named at module level into the slot
// pool so that the module symbol table can refer to them...
EnumerateValueSymbolTable(M->getValueSymbolTable());
EnumerateMDSymbolTable(M->getMDSymbolTable());
SmallVector<std::pair<unsigned, MDNode*>, 8> MDs;
@ -196,6 +197,14 @@ void ValueEnumerator::EnumerateValueSymbolTable(const ValueSymbolTable &VST) {
EnumerateValue(VI->getValue());
}
/// EnumerateMDSymbolTable - Insert all of the values in the specified metadata
/// table.
void ValueEnumerator::EnumerateMDSymbolTable(const MDSymbolTable &MST) {
for (MDSymbolTable::const_iterator MI = MST.begin(), ME = MST.end();
MI != ME; ++MI)
EnumerateValue(MI->getValue());
}
void ValueEnumerator::EnumerateMetadata(const MetadataBase *MD) {
// Check to see if it's already in!
unsigned &MDValueID = MDValueMap[MD];

View File

@ -30,6 +30,7 @@ class MetadataBase;
class AttrListPtr;
class TypeSymbolTable;
class ValueSymbolTable;
class MDSymbolTable;
class ValueEnumerator {
public:
@ -133,6 +134,7 @@ private:
void EnumerateTypeSymbolTable(const TypeSymbolTable &ST);
void EnumerateValueSymbolTable(const ValueSymbolTable &ST);
void EnumerateMDSymbolTable(const MDSymbolTable &ST);
};
} // End llvm namespace

View File

@ -214,20 +214,21 @@ static SmallVector<WeakVH, 4> &getNMDOps(void *Operands) {
return *(SmallVector<WeakVH, 4>*)Operands;
}
NamedMDNode::NamedMDNode(LLVMContext &C, const Twine &N,
NamedMDNode::NamedMDNode(LLVMContext &C, StringRef N,
MDNode *const *MDs,
unsigned NumMDs, Module *ParentModule)
: MetadataBase(Type::getMetadataTy(C), Value::NamedMDNodeVal), Parent(0) {
setName(N);
Operands = new SmallVector<WeakVH, 4>();
SmallVector<WeakVH, 4> &Node = getNMDOps(Operands);
for (unsigned i = 0; i != NumMDs; ++i)
Node.push_back(WeakVH(MDs[i]));
if (ParentModule)
if (ParentModule) {
ParentModule->getNamedMDList().push_back(this);
ParentModule->addMDNodeName(N, this);
}
}
NamedMDNode *NamedMDNode::Create(const NamedMDNode *NMD, Module *M) {
@ -265,6 +266,7 @@ void NamedMDNode::addOperand(MDNode *M) {
/// eraseFromParent - Drop all references and remove the node from parent
/// module.
void NamedMDNode::eraseFromParent() {
getParent()->getMDSymbolTable().remove(getName());
getParent()->getNamedMDList().erase(this);
}
@ -273,6 +275,16 @@ void NamedMDNode::dropAllReferences() {
getNMDOps(Operands).clear();
}
/// setName - Set the name of this named metadata.
void NamedMDNode::setName(StringRef N) {
if (!N.empty())
Name = N.str();
}
/// getName - Return a constant reference to this named metadata's name.
StringRef NamedMDNode::getName() const {
return StringRef(Name);
}
//===----------------------------------------------------------------------===//
// LLVMContext MDKind naming implementation.

View File

@ -59,6 +59,7 @@ Module::Module(StringRef MID, LLVMContext& C)
: Context(C), ModuleID(MID), DataLayout("") {
ValSymTab = new ValueSymbolTable();
TypeSymTab = new TypeSymbolTable();
NamedMDSymTab = new MDSymbolTable();
}
Module::~Module() {
@ -307,20 +308,25 @@ GlobalAlias *Module::getNamedAlias(StringRef Name) const {
/// specified name. This method returns null if a NamedMDNode with the
//// specified name is not found.
NamedMDNode *Module::getNamedMetadata(StringRef Name) const {
return dyn_cast_or_null<NamedMDNode>(getValueSymbolTable().lookup(Name));
return NamedMDSymTab->lookup(Name);
}
/// getOrInsertNamedMetadata - Return the first named MDNode in the module
/// with the specified name. This method returns a new NamedMDNode if a
/// NamedMDNode with the specified name is not found.
NamedMDNode *Module::getOrInsertNamedMetadata(StringRef Name) {
NamedMDNode *NMD =
dyn_cast_or_null<NamedMDNode>(getValueSymbolTable().lookup(Name));
NamedMDNode *NMD = NamedMDSymTab->lookup(Name);
if (!NMD)
NMD = NamedMDNode::Create(getContext(), Name, NULL, 0, this);
return NMD;
}
/// addMDNodeName - Insert an entry in the NamedMDNode symbol table mapping
/// Name to NMD.
void Module::addMDNodeName(StringRef Name, NamedMDNode *NMD) {
NamedMDSymTab->insert(Name, NMD);
}
//===----------------------------------------------------------------------===//
// Methods for easy access to the types in the module.
//

View File

@ -4,3 +4,6 @@
!0 = metadata !{i32 42}
!1 = metadata !{metadata !"foo"}
!llvm.stuff = !{!0, !1, null}
!samename = !{!0, !1}
define void @samename() {}