1//===---- llvm/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_IR_MDBUILDER_H
16#define LLVM_IR_MDBUILDER_H
17
18#include "llvm/IR/Constants.h"
19#include "llvm/IR/DerivedTypes.h"
20#include "llvm/IR/Metadata.h"
21
22namespace llvm {
23
24class APInt;
25class LLVMContext;
26
27class MDBuilder {
28  LLVMContext &Context;
29
30public:
31  MDBuilder(LLVMContext &context) : Context(context) {}
32
33  /// \brief Return the given string as metadata.
34  MDString *createString(StringRef Str) {
35    return MDString::get(Context, Str);
36  }
37
38  //===------------------------------------------------------------------===//
39  // FPMath metadata.
40  //===------------------------------------------------------------------===//
41
42  /// \brief Return metadata with the given settings.  The special value 0.0
43  /// for the Accuracy parameter indicates the default (maximal precision)
44  /// setting.
45  MDNode *createFPMath(float Accuracy) {
46    if (Accuracy == 0.0)
47      return 0;
48    assert(Accuracy > 0.0 && "Invalid fpmath accuracy!");
49    Value *Op = ConstantFP::get(Type::getFloatTy(Context), Accuracy);
50    return MDNode::get(Context, Op);
51  }
52
53  //===------------------------------------------------------------------===//
54  // Prof metadata.
55  //===------------------------------------------------------------------===//
56
57  /// \brief Return metadata containing two branch weights.
58  MDNode *createBranchWeights(uint32_t TrueWeight, uint32_t FalseWeight) {
59    uint32_t Weights[] = { TrueWeight, FalseWeight };
60    return createBranchWeights(Weights);
61  }
62
63  /// \brief Return metadata containing a number of branch weights.
64  MDNode *createBranchWeights(ArrayRef<uint32_t> Weights) {
65    assert(Weights.size() >= 2 && "Need at least two branch weights!");
66
67    SmallVector<Value *, 4> Vals(Weights.size()+1);
68    Vals[0] = createString("branch_weights");
69
70    Type *Int32Ty = Type::getInt32Ty(Context);
71    for (unsigned i = 0, e = Weights.size(); i != e; ++i)
72      Vals[i+1] = ConstantInt::get(Int32Ty, Weights[i]);
73
74    return MDNode::get(Context, Vals);
75  }
76
77  //===------------------------------------------------------------------===//
78  // Range metadata.
79  //===------------------------------------------------------------------===//
80
81  /// \brief Return metadata describing the range [Lo, Hi).
82  MDNode *createRange(const APInt &Lo, const APInt &Hi) {
83    assert(Lo.getBitWidth() == Hi.getBitWidth() && "Mismatched bitwidths!");
84    // If the range is everything then it is useless.
85    if (Hi == Lo)
86      return 0;
87
88    // Return the range [Lo, Hi).
89    Type *Ty = IntegerType::get(Context, Lo.getBitWidth());
90    Value *Range[2] = { ConstantInt::get(Ty, Lo), ConstantInt::get(Ty, Hi) };
91    return MDNode::get(Context, Range);
92  }
93
94
95  //===------------------------------------------------------------------===//
96  // TBAA metadata.
97  //===------------------------------------------------------------------===//
98
99  /// \brief Return metadata appropriate for a TBAA root node.  Each returned
100  /// node is distinct from all other metadata and will never be identified
101  /// (uniqued) with anything else.
102  MDNode *createAnonymousTBAARoot() {
103    // To ensure uniqueness the root node is self-referential.
104    MDNode *Dummy = MDNode::getTemporary(Context, ArrayRef<Value*>());
105    MDNode *Root = MDNode::get(Context, Dummy);
106    // At this point we have
107    //   !0 = metadata !{}            <- dummy
108    //   !1 = metadata !{metadata !0} <- root
109    // Replace the dummy operand with the root node itself and delete the dummy.
110    Root->replaceOperandWith(0, Root);
111    MDNode::deleteTemporary(Dummy);
112    // We now have
113    //   !1 = metadata !{metadata !1} <- self-referential root
114    return Root;
115  }
116
117  /// \brief Return metadata appropriate for a TBAA root node with the given
118  /// name.  This may be identified (uniqued) with other roots with the same
119  /// name.
120  MDNode *createTBAARoot(StringRef Name) {
121    return MDNode::get(Context, createString(Name));
122  }
123
124  /// \brief Return metadata for a non-root TBAA node with the given name,
125  /// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
126  MDNode *createTBAANode(StringRef Name, MDNode *Parent,
127                         bool isConstant = false) {
128    if (isConstant) {
129      Constant *Flags = ConstantInt::get(Type::getInt64Ty(Context), 1);
130      Value *Ops[3] = { createString(Name), Parent, Flags };
131      return MDNode::get(Context, Ops);
132    } else {
133      Value *Ops[2] = { createString(Name), Parent };
134      return MDNode::get(Context, Ops);
135    }
136  }
137
138  struct TBAAStructField {
139    uint64_t Offset;
140    uint64_t Size;
141    MDNode *TBAA;
142    TBAAStructField(uint64_t Offset, uint64_t Size, MDNode *TBAA) :
143      Offset(Offset), Size(Size), TBAA(TBAA) {}
144  };
145
146  /// \brief Return metadata for a tbaa.struct node with the given
147  /// struct field descriptions.
148  MDNode *createTBAAStructNode(ArrayRef<TBAAStructField> Fields) {
149    SmallVector<Value *, 4> Vals(Fields.size() * 3);
150    Type *Int64 = IntegerType::get(Context, 64);
151    for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
152      Vals[i * 3 + 0] = ConstantInt::get(Int64, Fields[i].Offset);
153      Vals[i * 3 + 1] = ConstantInt::get(Int64, Fields[i].Size);
154      Vals[i * 3 + 2] = Fields[i].TBAA;
155    }
156    return MDNode::get(Context, Vals);
157  }
158
159  /// \brief Return metadata for a TBAA struct node in the type DAG
160  /// with the given name, a list of pairs (offset, field type in the type DAG).
161  MDNode *createTBAAStructTypeNode(StringRef Name,
162             ArrayRef<std::pair<MDNode*, uint64_t> > Fields) {
163    SmallVector<Value *, 4> Ops(Fields.size() * 2 + 1);
164    Type *Int64 = IntegerType::get(Context, 64);
165    Ops[0] = createString(Name);
166    for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
167      Ops[i * 2 + 1] = Fields[i].first;
168      Ops[i * 2 + 2] = ConstantInt::get(Int64, Fields[i].second);
169    }
170    return MDNode::get(Context, Ops);
171  }
172
173  /// \brief Return metadata for a TBAA scalar type node with the
174  /// given name, an offset and a parent in the TBAA type DAG.
175  MDNode *createTBAAScalarTypeNode(StringRef Name, MDNode *Parent,
176                                   uint64_t Offset = 0) {
177    SmallVector<Value *, 4> Ops(3);
178    Type *Int64 = IntegerType::get(Context, 64);
179    Ops[0] = createString(Name);
180    Ops[1] = Parent;
181    Ops[2] = ConstantInt::get(Int64, Offset);
182    return MDNode::get(Context, Ops);
183  }
184
185  /// \brief Return metadata for a TBAA tag node with the given
186  /// base type, access type and offset relative to the base type.
187  MDNode *createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType,
188                                  uint64_t Offset) {
189    Type *Int64 = IntegerType::get(Context, 64);
190    Value *Ops[3] = { BaseType, AccessType, ConstantInt::get(Int64, Offset) };
191    return MDNode::get(Context, Ops);
192  }
193
194};
195
196} // end namespace llvm
197
198#endif
199