LLVM  4.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/StringRef.h"
19 #include "llvm/Support/DataTypes.h"
20 #include <utility>
21 
22 namespace llvm {
23 
24 class APInt;
25 template <typename T> class ArrayRef;
26 class LLVMContext;
27 class Constant;
28 class ConstantAsMetadata;
29 class MDNode;
30 class MDString;
31 
32 class MDBuilder {
33  LLVMContext &Context;
34 
35 public:
36  MDBuilder(LLVMContext &context) : Context(context) {}
37 
38  /// \brief Return the given string as metadata.
40 
41  /// \brief Return the given constant as metadata.
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.
62 
63  /// Return metadata specifying that a branch or switch is unpredictable.
65 
66  /// Return metadata containing the entry count for a function.
67  MDNode *createFunctionEntryCount(uint64_t Count);
68 
69  /// Return metadata containing the section prefix for a function.
71 
72  //===------------------------------------------------------------------===//
73  // Range metadata.
74  //===------------------------------------------------------------------===//
75 
76  /// \brief Return metadata describing the range [Lo, Hi).
77  MDNode *createRange(const APInt &Lo, const APInt &Hi);
78 
79  /// \brief Return metadata describing the range [Lo, Hi).
81 
82  //===------------------------------------------------------------------===//
83  // AA metadata.
84  //===------------------------------------------------------------------===//
85 
86 protected:
87  /// \brief Return metadata appropriate for a AA root node (scope or TBAA).
88  /// Each returned node is distinct from all other metadata and will never
89  /// be identified (uniqued) with anything else.
91  MDNode *Extra = nullptr);
92 
93 public:
94  /// \brief Return metadata appropriate for a TBAA root node. Each returned
95  /// node is distinct from all other metadata and will never be identified
96  /// (uniqued) with anything else.
98  return createAnonymousAARoot();
99  }
100 
101  /// \brief Return metadata appropriate for an alias scope domain node.
102  /// Each returned node is distinct from all other metadata and will never
103  /// be identified (uniqued) with anything else.
105  return createAnonymousAARoot(Name);
106  }
107 
108  /// \brief Return metadata appropriate for an alias scope root node.
109  /// Each returned node is distinct from all other metadata and will never
110  /// be identified (uniqued) with anything else.
112  StringRef Name = StringRef()) {
113  return createAnonymousAARoot(Name, Domain);
114  }
115 
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
118  /// name.
120 
121  /// \brief Return metadata appropriate for an alias scope domain node with
122  /// the given name. This may be identified (uniqued) with other roots with
123  /// the same name.
125 
126  /// \brief Return metadata appropriate for an alias scope node with
127  /// the given name. This may be identified (uniqued) with other scopes with
128  /// the same name and domain.
130 
131  /// \brief Return metadata for a non-root TBAA node with the given name,
132  /// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
134  bool isConstant = false);
135 
137  uint64_t Offset;
138  uint64_t Size;
140  TBAAStructField(uint64_t Offset, uint64_t Size, MDNode *TBAA) :
141  Offset(Offset), Size(Size), TBAA(TBAA) {}
142  };
143 
144  /// \brief Return metadata for a tbaa.struct node with the given
145  /// struct field descriptions.
147 
148  /// \brief Return metadata for a TBAA struct node in the type DAG
149  /// with the given name, a list of pairs (offset, field type in the type DAG).
150  MDNode *
152  ArrayRef<std::pair<MDNode *, uint64_t>> Fields);
153 
154  /// \brief Return metadata for a TBAA scalar type node with the
155  /// given name, an offset and a parent in the TBAA type DAG.
157  uint64_t Offset = 0);
158 
159  /// \brief Return metadata for a TBAA tag node with the given
160  /// base type, access type and offset relative to the base type.
161  MDNode *createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType,
162  uint64_t Offset, bool IsConstant = false);
163 };
164 
165 } // end namespace llvm
166 
167 #endif
MDNode * createRange(const APInt &Lo, const APInt &Hi)
Return metadata describing the range [Lo, Hi).
Definition: MDBuilder.cpp:72
MDNode * createAnonymousAARoot(StringRef Name=StringRef(), MDNode *Extra=nullptr)
Return metadata appropriate for a AA root node (scope or TBAA).
Definition: MDBuilder.cpp:88
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:97
MDNode * createAnonymousAliasScope(MDNode *Domain, StringRef Name=StringRef())
Return metadata appropriate for an alias scope root node.
Definition: MDBuilder.h:111
MDBuilder(LLVMContext &context)
Definition: MDBuilder.h:36
TBAAStructField(uint64_t Offset, uint64_t Size, MDNode *TBAA)
Definition: MDBuilder.h:140
Metadata node.
Definition: Metadata.h:830
MDNode * createFPMath(float Accuracy)
Return metadata with the given settings.
Definition: MDBuilder.cpp:28
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:48
This is an important base class in LLVM.
Definition: Constant.h:42
uint32_t Offset
MDNode * createAliasScopeDomain(StringRef Name)
Return metadata appropriate for an alias scope domain node with the given name.
Definition: MDBuilder.cpp:126
MDNode * createFunctionEntryCount(uint64_t Count)
Return metadata containing the entry count for a function.
Definition: MDBuilder.cpp:59
MDNode * createTBAAStructNode(ArrayRef< TBAAStructField > Fields)
Return metadata for a tbaa.struct node with the given struct field descriptions.
Definition: MDBuilder.cpp:136
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:104
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:163
static GCRegistry::Add< ShadowStackGC > C("shadow-stack","Very portable GC for uncooperative code generators")
Class for arbitrary precision integers.
Definition: APInt.h:77
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:149
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:130
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:172
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:116
MDNode * createFunctionSectionPrefix(StringRef Prefix)
Return metadata containing the section prefix for a function.
Definition: MDBuilder.cpp:66
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
MDNode * createTBAARoot(StringRef Name)
Return metadata appropriate for a TBAA root node with the given name.
Definition: MDBuilder.cpp:110
A single uniqued string.
Definition: Metadata.h:586