Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
5.9 KiB

  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. #ifndef LLVM_MDBUILDER_H
  15. #define LLVM_MDBUILDER_H
  16. #include "llvm/Constants.h"
  17. #include "llvm/DerivedTypes.h"
  18. #include "llvm/LLVMContext.h"
  19. #include "llvm/Metadata.h"
  20. #include "llvm/ADT/APInt.h"
  21. namespace llvm {
  22. class MDBuilder {
  23. LLVMContext &Context;
  24. public:
  25. MDBuilder(LLVMContext &context) : Context(context) {}
  26. /// \brief Return the given string as metadata.
  27. MDString *createString(StringRef Str) {
  28. return MDString::get(Context, Str);
  29. }
  30. //===------------------------------------------------------------------===//
  31. // FPMath metadata.
  32. //===------------------------------------------------------------------===//
  33. /// \brief Return metadata with the given settings. The special value 0.0
  34. /// for the Accuracy parameter indicates the default (maximal precision)
  35. /// setting.
  36. MDNode *createFPMath(float Accuracy) {
  37. if (Accuracy == 0.0)
  38. return 0;
  39. assert(Accuracy > 0.0 && "Invalid fpmath accuracy!");
  40. Value *Op = ConstantFP::get(Type::getFloatTy(Context), Accuracy);
  41. return MDNode::get(Context, Op);
  42. }
  43. //===------------------------------------------------------------------===//
  44. // Prof metadata.
  45. //===------------------------------------------------------------------===//
  46. /// \brief Return metadata containing two branch weights.
  47. MDNode *createBranchWeights(uint32_t TrueWeight, uint32_t FalseWeight) {
  48. uint32_t Weights[] = { TrueWeight, FalseWeight };
  49. return createBranchWeights(Weights);
  50. }
  51. /// \brief Return metadata containing a number of branch weights.
  52. MDNode *createBranchWeights(ArrayRef<uint32_t> Weights) {
  53. assert(Weights.size() >= 2 && "Need at least two branch weights!");
  54. SmallVector<Value *, 4> Vals(Weights.size()+1);
  55. Vals[0] = createString("branch_weights");
  56. Type *Int32Ty = Type::getInt32Ty(Context);
  57. for (unsigned i = 0, e = Weights.size(); i != e; ++i)
  58. Vals[i+1] = ConstantInt::get(Int32Ty, Weights[i]);
  59. return MDNode::get(Context, Vals);
  60. }
  61. //===------------------------------------------------------------------===//
  62. // Range metadata.
  63. //===------------------------------------------------------------------===//
  64. /// \brief Return metadata describing the range [Lo, Hi).
  65. MDNode *createRange(const APInt &Lo, const APInt &Hi) {
  66. assert(Lo.getBitWidth() == Hi.getBitWidth() && "Mismatched bitwidths!");
  67. // If the range is everything then it is useless.
  68. if (Hi == Lo)
  69. return 0;
  70. // Return the range [Lo, Hi).
  71. Type *Ty = IntegerType::get(Context, Lo.getBitWidth());
  72. Value *Range[2] = { ConstantInt::get(Ty, Lo), ConstantInt::get(Ty, Hi) };
  73. return MDNode::get(Context, Range);
  74. }
  75. //===------------------------------------------------------------------===//
  76. // TBAA metadata.
  77. //===------------------------------------------------------------------===//
  78. /// \brief Return metadata appropriate for a TBAA root node. Each returned
  79. /// node is distinct from all other metadata and will never be identified
  80. /// (uniqued) with anything else.
  81. MDNode *createAnonymousTBAARoot() {
  82. // To ensure uniqueness the root node is self-referential.
  83. MDNode *Dummy = MDNode::getTemporary(Context, ArrayRef<Value*>());
  84. MDNode *Root = MDNode::get(Context, Dummy);
  85. // At this point we have
  86. // !0 = metadata !{} <- dummy
  87. // !1 = metadata !{metadata !0} <- root
  88. // Replace the dummy operand with the root node itself and delete the dummy.
  89. Root->replaceOperandWith(0, Root);
  90. MDNode::deleteTemporary(Dummy);
  91. // We now have
  92. // !1 = metadata !{metadata !1} <- self-referential root
  93. return Root;
  94. }
  95. /// \brief Return metadata appropriate for a TBAA root node with the given
  96. /// name. This may be identified (uniqued) with other roots with the same
  97. /// name.
  98. MDNode *createTBAARoot(StringRef Name) {
  99. return MDNode::get(Context, createString(Name));
  100. }
  101. /// \brief Return metadata for a non-root TBAA node with the given name,
  102. /// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
  103. MDNode *createTBAANode(StringRef Name, MDNode *Parent,
  104. bool isConstant = false) {
  105. if (isConstant) {
  106. Constant *Flags = ConstantInt::get(Type::getInt64Ty(Context), 1);
  107. Value *Ops[3] = { createString(Name), Parent, Flags };
  108. return MDNode::get(Context, Ops);
  109. } else {
  110. Value *Ops[2] = { createString(Name), Parent };
  111. return MDNode::get(Context, Ops);
  112. }
  113. }
  114. struct TBAAStructField {
  115. uint64_t Offset;
  116. uint64_t Size;
  117. MDNode *TBAA;
  118. TBAAStructField(uint64_t Offset, uint64_t Size, MDNode *TBAA) :
  119. Offset(Offset), Size(Size), TBAA(TBAA) {}
  120. };
  121. /// \brief Return metadata for a tbaa.struct node with the given
  122. /// struct field descriptions.
  123. MDNode *createTBAAStructNode(ArrayRef<TBAAStructField> Fields) {
  124. SmallVector<Value *, 4> Vals(Fields.size() * 3);
  125. Type *Int64 = IntegerType::get(Context, 64);
  126. for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
  127. Vals[i * 3 + 0] = ConstantInt::get(Int64, Fields[i].Offset);
  128. Vals[i * 3 + 1] = ConstantInt::get(Int64, Fields[i].Size);
  129. Vals[i * 3 + 2] = Fields[i].TBAA;
  130. }
  131. return MDNode::get(Context, Vals);
  132. }
  133. };
  134. } // end namespace llvm
  135. #endif