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 /// \brief Return the given string as metadata.
33 MDString *createString(StringRef Str) {
34 return MDString::get(Context, Str);
37 //===------------------------------------------------------------------===//
39 //===------------------------------------------------------------------===//
41 /// \brief Return metadata with the given settings. The special value 0.0
42 /// for the Accuracy parameter indicates the default (maximal precision)
44 MDNode *createFPMath(float Accuracy) {
47 assert(Accuracy > 0.0 && "Invalid fpmath accuracy!");
48 Value *Op = ConstantFP::get(Type::getFloatTy(Context), Accuracy);
49 return MDNode::get(Context, Op);
53 //===------------------------------------------------------------------===//
55 //===------------------------------------------------------------------===//
57 /// \brief Return metadata describing the range [Lo, Hi).
58 MDNode *createRange(const APInt &Lo, const APInt &Hi) {
59 assert(Lo.getBitWidth() == Hi.getBitWidth() && "Mismatched bitwidths!");
60 // If the range is everything then it is useless.
64 // Return the range [Lo, Hi).
65 Type *Ty = IntegerType::get(Context, Lo.getBitWidth());
66 Value *Range[2] = { ConstantInt::get(Ty, Lo), ConstantInt::get(Ty, Hi) };
67 return MDNode::get(Context, Range);
71 //===------------------------------------------------------------------===//
73 //===------------------------------------------------------------------===//
75 /// \brief Return metadata appropriate for a TBAA root node. Each returned
76 /// node is distinct from all other metadata and will never be identified
77 /// (uniqued) with anything else.
78 MDNode *createAnonymousTBAARoot() {
79 // To ensure uniqueness the root node is self-referential.
80 MDNode *Dummy = MDNode::getTemporary(Context, ArrayRef<Value*>());
81 MDNode *Root = MDNode::get(Context, Dummy);
82 // At this point we have
83 // !0 = metadata !{} <- dummy
84 // !1 = metadata !{metadata !0} <- root
85 // Replace the dummy operand with the root node itself and delete the dummy.
86 Root->replaceOperandWith(0, Root);
87 MDNode::deleteTemporary(Dummy);
89 // !1 = metadata !{metadata !1} <- self-referential root
93 /// \brief Return metadata appropriate for a TBAA root node with the given
94 /// name. This may be identified (uniqued) with other roots with the same
96 MDNode *createTBAARoot(StringRef Name) {
97 return MDNode::get(Context, createString(Name));
100 /// \brief Return metadata for a non-root TBAA node with the given name,
101 /// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
102 MDNode *createTBAANode(StringRef Name, MDNode *Parent,
103 bool isConstant = false) {
105 Constant *Flags = ConstantInt::get(Type::getInt64Ty(Context), 1);
106 Value *Ops[3] = { createString(Name), Parent, Flags };
107 return MDNode::get(Context, Ops);
109 Value *Ops[2] = { createString(Name), Parent };
110 return MDNode::get(Context, Ops);
116 } // end namespace llvm