121cc4460efa104e8591b05a90f20130291614344Nick Lewycky//===- llvm/unittest/VMCore/Metadata.cpp - Metadata unit tests ------------===//
221cc4460efa104e8591b05a90f20130291614344Nick Lewycky//
321cc4460efa104e8591b05a90f20130291614344Nick Lewycky//                     The LLVM Compiler Infrastructure
421cc4460efa104e8591b05a90f20130291614344Nick Lewycky//
521cc4460efa104e8591b05a90f20130291614344Nick Lewycky// This file is distributed under the University of Illinois Open Source
621cc4460efa104e8591b05a90f20130291614344Nick Lewycky// License. See LICENSE.TXT for details.
721cc4460efa104e8591b05a90f20130291614344Nick Lewycky//
821cc4460efa104e8591b05a90f20130291614344Nick Lewycky//===----------------------------------------------------------------------===//
921cc4460efa104e8591b05a90f20130291614344Nick Lewycky
1021cc4460efa104e8591b05a90f20130291614344Nick Lewycky#include "gtest/gtest.h"
1121cc4460efa104e8591b05a90f20130291614344Nick Lewycky#include "llvm/Constants.h"
12cb33799b9f4e152e3460faa83e59b53ff604c87dNick Lewycky#include "llvm/Instructions.h"
13f3523592b21af09a5a0032f8261f2f61c302fbd9Chris Lattner#include "llvm/LLVMContext.h"
14f83264b423af49533c5c19de03c3fa82827878fdBenjamin Kramer#include "llvm/Metadata.h"
15fa7c4dcef21003aec7f4630eec3d0e1117fb2259Devang Patel#include "llvm/Module.h"
16cb33799b9f4e152e3460faa83e59b53ff604c87dNick Lewycky#include "llvm/Type.h"
170c47a412079c11656fdcc7e125d604e3aa543903Chris Lattner#include "llvm/Support/raw_ostream.h"
18cb33799b9f4e152e3460faa83e59b53ff604c87dNick Lewycky#include "llvm/Support/ValueHandle.h"
1921cc4460efa104e8591b05a90f20130291614344Nick Lewyckyusing namespace llvm;
2021cc4460efa104e8591b05a90f20130291614344Nick Lewycky
2121cc4460efa104e8591b05a90f20130291614344Nick Lewyckynamespace {
2221cc4460efa104e8591b05a90f20130291614344Nick Lewycky
23e5790a432a320c64440bf62adbcec378046eef6bJeffrey Yasskinclass MetadataTest : public testing::Test {
24e5790a432a320c64440bf62adbcec378046eef6bJeffrey Yasskinprotected:
25e5790a432a320c64440bf62adbcec378046eef6bJeffrey Yasskin  LLVMContext Context;
26e5790a432a320c64440bf62adbcec378046eef6bJeffrey Yasskin};
27e5790a432a320c64440bf62adbcec378046eef6bJeffrey Yasskintypedef MetadataTest MDStringTest;
285d0bf1bc6fc5d1d049adb98f95cda3e4737abaf2Owen Anderson
2921cc4460efa104e8591b05a90f20130291614344Nick Lewycky// Test that construction of MDString with different value produces different
3021cc4460efa104e8591b05a90f20130291614344Nick Lewycky// MDString objects, even with the same string pointer and nulls in the string.
31e5790a432a320c64440bf62adbcec378046eef6bJeffrey YasskinTEST_F(MDStringTest, CreateDifferent) {
3221cc4460efa104e8591b05a90f20130291614344Nick Lewycky  char x[3] = { 'f', 0, 'A' };
335d0bf1bc6fc5d1d049adb98f95cda3e4737abaf2Owen Anderson  MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
3421cc4460efa104e8591b05a90f20130291614344Nick Lewycky  x[2] = 'B';
355d0bf1bc6fc5d1d049adb98f95cda3e4737abaf2Owen Anderson  MDString *s2 = MDString::get(Context, StringRef(&x[0], 3));
3621cc4460efa104e8591b05a90f20130291614344Nick Lewycky  EXPECT_NE(s1, s2);
3721cc4460efa104e8591b05a90f20130291614344Nick Lewycky}
3821cc4460efa104e8591b05a90f20130291614344Nick Lewycky
3921cc4460efa104e8591b05a90f20130291614344Nick Lewycky// Test that creation of MDStrings with the same string contents produces the
4021cc4460efa104e8591b05a90f20130291614344Nick Lewycky// same MDString object, even with different pointers.
41e5790a432a320c64440bf62adbcec378046eef6bJeffrey YasskinTEST_F(MDStringTest, CreateSame) {
4221cc4460efa104e8591b05a90f20130291614344Nick Lewycky  char x[4] = { 'a', 'b', 'c', 'X' };
4321cc4460efa104e8591b05a90f20130291614344Nick Lewycky  char y[4] = { 'a', 'b', 'c', 'Y' };
4421cc4460efa104e8591b05a90f20130291614344Nick Lewycky
455d0bf1bc6fc5d1d049adb98f95cda3e4737abaf2Owen Anderson  MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
465d0bf1bc6fc5d1d049adb98f95cda3e4737abaf2Owen Anderson  MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
4721cc4460efa104e8591b05a90f20130291614344Nick Lewycky  EXPECT_EQ(s1, s2);
4821cc4460efa104e8591b05a90f20130291614344Nick Lewycky}
4921cc4460efa104e8591b05a90f20130291614344Nick Lewycky
5021cc4460efa104e8591b05a90f20130291614344Nick Lewycky// Test that MDString prints out the string we fed it.
51e5790a432a320c64440bf62adbcec378046eef6bJeffrey YasskinTEST_F(MDStringTest, PrintingSimple) {
5221cc4460efa104e8591b05a90f20130291614344Nick Lewycky  char *str = new char[13];
5321cc4460efa104e8591b05a90f20130291614344Nick Lewycky  strncpy(str, "testing 1 2 3", 13);
545d0bf1bc6fc5d1d049adb98f95cda3e4737abaf2Owen Anderson  MDString *s = MDString::get(Context, StringRef(str, 13));
5521cc4460efa104e8591b05a90f20130291614344Nick Lewycky  strncpy(str, "aaaaaaaaaaaaa", 13);
5621cc4460efa104e8591b05a90f20130291614344Nick Lewycky  delete[] str;
5721cc4460efa104e8591b05a90f20130291614344Nick Lewycky
580c47a412079c11656fdcc7e125d604e3aa543903Chris Lattner  std::string Str;
590c47a412079c11656fdcc7e125d604e3aa543903Chris Lattner  raw_string_ostream oss(Str);
6021cc4460efa104e8591b05a90f20130291614344Nick Lewycky  s->print(oss);
617a0370f66ab5739f42ffe822f33494e0de9b182bNick Lewycky  EXPECT_STREQ("metadata !\"testing 1 2 3\"", oss.str().c_str());
6221cc4460efa104e8591b05a90f20130291614344Nick Lewycky}
6321cc4460efa104e8591b05a90f20130291614344Nick Lewycky
6421cc4460efa104e8591b05a90f20130291614344Nick Lewycky// Test printing of MDString with non-printable characters.
65e5790a432a320c64440bf62adbcec378046eef6bJeffrey YasskinTEST_F(MDStringTest, PrintingComplex) {
66cda2a146d1fcf3f499a1aa535377fb332e918bd5Jeffrey Yasskin  char str[5] = {0, '\n', '"', '\\', (char)-1};
675d0bf1bc6fc5d1d049adb98f95cda3e4737abaf2Owen Anderson  MDString *s = MDString::get(Context, StringRef(str+0, 5));
680c47a412079c11656fdcc7e125d604e3aa543903Chris Lattner  std::string Str;
690c47a412079c11656fdcc7e125d604e3aa543903Chris Lattner  raw_string_ostream oss(Str);
7021cc4460efa104e8591b05a90f20130291614344Nick Lewycky  s->print(oss);
717a0370f66ab5739f42ffe822f33494e0de9b182bNick Lewycky  EXPECT_STREQ("metadata !\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str());
7221cc4460efa104e8591b05a90f20130291614344Nick Lewycky}
7321cc4460efa104e8591b05a90f20130291614344Nick Lewycky
74e5790a432a320c64440bf62adbcec378046eef6bJeffrey Yasskintypedef MetadataTest MDNodeTest;
75e5790a432a320c64440bf62adbcec378046eef6bJeffrey Yasskin
7621cc4460efa104e8591b05a90f20130291614344Nick Lewycky// Test the two constructors, and containing other Constants.
77e5790a432a320c64440bf62adbcec378046eef6bJeffrey YasskinTEST_F(MDNodeTest, Simple) {
7821cc4460efa104e8591b05a90f20130291614344Nick Lewycky  char x[3] = { 'a', 'b', 'c' };
7921cc4460efa104e8591b05a90f20130291614344Nick Lewycky  char y[3] = { '1', '2', '3' };
8021cc4460efa104e8591b05a90f20130291614344Nick Lewycky
815d0bf1bc6fc5d1d049adb98f95cda3e4737abaf2Owen Anderson  MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
825d0bf1bc6fc5d1d049adb98f95cda3e4737abaf2Owen Anderson  MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
83eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson  ConstantInt *CI = ConstantInt::get(getGlobalContext(), APInt(8, 0));
8421cc4460efa104e8591b05a90f20130291614344Nick Lewycky
85cb33799b9f4e152e3460faa83e59b53ff604c87dNick Lewycky  std::vector<Value *> V;
8621cc4460efa104e8591b05a90f20130291614344Nick Lewycky  V.push_back(s1);
8721cc4460efa104e8591b05a90f20130291614344Nick Lewycky  V.push_back(CI);
8821cc4460efa104e8591b05a90f20130291614344Nick Lewycky  V.push_back(s2);
8921cc4460efa104e8591b05a90f20130291614344Nick Lewycky
90ec9186bcf975c9ffa3ec7ca97867f0ec6eb55115Jay Foad  MDNode *n1 = MDNode::get(Context, V);
91cb33799b9f4e152e3460faa83e59b53ff604c87dNick Lewycky  Value *const c1 = n1;
92ec9186bcf975c9ffa3ec7ca97867f0ec6eb55115Jay Foad  MDNode *n2 = MDNode::get(Context, c1);
934000afe712a7fd9e584919c43d2aa09b154946c1Duncan Sands  Value *const c2 = n2;
94ec9186bcf975c9ffa3ec7ca97867f0ec6eb55115Jay Foad  MDNode *n3 = MDNode::get(Context, V);
954000afe712a7fd9e584919c43d2aa09b154946c1Duncan Sands  MDNode *n4 = MDNode::getIfExists(Context, V);
964000afe712a7fd9e584919c43d2aa09b154946c1Duncan Sands  MDNode *n5 = MDNode::getIfExists(Context, c1);
974000afe712a7fd9e584919c43d2aa09b154946c1Duncan Sands  MDNode *n6 = MDNode::getIfExists(Context, c2);
9821cc4460efa104e8591b05a90f20130291614344Nick Lewycky  EXPECT_NE(n1, n2);
997b26b581cf3c20f9943c904e44ee5ec6d77f5aa2Daniel Dunbar#ifdef ENABLE_MDNODE_UNIQUING
1005f4ac848d94b0a92e19ac7f2b3d0284d7d323173Devang Patel  EXPECT_EQ(n1, n3);
1017b26b581cf3c20f9943c904e44ee5ec6d77f5aa2Daniel Dunbar#else
1027b26b581cf3c20f9943c904e44ee5ec6d77f5aa2Daniel Dunbar  (void) n3;
1037b26b581cf3c20f9943c904e44ee5ec6d77f5aa2Daniel Dunbar#endif
1044000afe712a7fd9e584919c43d2aa09b154946c1Duncan Sands  EXPECT_EQ(n4, n1);
1054000afe712a7fd9e584919c43d2aa09b154946c1Duncan Sands  EXPECT_EQ(n5, n2);
1064000afe712a7fd9e584919c43d2aa09b154946c1Duncan Sands  EXPECT_EQ(n6, (Value*)0);
10721cc4460efa104e8591b05a90f20130291614344Nick Lewycky
1085d0cacdbb6577f2449986f345858db17dc1bcf59Chris Lattner  EXPECT_EQ(3u, n1->getNumOperands());
1095d0cacdbb6577f2449986f345858db17dc1bcf59Chris Lattner  EXPECT_EQ(s1, n1->getOperand(0));
1105d0cacdbb6577f2449986f345858db17dc1bcf59Chris Lattner  EXPECT_EQ(CI, n1->getOperand(1));
1115d0cacdbb6577f2449986f345858db17dc1bcf59Chris Lattner  EXPECT_EQ(s2, n1->getOperand(2));
11221cc4460efa104e8591b05a90f20130291614344Nick Lewycky
1135d0cacdbb6577f2449986f345858db17dc1bcf59Chris Lattner  EXPECT_EQ(1u, n2->getNumOperands());
1145d0cacdbb6577f2449986f345858db17dc1bcf59Chris Lattner  EXPECT_EQ(n1, n2->getOperand(0));
11521cc4460efa104e8591b05a90f20130291614344Nick Lewycky}
116cb33799b9f4e152e3460faa83e59b53ff604c87dNick Lewycky
117e5790a432a320c64440bf62adbcec378046eef6bJeffrey YasskinTEST_F(MDNodeTest, Delete) {
1181d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson  Constant *C = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 1);
1191d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson  Instruction *I = new BitCastInst(C, Type::getInt32Ty(getGlobalContext()));
120cb33799b9f4e152e3460faa83e59b53ff604c87dNick Lewycky
121cb33799b9f4e152e3460faa83e59b53ff604c87dNick Lewycky  Value *const V = I;
122ec9186bcf975c9ffa3ec7ca97867f0ec6eb55115Jay Foad  MDNode *n = MDNode::get(Context, V);
123cb33799b9f4e152e3460faa83e59b53ff604c87dNick Lewycky  WeakVH wvh = n;
124cb33799b9f4e152e3460faa83e59b53ff604c87dNick Lewycky
125cb33799b9f4e152e3460faa83e59b53ff604c87dNick Lewycky  EXPECT_EQ(n, wvh);
126cb33799b9f4e152e3460faa83e59b53ff604c87dNick Lewycky
127cb33799b9f4e152e3460faa83e59b53ff604c87dNick Lewycky  delete I;
128cb33799b9f4e152e3460faa83e59b53ff604c87dNick Lewycky}
129fa7c4dcef21003aec7f4630eec3d0e1117fb2259Devang Patel
130fa7c4dcef21003aec7f4630eec3d0e1117fb2259Devang PatelTEST(NamedMDNodeTest, Search) {
131e5790a432a320c64440bf62adbcec378046eef6bJeffrey Yasskin  LLVMContext Context;
132e5790a432a320c64440bf62adbcec378046eef6bJeffrey Yasskin  Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 1);
133e5790a432a320c64440bf62adbcec378046eef6bJeffrey Yasskin  Constant *C2 = ConstantInt::get(Type::getInt32Ty(Context), 2);
134fa7c4dcef21003aec7f4630eec3d0e1117fb2259Devang Patel
135fa7c4dcef21003aec7f4630eec3d0e1117fb2259Devang Patel  Value *const V = C;
136fa7c4dcef21003aec7f4630eec3d0e1117fb2259Devang Patel  Value *const V2 = C2;
137ec9186bcf975c9ffa3ec7ca97867f0ec6eb55115Jay Foad  MDNode *n = MDNode::get(Context, V);
138ec9186bcf975c9ffa3ec7ca97867f0ec6eb55115Jay Foad  MDNode *n2 = MDNode::get(Context, V2);
139fa7c4dcef21003aec7f4630eec3d0e1117fb2259Devang Patel
1409d0b3dda1e44b5160697d8b2d993c4e4e0db2a6eJeffrey Yasskin  Module M("MyModule", Context);
141fa7c4dcef21003aec7f4630eec3d0e1117fb2259Devang Patel  const char *Name = "llvm.NMD1";
14217aa92c92a925b4a674440c7ef088c223990e854Dan Gohman  NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name);
14317aa92c92a925b4a674440c7ef088c223990e854Dan Gohman  NMD->addOperand(n);
14417aa92c92a925b4a674440c7ef088c223990e854Dan Gohman  NMD->addOperand(n2);
14517aa92c92a925b4a674440c7ef088c223990e854Dan Gohman
1460c47a412079c11656fdcc7e125d604e3aa543903Chris Lattner  std::string Str;
1470c47a412079c11656fdcc7e125d604e3aa543903Chris Lattner  raw_string_ostream oss(Str);
148fa7c4dcef21003aec7f4630eec3d0e1117fb2259Devang Patel  NMD->print(oss);
149ab2f2f1ace7e02ac6e47ede9bce2d41a111796d7Chris Lattner  EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n",
150fa7c4dcef21003aec7f4630eec3d0e1117fb2259Devang Patel               oss.str().c_str());
151fa7c4dcef21003aec7f4630eec3d0e1117fb2259Devang Patel}
15221cc4460efa104e8591b05a90f20130291614344Nick Lewycky}
153