2009-07-28 21:49:47 +00:00
|
|
|
//===-- llvm/Metadata.h - Metadata definitions ------------------*- C++ -*-===//
|
2009-05-10 20:57:05 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
/// @file
|
2009-07-28 21:49:47 +00:00
|
|
|
/// This file contains the declarations for metadata subclasses.
|
|
|
|
/// They represent the different flavors of metadata that live in LLVM.
|
2009-05-10 20:57:05 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-10-20 11:44:38 +00:00
|
|
|
#ifndef LLVM_METADATA_H
|
|
|
|
#define LLVM_METADATA_H
|
2009-05-10 20:57:05 +00:00
|
|
|
|
2009-10-21 23:57:35 +00:00
|
|
|
#include "llvm/Value.h"
|
2009-05-10 23:27:41 +00:00
|
|
|
#include "llvm/Type.h"
|
2009-09-03 01:39:20 +00:00
|
|
|
#include "llvm/ADT/FoldingSet.h"
|
2009-07-29 17:16:17 +00:00
|
|
|
#include "llvm/ADT/ilist_node.h"
|
2009-05-10 20:57:05 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
2009-07-29 15:24:54 +00:00
|
|
|
class Constant;
|
2009-09-16 18:09:00 +00:00
|
|
|
class Instruction;
|
2009-08-11 17:45:13 +00:00
|
|
|
class LLVMContext;
|
2009-10-22 19:36:54 +00:00
|
|
|
class MetadataContextImpl;
|
2009-12-28 08:26:43 +00:00
|
|
|
template <typename T> class SmallVectorImpl;
|
|
|
|
template<class PtrType, unsigned SmallSize> class SmallPtrSet;
|
2009-05-10 20:57:05 +00:00
|
|
|
|
2009-07-22 17:43:22 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2009-07-29 00:33:07 +00:00
|
|
|
// MetadataBase - A base class for MDNode, MDString and NamedMDNode.
|
2009-10-21 23:57:35 +00:00
|
|
|
class MetadataBase : public Value {
|
2009-07-23 01:19:53 +00:00
|
|
|
protected:
|
2009-07-22 17:43:22 +00:00
|
|
|
MetadataBase(const Type *Ty, unsigned scid)
|
2009-10-21 23:57:35 +00:00
|
|
|
: Value(Ty, scid) {}
|
2009-07-22 17:43:22 +00:00
|
|
|
|
2009-07-23 01:19:53 +00:00
|
|
|
public:
|
2009-07-22 17:43:22 +00:00
|
|
|
|
|
|
|
/// Methods for support type inquiry through isa, cast, and dyn_cast:
|
2009-07-30 18:45:09 +00:00
|
|
|
static inline bool classof(const MetadataBase *) { return true; }
|
2009-07-22 17:43:22 +00:00
|
|
|
static bool classof(const Value *V) {
|
2009-07-29 00:33:07 +00:00
|
|
|
return V->getValueID() == MDStringVal || V->getValueID() == MDNodeVal
|
|
|
|
|| V->getValueID() == NamedMDNodeVal;
|
2009-07-22 17:43:22 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// 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.
|
2009-07-22 17:43:22 +00:00
|
|
|
class MDString : public MetadataBase {
|
|
|
|
MDString(const MDString &); // DO NOT IMPLEMENT
|
|
|
|
|
2009-08-03 22:51:10 +00:00
|
|
|
StringRef Str;
|
2009-07-23 01:19:53 +00:00
|
|
|
protected:
|
2009-10-22 00:22:05 +00:00
|
|
|
explicit MDString(LLVMContext &C, StringRef S)
|
|
|
|
: MetadataBase(Type::getMetadataTy(C), Value::MDStringVal), Str(S) {}
|
2009-07-22 17:43:22 +00:00
|
|
|
|
2009-07-23 01:19:53 +00:00
|
|
|
public:
|
2009-10-22 00:10:15 +00:00
|
|
|
static MDString *get(LLVMContext &Context, StringRef Str);
|
2009-11-12 00:50:58 +00:00
|
|
|
static MDString *get(LLVMContext &Context, const char *Str);
|
2009-07-31 21:35:40 +00:00
|
|
|
|
2009-07-25 23:55:21 +00:00
|
|
|
StringRef getString() const { return Str; }
|
|
|
|
|
2009-11-04 03:08:57 +00:00
|
|
|
unsigned getLength() const { return (unsigned)Str.size(); }
|
2009-07-22 17:43:22 +00:00
|
|
|
|
2009-10-19 07:10:59 +00:00
|
|
|
typedef StringRef::iterator iterator;
|
|
|
|
|
2009-07-22 17:43:22 +00:00
|
|
|
/// begin() - Pointer to the first byte of the string.
|
|
|
|
///
|
2009-10-19 07:10:59 +00:00
|
|
|
iterator begin() const { return Str.begin(); }
|
2009-07-22 17:43:22 +00:00
|
|
|
|
|
|
|
/// 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(); }
|
2009-07-22 17:43:22 +00:00
|
|
|
|
|
|
|
/// 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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-12-28 07:41:54 +00:00
|
|
|
|
|
|
|
class MDNodeElement;
|
|
|
|
|
2009-05-10 20:57:05 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// MDNode - a tuple of other values.
|
2009-07-23 01:07:34 +00:00
|
|
|
/// These contain a list of the values that represent the metadata.
|
2009-07-29 00:33:07 +00:00
|
|
|
/// MDNode is always unnamed.
|
2009-09-03 01:39:20 +00:00
|
|
|
class MDNode : public MetadataBase, public FoldingSetNode {
|
2009-08-03 22:51:10 +00:00
|
|
|
MDNode(const MDNode &); // DO NOT IMPLEMENT
|
2009-05-10 20:57:05 +00:00
|
|
|
|
2009-12-28 07:41:54 +00:00
|
|
|
friend class MDNodeElement;
|
2009-12-18 20:09:14 +00:00
|
|
|
|
|
|
|
static const unsigned short FunctionLocalBit = 1;
|
|
|
|
|
2009-09-03 01:39:20 +00:00
|
|
|
// Replace each instance of F from the element list of this node with T.
|
|
|
|
void replaceElement(Value *F, Value *T);
|
|
|
|
|
2009-12-28 07:41:54 +00:00
|
|
|
MDNodeElement *Node;
|
2009-10-21 21:25:09 +00:00
|
|
|
unsigned NodeSize;
|
2009-09-03 01:39:20 +00:00
|
|
|
|
2009-05-10 20:57:05 +00:00
|
|
|
protected:
|
2009-12-16 02:52:09 +00:00
|
|
|
explicit MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals,
|
2009-12-18 20:09:14 +00:00
|
|
|
bool isFunctionLocal);
|
2009-05-10 20:57:05 +00:00
|
|
|
public:
|
2009-08-03 22:51:10 +00:00
|
|
|
// Constructors and destructors.
|
2009-12-18 20:09:14 +00:00
|
|
|
static MDNode *get(LLVMContext &Context, Value *const *Vals, unsigned NumVals,
|
|
|
|
bool isFunctionLocal = false);
|
2009-05-10 20:57:05 +00:00
|
|
|
|
2009-08-11 17:15:47 +00:00
|
|
|
/// ~MDNode - Destroy MDNode.
|
2009-08-03 22:51:10 +00:00
|
|
|
~MDNode();
|
|
|
|
|
|
|
|
/// getElement - Return specified element.
|
2009-12-28 07:41:54 +00:00
|
|
|
Value *getElement(unsigned i) const;
|
|
|
|
|
2009-08-03 22:51:10 +00:00
|
|
|
/// getNumElements - Return number of MDNode elements.
|
2009-10-21 21:25:09 +00:00
|
|
|
unsigned getNumElements() const { return NodeSize; }
|
2009-12-16 02:52:09 +00:00
|
|
|
|
|
|
|
/// isFunctionLocal - Return whether MDNode is local to a function.
|
2009-12-18 20:09:14 +00:00
|
|
|
/// 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; }
|
|
|
|
|
|
|
|
/// getLocalFunction - Return false if MDNode's recursive function-localness
|
|
|
|
/// is invalid (local to more than one function). Return true otherwise.
|
|
|
|
/// If MDNode has one function to which it is local, set LocalFunction to that
|
|
|
|
/// function.
|
|
|
|
bool getLocalFunction(Function *LocalFunction,
|
|
|
|
SmallPtrSet<MDNode *, 32> *VisitedMDNodes = NULL);
|
2009-05-10 20:57:05 +00:00
|
|
|
|
2009-09-03 01:39:20 +00:00
|
|
|
/// Profile - calculate a unique identifier for this MDNode to collapse
|
|
|
|
/// duplicates
|
|
|
|
void Profile(FoldingSetNodeID &ID) const;
|
|
|
|
|
2009-05-10 20:57:05 +00:00
|
|
|
/// 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.
|
2009-07-29 17:16:17 +00:00
|
|
|
template<typename ValueSubClass, typename ItemParentClass>
|
|
|
|
class SymbolTableListTraits;
|
|
|
|
|
|
|
|
class NamedMDNode : public MetadataBase, public ilist_node<NamedMDNode> {
|
|
|
|
friend class SymbolTableListTraits<NamedMDNode, Module>;
|
2009-08-11 17:45:13 +00:00
|
|
|
friend class LLVMContextImpl;
|
2009-07-29 00:33:07 +00:00
|
|
|
|
2009-08-03 22:51:10 +00:00
|
|
|
NamedMDNode(const NamedMDNode &); // DO NOT IMPLEMENT
|
|
|
|
|
2009-07-29 00:33:07 +00:00
|
|
|
Module *Parent;
|
2009-12-28 08:07:14 +00:00
|
|
|
void *Operands; // SmallVector<TrackingVH<MetadataBase>, 4>
|
2009-07-29 00:33:07 +00:00
|
|
|
|
2009-10-21 17:33:41 +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,
|
2009-07-29 21:58:56 +00:00
|
|
|
unsigned NumVals, Module *M = 0);
|
2009-07-29 00:33:07 +00:00
|
|
|
public:
|
2009-08-13 21:58:54 +00:00
|
|
|
static NamedMDNode *Create(LLVMContext &C, const Twine &N,
|
2009-10-19 07:10:59 +00:00
|
|
|
MetadataBase *const *MDs,
|
2009-07-29 21:58:56 +00:00
|
|
|
unsigned NumMDs, Module *M = 0) {
|
2009-08-13 21:58:54 +00:00
|
|
|
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);
|
|
|
|
|
2009-08-03 06:19:01 +00:00
|
|
|
/// eraseFromParent - Drop all references and remove the node from parent
|
|
|
|
/// module.
|
|
|
|
void eraseFromParent();
|
|
|
|
|
|
|
|
/// dropAllReferences - Remove all uses and clear node vector.
|
|
|
|
void dropAllReferences();
|
|
|
|
|
2009-08-03 22:51:10 +00:00
|
|
|
/// ~NamedMDNode - Destroy NamedMDNode.
|
2009-08-03 06:19:01 +00:00
|
|
|
~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; }
|
|
|
|
|
2009-08-03 22:51:10 +00:00
|
|
|
/// getElement - Return specified element.
|
2009-12-28 08:07:14 +00:00
|
|
|
MetadataBase *getElement(unsigned i) const;
|
|
|
|
|
2009-08-03 22:51:10 +00:00
|
|
|
/// getNumElements - Return number of NamedMDNode elements.
|
2009-12-28 08:07:14 +00:00
|
|
|
unsigned getNumElements() const;
|
2009-07-29 00:33:07 +00:00
|
|
|
|
2009-07-30 23:57:23 +00:00
|
|
|
/// addElement - Add metadata element.
|
2009-12-28 08:07:14 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-09-16 18:09:00 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2009-09-28 21:41:20 +00:00
|
|
|
/// MetadataContext -
|
2009-09-29 00:01:14 +00:00
|
|
|
/// MetadataContext handles uniquing and assignment of IDs for custom metadata
|
|
|
|
/// types. Custom metadata handler names do not contain spaces. And the name
|
|
|
|
/// must start with an alphabet. The regular expression used to check name
|
|
|
|
/// is [a-zA-Z$._][a-zA-Z$._0-9]*
|
2009-09-28 21:41:20 +00:00
|
|
|
class MetadataContext {
|
2009-10-22 19:36:54 +00:00
|
|
|
// DO NOT IMPLEMENT
|
|
|
|
MetadataContext(MetadataContext&);
|
|
|
|
void operator=(MetadataContext&);
|
2009-09-16 18:09:00 +00:00
|
|
|
|
2009-10-22 19:36:54 +00:00
|
|
|
MetadataContextImpl *const pImpl;
|
2009-09-16 18:09:00 +00:00
|
|
|
public:
|
2009-10-22 19:36:54 +00:00
|
|
|
MetadataContext();
|
|
|
|
~MetadataContext();
|
|
|
|
|
2009-10-20 22:50:27 +00:00
|
|
|
/// registerMDKind - Register a new metadata kind and return its ID.
|
2009-09-16 18:09:00 +00:00
|
|
|
/// A metadata kind can be registered only once.
|
2009-10-22 00:10:15 +00:00
|
|
|
unsigned registerMDKind(StringRef Name);
|
2009-09-16 18:09:00 +00:00
|
|
|
|
|
|
|
/// getMDKind - Return metadata kind. If the requested metadata kind
|
|
|
|
/// is not registered then return 0.
|
2009-10-22 00:10:15 +00:00
|
|
|
unsigned getMDKind(StringRef Name) const;
|
2009-09-16 18:09:00 +00:00
|
|
|
|
2009-10-20 22:50:27 +00:00
|
|
|
/// isValidName - Return true if Name is a valid custom metadata handler name.
|
2009-10-22 00:10:15 +00:00
|
|
|
static bool isValidName(StringRef Name);
|
2009-09-29 00:01:14 +00:00
|
|
|
|
2009-10-20 22:50:27 +00:00
|
|
|
/// getMD - Get the metadata of given kind attached to an Instruction.
|
2009-09-16 18:09:00 +00:00
|
|
|
/// If the metadata is not found then return 0.
|
2009-09-28 21:14:55 +00:00
|
|
|
MDNode *getMD(unsigned Kind, const Instruction *Inst);
|
2009-09-16 18:09:00 +00:00
|
|
|
|
2009-10-20 22:50:27 +00:00
|
|
|
/// getMDs - Get the metadata attached to an Instruction.
|
2009-10-22 19:36:54 +00:00
|
|
|
void getMDs(const Instruction *Inst,
|
2009-12-28 08:14:54 +00:00
|
|
|
SmallVectorImpl<std::pair<unsigned, MDNode*> > &MDs) const;
|
2009-09-16 20:21:17 +00:00
|
|
|
|
2009-10-20 22:50:27 +00:00
|
|
|
/// addMD - Attach the metadata of given kind to an Instruction.
|
2009-09-29 20:30:57 +00:00
|
|
|
void addMD(unsigned Kind, MDNode *Node, Instruction *Inst);
|
2009-09-16 18:09:00 +00:00
|
|
|
|
2009-09-29 20:42:25 +00:00
|
|
|
/// removeMD - Remove metadata of given kind attached with an instuction.
|
|
|
|
void removeMD(unsigned Kind, Instruction *Inst);
|
|
|
|
|
2009-10-21 17:33:41 +00:00
|
|
|
/// removeAllMetadata - Remove all metadata attached with an instruction.
|
|
|
|
void removeAllMetadata(Instruction *Inst);
|
2009-09-29 20:42:25 +00:00
|
|
|
|
2009-10-14 17:02:49 +00:00
|
|
|
/// copyMD - If metadata is attached with Instruction In1 then attach
|
|
|
|
/// the same metadata to In2.
|
|
|
|
void copyMD(Instruction *In1, Instruction *In2);
|
|
|
|
|
2009-11-30 23:30:43 +00:00
|
|
|
/// getHandlerNames - Populate client supplied smallvector using custom
|
2009-10-22 17:40:37 +00:00
|
|
|
/// metadata name and ID.
|
|
|
|
void getHandlerNames(SmallVectorImpl<std::pair<unsigned, StringRef> >&) const;
|
2009-09-18 19:26:43 +00:00
|
|
|
|
2009-09-16 18:09:00 +00:00
|
|
|
/// ValueIsDeleted - This handler is used to update metadata store
|
|
|
|
/// when a value is deleted.
|
2009-10-14 20:28:33 +00:00
|
|
|
void ValueIsDeleted(const Value *) {}
|
2009-10-22 19:36:54 +00:00
|
|
|
void ValueIsDeleted(Instruction *Inst);
|
2009-10-13 17:00:54 +00:00
|
|
|
void ValueIsRAUWd(Value *V1, Value *V2);
|
2009-09-23 18:32:25 +00:00
|
|
|
|
|
|
|
/// ValueIsCloned - This handler is used to update metadata store
|
|
|
|
/// when In1 is cloned to create In2.
|
|
|
|
void ValueIsCloned(const Instruction *In1, Instruction *In2);
|
2009-09-16 18:09:00 +00:00
|
|
|
};
|
|
|
|
|
2009-05-10 20:57:05 +00:00
|
|
|
} // end llvm namespace
|
|
|
|
|
|
|
|
#endif
|