mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2024-08-25 00:29:20 +00:00
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
235 lines
6.9 KiB
C++
235 lines
6.9 KiB
C++
//===- llvm/unittest/IR/Metadata.cpp - Metadata unit tests ----------------===//
|
|
//
|
|
// The LLVM Compiler Infrastructure
|
|
//
|
|
// This file is distributed under the University of Illinois Open Source
|
|
// License. See LICENSE.TXT for details.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "llvm/IR/Metadata.h"
|
|
#include "llvm/IR/Constants.h"
|
|
#include "llvm/IR/Instructions.h"
|
|
#include "llvm/IR/LLVMContext.h"
|
|
#include "llvm/IR/Module.h"
|
|
#include "llvm/IR/Type.h"
|
|
#include "llvm/Support/raw_ostream.h"
|
|
#include "gtest/gtest.h"
|
|
using namespace llvm;
|
|
|
|
namespace {
|
|
|
|
class MetadataTest : public testing::Test {
|
|
protected:
|
|
LLVMContext Context;
|
|
};
|
|
typedef MetadataTest MDStringTest;
|
|
|
|
// Test that construction of MDString with different value produces different
|
|
// MDString objects, even with the same string pointer and nulls in the string.
|
|
TEST_F(MDStringTest, CreateDifferent) {
|
|
char x[3] = { 'f', 0, 'A' };
|
|
MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
|
|
x[2] = 'B';
|
|
MDString *s2 = MDString::get(Context, StringRef(&x[0], 3));
|
|
EXPECT_NE(s1, s2);
|
|
}
|
|
|
|
// Test that creation of MDStrings with the same string contents produces the
|
|
// same MDString object, even with different pointers.
|
|
TEST_F(MDStringTest, CreateSame) {
|
|
char x[4] = { 'a', 'b', 'c', 'X' };
|
|
char y[4] = { 'a', 'b', 'c', 'Y' };
|
|
|
|
MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
|
|
MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
|
|
EXPECT_EQ(s1, s2);
|
|
}
|
|
|
|
// Test that MDString prints out the string we fed it.
|
|
TEST_F(MDStringTest, PrintingSimple) {
|
|
char *str = new char[13];
|
|
strncpy(str, "testing 1 2 3", 13);
|
|
MDString *s = MDString::get(Context, StringRef(str, 13));
|
|
strncpy(str, "aaaaaaaaaaaaa", 13);
|
|
delete[] str;
|
|
|
|
std::string Str;
|
|
raw_string_ostream oss(Str);
|
|
s->print(oss);
|
|
EXPECT_STREQ("metadata !\"testing 1 2 3\"", oss.str().c_str());
|
|
}
|
|
|
|
// Test printing of MDString with non-printable characters.
|
|
TEST_F(MDStringTest, PrintingComplex) {
|
|
char str[5] = {0, '\n', '"', '\\', (char)-1};
|
|
MDString *s = MDString::get(Context, StringRef(str+0, 5));
|
|
std::string Str;
|
|
raw_string_ostream oss(Str);
|
|
s->print(oss);
|
|
EXPECT_STREQ("metadata !\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str());
|
|
}
|
|
|
|
typedef MetadataTest MDNodeTest;
|
|
|
|
// Test the two constructors, and containing other Constants.
|
|
TEST_F(MDNodeTest, Simple) {
|
|
char x[3] = { 'a', 'b', 'c' };
|
|
char y[3] = { '1', '2', '3' };
|
|
|
|
MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
|
|
MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
|
|
ConstantAsMetadata *CI = ConstantAsMetadata::get(
|
|
ConstantInt::get(getGlobalContext(), APInt(8, 0)));
|
|
|
|
std::vector<Metadata *> V;
|
|
V.push_back(s1);
|
|
V.push_back(CI);
|
|
V.push_back(s2);
|
|
|
|
MDNode *n1 = MDNode::get(Context, V);
|
|
Metadata *const c1 = n1;
|
|
MDNode *n2 = MDNode::get(Context, c1);
|
|
Metadata *const c2 = n2;
|
|
MDNode *n3 = MDNode::get(Context, V);
|
|
MDNode *n4 = MDNode::getIfExists(Context, V);
|
|
MDNode *n5 = MDNode::getIfExists(Context, c1);
|
|
MDNode *n6 = MDNode::getIfExists(Context, c2);
|
|
EXPECT_NE(n1, n2);
|
|
EXPECT_EQ(n1, n3);
|
|
EXPECT_EQ(n4, n1);
|
|
EXPECT_EQ(n5, n2);
|
|
EXPECT_EQ(n6, (Metadata *)nullptr);
|
|
|
|
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(1u, n2->getNumOperands());
|
|
EXPECT_EQ(n1, n2->getOperand(0));
|
|
}
|
|
|
|
TEST_F(MDNodeTest, Delete) {
|
|
Constant *C = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 1);
|
|
Instruction *I = new BitCastInst(C, Type::getInt32Ty(getGlobalContext()));
|
|
|
|
Metadata *const V = LocalAsMetadata::get(I);
|
|
MDNode *n = MDNode::get(Context, V);
|
|
TrackingMDRef wvh(n);
|
|
|
|
EXPECT_EQ(n, wvh);
|
|
|
|
delete I;
|
|
}
|
|
|
|
TEST_F(MDNodeTest, SelfReference) {
|
|
// !0 = metadata !{metadata !0}
|
|
// !1 = metadata !{metadata !0}
|
|
{
|
|
MDNode *Temp = MDNode::getTemporary(Context, None);
|
|
Metadata *Args[] = {Temp};
|
|
MDNode *Self = MDNode::get(Context, Args);
|
|
Self->replaceOperandWith(0, Self);
|
|
MDNode::deleteTemporary(Temp);
|
|
ASSERT_EQ(Self, Self->getOperand(0));
|
|
|
|
// Self-references should be distinct, so MDNode::get() should grab a
|
|
// uniqued node that references Self, not Self.
|
|
Args[0] = Self;
|
|
MDNode *Ref1 = MDNode::get(Context, Args);
|
|
MDNode *Ref2 = MDNode::get(Context, Args);
|
|
EXPECT_NE(Self, Ref1);
|
|
EXPECT_EQ(Ref1, Ref2);
|
|
}
|
|
|
|
// !0 = metadata !{metadata !0, metadata !{}}
|
|
// !1 = metadata !{metadata !0, metadata !{}}
|
|
{
|
|
MDNode *Temp = MDNode::getTemporary(Context, None);
|
|
Metadata *Args[] = {Temp, MDNode::get(Context, None)};
|
|
MDNode *Self = MDNode::get(Context, Args);
|
|
Self->replaceOperandWith(0, Self);
|
|
MDNode::deleteTemporary(Temp);
|
|
ASSERT_EQ(Self, Self->getOperand(0));
|
|
|
|
// Self-references should be distinct, so MDNode::get() should grab a
|
|
// uniqued node that references Self, not Self itself.
|
|
Args[0] = Self;
|
|
MDNode *Ref1 = MDNode::get(Context, Args);
|
|
MDNode *Ref2 = MDNode::get(Context, Args);
|
|
EXPECT_NE(Self, Ref1);
|
|
EXPECT_EQ(Ref1, Ref2);
|
|
}
|
|
}
|
|
|
|
typedef MetadataTest MetadataAsValueTest;
|
|
|
|
TEST_F(MetadataAsValueTest, MDNode) {
|
|
MDNode *N = MDNode::get(Context, None);
|
|
auto *V = MetadataAsValue::get(Context, N);
|
|
EXPECT_TRUE(V->getType()->isMetadataTy());
|
|
EXPECT_EQ(N, V->getMetadata());
|
|
|
|
auto *V2 = MetadataAsValue::get(Context, N);
|
|
EXPECT_EQ(V, V2);
|
|
}
|
|
|
|
TEST_F(MetadataAsValueTest, MDNodeMDNode) {
|
|
MDNode *N = MDNode::get(Context, None);
|
|
Metadata *Ops[] = {N};
|
|
MDNode *N2 = MDNode::get(Context, Ops);
|
|
auto *V = MetadataAsValue::get(Context, N2);
|
|
EXPECT_TRUE(V->getType()->isMetadataTy());
|
|
EXPECT_EQ(N2, V->getMetadata());
|
|
|
|
auto *V2 = MetadataAsValue::get(Context, N2);
|
|
EXPECT_EQ(V, V2);
|
|
|
|
auto *V3 = MetadataAsValue::get(Context, N);
|
|
EXPECT_TRUE(V3->getType()->isMetadataTy());
|
|
EXPECT_NE(V, V3);
|
|
EXPECT_EQ(N, V3->getMetadata());
|
|
}
|
|
|
|
TEST_F(MetadataAsValueTest, MDNodeConstant) {
|
|
auto *C = ConstantInt::getTrue(Context);
|
|
auto *MD = ConstantAsMetadata::get(C);
|
|
Metadata *Ops[] = {MD};
|
|
auto *N = MDNode::get(Context, Ops);
|
|
|
|
auto *V = MetadataAsValue::get(Context, MD);
|
|
EXPECT_TRUE(V->getType()->isMetadataTy());
|
|
EXPECT_EQ(MD, V->getMetadata());
|
|
|
|
auto *V2 = MetadataAsValue::get(Context, N);
|
|
EXPECT_EQ(MD, V2->getMetadata());
|
|
EXPECT_EQ(V, V2);
|
|
}
|
|
|
|
TEST(NamedMDNodeTest, Search) {
|
|
LLVMContext Context;
|
|
ConstantAsMetadata *C =
|
|
ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 1));
|
|
ConstantAsMetadata *C2 =
|
|
ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 2));
|
|
|
|
Metadata *const V = C;
|
|
Metadata *const V2 = C2;
|
|
MDNode *n = MDNode::get(Context, V);
|
|
MDNode *n2 = MDNode::get(Context, V2);
|
|
|
|
Module M("MyModule", Context);
|
|
const char *Name = "llvm.NMD1";
|
|
NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name);
|
|
NMD->addOperand(n);
|
|
NMD->addOperand(n2);
|
|
|
|
std::string Str;
|
|
raw_string_ostream oss(Str);
|
|
NMD->print(oss);
|
|
EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n",
|
|
oss.str().c_str());
|
|
}
|
|
}
|