• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===---- llvm/MDBuilder.cpp - Builder for LLVM metadata ------------------===//
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 #include "llvm/IR/MDBuilder.h"
16 #include "llvm/IR/Constants.h"
17 #include "llvm/IR/Metadata.h"
18 using namespace llvm;
19 
createString(StringRef Str)20 MDString *MDBuilder::createString(StringRef Str) {
21   return MDString::get(Context, Str);
22 }
23 
createConstant(Constant * C)24 ConstantAsMetadata *MDBuilder::createConstant(Constant *C) {
25   return ConstantAsMetadata::get(C);
26 }
27 
createFPMath(float Accuracy)28 MDNode *MDBuilder::createFPMath(float Accuracy) {
29   if (Accuracy == 0.0)
30     return nullptr;
31   assert(Accuracy > 0.0 && "Invalid fpmath accuracy!");
32   auto *Op =
33       createConstant(ConstantFP::get(Type::getFloatTy(Context), Accuracy));
34   return MDNode::get(Context, Op);
35 }
36 
createBranchWeights(uint32_t TrueWeight,uint32_t FalseWeight)37 MDNode *MDBuilder::createBranchWeights(uint32_t TrueWeight,
38                                        uint32_t FalseWeight) {
39   uint32_t Weights[] = {TrueWeight, FalseWeight};
40   return createBranchWeights(Weights);
41 }
42 
createBranchWeights(ArrayRef<uint32_t> Weights)43 MDNode *MDBuilder::createBranchWeights(ArrayRef<uint32_t> Weights) {
44   assert(Weights.size() >= 2 && "Need at least two branch weights!");
45 
46   SmallVector<Metadata *, 4> Vals(Weights.size() + 1);
47   Vals[0] = createString("branch_weights");
48 
49   Type *Int32Ty = Type::getInt32Ty(Context);
50   for (unsigned i = 0, e = Weights.size(); i != e; ++i)
51     Vals[i + 1] = createConstant(ConstantInt::get(Int32Ty, Weights[i]));
52 
53   return MDNode::get(Context, Vals);
54 }
55 
createRange(const APInt & Lo,const APInt & Hi)56 MDNode *MDBuilder::createRange(const APInt &Lo, const APInt &Hi) {
57   assert(Lo.getBitWidth() == Hi.getBitWidth() && "Mismatched bitwidths!");
58 
59   Type *Ty = IntegerType::get(Context, Lo.getBitWidth());
60   return createRange(ConstantInt::get(Ty, Lo), ConstantInt::get(Ty, Hi));
61 }
62 
createRange(Constant * Lo,Constant * Hi)63 MDNode *MDBuilder::createRange(Constant *Lo, Constant *Hi) {
64   // If the range is everything then it is useless.
65   if (Hi == Lo)
66     return nullptr;
67 
68   // Return the range [Lo, Hi).
69   Metadata *Range[2] = {createConstant(Lo), createConstant(Hi)};
70   return MDNode::get(Context, Range);
71 }
72 
createAnonymousAARoot(StringRef Name,MDNode * Extra)73 MDNode *MDBuilder::createAnonymousAARoot(StringRef Name, MDNode *Extra) {
74   // To ensure uniqueness the root node is self-referential.
75   auto Dummy = MDNode::getTemporary(Context, None);
76 
77   SmallVector<Metadata *, 3> Args(1, Dummy.get());
78   if (Extra)
79     Args.push_back(Extra);
80   if (!Name.empty())
81     Args.push_back(createString(Name));
82   MDNode *Root = MDNode::get(Context, Args);
83 
84   // At this point we have
85   //   !0 = metadata !{}            <- dummy
86   //   !1 = metadata !{metadata !0} <- root
87   // Replace the dummy operand with the root node itself and delete the dummy.
88   Root->replaceOperandWith(0, Root);
89 
90   // We now have
91   //   !1 = metadata !{metadata !1} <- self-referential root
92   return Root;
93 }
94 
createTBAARoot(StringRef Name)95 MDNode *MDBuilder::createTBAARoot(StringRef Name) {
96   return MDNode::get(Context, createString(Name));
97 }
98 
99 /// \brief Return metadata for a non-root TBAA node with the given name,
100 /// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
createTBAANode(StringRef Name,MDNode * Parent,bool isConstant)101 MDNode *MDBuilder::createTBAANode(StringRef Name, MDNode *Parent,
102                                   bool isConstant) {
103   if (isConstant) {
104     Constant *Flags = ConstantInt::get(Type::getInt64Ty(Context), 1);
105     Metadata *Ops[3] = {createString(Name), Parent, createConstant(Flags)};
106     return MDNode::get(Context, Ops);
107   } else {
108     Metadata *Ops[2] = {createString(Name), Parent};
109     return MDNode::get(Context, Ops);
110   }
111 }
112 
createAliasScopeDomain(StringRef Name)113 MDNode *MDBuilder::createAliasScopeDomain(StringRef Name) {
114   return MDNode::get(Context, createString(Name));
115 }
116 
createAliasScope(StringRef Name,MDNode * Domain)117 MDNode *MDBuilder::createAliasScope(StringRef Name, MDNode *Domain) {
118   Metadata *Ops[2] = {createString(Name), Domain};
119   return MDNode::get(Context, Ops);
120 }
121 
122 /// \brief Return metadata for a tbaa.struct node with the given
123 /// struct field descriptions.
createTBAAStructNode(ArrayRef<TBAAStructField> Fields)124 MDNode *MDBuilder::createTBAAStructNode(ArrayRef<TBAAStructField> Fields) {
125   SmallVector<Metadata *, 4> Vals(Fields.size() * 3);
126   Type *Int64 = Type::getInt64Ty(Context);
127   for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
128     Vals[i * 3 + 0] = createConstant(ConstantInt::get(Int64, Fields[i].Offset));
129     Vals[i * 3 + 1] = createConstant(ConstantInt::get(Int64, Fields[i].Size));
130     Vals[i * 3 + 2] = Fields[i].TBAA;
131   }
132   return MDNode::get(Context, Vals);
133 }
134 
135 /// \brief Return metadata for a TBAA struct node in the type DAG
136 /// with the given name, a list of pairs (offset, field type in the type DAG).
createTBAAStructTypeNode(StringRef Name,ArrayRef<std::pair<MDNode *,uint64_t>> Fields)137 MDNode *MDBuilder::createTBAAStructTypeNode(
138     StringRef Name, ArrayRef<std::pair<MDNode *, uint64_t>> Fields) {
139   SmallVector<Metadata *, 4> Ops(Fields.size() * 2 + 1);
140   Type *Int64 = Type::getInt64Ty(Context);
141   Ops[0] = createString(Name);
142   for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
143     Ops[i * 2 + 1] = Fields[i].first;
144     Ops[i * 2 + 2] = createConstant(ConstantInt::get(Int64, Fields[i].second));
145   }
146   return MDNode::get(Context, Ops);
147 }
148 
149 /// \brief Return metadata for a TBAA scalar type node with the
150 /// given name, an offset and a parent in the TBAA type DAG.
createTBAAScalarTypeNode(StringRef Name,MDNode * Parent,uint64_t Offset)151 MDNode *MDBuilder::createTBAAScalarTypeNode(StringRef Name, MDNode *Parent,
152                                             uint64_t Offset) {
153   ConstantInt *Off = ConstantInt::get(Type::getInt64Ty(Context), Offset);
154   Metadata *Ops[3] = {createString(Name), Parent, createConstant(Off)};
155   return MDNode::get(Context, Ops);
156 }
157 
158 /// \brief Return metadata for a TBAA tag node with the given
159 /// base type, access type and offset relative to the base type.
createTBAAStructTagNode(MDNode * BaseType,MDNode * AccessType,uint64_t Offset)160 MDNode *MDBuilder::createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType,
161                                            uint64_t Offset) {
162   Type *Int64 = Type::getInt64Ty(Context);
163   Metadata *Ops[3] = {BaseType, AccessType,
164                       createConstant(ConstantInt::get(Int64, Offset))};
165   return MDNode::get(Context, Ops);
166 }
167