mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-01-14 16:33:28 +00:00
Add the MDBuilder helper class for conveniently creating metadata.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@154766 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
362a05a635
commit
e747fadedf
@ -532,6 +532,8 @@ syntax, there are still significant gaps in that support.</p>
|
||||
<li><code>llvm::getTrapFunctionName()</code></li>
|
||||
<li><code>llvm::EnableSegmentedStacks</code></li>
|
||||
</ul></li>
|
||||
<li>The MDBuilder class has been added to simplify the creation of
|
||||
metadata.</li>
|
||||
<li>....</li>
|
||||
</ul>
|
||||
|
||||
|
101
include/llvm/Support/MDBuilder.h
Normal file
101
include/llvm/Support/MDBuilder.h
Normal file
@ -0,0 +1,101 @@
|
||||
//===---- llvm/Support/MDBuilder.h - Builder for LLVM metadata --*- C++ -*-===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is distributed under the University of Illinois Open Source
|
||||
// License. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This file defines the MDBuilder class, which is used as a convenient way to
|
||||
// create LLVM metadata with a consistent and simplified interface.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#ifndef LLVM_SUPPORT_MDBUILDER_H
|
||||
#define LLVM_SUPPORT_MDBUILDER_H
|
||||
|
||||
#include "llvm/Constants.h"
|
||||
#include "llvm/DerivedTypes.h"
|
||||
#include "llvm/LLVMContext.h"
|
||||
#include "llvm/Metadata.h"
|
||||
#include "llvm/ADT/APInt.h"
|
||||
|
||||
namespace llvm {
|
||||
|
||||
class MDBuilder {
|
||||
LLVMContext &Context;
|
||||
|
||||
public:
|
||||
MDBuilder(LLVMContext &context) : Context(context) {}
|
||||
|
||||
/// CreateString - Return the given string as metadata.
|
||||
MDString *CreateString(StringRef Str) const {
|
||||
return MDString::get(Context, Str);
|
||||
}
|
||||
|
||||
//===------------------------------------------------------------------===//
|
||||
// Range metadata.
|
||||
//===------------------------------------------------------------------===//
|
||||
|
||||
/// CreateRange - Return metadata describing the range [Lo, Hi).
|
||||
MDNode *CreateRange(const APInt &Lo, const APInt &Hi) const {
|
||||
assert(Lo.getBitWidth() == Hi.getBitWidth() && "Mismatched bitwidths!");
|
||||
// If the range is everything then it is useless.
|
||||
if (Hi == Lo)
|
||||
return 0;
|
||||
|
||||
// Return the range [Lo, Hi).
|
||||
Type *Ty = IntegerType::get(Context, Lo.getBitWidth());
|
||||
Value *Range[2] = { ConstantInt::get(Ty, Lo), ConstantInt::get(Ty, Hi) };
|
||||
return MDNode::get(Context, Range);
|
||||
}
|
||||
|
||||
|
||||
//===------------------------------------------------------------------===//
|
||||
// TBAA metadata.
|
||||
//===------------------------------------------------------------------===//
|
||||
|
||||
/// CreateAnonymousTBAARoot - Return metadata appropriate for a TBAA root
|
||||
/// node. Each returned node is distinct from all other metadata and will
|
||||
/// never be identified (uniqued) with anything else.
|
||||
MDNode *CreateAnonymousTBAARoot() const {
|
||||
// To ensure uniqueness the root node is self-referential.
|
||||
MDNode *Dummy = MDNode::getTemporary(Context, ArrayRef<Value*>());
|
||||
MDNode *Root = MDNode::get(Context, Dummy);
|
||||
// At this point we have
|
||||
// !0 = metadata !{} <- dummy
|
||||
// !1 = metadata !{metadata !0} <- root
|
||||
// Replace the dummy operand with the root node itself and delete the dummy.
|
||||
Root->replaceOperandWith(0, Root);
|
||||
MDNode::deleteTemporary(Dummy);
|
||||
// We now have
|
||||
// !1 = metadata !{metadata !1} <- self-referential root
|
||||
return Root;
|
||||
}
|
||||
|
||||
/// CreateTBAARoot - Return metadata appropriate for a TBAA root node with
|
||||
/// the given name. This may be identified (uniqued) with other roots with
|
||||
/// the same name.
|
||||
MDNode *CreateTBAARoot(StringRef Name) const {
|
||||
return MDNode::get(Context, CreateString(Name));
|
||||
}
|
||||
|
||||
/// CreateTBAANode - Return metadata for a non-root TBAA node with the given
|
||||
/// name, parent in the TBAA tree, and value for 'pointsToConstantMemory'.
|
||||
MDNode *CreateTBAANode(StringRef Name, MDNode *Parent,
|
||||
bool isConstant = false) const {
|
||||
if (isConstant) {
|
||||
Constant *Flags = ConstantInt::get(Type::getInt64Ty(Context), 1);
|
||||
Value *Ops[3] = { CreateString(Name), Parent, Flags };
|
||||
return MDNode::get(Context, Ops);
|
||||
} else {
|
||||
Value *Ops[2] = { CreateString(Name), Parent };
|
||||
return MDNode::get(Context, Ops);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} // end namespace llvm
|
||||
|
||||
#endif
|
91
unittests/Support/MDBuilderTest.cpp
Normal file
91
unittests/Support/MDBuilderTest.cpp
Normal file
@ -0,0 +1,91 @@
|
||||
//===- llvm/unittests/Support/MDBuilderTest.cpp - MDBuilder unit tests ----===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is distributed under the University of Illinois Open Source
|
||||
// License. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "llvm/Support/MDBuilder.h"
|
||||
using namespace llvm;
|
||||
|
||||
namespace {
|
||||
|
||||
class MDBuilderTest : public testing::Test {
|
||||
protected:
|
||||
LLVMContext Context;
|
||||
};
|
||||
|
||||
TEST_F(MDBuilderTest, CreateString) {
|
||||
MDBuilder MDHelper(Context);
|
||||
MDString *Str0 = MDHelper.CreateString("");
|
||||
MDString *Str1 = MDHelper.CreateString("string");
|
||||
EXPECT_EQ(Str0->getString(), StringRef(""));
|
||||
EXPECT_EQ(Str1->getString(), StringRef("string"));
|
||||
}
|
||||
|
||||
TEST_F(MDBuilderTest, CreateRangeMetadata) {
|
||||
MDBuilder MDHelper(Context);
|
||||
APInt A(8, 1), B(8, 2);
|
||||
MDNode *R0 = MDHelper.CreateRange(A, A);
|
||||
MDNode *R1 = MDHelper.CreateRange(A, B);
|
||||
EXPECT_EQ(R0, (MDNode *)0);
|
||||
EXPECT_NE(R1, (MDNode *)0);
|
||||
EXPECT_EQ(R1->getNumOperands(), 2U);
|
||||
EXPECT_TRUE(isa<ConstantInt>(R1->getOperand(0)));
|
||||
EXPECT_TRUE(isa<ConstantInt>(R1->getOperand(1)));
|
||||
ConstantInt *C0 = cast<ConstantInt>(R1->getOperand(0));
|
||||
ConstantInt *C1 = cast<ConstantInt>(R1->getOperand(1));
|
||||
EXPECT_EQ(C0->getValue(), A);
|
||||
EXPECT_EQ(C1->getValue(), B);
|
||||
}
|
||||
TEST_F(MDBuilderTest, CreateAnonymousTBAARoot) {
|
||||
MDBuilder MDHelper(Context);
|
||||
MDNode *R0 = MDHelper.CreateAnonymousTBAARoot();
|
||||
MDNode *R1 = MDHelper.CreateAnonymousTBAARoot();
|
||||
EXPECT_NE(R0, R1);
|
||||
EXPECT_GE(R0->getNumOperands(), 1U);
|
||||
EXPECT_GE(R1->getNumOperands(), 1U);
|
||||
EXPECT_EQ(R0->getOperand(0), R0);
|
||||
EXPECT_EQ(R1->getOperand(0), R1);
|
||||
EXPECT_TRUE(R0->getNumOperands() == 1 || R0->getOperand(1) == 0);
|
||||
EXPECT_TRUE(R1->getNumOperands() == 1 || R1->getOperand(1) == 0);
|
||||
}
|
||||
TEST_F(MDBuilderTest, CreateTBAARoot) {
|
||||
MDBuilder MDHelper(Context);
|
||||
MDNode *R0 = MDHelper.CreateTBAARoot("Root");
|
||||
MDNode *R1 = MDHelper.CreateTBAARoot("Root");
|
||||
EXPECT_EQ(R0, R1);
|
||||
EXPECT_GE(R0->getNumOperands(), 1U);
|
||||
EXPECT_TRUE(isa<MDString>(R0->getOperand(0)));
|
||||
EXPECT_EQ(cast<MDString>(R0->getOperand(0))->getString(), "Root");
|
||||
EXPECT_TRUE(R0->getNumOperands() == 1 || R0->getOperand(1) == 0);
|
||||
}
|
||||
TEST_F(MDBuilderTest, CreateTBAANode) {
|
||||
MDBuilder MDHelper(Context);
|
||||
MDNode *R = MDHelper.CreateTBAARoot("Root");
|
||||
MDNode *N0 = MDHelper.CreateTBAANode("Node", R);
|
||||
MDNode *N1 = MDHelper.CreateTBAANode("edoN", R);
|
||||
MDNode *N2 = MDHelper.CreateTBAANode("Node", R, true);
|
||||
MDNode *N3 = MDHelper.CreateTBAANode("Node", R);
|
||||
EXPECT_EQ(N0, N3);
|
||||
EXPECT_NE(N0, N1);
|
||||
EXPECT_NE(N0, N2);
|
||||
EXPECT_GE(N0->getNumOperands(), 2U);
|
||||
EXPECT_GE(N1->getNumOperands(), 2U);
|
||||
EXPECT_GE(N2->getNumOperands(), 3U);
|
||||
EXPECT_TRUE(isa<MDString>(N0->getOperand(0)));
|
||||
EXPECT_TRUE(isa<MDString>(N1->getOperand(0)));
|
||||
EXPECT_TRUE(isa<MDString>(N2->getOperand(0)));
|
||||
EXPECT_EQ(cast<MDString>(N0->getOperand(0))->getString(), "Node");
|
||||
EXPECT_EQ(cast<MDString>(N1->getOperand(0))->getString(), "edoN");
|
||||
EXPECT_EQ(cast<MDString>(N2->getOperand(0))->getString(), "Node");
|
||||
EXPECT_EQ(N0->getOperand(1), R);
|
||||
EXPECT_EQ(N1->getOperand(1), R);
|
||||
EXPECT_EQ(N2->getOperand(1), R);
|
||||
EXPECT_TRUE(isa<ConstantInt>(N2->getOperand(2)));
|
||||
EXPECT_EQ(cast<ConstantInt>(N2->getOperand(2))->getZExtValue(), 1U);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user