1c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot//===---- llvm/MDBuilder.h - Builder for LLVM metadata ----------*- C++ -*-===//
2c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot//
3c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot//                     The LLVM Compiler Infrastructure
4c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot//
5c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot// This file is distributed under the University of Illinois Open Source
6c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot// License. See LICENSE.TXT for details.
7c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot//
8c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot//===----------------------------------------------------------------------===//
9c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot//
10c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot// This file defines the MDBuilder class, which is used as a convenient way to
11c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot// create LLVM metadata with a consistent and simplified interface.
12c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot//
13c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot//===----------------------------------------------------------------------===//
14c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
15c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#ifndef LLVM_IR_MDBUILDER_H
16c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#define LLVM_IR_MDBUILDER_H
17c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
18c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#include "llvm/ADT/DenseSet.h"
19c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#include "llvm/ADT/StringRef.h"
20c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#include "llvm/IR/GlobalValue.h"
21c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#include "llvm/Support/DataTypes.h"
22c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#include <utility>
23c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
24c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotnamespace llvm {
25c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
26c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotclass APInt;
27c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robottemplate <typename T> class ArrayRef;
28c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotclass LLVMContext;
29c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotclass Constant;
30c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotclass ConstantAsMetadata;
31c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotclass MDNode;
32c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotclass MDString;
33c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
34c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotclass MDBuilder {
35c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  LLVMContext &Context;
36c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
37c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotpublic:
38c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  MDBuilder(LLVMContext &context) : Context(context) {}
39c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
40c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// \brief Return the given string as metadata.
41c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  MDString *createString(StringRef Str);
42c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
43c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// \brief Return the given constant as metadata.
44c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  ConstantAsMetadata *createConstant(Constant *C);
45c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
46c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  //===------------------------------------------------------------------===//
47c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  // FPMath metadata.
48c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  //===------------------------------------------------------------------===//
49c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
50c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// \brief Return metadata with the given settings.  The special value 0.0
51c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// for the Accuracy parameter indicates the default (maximal precision)
52c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// setting.
53c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  MDNode *createFPMath(float Accuracy);
54c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
55c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  //===------------------------------------------------------------------===//
56c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  // Prof metadata.
57c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  //===------------------------------------------------------------------===//
58c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
59c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// \brief Return metadata containing two branch weights.
60c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  MDNode *createBranchWeights(uint32_t TrueWeight, uint32_t FalseWeight);
61c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
62c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// \brief Return metadata containing a number of branch weights.
63c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  MDNode *createBranchWeights(ArrayRef<uint32_t> Weights);
64c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
65c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// Return metadata specifying that a branch or switch is unpredictable.
66c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  MDNode *createUnpredictable();
67c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
68c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// Return metadata containing the entry \p Count for a function, and the
69c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// GUIDs stored in \p Imports that need to be imported for sample PGO, to
70c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// enable the same inlines as the profiled optimized binary
71c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  MDNode *createFunctionEntryCount(uint64_t Count,
72c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot                                   const DenseSet<GlobalValue::GUID> *Imports);
73c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
74c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// Return metadata containing the section prefix for a function.
75c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  MDNode *createFunctionSectionPrefix(StringRef Prefix);
76c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
77c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  //===------------------------------------------------------------------===//
78c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  // Range metadata.
79c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  //===------------------------------------------------------------------===//
80c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
81c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// \brief Return metadata describing the range [Lo, Hi).
82c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  MDNode *createRange(const APInt &Lo, const APInt &Hi);
83c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
84c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// \brief Return metadata describing the range [Lo, Hi).
85c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  MDNode *createRange(Constant *Lo, Constant *Hi);
86c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
87c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  //===------------------------------------------------------------------===//
88c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  // AA metadata.
89c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  //===------------------------------------------------------------------===//
90c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
91c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotprotected:
92c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// \brief Return metadata appropriate for a AA root node (scope or TBAA).
93c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// Each returned node is distinct from all other metadata and will never
94c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// be identified (uniqued) with anything else.
95c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  MDNode *createAnonymousAARoot(StringRef Name = StringRef(),
96c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot                                MDNode *Extra = nullptr);
97c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
98c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotpublic:
99c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// \brief Return metadata appropriate for a TBAA root node. Each returned
100c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// node is distinct from all other metadata and will never be identified
101c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// (uniqued) with anything else.
102c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  MDNode *createAnonymousTBAARoot() {
103c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    return createAnonymousAARoot();
104c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  }
105c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
106c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// \brief Return metadata appropriate for an alias scope domain node.
107c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// Each returned node is distinct from all other metadata and will never
108c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// be identified (uniqued) with anything else.
109c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  MDNode *createAnonymousAliasScopeDomain(StringRef Name = StringRef()) {
110c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    return createAnonymousAARoot(Name);
111c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  }
112c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
113c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// \brief Return metadata appropriate for an alias scope root node.
114c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// Each returned node is distinct from all other metadata and will never
115c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// be identified (uniqued) with anything else.
116c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  MDNode *createAnonymousAliasScope(MDNode *Domain,
117c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot                                    StringRef Name = StringRef()) {
118c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    return createAnonymousAARoot(Name, Domain);
119c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  }
120c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
121c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// \brief Return metadata appropriate for a TBAA root node with the given
122c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// name.  This may be identified (uniqued) with other roots with the same
123c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// name.
124c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  MDNode *createTBAARoot(StringRef Name);
125c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
126c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// \brief Return metadata appropriate for an alias scope domain node with
127c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// the given name. This may be identified (uniqued) with other roots with
128c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// the same name.
129c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  MDNode *createAliasScopeDomain(StringRef Name);
130c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
131c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// \brief Return metadata appropriate for an alias scope node with
132c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// the given name. This may be identified (uniqued) with other scopes with
133c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// the same name and domain.
134c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  MDNode *createAliasScope(StringRef Name, MDNode *Domain);
135c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
136c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// \brief Return metadata for a non-root TBAA node with the given name,
137c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
138c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  MDNode *createTBAANode(StringRef Name, MDNode *Parent,
139c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot                         bool isConstant = false);
140c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
141c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  struct TBAAStructField {
142c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    uint64_t Offset;
143c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    uint64_t Size;
144c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    MDNode *TBAA;
145c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    TBAAStructField(uint64_t Offset, uint64_t Size, MDNode *TBAA) :
146c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot      Offset(Offset), Size(Size), TBAA(TBAA) {}
147c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  };
148c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
149c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// \brief Return metadata for a tbaa.struct node with the given
150c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// struct field descriptions.
151c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  MDNode *createTBAAStructNode(ArrayRef<TBAAStructField> Fields);
152c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
153c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// \brief Return metadata for a TBAA struct node in the type DAG
154c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// with the given name, a list of pairs (offset, field type in the type DAG).
155c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  MDNode *
156c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  createTBAAStructTypeNode(StringRef Name,
157c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot                           ArrayRef<std::pair<MDNode *, uint64_t>> Fields);
158c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
159c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// \brief Return metadata for a TBAA scalar type node with the
160c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// given name, an offset and a parent in the TBAA type DAG.
161c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  MDNode *createTBAAScalarTypeNode(StringRef Name, MDNode *Parent,
162c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot                                   uint64_t Offset = 0);
163c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
164c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// \brief Return metadata for a TBAA tag node with the given
165c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// base type, access type and offset relative to the base type.
166c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  MDNode *createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType,
167c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot                                  uint64_t Offset, bool IsConstant = false);
168c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot};
169c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
170c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot} // end namespace llvm
171c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
172c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#endif
173