LLVM API Documentation

MDBuilder.h
Go to the documentation of this file.
00001 //===---- llvm/MDBuilder.h - Builder for LLVM metadata ----------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines the MDBuilder class, which is used as a convenient way to
00011 // create LLVM metadata with a consistent and simplified interface.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_IR_MDBUILDER_H
00016 #define LLVM_IR_MDBUILDER_H
00017 
00018 #include "llvm/ADT/StringRef.h"
00019 #include "llvm/Support/DataTypes.h"
00020 #include <utility>
00021 
00022 namespace llvm {
00023 
00024 class APInt;
00025 template <typename T> class ArrayRef;
00026 class LLVMContext;
00027 class MDNode;
00028 class MDString;
00029 
00030 class MDBuilder {
00031   LLVMContext &Context;
00032 
00033 public:
00034   MDBuilder(LLVMContext &context) : Context(context) {}
00035 
00036   /// \brief Return the given string as metadata.
00037   MDString *createString(StringRef Str);
00038 
00039   //===------------------------------------------------------------------===//
00040   // FPMath metadata.
00041   //===------------------------------------------------------------------===//
00042 
00043   /// \brief Return metadata with the given settings.  The special value 0.0
00044   /// for the Accuracy parameter indicates the default (maximal precision)
00045   /// setting.
00046   MDNode *createFPMath(float Accuracy);
00047 
00048   //===------------------------------------------------------------------===//
00049   // Prof metadata.
00050   //===------------------------------------------------------------------===//
00051 
00052   /// \brief Return metadata containing two branch weights.
00053   MDNode *createBranchWeights(uint32_t TrueWeight, uint32_t FalseWeight);
00054 
00055   /// \brief Return metadata containing a number of branch weights.
00056   MDNode *createBranchWeights(ArrayRef<uint32_t> Weights);
00057 
00058   //===------------------------------------------------------------------===//
00059   // Range metadata.
00060   //===------------------------------------------------------------------===//
00061 
00062   /// \brief Return metadata describing the range [Lo, Hi).
00063   MDNode *createRange(const APInt &Lo, const APInt &Hi);
00064 
00065   //===------------------------------------------------------------------===//
00066   // AA metadata.
00067   //===------------------------------------------------------------------===//
00068 
00069 protected:
00070   /// \brief Return metadata appropriate for a AA root node (scope or TBAA).
00071   /// Each returned node is distinct from all other metadata and will never
00072   /// be identified (uniqued) with anything else.
00073   MDNode *createAnonymousAARoot(StringRef Name = StringRef(),
00074                                 MDNode *Extra = nullptr);
00075 
00076 public:
00077   /// \brief Return metadata appropriate for a TBAA root node. Each returned
00078   /// node is distinct from all other metadata and will never be identified
00079   /// (uniqued) with anything else.
00080   MDNode *createAnonymousTBAARoot() {
00081     return createAnonymousAARoot();
00082   }
00083 
00084   /// \brief Return metadata appropriate for an alias scope domain node.
00085   /// Each returned node is distinct from all other metadata and will never
00086   /// be identified (uniqued) with anything else.
00087   MDNode *createAnonymousAliasScopeDomain(StringRef Name = StringRef()) {
00088     return createAnonymousAARoot(Name);
00089   }
00090 
00091   /// \brief Return metadata appropriate for an alias scope root node.
00092   /// Each returned node is distinct from all other metadata and will never
00093   /// be identified (uniqued) with anything else.
00094   MDNode *createAnonymousAliasScope(MDNode *Domain,
00095                                     StringRef Name = StringRef()) {
00096     return createAnonymousAARoot(Name, Domain);
00097   }
00098 
00099   /// \brief Return metadata appropriate for a TBAA root node with the given
00100   /// name.  This may be identified (uniqued) with other roots with the same
00101   /// name.
00102   MDNode *createTBAARoot(StringRef Name);
00103 
00104   /// \brief Return metadata appropriate for an alias scope domain node with
00105   /// the given name. This may be identified (uniqued) with other roots with
00106   /// the same name.
00107   MDNode *createAliasScopeDomain(StringRef Name);
00108 
00109   /// \brief Return metadata appropriate for an alias scope node with
00110   /// the given name. This may be identified (uniqued) with other scopes with
00111   /// the same name and domain.
00112   MDNode *createAliasScope(StringRef Name, MDNode *Domain);
00113 
00114   /// \brief Return metadata for a non-root TBAA node with the given name,
00115   /// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
00116   MDNode *createTBAANode(StringRef Name, MDNode *Parent,
00117                          bool isConstant = false);
00118 
00119   struct TBAAStructField {
00120     uint64_t Offset;
00121     uint64_t Size;
00122     MDNode *TBAA;
00123     TBAAStructField(uint64_t Offset, uint64_t Size, MDNode *TBAA) :
00124       Offset(Offset), Size(Size), TBAA(TBAA) {}
00125   };
00126 
00127   /// \brief Return metadata for a tbaa.struct node with the given
00128   /// struct field descriptions.
00129   MDNode *createTBAAStructNode(ArrayRef<TBAAStructField> Fields);
00130 
00131   /// \brief Return metadata for a TBAA struct node in the type DAG
00132   /// with the given name, a list of pairs (offset, field type in the type DAG).
00133   MDNode *
00134   createTBAAStructTypeNode(StringRef Name,
00135                            ArrayRef<std::pair<MDNode *, uint64_t>> Fields);
00136 
00137   /// \brief Return metadata for a TBAA scalar type node with the
00138   /// given name, an offset and a parent in the TBAA type DAG.
00139   MDNode *createTBAAScalarTypeNode(StringRef Name, MDNode *Parent,
00140                                    uint64_t Offset = 0);
00141 
00142   /// \brief Return metadata for a TBAA tag node with the given
00143   /// base type, access type and offset relative to the base type.
00144   MDNode *createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType,
00145                                   uint64_t Offset);
00146 };
00147 
00148 } // end namespace llvm
00149 
00150 #endif