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  /// Return metadata specifying that a branch or switch is unpredictable.
64  MDNode *createUnpredictable();
65
66  /// Return metadata containing the entry count for a function.
67  MDNode *createFunctionEntryCount(uint64_t Count);
68
69  //===------------------------------------------------------------------===//
70  // Range metadata.
71  //===------------------------------------------------------------------===//
72
73  /// \brief Return metadata describing the range [Lo, Hi).
74  MDNode *createRange(const APInt &Lo, const APInt &Hi);
75
76  /// \brief Return metadata describing the range [Lo, Hi).
77  MDNode *createRange(Constant *Lo, Constant *Hi);
78
79  //===------------------------------------------------------------------===//
80  // AA metadata.
81  //===------------------------------------------------------------------===//
82
83protected:
84  /// \brief Return metadata appropriate for a AA root node (scope or TBAA).
85  /// Each returned node is distinct from all other metadata and will never
86  /// be identified (uniqued) with anything else.
87  MDNode *createAnonymousAARoot(StringRef Name = StringRef(),
88                                MDNode *Extra = nullptr);
89
90public:
91  /// \brief Return metadata appropriate for a TBAA root node. Each returned
92  /// node is distinct from all other metadata and will never be identified
93  /// (uniqued) with anything else.
94  MDNode *createAnonymousTBAARoot() {
95    return createAnonymousAARoot();
96  }
97
98  /// \brief Return metadata appropriate for an alias scope domain node.
99  /// Each returned node is distinct from all other metadata and will never
100  /// be identified (uniqued) with anything else.
101  MDNode *createAnonymousAliasScopeDomain(StringRef Name = StringRef()) {
102    return createAnonymousAARoot(Name);
103  }
104
105  /// \brief Return metadata appropriate for an alias scope root node.
106  /// Each returned node is distinct from all other metadata and will never
107  /// be identified (uniqued) with anything else.
108  MDNode *createAnonymousAliasScope(MDNode *Domain,
109                                    StringRef Name = StringRef()) {
110    return createAnonymousAARoot(Name, Domain);
111  }
112
113  /// \brief Return metadata appropriate for a TBAA root node with the given
114  /// name.  This may be identified (uniqued) with other roots with the same
115  /// name.
116  MDNode *createTBAARoot(StringRef Name);
117
118  /// \brief Return metadata appropriate for an alias scope domain node with
119  /// the given name. This may be identified (uniqued) with other roots with
120  /// the same name.
121  MDNode *createAliasScopeDomain(StringRef Name);
122
123  /// \brief Return metadata appropriate for an alias scope node with
124  /// the given name. This may be identified (uniqued) with other scopes with
125  /// the same name and domain.
126  MDNode *createAliasScope(StringRef Name, MDNode *Domain);
127
128  /// \brief Return metadata for a non-root TBAA node with the given name,
129  /// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
130  MDNode *createTBAANode(StringRef Name, MDNode *Parent,
131                         bool isConstant = false);
132
133  struct TBAAStructField {
134    uint64_t Offset;
135    uint64_t Size;
136    MDNode *TBAA;
137    TBAAStructField(uint64_t Offset, uint64_t Size, MDNode *TBAA) :
138      Offset(Offset), Size(Size), TBAA(TBAA) {}
139  };
140
141  /// \brief Return metadata for a tbaa.struct node with the given
142  /// struct field descriptions.
143  MDNode *createTBAAStructNode(ArrayRef<TBAAStructField> Fields);
144
145  /// \brief Return metadata for a TBAA struct node in the type DAG
146  /// with the given name, a list of pairs (offset, field type in the type DAG).
147  MDNode *
148  createTBAAStructTypeNode(StringRef Name,
149                           ArrayRef<std::pair<MDNode *, uint64_t>> Fields);
150
151  /// \brief Return metadata for a TBAA scalar type node with the
152  /// given name, an offset and a parent in the TBAA type DAG.
153  MDNode *createTBAAScalarTypeNode(StringRef Name, MDNode *Parent,
154                                   uint64_t Offset = 0);
155
156  /// \brief Return metadata for a TBAA tag node with the given
157  /// base type, access type and offset relative to the base type.
158  MDNode *createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType,
159                                  uint64_t Offset, bool IsConstant = false);
160};
161
162} // end namespace llvm
163
164#endif
165