LLVM  5.0.0
MDBuilder.h
Go to the documentation of this file.
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/DenseSet.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/IR/GlobalValue.h"
21 #include "llvm/Support/DataTypes.h"
22 #include <utility>
23 
24 namespace llvm {
25 
26 class APInt;
27 template <typename T> class ArrayRef;
28 class LLVMContext;
29 class Constant;
30 class ConstantAsMetadata;
31 class MDNode;
32 class MDString;
33 
34 class MDBuilder {
35  LLVMContext &Context;
36 
37 public:
38  MDBuilder(LLVMContext &context) : Context(context) {}
39 
40  /// \brief Return the given string as metadata.
42 
43  /// \brief Return the given constant as metadata.
45 
46  //===------------------------------------------------------------------===//
47  // FPMath metadata.
48  //===------------------------------------------------------------------===//
49 
50  /// \brief Return metadata with the given settings. The special value 0.0
51  /// for the Accuracy parameter indicates the default (maximal precision)
52  /// setting.
53  MDNode *createFPMath(float Accuracy);
54 
55  //===------------------------------------------------------------------===//
56  // Prof metadata.
57  //===------------------------------------------------------------------===//
58 
59  /// \brief Return metadata containing two branch weights.
60  MDNode *createBranchWeights(uint32_t TrueWeight, uint32_t FalseWeight);
61 
62  /// \brief Return metadata containing a number of branch weights.
64 
65  /// Return metadata specifying that a branch or switch is unpredictable.
67 
68  /// Return metadata containing the entry \p Count for a function, and the
69  /// GUIDs stored in \p Imports that need to be imported for sample PGO, to
70  /// enable the same inlines as the profiled optimized binary
71  MDNode *createFunctionEntryCount(uint64_t Count,
72  const DenseSet<GlobalValue::GUID> *Imports);
73 
74  /// Return metadata containing the section prefix for a function.
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 
84  /// \brief Return metadata describing the range [Lo, Hi).
86 
87  //===------------------------------------------------------------------===//
88  // AA metadata.
89  //===------------------------------------------------------------------===//
90 
91 protected:
92  /// \brief Return metadata appropriate for a AA root node (scope or TBAA).
93  /// Each returned node is distinct from all other metadata and will never
94  /// be identified (uniqued) with anything else.
96  MDNode *Extra = nullptr);
97 
98 public:
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.
103  return createAnonymousAARoot();
104  }
105 
106  /// \brief Return metadata appropriate for an alias scope domain node.
107  /// Each returned node is distinct from all other metadata and will never
108  /// be identified (uniqued) with anything else.
110  return createAnonymousAARoot(Name);
111  }
112 
113  /// \brief Return metadata appropriate for an alias scope root node.
114  /// Each returned node is distinct from all other metadata and will never
115  /// be identified (uniqued) with anything else.
117  StringRef Name = StringRef()) {
118  return createAnonymousAARoot(Name, Domain);
119  }
120 
121  /// \brief Return metadata appropriate for a TBAA root node with the given
122  /// name. This may be identified (uniqued) with other roots with the same
123  /// name.
125 
126  /// \brief Return metadata appropriate for an alias scope domain node with
127  /// the given name. This may be identified (uniqued) with other roots with
128  /// the same name.
130 
131  /// \brief Return metadata appropriate for an alias scope node with
132  /// the given name. This may be identified (uniqued) with other scopes with
133  /// the same name and domain.
135 
136  /// \brief Return metadata for a non-root TBAA node with the given name,
137  /// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
139  bool isConstant = false);
140 
142  uint64_t Offset;
143  uint64_t Size;
145  TBAAStructField(uint64_t Offset, uint64_t Size, MDNode *TBAA) :
146  Offset(Offset), Size(Size), TBAA(TBAA) {}
147  };
148 
149  /// \brief Return metadata for a tbaa.struct node with the given
150  /// struct field descriptions.
152 
153  /// \brief Return metadata for a TBAA struct node in the type DAG
154  /// with the given name, a list of pairs (offset, field type in the type DAG).
155  MDNode *
157  ArrayRef<std::pair<MDNode *, uint64_t>> Fields);
158 
159  /// \brief Return metadata for a TBAA scalar type node with the
160  /// given name, an offset and a parent in the TBAA type DAG.
162  uint64_t Offset = 0);
163 
164  /// \brief Return metadata for a TBAA tag node with the given
165  /// base type, access type and offset relative to the base type.
167  uint64_t Offset, bool IsConstant = false);
168 };
169 
170 } // end namespace llvm
171 
172 #endif
MDNode * createRange(const APInt &Lo, const APInt &Hi)
Return metadata describing the range [Lo, Hi).
Definition: MDBuilder.cpp:77
static bool isConstant(const MachineInstr &MI)
MDNode * createAnonymousAARoot(StringRef Name=StringRef(), MDNode *Extra=nullptr)
Return metadata appropriate for a AA root node (scope or TBAA).
Definition: MDBuilder.cpp:93
MDNode * createUnpredictable()
Return metadata specifying that a branch or switch is unpredictable.
Definition: MDBuilder.cpp:55
ConstantAsMetadata * createConstant(Constant *C)
Return the given constant as metadata.
Definition: MDBuilder.cpp:24
MDNode * createAnonymousTBAARoot()
Return metadata appropriate for a TBAA root node.
Definition: MDBuilder.h:102
MDNode * createFunctionEntryCount(uint64_t Count, const DenseSet< GlobalValue::GUID > *Imports)
Return metadata containing the entry Count for a function, and the GUIDs stored in Imports that need ...
Definition: MDBuilder.cpp:59
Implements a dense probed hash-table based set.
Definition: DenseSet.h:221
MDNode * createAnonymousAliasScope(MDNode *Domain, StringRef Name=StringRef())
Return metadata appropriate for an alias scope root node.
Definition: MDBuilder.h:116
MDBuilder(LLVMContext &context)
Definition: MDBuilder.h:38
TBAAStructField(uint64_t Offset, uint64_t Size, MDNode *TBAA)
Definition: MDBuilder.h:145
Metadata node.
Definition: Metadata.h:862
MDNode * createFPMath(float Accuracy)
Return metadata with the given settings.
Definition: MDBuilder.cpp:28
const char * Name
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
This is an important base class in LLVM.
Definition: Constant.h:42
MDNode * createAliasScopeDomain(StringRef Name)
Return metadata appropriate for an alias scope domain node with the given name.
Definition: MDBuilder.cpp:131
MDNode * createTBAAStructNode(ArrayRef< TBAAStructField > Fields)
Return metadata for a tbaa.struct node with the given struct field descriptions.
Definition: MDBuilder.cpp:141
MDNode * createBranchWeights(uint32_t TrueWeight, uint32_t FalseWeight)
Return metadata containing two branch weights.
Definition: MDBuilder.cpp:37
MDNode * createAnonymousAliasScopeDomain(StringRef Name=StringRef())
Return metadata appropriate for an alias scope domain node.
Definition: MDBuilder.h:109
MDNode * createTBAAScalarTypeNode(StringRef Name, MDNode *Parent, uint64_t Offset=0)
Return metadata for a TBAA scalar type node with the given name, an offset and a parent in the TBAA t...
Definition: MDBuilder.cpp:168
BaseType
A given derived pointer can have multiple base pointers through phi/selects.
Class for arbitrary precision integers.
Definition: APInt.h:69
MDNode * createTBAAStructTypeNode(StringRef Name, ArrayRef< std::pair< MDNode *, uint64_t >> Fields)
Return metadata for a TBAA struct node in the type DAG with the given name, a list of pairs (offset...
Definition: MDBuilder.cpp:154
#define C
Definition: LargeTest.cpp:25
MDString * createString(StringRef Str)
Return the given string as metadata.
Definition: MDBuilder.cpp:20
MDNode * createAliasScope(StringRef Name, MDNode *Domain)
Return metadata appropriate for an alias scope node with the given name.
Definition: MDBuilder.cpp:135
MDNode * createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType, uint64_t Offset, bool IsConstant=false)
Return metadata for a TBAA tag node with the given base type, access type and offset relative to the ...
Definition: MDBuilder.cpp:177
MDNode * createTBAANode(StringRef Name, MDNode *Parent, bool isConstant=false)
Return metadata for a non-root TBAA node with the given name, parent in the TBAA tree, and value for 'pointsToConstantMemory'.
Definition: MDBuilder.cpp:121
MDNode * createFunctionSectionPrefix(StringRef Prefix)
Return metadata containing the section prefix for a function.
Definition: MDBuilder.cpp:71
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
MDNode * createTBAARoot(StringRef Name)
Return metadata appropriate for a TBAA root node with the given name.
Definition: MDBuilder.cpp:115
A single uniqued string.
Definition: Metadata.h:602