1 //===---- llvm/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_MDBUILDER_H
16 #define LLVM_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);
52 //===------------------------------------------------------------------===//
54 //===------------------------------------------------------------------===//
56 /// \brief Return metadata containing two branch weights.
57 MDNode *createBranchWeights(uint32_t TrueWeight, uint32_t FalseWeight) {
58 uint32_t Weights[] = { TrueWeight, FalseWeight };
59 return createBranchWeights(Weights);
62 /// \brief Return metadata containing a number of branch weights.
63 MDNode *createBranchWeights(ArrayRef<uint32_t> Weights) {
64 assert(Weights.size() >= 2 && "Need at least two branch weights!");
66 SmallVector<Value *, 4> Vals(Weights.size()+1);
67 Vals[0] = createString("branch_weights");
69 Type *Int32Ty = Type::getInt32Ty(Context);
70 for (unsigned i = 0, e = Weights.size(); i != e; ++i)
71 Vals[i+1] = ConstantInt::get(Int32Ty, Weights[i]);
73 return MDNode::get(Context, Vals);
76 //===------------------------------------------------------------------===//
78 //===------------------------------------------------------------------===//
80 /// \brief Return metadata describing the range [Lo, Hi).
81 MDNode *createRange(const APInt &Lo, const APInt &Hi) {
82 assert(Lo.getBitWidth() == Hi.getBitWidth() && "Mismatched bitwidths!");
83 // If the range is everything then it is useless.
87 // Return the range [Lo, Hi).
88 Type *Ty = IntegerType::get(Context, Lo.getBitWidth());
89 Value *Range[2] = { ConstantInt::get(Ty, Lo), ConstantInt::get(Ty, Hi) };
90 return MDNode::get(Context, Range);
94 //===------------------------------------------------------------------===//
96 //===------------------------------------------------------------------===//
98 /// \brief Return metadata appropriate for a TBAA root node. Each returned
99 /// node is distinct from all other metadata and will never be identified
100 /// (uniqued) with anything else.
101 MDNode *createAnonymousTBAARoot() {
102 // To ensure uniqueness the root node is self-referential.
103 MDNode *Dummy = MDNode::getTemporary(Context, ArrayRef<Value*>());
104 MDNode *Root = MDNode::get(Context, Dummy);
105 // At this point we have
106 // !0 = metadata !{} <- dummy
107 // !1 = metadata !{metadata !0} <- root
108 // Replace the dummy operand with the root node itself and delete the dummy.
109 Root->replaceOperandWith(0, Root);
110 MDNode::deleteTemporary(Dummy);
112 // !1 = metadata !{metadata !1} <- self-referential root
116 /// \brief Return metadata appropriate for a TBAA root node with the given
117 /// name. This may be identified (uniqued) with other roots with the same
119 MDNode *createTBAARoot(StringRef Name) {
120 return MDNode::get(Context, createString(Name));
123 /// \brief Return metadata for a non-root TBAA node with the given name,
124 /// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
125 MDNode *createTBAANode(StringRef Name, MDNode *Parent,
126 bool isConstant = false) {
128 Constant *Flags = ConstantInt::get(Type::getInt64Ty(Context), 1);
129 Value *Ops[3] = { createString(Name), Parent, Flags };
130 return MDNode::get(Context, Ops);
132 Value *Ops[2] = { createString(Name), Parent };
133 return MDNode::get(Context, Ops);
137 struct TBAAStructField {
141 TBAAStructField(uint64_t Offset, uint64_t Size, MDNode *TBAA) :
142 Offset(Offset), Size(Size), TBAA(TBAA) {}
145 /// \brief Return metadata for a tbaa.struct node with the given
146 /// struct field descriptions.
147 MDNode *createTBAAStructNode(ArrayRef<TBAAStructField> Fields) {
148 SmallVector<Value *, 4> Vals(Fields.size() * 3);
149 Type *Int64 = IntegerType::get(Context, 64);
150 for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
151 Vals[i * 3 + 0] = ConstantInt::get(Int64, Fields[i].Offset);
152 Vals[i * 3 + 1] = ConstantInt::get(Int64, Fields[i].Size);
153 Vals[i * 3 + 2] = Fields[i].TBAA;
155 return MDNode::get(Context, Vals);
160 } // end namespace llvm