llvm.org GIT mirror llvm / e747fad
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 Duncan Sands 8 years ago
3 changed file(s) with 194 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
531531
  • llvm::getTrapFunctionName()
  • 532532
  • llvm::EnableSegmentedStacks
  • 533533
    534
  • The MDBuilder class has been added to simplify the creation of
  • 535 metadata.
    534536
  • ....
  • 535537
    536538
    0 //===---- llvm/Support/MDBuilder.h - Builder for LLVM metadata --*- C++ -*-===//
    1 //
    2 // The LLVM Compiler Infrastructure
    3 //
    4 // This file is distributed under the University of Illinois Open Source
    5 // License. See LICENSE.TXT for details.
    6 //
    7 //===----------------------------------------------------------------------===//
    8 //
    9 // This file defines the MDBuilder class, which is used as a convenient way to
    10 // create LLVM metadata with a consistent and simplified interface.
    11 //
    12 //===----------------------------------------------------------------------===//
    13
    14 #ifndef LLVM_SUPPORT_MDBUILDER_H
    15 #define LLVM_SUPPORT_MDBUILDER_H
    16
    17 #include "llvm/Constants.h"
    18 #include "llvm/DerivedTypes.h"
    19 #include "llvm/LLVMContext.h"
    20 #include "llvm/Metadata.h"
    21 #include "llvm/ADT/APInt.h"
    22
    23 namespace llvm {
    24
    25 class MDBuilder {
    26 LLVMContext &Context;
    27
    28 public:
    29 MDBuilder(LLVMContext &context) : Context(context) {}
    30
    31 /// CreateString - Return the given string as metadata.
    32 MDString *CreateString(StringRef Str) const {
    33 return MDString::get(Context, Str);
    34 }
    35
    36 //===------------------------------------------------------------------===//
    37 // Range metadata.
    38 //===------------------------------------------------------------------===//
    39
    40 /// CreateRange - Return metadata describing the range [Lo, Hi).
    41 MDNode *CreateRange(const APInt &Lo, const APInt &Hi) const {
    42 assert(Lo.getBitWidth() == Hi.getBitWidth() && "Mismatched bitwidths!");
    43 // If the range is everything then it is useless.
    44 if (Hi == Lo)
    45 return 0;
    46
    47 // Return the range [Lo, Hi).
    48 Type *Ty = IntegerType::get(Context, Lo.getBitWidth());
    49 Value *Range[2] = { ConstantInt::get(Ty, Lo), ConstantInt::get(Ty, Hi) };
    50 return MDNode::get(Context, Range);
    51 }
    52
    53
    54 //===------------------------------------------------------------------===//
    55 // TBAA metadata.
    56 //===------------------------------------------------------------------===//
    57
    58 /// CreateAnonymousTBAARoot - Return metadata appropriate for a TBAA root
    59 /// node. Each returned node is distinct from all other metadata and will
    60 /// never be identified (uniqued) with anything else.
    61 MDNode *CreateAnonymousTBAARoot() const {
    62 // To ensure uniqueness the root node is self-referential.
    63 MDNode *Dummy = MDNode::getTemporary(Context, ArrayRef());
    64 MDNode *Root = MDNode::get(Context, Dummy);
    65 // At this point we have
    66 // !0 = metadata !{} <- dummy
    67 // !1 = metadata !{metadata !0} <- root
    68 // Replace the dummy operand with the root node itself and delete the dummy.
    69 Root->replaceOperandWith(0, Root);
    70 MDNode::deleteTemporary(Dummy);
    71 // We now have
    72 // !1 = metadata !{metadata !1} <- self-referential root
    73 return Root;
    74 }
    75
    76 /// CreateTBAARoot - Return metadata appropriate for a TBAA root node with
    77 /// the given name. This may be identified (uniqued) with other roots with
    78 /// the same name.
    79 MDNode *CreateTBAARoot(StringRef Name) const {
    80 return MDNode::get(Context, CreateString(Name));
    81 }
    82
    83 /// CreateTBAANode - Return metadata for a non-root TBAA node with the given
    84 /// name, parent in the TBAA tree, and value for 'pointsToConstantMemory'.
    85 MDNode *CreateTBAANode(StringRef Name, MDNode *Parent,
    86 bool isConstant = false) const {
    87 if (isConstant) {
    88 Constant *Flags = ConstantInt::get(Type::getInt64Ty(Context), 1);
    89 Value *Ops[3] = { CreateString(Name), Parent, Flags };
    90 return MDNode::get(Context, Ops);
    91 } else {
    92 Value *Ops[2] = { CreateString(Name), Parent };
    93 return MDNode::get(Context, Ops);
    94 }
    95 }
    96 };
    97
    98 } // end namespace llvm
    99
    100 #endif
    0 //===- llvm/unittests/Support/MDBuilderTest.cpp - MDBuilder unit tests ----===//
    1 //
    2 // The LLVM Compiler Infrastructure
    3 //
    4 // This file is distributed under the University of Illinois Open Source
    5 // License. See LICENSE.TXT for details.
    6 //
    7 //===----------------------------------------------------------------------===//
    8
    9 #include "gtest/gtest.h"
    10 #include "llvm/Support/MDBuilder.h"
    11 using namespace llvm;
    12
    13 namespace {
    14
    15 class MDBuilderTest : public testing::Test {
    16 protected:
    17 LLVMContext Context;
    18 };
    19
    20 TEST_F(MDBuilderTest, CreateString) {
    21 MDBuilder MDHelper(Context);
    22 MDString *Str0 = MDHelper.CreateString("");
    23 MDString *Str1 = MDHelper.CreateString("string");
    24 EXPECT_EQ(Str0->getString(), StringRef(""));
    25 EXPECT_EQ(Str1->getString(), StringRef("string"));
    26 }
    27
    28 TEST_F(MDBuilderTest, CreateRangeMetadata) {
    29 MDBuilder MDHelper(Context);
    30 APInt A(8, 1), B(8, 2);
    31 MDNode *R0 = MDHelper.CreateRange(A, A);
    32 MDNode *R1 = MDHelper.CreateRange(A, B);
    33 EXPECT_EQ(R0, (MDNode *)0);
    34 EXPECT_NE(R1, (MDNode *)0);
    35 EXPECT_EQ(R1->getNumOperands(), 2U);
    36 EXPECT_TRUE(isa(R1->getOperand(0)));
    37 EXPECT_TRUE(isa(R1->getOperand(1)));
    38 ConstantInt *C0 = cast(R1->getOperand(0));
    39 ConstantInt *C1 = cast(R1->getOperand(1));
    40 EXPECT_EQ(C0->getValue(), A);
    41 EXPECT_EQ(C1->getValue(), B);
    42 }
    43 TEST_F(MDBuilderTest, CreateAnonymousTBAARoot) {
    44 MDBuilder MDHelper(Context);
    45 MDNode *R0 = MDHelper.CreateAnonymousTBAARoot();
    46 MDNode *R1 = MDHelper.CreateAnonymousTBAARoot();
    47 EXPECT_NE(R0, R1);
    48 EXPECT_GE(R0->getNumOperands(), 1U);
    49 EXPECT_GE(R1->getNumOperands(), 1U);
    50 EXPECT_EQ(R0->getOperand(0), R0);
    51 EXPECT_EQ(R1->getOperand(0), R1);
    52 EXPECT_TRUE(R0->getNumOperands() == 1 || R0->getOperand(1) == 0);
    53 EXPECT_TRUE(R1->getNumOperands() == 1 || R1->getOperand(1) == 0);
    54 }
    55 TEST_F(MDBuilderTest, CreateTBAARoot) {
    56 MDBuilder MDHelper(Context);
    57 MDNode *R0 = MDHelper.CreateTBAARoot("Root");
    58 MDNode *R1 = MDHelper.CreateTBAARoot("Root");
    59 EXPECT_EQ(R0, R1);
    60 EXPECT_GE(R0->getNumOperands(), 1U);
    61 EXPECT_TRUE(isa(R0->getOperand(0)));
    62 EXPECT_EQ(cast(R0->getOperand(0))->getString(), "Root");
    63 EXPECT_TRUE(R0->getNumOperands() == 1 || R0->getOperand(1) == 0);
    64 }
    65 TEST_F(MDBuilderTest, CreateTBAANode) {
    66 MDBuilder MDHelper(Context);
    67 MDNode *R = MDHelper.CreateTBAARoot("Root");
    68 MDNode *N0 = MDHelper.CreateTBAANode("Node", R);
    69 MDNode *N1 = MDHelper.CreateTBAANode("edoN", R);
    70 MDNode *N2 = MDHelper.CreateTBAANode("Node", R, true);
    71 MDNode *N3 = MDHelper.CreateTBAANode("Node", R);
    72 EXPECT_EQ(N0, N3);
    73 EXPECT_NE(N0, N1);
    74 EXPECT_NE(N0, N2);
    75 EXPECT_GE(N0->getNumOperands(), 2U);
    76 EXPECT_GE(N1->getNumOperands(), 2U);
    77 EXPECT_GE(N2->getNumOperands(), 3U);
    78 EXPECT_TRUE(isa(N0->getOperand(0)));
    79 EXPECT_TRUE(isa(N1->getOperand(0)));
    80 EXPECT_TRUE(isa(N2->getOperand(0)));
    81 EXPECT_EQ(cast(N0->getOperand(0))->getString(), "Node");
    82 EXPECT_EQ(cast(N1->getOperand(0))->getString(), "edoN");
    83 EXPECT_EQ(cast(N2->getOperand(0))->getString(), "Node");
    84 EXPECT_EQ(N0->getOperand(1), R);
    85 EXPECT_EQ(N1->getOperand(1), R);
    86 EXPECT_EQ(N2->getOperand(1), R);
    87 EXPECT_TRUE(isa(N2->getOperand(2)));
    88 EXPECT_EQ(cast(N2->getOperand(2))->getZExtValue(), 1U);
    89 }
    90 }