Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===---- llvm/MDBuilder.h - Builder for LLVM metadata ----------*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file defines the MDBuilder class, which is used as a convenient way to |
| 10 | // create LLVM metadata with a consistent and simplified interface. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef LLVM_IR_MDBUILDER_H |
| 15 | #define LLVM_IR_MDBUILDER_H |
| 16 | |
| 17 | #include "llvm/ADT/DenseSet.h" |
| 18 | #include "llvm/ADT/StringRef.h" |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 19 | #include "llvm/IR/Constants.h" |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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 | class Metadata; |
| 34 | |
| 35 | class MDBuilder { |
| 36 | LLVMContext &Context; |
| 37 | |
| 38 | public: |
| 39 | MDBuilder(LLVMContext &context) : Context(context) {} |
| 40 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 41 | /// Return the given string as metadata. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 42 | MDString *createString(StringRef Str); |
| 43 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 44 | /// Return the given constant as metadata. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 45 | ConstantAsMetadata *createConstant(Constant *C); |
| 46 | |
| 47 | //===------------------------------------------------------------------===// |
| 48 | // FPMath metadata. |
| 49 | //===------------------------------------------------------------------===// |
| 50 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 51 | /// Return metadata with the given settings. The special value 0.0 |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 52 | /// for the Accuracy parameter indicates the default (maximal precision) |
| 53 | /// setting. |
| 54 | MDNode *createFPMath(float Accuracy); |
| 55 | |
| 56 | //===------------------------------------------------------------------===// |
| 57 | // Prof metadata. |
| 58 | //===------------------------------------------------------------------===// |
| 59 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 60 | /// Return metadata containing two branch weights. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 61 | MDNode *createBranchWeights(uint32_t TrueWeight, uint32_t FalseWeight); |
| 62 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 63 | /// Return metadata containing a number of branch weights. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 64 | MDNode *createBranchWeights(ArrayRef<uint32_t> Weights); |
| 65 | |
| 66 | /// Return metadata specifying that a branch or switch is unpredictable. |
| 67 | MDNode *createUnpredictable(); |
| 68 | |
| 69 | /// Return metadata containing the entry \p Count for a function, a boolean |
| 70 | /// \Synthetic indicating whether the counts were synthetized, and the |
| 71 | /// GUIDs stored in \p Imports that need to be imported for sample PGO, to |
| 72 | /// enable the same inlines as the profiled optimized binary |
| 73 | MDNode *createFunctionEntryCount(uint64_t Count, bool Synthetic, |
| 74 | const DenseSet<GlobalValue::GUID> *Imports); |
| 75 | |
| 76 | /// Return metadata containing the section prefix for a function. |
| 77 | MDNode *createFunctionSectionPrefix(StringRef Prefix); |
| 78 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 79 | /// Return metadata containing the pseudo probe descriptor for a function. |
| 80 | MDNode *createPseudoProbeDesc(uint64_t GUID, uint64_t Hash, Function *F); |
| 81 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 82 | //===------------------------------------------------------------------===// |
| 83 | // Range metadata. |
| 84 | //===------------------------------------------------------------------===// |
| 85 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 86 | /// Return metadata describing the range [Lo, Hi). |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 87 | MDNode *createRange(const APInt &Lo, const APInt &Hi); |
| 88 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 89 | /// Return metadata describing the range [Lo, Hi). |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 90 | MDNode *createRange(Constant *Lo, Constant *Hi); |
| 91 | |
| 92 | //===------------------------------------------------------------------===// |
| 93 | // Callees metadata. |
| 94 | //===------------------------------------------------------------------===// |
| 95 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 96 | /// Return metadata indicating the possible callees of indirect |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 97 | /// calls. |
| 98 | MDNode *createCallees(ArrayRef<Function *> Callees); |
| 99 | |
| 100 | //===------------------------------------------------------------------===// |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 101 | // Callback metadata. |
| 102 | //===------------------------------------------------------------------===// |
| 103 | |
| 104 | /// Return metadata describing a callback (see llvm::AbstractCallSite). |
| 105 | MDNode *createCallbackEncoding(unsigned CalleeArgNo, ArrayRef<int> Arguments, |
| 106 | bool VarArgsArePassed); |
| 107 | |
| 108 | /// Merge the new callback encoding \p NewCB into \p ExistingCallbacks. |
| 109 | MDNode *mergeCallbackEncodings(MDNode *ExistingCallbacks, MDNode *NewCB); |
| 110 | |
| 111 | //===------------------------------------------------------------------===// |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 112 | // AA metadata. |
| 113 | //===------------------------------------------------------------------===// |
| 114 | |
| 115 | protected: |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 116 | /// Return metadata appropriate for a AA root node (scope or TBAA). |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 117 | /// Each returned node is distinct from all other metadata and will never |
| 118 | /// be identified (uniqued) with anything else. |
| 119 | MDNode *createAnonymousAARoot(StringRef Name = StringRef(), |
| 120 | MDNode *Extra = nullptr); |
| 121 | |
| 122 | public: |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 123 | /// Return metadata appropriate for a TBAA root node. Each returned |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 124 | /// node is distinct from all other metadata and will never be identified |
| 125 | /// (uniqued) with anything else. |
| 126 | MDNode *createAnonymousTBAARoot() { |
| 127 | return createAnonymousAARoot(); |
| 128 | } |
| 129 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 130 | /// Return metadata appropriate for an alias scope domain node. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 131 | /// Each returned node is distinct from all other metadata and will never |
| 132 | /// be identified (uniqued) with anything else. |
| 133 | MDNode *createAnonymousAliasScopeDomain(StringRef Name = StringRef()) { |
| 134 | return createAnonymousAARoot(Name); |
| 135 | } |
| 136 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 137 | /// Return metadata appropriate for an alias scope root node. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 138 | /// Each returned node is distinct from all other metadata and will never |
| 139 | /// be identified (uniqued) with anything else. |
| 140 | MDNode *createAnonymousAliasScope(MDNode *Domain, |
| 141 | StringRef Name = StringRef()) { |
| 142 | return createAnonymousAARoot(Name, Domain); |
| 143 | } |
| 144 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 145 | /// Return metadata appropriate for a TBAA root node with the given |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 146 | /// name. This may be identified (uniqued) with other roots with the same |
| 147 | /// name. |
| 148 | MDNode *createTBAARoot(StringRef Name); |
| 149 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 150 | /// Return metadata appropriate for an alias scope domain node with |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 151 | /// the given name. This may be identified (uniqued) with other roots with |
| 152 | /// the same name. |
| 153 | MDNode *createAliasScopeDomain(StringRef Name); |
| 154 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 155 | /// Return metadata appropriate for an alias scope node with |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 156 | /// the given name. This may be identified (uniqued) with other scopes with |
| 157 | /// the same name and domain. |
| 158 | MDNode *createAliasScope(StringRef Name, MDNode *Domain); |
| 159 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 160 | /// Return metadata for a non-root TBAA node with the given name, |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 161 | /// parent in the TBAA tree, and value for 'pointsToConstantMemory'. |
| 162 | MDNode *createTBAANode(StringRef Name, MDNode *Parent, |
| 163 | bool isConstant = false); |
| 164 | |
| 165 | struct TBAAStructField { |
| 166 | uint64_t Offset; |
| 167 | uint64_t Size; |
| 168 | MDNode *Type; |
| 169 | TBAAStructField(uint64_t Offset, uint64_t Size, MDNode *Type) : |
| 170 | Offset(Offset), Size(Size), Type(Type) {} |
| 171 | }; |
| 172 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 173 | /// Return metadata for a tbaa.struct node with the given |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 174 | /// struct field descriptions. |
| 175 | MDNode *createTBAAStructNode(ArrayRef<TBAAStructField> Fields); |
| 176 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 177 | /// Return metadata for a TBAA struct node in the type DAG |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 178 | /// with the given name, a list of pairs (offset, field type in the type DAG). |
| 179 | MDNode * |
| 180 | createTBAAStructTypeNode(StringRef Name, |
| 181 | ArrayRef<std::pair<MDNode *, uint64_t>> Fields); |
| 182 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 183 | /// Return metadata for a TBAA scalar type node with the |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 184 | /// given name, an offset and a parent in the TBAA type DAG. |
| 185 | MDNode *createTBAAScalarTypeNode(StringRef Name, MDNode *Parent, |
| 186 | uint64_t Offset = 0); |
| 187 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 188 | /// Return metadata for a TBAA tag node with the given |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 189 | /// base type, access type and offset relative to the base type. |
| 190 | MDNode *createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType, |
| 191 | uint64_t Offset, bool IsConstant = false); |
| 192 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 193 | /// Return metadata for a TBAA type node in the TBAA type DAG with the |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 194 | /// given parent type, size in bytes, type identifier and a list of fields. |
| 195 | MDNode *createTBAATypeNode(MDNode *Parent, uint64_t Size, Metadata *Id, |
| 196 | ArrayRef<TBAAStructField> Fields = |
| 197 | ArrayRef<TBAAStructField>()); |
| 198 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 199 | /// Return metadata for a TBAA access tag with the given base type, |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 200 | /// final access type, offset of the access relative to the base type, size of |
| 201 | /// the access and flag indicating whether the accessed object can be |
| 202 | /// considered immutable for the purposes of the TBAA analysis. |
| 203 | MDNode *createTBAAAccessTag(MDNode *BaseType, MDNode *AccessType, |
| 204 | uint64_t Offset, uint64_t Size, |
| 205 | bool IsImmutable = false); |
| 206 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 207 | /// Return mutable version of the given mutable or immutable TBAA |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 208 | /// access tag. |
| 209 | MDNode *createMutableTBAAAccessTag(MDNode *Tag); |
| 210 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 211 | /// Return metadata containing an irreducible loop header weight. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 212 | MDNode *createIrrLoopHeaderWeight(uint64_t Weight); |
| 213 | }; |
| 214 | |
| 215 | } // end namespace llvm |
| 216 | |
| 217 | #endif |