Add the MDBuilder helper class for conveniently creating metadata.
[oota-llvm.git] / include / llvm / Support / MDBuilder.h
1 //===---- llvm/Support/MDBuilder.h - Builder for LLVM metadata --*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_SUPPORT_MDBUILDER_H
16 #define LLVM_SUPPORT_MDBUILDER_H
17
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"
23
24 namespace llvm {
25
26   class MDBuilder {
27     LLVMContext &Context;
28
29   public:
30     MDBuilder(LLVMContext &context) : Context(context) {}
31
32     /// CreateString - Return the given string as metadata.
33     MDString *CreateString(StringRef Str) const {
34       return MDString::get(Context, Str);
35     }
36
37     //===------------------------------------------------------------------===//
38     // Range metadata.
39     //===------------------------------------------------------------------===//
40
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.
45       if (Hi == Lo)
46         return 0;
47
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);
52     }
53
54
55     //===------------------------------------------------------------------===//
56     // TBAA metadata.
57     //===------------------------------------------------------------------===//
58
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);
72       // We now have
73       //   !1 = metadata !{metadata !1} <- self-referential root
74       return Root;
75     }
76
77     /// CreateTBAARoot - Return metadata appropriate for a TBAA root node with
78     /// the given name.  This may be identified (uniqued) with other roots with
79     /// the same name.
80     MDNode *CreateTBAARoot(StringRef Name) const {
81       return MDNode::get(Context, CreateString(Name));
82     }
83
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 {
88       if (isConstant) {
89         Constant *Flags = ConstantInt::get(Type::getInt64Ty(Context), 1);
90         Value *Ops[3] = { CreateString(Name), Parent, Flags };
91         return MDNode::get(Context, Ops);
92       } else {
93         Value *Ops[2] = { CreateString(Name), Parent };
94         return MDNode::get(Context, Ops);
95       }
96     }
97   };
98
99 } // end namespace llvm
100
101 #endif