1 //===---- llvm/Support/MDBuilder.h - Builder for LLVM metadata --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the MDBuilder class, which is used as a convenient way to
11 // create LLVM metadata with a consistent and simplified interface.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_SUPPORT_MDBUILDER_H
16 #define LLVM_SUPPORT_MDBUILDER_H
18 #include "llvm/Constants.h"
19 #include "llvm/DerivedTypes.h"
20 #include "llvm/LLVMContext.h"
21 #include "llvm/Metadata.h"
22 #include "llvm/ADT/APInt.h"
30 MDBuilder(LLVMContext &context) : Context(context) {}
32 /// CreateString - Return the given string as metadata.
33 MDString *CreateString(StringRef Str) const {
34 return MDString::get(Context, Str);
37 //===------------------------------------------------------------------===//
39 //===------------------------------------------------------------------===//
41 /// CreateRange - Return metadata describing the range [Lo, Hi).
42 MDNode *CreateRange(const APInt &Lo, const APInt &Hi) const {
43 assert(Lo.getBitWidth() == Hi.getBitWidth() && "Mismatched bitwidths!");
44 // If the range is everything then it is useless.
48 // Return the range [Lo, Hi).
49 Type *Ty = IntegerType::get(Context, Lo.getBitWidth());
50 Value *Range[2] = { ConstantInt::get(Ty, Lo), ConstantInt::get(Ty, Hi) };
51 return MDNode::get(Context, Range);
55 //===------------------------------------------------------------------===//
57 //===------------------------------------------------------------------===//
59 /// CreateAnonymousTBAARoot - Return metadata appropriate for a TBAA root
60 /// node. Each returned node is distinct from all other metadata and will
61 /// never be identified (uniqued) with anything else.
62 MDNode *CreateAnonymousTBAARoot() const {
63 // To ensure uniqueness the root node is self-referential.
64 MDNode *Dummy = MDNode::getTemporary(Context, ArrayRef<Value*>());
65 MDNode *Root = MDNode::get(Context, Dummy);
66 // At this point we have
67 // !0 = metadata !{} <- dummy
68 // !1 = metadata !{metadata !0} <- root
69 // Replace the dummy operand with the root node itself and delete the dummy.
70 Root->replaceOperandWith(0, Root);
71 MDNode::deleteTemporary(Dummy);
73 // !1 = metadata !{metadata !1} <- self-referential root
77 /// CreateTBAARoot - Return metadata appropriate for a TBAA root node with
78 /// the given name. This may be identified (uniqued) with other roots with
80 MDNode *CreateTBAARoot(StringRef Name) const {
81 return MDNode::get(Context, CreateString(Name));
84 /// CreateTBAANode - Return metadata for a non-root TBAA node with the given
85 /// name, parent in the TBAA tree, and value for 'pointsToConstantMemory'.
86 MDNode *CreateTBAANode(StringRef Name, MDNode *Parent,
87 bool isConstant = false) const {
89 Constant *Flags = ConstantInt::get(Type::getInt64Ty(Context), 1);
90 Value *Ops[3] = { CreateString(Name), Parent, Flags };
91 return MDNode::get(Context, Ops);
93 Value *Ops[2] = { CreateString(Name), Parent };
94 return MDNode::get(Context, Ops);
99 } // end namespace llvm