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/Support/DataTypes.h"
19#include <utility>
20
21namespace llvm {
22
23class APInt;
24template <typename T> class ArrayRef;
25class LLVMContext;
26class MDNode;
27class MDString;
28class StringRef;
29
30class MDBuilder {
31  LLVMContext &Context;
32
33public:
34  MDBuilder(LLVMContext &context) : Context(context) {}
35
36  /// \brief Return the given string as metadata.
37  MDString *createString(StringRef Str);
38
39  //===------------------------------------------------------------------===//
40  // FPMath metadata.
41  //===------------------------------------------------------------------===//
42
43  /// \brief Return metadata with the given settings.  The special value 0.0
44  /// for the Accuracy parameter indicates the default (maximal precision)
45  /// setting.
46  MDNode *createFPMath(float Accuracy);
47
48  //===------------------------------------------------------------------===//
49  // Prof metadata.
50  //===------------------------------------------------------------------===//
51
52  /// \brief Return metadata containing two branch weights.
53  MDNode *createBranchWeights(uint32_t TrueWeight, uint32_t FalseWeight);
54
55  /// \brief Return metadata containing a number of branch weights.
56  MDNode *createBranchWeights(ArrayRef<uint32_t> Weights);
57
58  //===------------------------------------------------------------------===//
59  // Range metadata.
60  //===------------------------------------------------------------------===//
61
62  /// \brief Return metadata describing the range [Lo, Hi).
63  MDNode *createRange(const APInt &Lo, const APInt &Hi);
64
65  //===------------------------------------------------------------------===//
66  // TBAA metadata.
67  //===------------------------------------------------------------------===//
68
69  /// \brief Return metadata appropriate for a TBAA root node.  Each returned
70  /// node is distinct from all other metadata and will never be identified
71  /// (uniqued) with anything else.
72  MDNode *createAnonymousTBAARoot();
73
74  /// \brief Return metadata appropriate for a TBAA root node with the given
75  /// name.  This may be identified (uniqued) with other roots with the same
76  /// name.
77  MDNode *createTBAARoot(StringRef Name);
78
79  /// \brief Return metadata for a non-root TBAA node with the given name,
80  /// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
81  MDNode *createTBAANode(StringRef Name, MDNode *Parent,
82                         bool isConstant = false);
83
84  struct TBAAStructField {
85    uint64_t Offset;
86    uint64_t Size;
87    MDNode *TBAA;
88    TBAAStructField(uint64_t Offset, uint64_t Size, MDNode *TBAA) :
89      Offset(Offset), Size(Size), TBAA(TBAA) {}
90  };
91
92  /// \brief Return metadata for a tbaa.struct node with the given
93  /// struct field descriptions.
94  MDNode *createTBAAStructNode(ArrayRef<TBAAStructField> Fields);
95
96  /// \brief Return metadata for a TBAA struct node in the type DAG
97  /// with the given name, a list of pairs (offset, field type in the type DAG).
98  MDNode *
99  createTBAAStructTypeNode(StringRef Name,
100                           ArrayRef<std::pair<MDNode *, uint64_t>> Fields);
101
102  /// \brief Return metadata for a TBAA scalar type node with the
103  /// given name, an offset and a parent in the TBAA type DAG.
104  MDNode *createTBAAScalarTypeNode(StringRef Name, MDNode *Parent,
105                                   uint64_t Offset = 0);
106
107  /// \brief Return metadata for a TBAA tag node with the given
108  /// base type, access type and offset relative to the base type.
109  MDNode *createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType,
110                                  uint64_t Offset);
111};
112
113} // end namespace llvm
114
115#endif
116