exclamation point). For example: "<tt>!{ { } !"test\00", i32 10}</tt>".
</p>
+<p>A metadata node will attempt to track changes to the values it holds. In
+the event that a value is deleted, it will be replaced with a typeless
+"<tt>null</tt>", such as "<tt>{ } !{null, i32 0}</tt>".</p>
+
<p>Optimizations may rely on metadata to provide additional information about
the program that isn't available in the instructions, or that isn't easily
computable. Similarly, the code generator may expect a certain metadata format
#include "llvm/OperandTraits.h"
#include "llvm/ADT/APInt.h"
#include "llvm/ADT/APFloat.h"
-#include "llvm/ADT/FoldingSet.h"
#include "llvm/ADT/SmallVector.h"
namespace llvm {
}
};
-//===----------------------------------------------------------------------===//
-/// MDNode - a tuple of other values.
-/// These contain a list of the Constants that represent the metadata.
-///
-class MDNode : public Constant, public FoldingSetNode {
- MDNode(const MDNode &); // DO NOT IMPLEMENT
-protected:
- explicit MDNode(Constant*const* Vals, unsigned NumVals);
-public:
- /// get() - Static factory methods - Return objects of the specified value.
- ///
- static MDNode *get(Constant*const* Vals, unsigned NumVals);
-
- // Transparently provide more efficient getOperand methods.
- DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
-
- /// getType() specialization - Type is always an empty struct.
- ///
- inline const Type *getType() const {
- return Type::EmptyStructTy;
- }
-
- /// isNullValue - Return true if this is the value that would be returned by
- /// getNullValue. This always returns false because getNullValue will never
- /// produce metadata.
- virtual bool isNullValue() const {
- return false;
- }
-
- /// Profile - calculate a unique identifier for this MDNode to collapse
- /// duplicates
- void Profile(FoldingSetNodeID &ID);
-
- virtual void destroyConstant();
- virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
-
- /// 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;
- }
-};
-
-template <>
-struct OperandTraits<MDNode> : VariadicOperandTraits<> {
-};
-
-DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(MDNode, Constant)
-
} // End llvm namespace
#endif
--- /dev/null
+//===-- llvm/Metadata.h - Constant class subclass 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 the subclasses of Constant,
+/// which represent the different flavors of constant values that live in LLVM.
+/// Note that Constants are immutable (once created they never change) and are
+/// fully shared by structural equivalence. This means that two structurally
+/// equivalent constants will always have the same address. Constant's are
+/// created on demand as needed and never deleted: thus clients don't have to
+/// worry about the lifetime of the objects.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_MDNODE_H
+#define LLVM_MDNODE_H
+
+#include "llvm/Constant.h"
+#include "llvm/ADT/FoldingSet.h"
+#include "llvm/ADT/SmallVector.h"
+#include "llvm/Support/ValueHandle.h"
+
+namespace llvm {
+
+//===----------------------------------------------------------------------===//
+/// MDNode - a tuple of other values.
+/// These contain a list of the Constants that represent the metadata. The
+/// operand list is always empty, query the element list instead.
+///
+/// This class will attempt to keep track of values as they are modified. When
+/// a value is replaced the element will be replaced with it, and when the
+/// value is deleted the element is set to a null pointer. In order to preserve
+/// structural equivalence while the elements mutate, the MDNode may call
+/// replaceAllUsesWith on itself. Because of this, users of MDNode must use a
+/// WeakVH or CallbackVH to hold the node pointer if there is a chance that one
+/// of the elements held by the node may change.
+///
+class MDNode : public Constant, public FoldingSetNode {
+ MDNode(const MDNode &); // DO NOT IMPLEMENT
+
+ friend class ElementVH;
+ struct ElementVH : public CallbackVH {
+ MDNode *OwningNode;
+
+ ElementVH(Value *V, MDNode *Parent)
+ : CallbackVH(V), OwningNode(Parent) {}
+
+ ~ElementVH() {}
+
+ /// deleted - Set this entry in the MDNode to 'null'. This will reallocate
+ /// the MDNode.
+ virtual void deleted() {
+ OwningNode->replaceElement(this->operator Value*(), 0);
+ }
+
+ /// allUsesReplacedWith - Modify the MDNode by replacing this entry with
+ /// new_value. This will reallocate the MDNode.
+ virtual void allUsesReplacedWith(Value *new_value) {
+ OwningNode->replaceElement(this->operator Value*(), new_value);
+ }
+ };
+
+ void replaceElement(Value *From, Value *To);
+
+ SmallVector<ElementVH, 4> Node;
+ typedef SmallVectorImpl<ElementVH>::iterator elem_iterator;
+protected:
+ explicit MDNode(Value*const* Vals, unsigned NumVals);
+public:
+ typedef SmallVectorImpl<ElementVH>::const_iterator const_elem_iterator;
+
+ /// get() - Static factory methods - Return objects of the specified value.
+ ///
+ static MDNode *get(Value*const* Vals, unsigned NumVals);
+
+ Value *getElement(unsigned i) const {
+ return Node[i];
+ }
+
+ unsigned getNumElements() const {
+ return Node.size();
+ }
+
+ const_elem_iterator elem_begin() const {
+ return Node.begin();
+ }
+
+ const_elem_iterator elem_end() const {
+ return Node.end();
+ }
+
+ /// getType() specialization - Type is always an empty struct.
+ ///
+ inline const Type *getType() const {
+ return Type::EmptyStructTy;
+ }
+
+ /// isNullValue - Return true if this is the value that would be returned by
+ /// getNullValue. This always returns false because getNullValue will never
+ /// produce metadata.
+ virtual bool isNullValue() const {
+ return false;
+ }
+
+ /// Profile - calculate a unique identifier for this MDNode to collapse
+ /// duplicates
+ void Profile(FoldingSetNodeID &ID) const;
+
+ virtual void destroyConstant();
+ virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) {
+ assert(0 && "This should never be called because MDNodes have no ops");
+ abort();
+ }
+
+ /// 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;
+ }
+};
+
+} // end llvm namespace
+
+#endif
#include "llvm/DerivedTypes.h"
#include "llvm/InlineAsm.h"
#include "llvm/Instructions.h"
+#include "llvm/MDNode.h"
#include "llvm/Module.h"
#include "llvm/ValueSymbolTable.h"
#include "llvm/ADT/SmallPtrSet.h"
ID.Kind = ValID::t_Constant;
Lex.Lex();
if (Lex.getKind() == lltok::lbrace) {
- // MDNode:
- // ::= '!' '{' TypeAndValue (',' TypeAndValue)* '}'
- SmallVector<Constant*, 16> Elts;
+ SmallVector<Value*, 16> Elts;
if (ParseMDNodeVector(Elts) ||
ParseToken(lltok::rbrace, "expected end of metadata node"))
return true;
-
+
ID.ConstantVal = MDNode::get(&Elts[0], Elts.size());
return false;
}
//===----------------------------------------------------------------------===//
/// ParseMDNodeVector
-/// ::= TypeAndValue (',' TypeAndValue)*
-bool LLParser::ParseMDNodeVector(SmallVectorImpl<Constant*> &Elts) {
+/// ::= Element (',' Element)*
+/// Element
+/// ::= 'null' | TypeAndValue
+bool LLParser::ParseMDNodeVector(SmallVectorImpl<Value*> &Elts) {
assert(Lex.getKind() == lltok::lbrace);
Lex.Lex();
do {
- Constant *C;
- if (ParseGlobalTypeAndValue(C)) return true;
- Elts.push_back(C);
+ Value *V;
+ if (Lex.getKind() == lltok::kw_null) {
+ Lex.Lex();
+ V = 0;
+ } else {
+ Constant *C;
+ if (ParseGlobalTypeAndValue(C)) return true;
+ V = C;
+ }
+ Elts.push_back(V);
} while (EatIfPresent(lltok::comma));
return false;
bool ParseGlobalValue(const Type *Ty, Constant *&V);
bool ParseGlobalTypeAndValue(Constant *&V);
bool ParseGlobalValueVector(SmallVectorImpl<Constant*> &Elts);
- bool ParseMDNodeVector(SmallVectorImpl<Constant*> &);
+ bool ParseMDNodeVector(SmallVectorImpl<Value*> &);
// Function Semantic Analysis.
#include "llvm/DerivedTypes.h"
#include "llvm/InlineAsm.h"
#include "llvm/Instructions.h"
+#include "llvm/MDNode.h"
#include "llvm/Module.h"
#include "llvm/AutoUpgrade.h"
#include "llvm/ADT/SmallString.h"
UserCS->getType()->isPacked());
} else if (isa<ConstantVector>(UserC)) {
NewC = ConstantVector::get(&NewOps[0], NewOps.size());
- } else if (isa<ConstantExpr>(UserC)) {
+ } else {
+ assert(isa<ConstantExpr>(UserC) && "Must be a ConstantExpr.");
NewC = cast<ConstantExpr>(UserC)->getWithOperands(&NewOps[0],
NewOps.size());
- } else {
- assert(isa<MDNode>(UserC) && "Must be a metadata node.");
- NewC = MDNode::get(&NewOps[0], NewOps.size());
}
UserC->replaceAllUsesWith(NewC);
NewOps.clear();
}
+ // Update all ValueHandles, they should be the only users at this point.
+ Placeholder->replaceAllUsesWith(RealVal);
delete Placeholder;
}
}
return Error("Invalid CST_MDNODE record");
unsigned Size = Record.size();
- SmallVector<Constant*, 8> Elts;
+ SmallVector<Value*, 8> Elts;
for (unsigned i = 0; i != Size; i += 2) {
const Type *Ty = getTypeByID(Record[i], false);
- Elts.push_back(ValueList.getConstantFwdRef(Record[i+1], Ty));
+ if (Ty != Type::VoidTy)
+ Elts.push_back(ValueList.getConstantFwdRef(Record[i+1], Ty));
+ else
+ Elts.push_back(NULL);
}
V = MDNode::get(&Elts[0], Elts.size());
break;
#include "llvm/DerivedTypes.h"
#include "llvm/InlineAsm.h"
#include "llvm/Instructions.h"
+#include "llvm/MDNode.h"
#include "llvm/Module.h"
#include "llvm/TypeSymbolTable.h"
#include "llvm/ValueSymbolTable.h"
}
} else if (const MDNode *N = dyn_cast<MDNode>(C)) {
Code = bitc::CST_CODE_MDNODE;
- for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
- Record.push_back(VE.getTypeID(N->getOperand(i)->getType()));
- Record.push_back(VE.getValueID(N->getOperand(i)));
+ for (unsigned i = 0, e = N->getNumElements(); i != e; ++i) {
+ if (N->getElement(i)) {
+ Record.push_back(VE.getTypeID(N->getElement(i)->getType()));
+ Record.push_back(VE.getValueID(N->getElement(i)));
+ } else {
+ Record.push_back(VE.getTypeID(Type::VoidTy));
+ Record.push_back(0);
+ }
}
} else {
assert(0 && "Unknown constant!");
#include "ValueEnumerator.h"
#include "llvm/Constants.h"
#include "llvm/DerivedTypes.h"
+#include "llvm/MDNode.h"
#include "llvm/Module.h"
#include "llvm/TypeSymbolTable.h"
#include "llvm/ValueSymbolTable.h"
// Finally, add the value. Doing this could make the ValueID reference be
// dangling, don't reuse it.
+ Values.push_back(std::make_pair(V, 1U));
+ ValueMap[V] = Values.size();
+ return;
+ } else if (const MDNode *N = dyn_cast<MDNode>(C)) {
+ for (MDNode::const_elem_iterator I = N->elem_begin(), E = N->elem_end();
+ I != E; ++I) {
+ if (*I)
+ EnumerateValue(*I);
+ else
+ EnumerateType(Type::VoidTy);
+ }
+
Values.push_back(std::make_pair(V, 1U));
ValueMap[V] = Values.size();
return;
// them.
for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
EnumerateOperandType(C->getOperand(i));
+
+ if (const MDNode *N = dyn_cast<MDNode>(V)) {
+ for (unsigned i = 0, e = N->getNumElements(); i != e; ++i)
+ EnumerateOperandType(N->getElement(i));
+ }
}
}
#include "llvm/InlineAsm.h"
#include "llvm/Instruction.h"
#include "llvm/Instructions.h"
+#include "llvm/MDNode.h"
#include "llvm/Module.h"
#include "llvm/ValueSymbolTable.h"
#include "llvm/TypeSymbolTable.h"
if (const MDNode *N = dyn_cast<MDNode>(CV)) {
Out << "!{";
- for (MDNode::const_op_iterator I = N->op_begin(), E = N->op_end(); I != E;){
- TypePrinter.print((*I)->getType(), Out);
- Out << ' ';
- WriteAsOperandInternal(Out, *I, TypePrinter, Machine);
+ for (MDNode::const_elem_iterator I = N->elem_begin(), E = N->elem_end();
+ I != E;) {
+ if (!*I) {
+ Out << "null";
+ } else {
+ TypePrinter.print((*I)->getType(), Out);
+ Out << ' ';
+ WriteAsOperandInternal(Out, *I, TypePrinter, Machine);
+ }
+
if (++I != E)
Out << ", ";
}
#include "llvm/DerivedTypes.h"
#include "llvm/GlobalValue.h"
#include "llvm/Instructions.h"
+#include "llvm/MDNode.h"
#include "llvm/Module.h"
#include "llvm/ADT/FoldingSet.h"
#include "llvm/ADT/StringExtras.h"
static ManagedStatic<FoldingSet<MDNode> > MDNodeSet;
-MDNode::MDNode(Constant*const* Vals, unsigned NumVals)
- : Constant(Type::EmptyStructTy, MDNodeVal,
- OperandTraits<MDNode>::op_end(this) - NumVals, NumVals) {
- std::copy(Vals, Vals + NumVals, OperandList);
+MDNode::MDNode(Value*const* Vals, unsigned NumVals)
+ : Constant(Type::EmptyStructTy, MDNodeVal, 0, 0) {
+ for (unsigned i = 0; i != NumVals; ++i)
+ Node.push_back(ElementVH(Vals[i], this));
}
-void MDNode::Profile(FoldingSetNodeID &ID) {
- for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
+void MDNode::Profile(FoldingSetNodeID &ID) const {
+ for (const_elem_iterator I = elem_begin(), E = elem_end(); I != E; ++I)
ID.AddPointer(*I);
}
-MDNode *MDNode::get(Constant*const* Vals, unsigned NumVals) {
+MDNode *MDNode::get(Value*const* Vals, unsigned NumVals) {
FoldingSetNodeID ID;
for (unsigned i = 0; i != NumVals; ++i)
ID.AddPointer(Vals[i]);
return N;
// InsertPoint will have been set by the FindNodeOrInsertPos call.
- MDNode *N = new(NumVals) MDNode(Vals, NumVals);
+ MDNode *N = new(0) MDNode(Vals, NumVals);
MDNodeSet->InsertNode(N, InsertPoint);
return N;
}
void MDNode::destroyConstant() {
+ MDNodeSet->RemoveNode(this);
destroyConstantImpl();
}
destroyConstant();
}
-void MDNode::replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) {
- assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
-
- SmallVector<Constant*, 8> Values;
- Values.reserve(getNumOperands()); // Build replacement array...
- for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
- Constant *Val = getOperand(i);
- if (Val == From) Val = cast<Constant>(To);
+void MDNode::replaceElement(Value *From, Value *To) {
+ SmallVector<Value*, 4> Values;
+ Values.reserve(getNumElements()); // Build replacement array...
+ for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
+ Value *Val = getElement(i);
+ if (Val == From) Val = To;
Values.push_back(Val);
}
-
- Constant *Replacement = MDNode::get(&Values[0], Values.size());
+
+ MDNode *Replacement = MDNode::get(&Values[0], Values.size());
assert(Replacement != this && "I didn't contain From!");
-
- // Everyone using this now uses the replacement.
+
uncheckedReplaceAllUsesWith(Replacement);
-
- // Delete the old constant!
+
destroyConstant();
}
declare i8 @llvm.something({ } %a)
-@llvm.foo = internal constant { } !{i17 123, { } !"foobar"}
+@llvm.foo = internal constant { } !{i17 123, null, { } !"foobar"}
define void @foo() {
%x = call i8 @llvm.something({ } !{{ } !"f\00oa", i42 123})
#include "gtest/gtest.h"
#include "llvm/Constants.h"
+#include "llvm/Instructions.h"
+#include "llvm/MDNode.h"
+#include "llvm/Type.h"
+#include "llvm/Support/ValueHandle.h"
#include <sstream>
using namespace llvm;
}
// Test the two constructors, and containing other Constants.
-TEST(MDNodeTest, Everything) {
+TEST(MDNodeTest, Simple) {
char x[3] = { 'a', 'b', 'c' };
char y[3] = { '1', '2', '3' };
MDString *s2 = MDString::get(&y[0], &y[3]);
ConstantInt *CI = ConstantInt::get(APInt(8, 0));
- std::vector<Constant *> V;
+ std::vector<Value *> V;
V.push_back(s1);
V.push_back(CI);
V.push_back(s2);
MDNode *n1 = MDNode::get(&V[0], 3);
- Constant *const c1 = n1;
+ Value *const c1 = n1;
MDNode *n2 = MDNode::get(&c1, 1);
MDNode *n3 = MDNode::get(&V[0], 3);
EXPECT_NE(n1, n2);
EXPECT_EQ(n1, n3);
- EXPECT_EQ(3u, n1->getNumOperands());
- EXPECT_EQ(s1, n1->getOperand(0));
- EXPECT_EQ(CI, n1->getOperand(1));
- EXPECT_EQ(s2, n1->getOperand(2));
+ EXPECT_EQ(3u, n1->getNumElements());
+ EXPECT_EQ(s1, n1->getElement(0));
+ EXPECT_EQ(CI, n1->getElement(1));
+ EXPECT_EQ(s2, n1->getElement(2));
- EXPECT_EQ(1u, n2->getNumOperands());
- EXPECT_EQ(n1, n2->getOperand(0));
+ EXPECT_EQ(1u, n2->getNumElements());
+ EXPECT_EQ(n1, n2->getElement(0));
std::ostringstream oss1, oss2;
n1->print(oss1);
EXPECT_STREQ("{ } !{{ } !{{ } !\"abc\", i8 0, { } !\"123\"}}",
oss2.str().c_str());
}
+
+TEST(MDNodeTest, RAUW) {
+ Constant *C = ConstantInt::get(Type::Int32Ty, 1);
+ Instruction *I = new BitCastInst(C, Type::Int32Ty);
+
+ Value *const V1 = I;
+ MDNode *n1 = MDNode::get(&V1, 1);
+ WeakVH wn1 = n1;
+
+ Value *const V2 = C;
+ MDNode *n2 = MDNode::get(&V2, 1);
+ WeakVH wn2 = n2;
+
+ EXPECT_NE(wn1, wn2);
+
+ I->replaceAllUsesWith(C);
+
+ EXPECT_EQ(wn1, wn2);
+}
+
+TEST(MDNodeTest, Delete) {
+ Constant *C = ConstantInt::get(Type::Int32Ty, 1);
+ Instruction *I = new BitCastInst(C, Type::Int32Ty);
+
+ Value *const V = I;
+ MDNode *n = MDNode::get(&V, 1);
+ WeakVH wvh = n;
+
+ EXPECT_EQ(n, wvh);
+
+ delete I;
+
+ std::ostringstream oss;
+ wvh->print(oss);
+ EXPECT_STREQ("{ } !{null}", oss.str().c_str());
+}
}