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/ADT/StringRef.h"
19#include "llvm/Support/DataTypes.h"
20#include <utility>
21
22namespace llvm {
23
24class APInt;
25template <typename T> class ArrayRef;
26class LLVMContext;
27class Constant;
28class ConstantAsMetadata;
29class MDNode;
30class MDString;
31
32class MDBuilder {
33  LLVMContext &Context;
34
35public:
36  MDBuilder(LLVMContext &context) : Context(context) {}
37
38  /// \brief Return the given string as metadata.
39  MDString *createString(StringRef Str);
40
41  /// \brief Return the given constant as metadata.
42  ConstantAsMetadata *createConstant(Constant *C);
43
44  //===------------------------------------------------------------------===//
45  // FPMath metadata.
46  //===------------------------------------------------------------------===//
47
48  /// \brief Return metadata with the given settings.  The special value 0.0
49  /// for the Accuracy parameter indicates the default (maximal precision)
50  /// setting.
51  MDNode *createFPMath(float Accuracy);
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
60  /// \brief Return metadata containing a number of branch weights.
61  MDNode *createBranchWeights(ArrayRef<uint32_t> Weights);
62
63  //===------------------------------------------------------------------===//
64  // Range metadata.
65  //===------------------------------------------------------------------===//
66
67  /// \brief Return metadata describing the range [Lo, Hi).
68  MDNode *createRange(const APInt &Lo, const APInt &Hi);
69
70  /// \brief Return metadata describing the range [Lo, Hi).
71  MDNode *createRange(Constant *Lo, Constant *Hi);
72
73  //===------------------------------------------------------------------===//
74  // AA metadata.
75  //===------------------------------------------------------------------===//
76
77protected:
78  /// \brief Return metadata appropriate for a AA root node (scope or TBAA).
79  /// Each returned node is distinct from all other metadata and will never
80  /// be identified (uniqued) with anything else.
81  MDNode *createAnonymousAARoot(StringRef Name = StringRef(),
82                                MDNode *Extra = nullptr);
83
84public:
85  /// \brief Return metadata appropriate for a TBAA root node. Each returned
86  /// node is distinct from all other metadata and will never be identified
87  /// (uniqued) with anything else.
88  MDNode *createAnonymousTBAARoot() {
89    return createAnonymousAARoot();
90  }
91
92  /// \brief Return metadata appropriate for an alias scope domain node.
93  /// Each returned node is distinct from all other metadata and will never
94  /// be identified (uniqued) with anything else.
95  MDNode *createAnonymousAliasScopeDomain(StringRef Name = StringRef()) {
96    return createAnonymousAARoot(Name);
97  }
98
99  /// \brief Return metadata appropriate for an alias scope root node.
100  /// Each returned node is distinct from all other metadata and will never
101  /// be identified (uniqued) with anything else.
102  MDNode *createAnonymousAliasScope(MDNode *Domain,
103                                    StringRef Name = StringRef()) {
104    return createAnonymousAARoot(Name, Domain);
105  }
106
107  /// \brief Return metadata appropriate for a TBAA root node with the given
108  /// name.  This may be identified (uniqued) with other roots with the same
109  /// name.
110  MDNode *createTBAARoot(StringRef Name);
111
112  /// \brief Return metadata appropriate for an alias scope domain node with
113  /// the given name. This may be identified (uniqued) with other roots with
114  /// the same name.
115  MDNode *createAliasScopeDomain(StringRef Name);
116
117  /// \brief Return metadata appropriate for an alias scope node with
118  /// the given name. This may be identified (uniqued) with other scopes with
119  /// the same name and domain.
120  MDNode *createAliasScope(StringRef Name, MDNode *Domain);
121
122  /// \brief Return metadata for a non-root TBAA node with the given name,
123  /// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
124  MDNode *createTBAANode(StringRef Name, MDNode *Parent,
125                         bool isConstant = false);
126
127  struct TBAAStructField {
128    uint64_t Offset;
129    uint64_t Size;
130    MDNode *TBAA;
131    TBAAStructField(uint64_t Offset, uint64_t Size, MDNode *TBAA) :
132      Offset(Offset), Size(Size), TBAA(TBAA) {}
133  };
134
135  /// \brief Return metadata for a tbaa.struct node with the given
136  /// struct field descriptions.
137  MDNode *createTBAAStructNode(ArrayRef<TBAAStructField> Fields);
138
139  /// \brief Return metadata for a TBAA struct node in the type DAG
140  /// with the given name, a list of pairs (offset, field type in the type DAG).
141  MDNode *
142  createTBAAStructTypeNode(StringRef Name,
143                           ArrayRef<std::pair<MDNode *, uint64_t>> Fields);
144
145  /// \brief Return metadata for a TBAA scalar type node with the
146  /// given name, an offset and a parent in the TBAA type DAG.
147  MDNode *createTBAAScalarTypeNode(StringRef Name, MDNode *Parent,
148                                   uint64_t Offset = 0);
149
150  /// \brief Return metadata for a TBAA tag node with the given
151  /// base type, access type and offset relative to the base type.
152  MDNode *createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType,
153                                  uint64_t Offset);
154};
155
156} // end namespace llvm
157
158#endif
159