llvm-mirror/include/llvm/Metadata.h

241 lines
8.0 KiB
C
Raw Normal View History

//===-- llvm/Metadata.h - Metadata definitions ------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
/// @file
/// This file contains the declarations for metadata subclasses.
/// They represent the different flavors of metadata that live in LLVM.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_METADATA_H
#define LLVM_METADATA_H
#include "llvm/Value.h"
#include "llvm/ADT/FoldingSet.h"
#include "llvm/ADT/ilist_node.h"
namespace llvm {
2009-07-29 15:24:54 +00:00
class Constant;
class Instruction;
class LLVMContext;
class Module;
class MetadataContextImpl;
2009-12-28 08:26:43 +00:00
template <typename T> class SmallVectorImpl;
//===----------------------------------------------------------------------===//
2009-07-29 00:33:07 +00:00
// MetadataBase - A base class for MDNode, MDString and NamedMDNode.
class MetadataBase : public Value {
2009-07-23 01:19:53 +00:00
protected:
MetadataBase(const Type *Ty, unsigned scid)
: Value(Ty, scid) {}
2009-07-23 01:19:53 +00:00
public:
/// Methods for support type inquiry through isa, cast, and dyn_cast:
static inline bool classof(const MetadataBase *) { return true; }
static bool classof(const Value *V) {
2009-07-29 00:33:07 +00:00
return V->getValueID() == MDStringVal || V->getValueID() == MDNodeVal
|| V->getValueID() == NamedMDNodeVal;
}
};
//===----------------------------------------------------------------------===//
/// MDString - a single uniqued string.
/// These are used to efficiently contain a byte sequence for metadata.
2009-07-29 00:33:07 +00:00
/// MDString is always unnamd.
class MDString : public MetadataBase {
MDString(const MDString &); // DO NOT IMPLEMENT
StringRef Str;
2009-07-23 01:19:53 +00:00
protected:
explicit MDString(LLVMContext &C, StringRef S);
2009-07-23 01:19:53 +00:00
public:
static MDString *get(LLVMContext &Context, StringRef Str);
static MDString *get(LLVMContext &Context, const char *Str);
StringRef getString() const { return Str; }
unsigned getLength() const { return (unsigned)Str.size(); }
2009-10-19 07:10:59 +00:00
typedef StringRef::iterator iterator;
/// begin() - Pointer to the first byte of the string.
///
2009-10-19 07:10:59 +00:00
iterator begin() const { return Str.begin(); }
/// end() - Pointer to one byte past the end of the string.
///
2009-10-19 07:10:59 +00:00
iterator end() const { return Str.end(); }
/// Methods for support type inquiry through isa, cast, and dyn_cast:
static inline bool classof(const MDString *) { return true; }
static bool classof(const Value *V) {
return V->getValueID() == MDStringVal;
}
};
class MDNodeElement;
//===----------------------------------------------------------------------===//
/// MDNode - a tuple of other values.
/// These contain a list of the values that represent the metadata.
2009-07-29 00:33:07 +00:00
/// MDNode is always unnamed.
class MDNode : public MetadataBase, public FoldingSetNode {
MDNode(const MDNode &); // DO NOT IMPLEMENT
void operator=(const MDNode &); // DO NOT IMPLEMENT
friend class MDNodeElement;
MDNodeElement *Operands;
unsigned NumOperands;
// Subclass data enums.
enum {
FunctionLocalBit = 1
};
// Replace each instance of F from the element list of this node with T.
void replaceElement(MDNodeElement *Op, Value *NewVal);
protected:
explicit MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals,
bool isFunctionLocal);
public:
// Constructors and destructors.
static MDNode *get(LLVMContext &Context, Value *const *Vals, unsigned NumVals,
bool isFunctionLocal = false);
/// ~MDNode - Destroy MDNode.
~MDNode();
/// getElement - Return specified element.
Value *getElement(unsigned i) const;
/// getNumElements - Return number of MDNode elements.
unsigned getNumElements() const { return NumOperands; }
/// isFunctionLocal - Return whether MDNode is local to a function.
/// Note: MDNodes are designated as function-local when created, and keep
/// that designation even if their operands are modified to no longer
/// refer to function-local IR.
bool isFunctionLocal() const { return SubclassData & FunctionLocalBit; }
/// Profile - calculate a unique identifier for this MDNode to collapse
/// duplicates
void Profile(FoldingSetNodeID &ID) const;
/// Methods for support type inquiry through isa, cast, and dyn_cast:
static inline bool classof(const MDNode *) { return true; }
static bool classof(const Value *V) {
return V->getValueID() == MDNodeVal;
}
};
2009-07-29 00:33:07 +00:00
//===----------------------------------------------------------------------===//
/// NamedMDNode - a tuple of other metadata.
/// NamedMDNode is always named. All NamedMDNode element has a type of metadata.
template<typename ValueSubClass, typename ItemParentClass>
class SymbolTableListTraits;
class NamedMDNode : public MetadataBase, public ilist_node<NamedMDNode> {
friend class SymbolTableListTraits<NamedMDNode, Module>;
friend class LLVMContextImpl;
2009-07-29 00:33:07 +00:00
NamedMDNode(const NamedMDNode &); // DO NOT IMPLEMENT
2009-07-29 00:33:07 +00:00
Module *Parent;
void *Operands; // SmallVector<TrackingVH<MetadataBase>, 4>
2009-07-29 00:33:07 +00:00
void setParent(Module *M) { Parent = M; }
2009-07-29 00:33:07 +00:00
protected:
2009-10-19 07:10:59 +00:00
explicit NamedMDNode(LLVMContext &C, const Twine &N, MetadataBase*const *Vals,
unsigned NumVals, Module *M = 0);
2009-07-29 00:33:07 +00:00
public:
static NamedMDNode *Create(LLVMContext &C, const Twine &N,
2009-10-19 07:10:59 +00:00
MetadataBase *const *MDs,
unsigned NumMDs, Module *M = 0) {
return new NamedMDNode(C, N, MDs, NumMDs, M);
2009-07-29 00:33:07 +00:00
}
2009-08-11 18:01:24 +00:00
static NamedMDNode *Create(const NamedMDNode *NMD, Module *M = 0);
/// eraseFromParent - Drop all references and remove the node from parent
/// module.
void eraseFromParent();
/// dropAllReferences - Remove all uses and clear node vector.
void dropAllReferences();
/// ~NamedMDNode - Destroy NamedMDNode.
~NamedMDNode();
2009-07-29 00:33:07 +00:00
/// getParent - Get the module that holds this named metadata collection.
inline Module *getParent() { return Parent; }
inline const Module *getParent() const { return Parent; }
/// getElement - Return specified element.
MetadataBase *getElement(unsigned i) const;
/// getNumElements - Return number of NamedMDNode elements.
unsigned getNumElements() const;
2009-07-29 00:33:07 +00:00
2009-07-30 23:57:23 +00:00
/// addElement - Add metadata element.
void addElement(MetadataBase *M);
2009-07-29 00:33:07 +00:00
/// 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) {
return V->getValueID() == NamedMDNodeVal;
}
};
//===----------------------------------------------------------------------===//
/// MetadataContext - MetadataContext handles uniquing and assignment of IDs for
/// custom metadata types.
///
class MetadataContext {
MetadataContext(MetadataContext&); // DO NOT IMPLEMENT
void operator=(MetadataContext&); // DO NOT IMPLEMENT
MetadataContextImpl *const pImpl;
friend class Instruction;
public:
MetadataContext();
~MetadataContext();
/// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
unsigned getMDKindID(StringRef Name) const;
/// isValidName - Return true if Name is a valid custom metadata handler name.
static bool isValidName(StringRef Name);
/// copyMD - If metadata is attached with Instruction In1 then attach
/// the same metadata to In2.
void copyMD(Instruction *In1, Instruction *In2);
/// getMDKindNames - Populate client supplied SmallVector with the name for
/// each custom metadata ID. ID #0 is not used, so it is filled in as empty.
void getMDKindNames(SmallVectorImpl<StringRef> &) const;
/// ValueIsDeleted - This handler is used to update metadata store
/// when a value is deleted.
void ValueIsDeleted(const Value *) {}
void ValueIsDeleted(Instruction *Inst);
void ValueIsRAUWd(Value *V1, Value *V2);
/// ValueIsCloned - This handler is used to update metadata store
/// when In1 is cloned to create In2.
void ValueIsCloned(const Instruction *In1, Instruction *In2);
};
} // end llvm namespace
#endif