Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:07

0001 //===---- llvm/MDBuilder.h - Builder for LLVM metadata ----------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file defines the MDBuilder class, which is used as a convenient way to
0010 // create LLVM metadata with a consistent and simplified interface.
0011 //
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_IR_MDBUILDER_H
0015 #define LLVM_IR_MDBUILDER_H
0016 
0017 #include "llvm/ADT/DenseSet.h"
0018 #include "llvm/ADT/SmallVector.h"
0019 #include "llvm/ADT/StringRef.h"
0020 #include "llvm/IR/GlobalValue.h"
0021 #include "llvm/Support/DataTypes.h"
0022 #include <utility>
0023 
0024 namespace llvm {
0025 
0026 class APInt;
0027 template <typename T> class ArrayRef;
0028 class LLVMContext;
0029 class Constant;
0030 class ConstantAsMetadata;
0031 class Function;
0032 class MDNode;
0033 class MDString;
0034 class Metadata;
0035 
0036 class MDBuilder {
0037   LLVMContext &Context;
0038 
0039 public:
0040   MDBuilder(LLVMContext &context) : Context(context) {}
0041 
0042   /// Return the given string as metadata.
0043   MDString *createString(StringRef Str);
0044 
0045   /// Return the given constant as metadata.
0046   ConstantAsMetadata *createConstant(Constant *C);
0047 
0048   //===------------------------------------------------------------------===//
0049   // FPMath metadata.
0050   //===------------------------------------------------------------------===//
0051 
0052   /// Return metadata with the given settings.  The special value 0.0
0053   /// for the Accuracy parameter indicates the default (maximal precision)
0054   /// setting.
0055   MDNode *createFPMath(float Accuracy);
0056 
0057   //===------------------------------------------------------------------===//
0058   // Prof metadata.
0059   //===------------------------------------------------------------------===//
0060 
0061   /// Return metadata containing two branch weights.
0062   /// @param TrueWeight the weight of the true branch
0063   /// @param FalseWeight the weight of the false branch
0064   /// @param Do these weights come from __builtin_expect*
0065   MDNode *createBranchWeights(uint32_t TrueWeight, uint32_t FalseWeight,
0066                               bool IsExpected = false);
0067 
0068   /// Return metadata containing two branch weights, with significant bias
0069   /// towards `true` destination.
0070   MDNode *createLikelyBranchWeights();
0071 
0072   /// Return metadata containing two branch weights, with significant bias
0073   /// towards `false` destination.
0074   MDNode *createUnlikelyBranchWeights();
0075 
0076   /// Return metadata containing a number of branch weights.
0077   /// @param Weights the weights of all the branches
0078   /// @param Do these weights come from __builtin_expect*
0079   MDNode *createBranchWeights(ArrayRef<uint32_t> Weights,
0080                               bool IsExpected = false);
0081 
0082   /// Return metadata specifying that a branch or switch is unpredictable.
0083   MDNode *createUnpredictable();
0084 
0085   /// Return metadata containing the entry \p Count for a function, a boolean
0086   /// \Synthetic indicating whether the counts were synthetized, and the
0087   /// GUIDs stored in \p Imports that need to be imported for sample PGO, to
0088   /// enable the same inlines as the profiled optimized binary
0089   MDNode *createFunctionEntryCount(uint64_t Count, bool Synthetic,
0090                                    const DenseSet<GlobalValue::GUID> *Imports);
0091 
0092   /// Return metadata containing the section prefix for a function.
0093   MDNode *createFunctionSectionPrefix(StringRef Prefix);
0094 
0095   /// Return metadata containing the pseudo probe descriptor for a function.
0096   MDNode *createPseudoProbeDesc(uint64_t GUID, uint64_t Hash, StringRef FName);
0097 
0098   /// Return metadata containing llvm statistics.
0099   MDNode *
0100   createLLVMStats(ArrayRef<std::pair<StringRef, uint64_t>> LLVMStatsVec);
0101 
0102   //===------------------------------------------------------------------===//
0103   // Range metadata.
0104   //===------------------------------------------------------------------===//
0105 
0106   /// Return metadata describing the range [Lo, Hi).
0107   MDNode *createRange(const APInt &Lo, const APInt &Hi);
0108 
0109   /// Return metadata describing the range [Lo, Hi).
0110   MDNode *createRange(Constant *Lo, Constant *Hi);
0111 
0112   //===------------------------------------------------------------------===//
0113   // Callees metadata.
0114   //===------------------------------------------------------------------===//
0115 
0116   /// Return metadata indicating the possible callees of indirect
0117   /// calls.
0118   MDNode *createCallees(ArrayRef<Function *> Callees);
0119 
0120   //===------------------------------------------------------------------===//
0121   // Callback metadata.
0122   //===------------------------------------------------------------------===//
0123 
0124   /// Return metadata describing a callback (see llvm::AbstractCallSite).
0125   MDNode *createCallbackEncoding(unsigned CalleeArgNo, ArrayRef<int> Arguments,
0126                                  bool VarArgsArePassed);
0127 
0128   /// Merge the new callback encoding \p NewCB into \p ExistingCallbacks.
0129   MDNode *mergeCallbackEncodings(MDNode *ExistingCallbacks, MDNode *NewCB);
0130 
0131   /// Return metadata feeding to the CodeGen about how to generate a function
0132   /// prologue for the "function" santizier.
0133   MDNode *createRTTIPointerPrologue(Constant *PrologueSig, Constant *RTTI);
0134 
0135   //===------------------------------------------------------------------===//
0136   // PC sections metadata.
0137   //===------------------------------------------------------------------===//
0138 
0139   /// A pair of PC section name with auxilliary constant data.
0140   using PCSection = std::pair<StringRef, SmallVector<Constant *>>;
0141 
0142   /// Return metadata for PC sections.
0143   MDNode *createPCSections(ArrayRef<PCSection> Sections);
0144 
0145   //===------------------------------------------------------------------===//
0146   // AA metadata.
0147   //===------------------------------------------------------------------===//
0148 
0149 protected:
0150   /// Return metadata appropriate for a AA root node (scope or TBAA).
0151   /// Each returned node is distinct from all other metadata and will never
0152   /// be identified (uniqued) with anything else.
0153   MDNode *createAnonymousAARoot(StringRef Name = StringRef(),
0154                                 MDNode *Extra = nullptr);
0155 
0156 public:
0157   /// Return metadata appropriate for a TBAA root node. Each returned
0158   /// node is distinct from all other metadata and will never be identified
0159   /// (uniqued) with anything else.
0160   MDNode *createAnonymousTBAARoot() {
0161     return createAnonymousAARoot();
0162   }
0163 
0164   /// Return metadata appropriate for an alias scope domain node.
0165   /// Each returned node is distinct from all other metadata and will never
0166   /// be identified (uniqued) with anything else.
0167   MDNode *createAnonymousAliasScopeDomain(StringRef Name = StringRef()) {
0168     return createAnonymousAARoot(Name);
0169   }
0170 
0171   /// Return metadata appropriate for an alias scope root node.
0172   /// Each returned node is distinct from all other metadata and will never
0173   /// be identified (uniqued) with anything else.
0174   MDNode *createAnonymousAliasScope(MDNode *Domain,
0175                                     StringRef Name = StringRef()) {
0176     return createAnonymousAARoot(Name, Domain);
0177   }
0178 
0179   /// Return metadata appropriate for a TBAA root node with the given
0180   /// name.  This may be identified (uniqued) with other roots with the same
0181   /// name.
0182   MDNode *createTBAARoot(StringRef Name);
0183 
0184   /// Return metadata appropriate for an alias scope domain node with
0185   /// the given name. This may be identified (uniqued) with other roots with
0186   /// the same name.
0187   MDNode *createAliasScopeDomain(StringRef Name);
0188 
0189   /// Return metadata appropriate for an alias scope node with
0190   /// the given name. This may be identified (uniqued) with other scopes with
0191   /// the same name and domain.
0192   MDNode *createAliasScope(StringRef Name, MDNode *Domain);
0193 
0194   /// Return metadata for a non-root TBAA node with the given name,
0195   /// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
0196   MDNode *createTBAANode(StringRef Name, MDNode *Parent,
0197                          bool isConstant = false);
0198 
0199   struct TBAAStructField {
0200     uint64_t Offset;
0201     uint64_t Size;
0202     MDNode *Type;
0203     TBAAStructField(uint64_t Offset, uint64_t Size, MDNode *Type) :
0204       Offset(Offset), Size(Size), Type(Type) {}
0205   };
0206 
0207   /// Return metadata for a tbaa.struct node with the given
0208   /// struct field descriptions.
0209   MDNode *createTBAAStructNode(ArrayRef<TBAAStructField> Fields);
0210 
0211   /// Return metadata for a TBAA struct node in the type DAG
0212   /// with the given name, a list of pairs (offset, field type in the type DAG).
0213   MDNode *
0214   createTBAAStructTypeNode(StringRef Name,
0215                            ArrayRef<std::pair<MDNode *, uint64_t>> Fields);
0216 
0217   /// Return metadata for a TBAA scalar type node with the
0218   /// given name, an offset and a parent in the TBAA type DAG.
0219   MDNode *createTBAAScalarTypeNode(StringRef Name, MDNode *Parent,
0220                                    uint64_t Offset = 0);
0221 
0222   /// Return metadata for a TBAA tag node with the given
0223   /// base type, access type and offset relative to the base type.
0224   MDNode *createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType,
0225                                   uint64_t Offset, bool IsConstant = false);
0226 
0227   /// Return metadata for a TBAA type node in the TBAA type DAG with the
0228   /// given parent type, size in bytes, type identifier and a list of fields.
0229   MDNode *createTBAATypeNode(MDNode *Parent, uint64_t Size, Metadata *Id,
0230                              ArrayRef<TBAAStructField> Fields =
0231                                  ArrayRef<TBAAStructField>());
0232 
0233   /// Return metadata for a TBAA access tag with the given base type,
0234   /// final access type, offset of the access relative to the base type, size of
0235   /// the access and flag indicating whether the accessed object can be
0236   /// considered immutable for the purposes of the TBAA analysis.
0237   MDNode *createTBAAAccessTag(MDNode *BaseType, MDNode *AccessType,
0238                               uint64_t Offset, uint64_t Size,
0239                               bool IsImmutable = false);
0240 
0241   /// Return mutable version of the given mutable or immutable TBAA
0242   /// access tag.
0243   MDNode *createMutableTBAAAccessTag(MDNode *Tag);
0244 
0245   /// Return metadata containing an irreducible loop header weight.
0246   MDNode *createIrrLoopHeaderWeight(uint64_t Weight);
0247 };
0248 
0249 } // end namespace llvm
0250 
0251 #endif