]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/llvm/include/llvm/Support/MDBuilder.h
MFC r234353:
[FreeBSD/stable/9.git] / contrib / llvm / 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     /// \brief Return the given string as metadata.
33     MDString *createString(StringRef Str) {
34       return MDString::get(Context, Str);
35     }
36
37     //===------------------------------------------------------------------===//
38     // FPMath metadata.
39     //===------------------------------------------------------------------===//
40
41     /// \brief Return metadata with the given settings.  The special value 0.0
42     /// for the Accuracy parameter indicates the default (maximal precision)
43     /// setting.
44     MDNode *createFPMath(float Accuracy) {
45       if (Accuracy == 0.0)
46         return 0;
47       assert(Accuracy > 0.0 && "Invalid fpmath accuracy!");
48       Value *Op = ConstantFP::get(Type::getFloatTy(Context), Accuracy);
49       return MDNode::get(Context, Op);
50     }
51
52
53     //===------------------------------------------------------------------===//
54     // Range metadata.
55     //===------------------------------------------------------------------===//
56
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.
61       if (Hi == Lo)
62         return 0;
63
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);
68     }
69
70
71     //===------------------------------------------------------------------===//
72     // TBAA metadata.
73     //===------------------------------------------------------------------===//
74
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);
88       // We now have
89       //   !1 = metadata !{metadata !1} <- self-referential root
90       return Root;
91     }
92
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
95     /// name.
96     MDNode *createTBAARoot(StringRef Name) {
97       return MDNode::get(Context, createString(Name));
98     }
99
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) {
104       if (isConstant) {
105         Constant *Flags = ConstantInt::get(Type::getInt64Ty(Context), 1);
106         Value *Ops[3] = { createString(Name), Parent, Flags };
107         return MDNode::get(Context, Ops);
108       } else {
109         Value *Ops[2] = { createString(Name), Parent };
110         return MDNode::get(Context, Ops);
111       }
112     }
113
114   };
115
116 } // end namespace llvm
117
118 #endif