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