LLVM API Documentation

MDBuilder.cpp
Go to the documentation of this file.
00001 //===---- llvm/MDBuilder.cpp - Builder for LLVM metadata ------------------===//
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 #include "llvm/IR/MDBuilder.h"
00016 #include "llvm/IR/Constants.h"
00017 #include "llvm/IR/Metadata.h"
00018 using namespace llvm;
00019 
00020 MDString *MDBuilder::createString(StringRef Str) {
00021   return MDString::get(Context, Str);
00022 }
00023 
00024 MDNode *MDBuilder::createFPMath(float Accuracy) {
00025   if (Accuracy == 0.0)
00026     return nullptr;
00027   assert(Accuracy > 0.0 && "Invalid fpmath accuracy!");
00028   Value *Op = ConstantFP::get(Type::getFloatTy(Context), Accuracy);
00029   return MDNode::get(Context, Op);
00030 }
00031 
00032 MDNode *MDBuilder::createBranchWeights(uint32_t TrueWeight,
00033                                        uint32_t FalseWeight) {
00034   uint32_t Weights[] = {TrueWeight, FalseWeight};
00035   return createBranchWeights(Weights);
00036 }
00037 
00038 MDNode *MDBuilder::createBranchWeights(ArrayRef<uint32_t> Weights) {
00039   assert(Weights.size() >= 2 && "Need at least two branch weights!");
00040 
00041   SmallVector<Value *, 4> Vals(Weights.size() + 1);
00042   Vals[0] = createString("branch_weights");
00043 
00044   Type *Int32Ty = Type::getInt32Ty(Context);
00045   for (unsigned i = 0, e = Weights.size(); i != e; ++i)
00046     Vals[i + 1] = ConstantInt::get(Int32Ty, Weights[i]);
00047 
00048   return MDNode::get(Context, Vals);
00049 }
00050 
00051 MDNode *MDBuilder::createRange(const APInt &Lo, const APInt &Hi) {
00052   assert(Lo.getBitWidth() == Hi.getBitWidth() && "Mismatched bitwidths!");
00053   // If the range is everything then it is useless.
00054   if (Hi == Lo)
00055     return nullptr;
00056 
00057   // Return the range [Lo, Hi).
00058   Type *Ty = IntegerType::get(Context, Lo.getBitWidth());
00059   Value *Range[2] = {ConstantInt::get(Ty, Lo), ConstantInt::get(Ty, Hi)};
00060   return MDNode::get(Context, Range);
00061 }
00062 
00063 MDNode *MDBuilder::createAnonymousAARoot(StringRef Name, MDNode *Extra) {
00064   // To ensure uniqueness the root node is self-referential.
00065   MDNode *Dummy = MDNode::getTemporary(Context, None);
00066 
00067   SmallVector<Value *, 3> Args(1, Dummy);
00068   if (Extra)
00069     Args.push_back(Extra);
00070   if (!Name.empty())
00071     Args.push_back(createString(Name));
00072   MDNode *Root = MDNode::get(Context, Args);
00073 
00074   // At this point we have
00075   //   !0 = metadata !{}            <- dummy
00076   //   !1 = metadata !{metadata !0} <- root
00077   // Replace the dummy operand with the root node itself and delete the dummy.
00078   Root->replaceOperandWith(0, Root);
00079   MDNode::deleteTemporary(Dummy);
00080   // We now have
00081   //   !1 = metadata !{metadata !1} <- self-referential root
00082   return Root;
00083 }
00084 
00085 MDNode *MDBuilder::createTBAARoot(StringRef Name) {
00086   return MDNode::get(Context, createString(Name));
00087 }
00088 
00089 /// \brief Return metadata for a non-root TBAA node with the given name,
00090 /// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
00091 MDNode *MDBuilder::createTBAANode(StringRef Name, MDNode *Parent,
00092                                   bool isConstant) {
00093   if (isConstant) {
00094     Constant *Flags = ConstantInt::get(Type::getInt64Ty(Context), 1);
00095     Value *Ops[3] = {createString(Name), Parent, Flags};
00096     return MDNode::get(Context, Ops);
00097   } else {
00098     Value *Ops[2] = {createString(Name), Parent};
00099     return MDNode::get(Context, Ops);
00100   }
00101 }
00102 
00103 MDNode *MDBuilder::createAliasScopeDomain(StringRef Name) {
00104   return MDNode::get(Context, createString(Name));
00105 }
00106 
00107 MDNode *MDBuilder::createAliasScope(StringRef Name, MDNode *Domain) {
00108   Value *Ops[2] = { createString(Name), Domain };
00109   return MDNode::get(Context, Ops);
00110 }
00111 
00112 /// \brief Return metadata for a tbaa.struct node with the given
00113 /// struct field descriptions.
00114 MDNode *MDBuilder::createTBAAStructNode(ArrayRef<TBAAStructField> Fields) {
00115   SmallVector<Value *, 4> Vals(Fields.size() * 3);
00116   Type *Int64 = Type::getInt64Ty(Context);
00117   for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
00118     Vals[i * 3 + 0] = ConstantInt::get(Int64, Fields[i].Offset);
00119     Vals[i * 3 + 1] = ConstantInt::get(Int64, Fields[i].Size);
00120     Vals[i * 3 + 2] = Fields[i].TBAA;
00121   }
00122   return MDNode::get(Context, Vals);
00123 }
00124 
00125 /// \brief Return metadata for a TBAA struct node in the type DAG
00126 /// with the given name, a list of pairs (offset, field type in the type DAG).
00127 MDNode *MDBuilder::createTBAAStructTypeNode(
00128     StringRef Name, ArrayRef<std::pair<MDNode *, uint64_t>> Fields) {
00129   SmallVector<Value *, 4> Ops(Fields.size() * 2 + 1);
00130   Type *Int64 = Type::getInt64Ty(Context);
00131   Ops[0] = createString(Name);
00132   for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
00133     Ops[i * 2 + 1] = Fields[i].first;
00134     Ops[i * 2 + 2] = ConstantInt::get(Int64, Fields[i].second);
00135   }
00136   return MDNode::get(Context, Ops);
00137 }
00138 
00139 /// \brief Return metadata for a TBAA scalar type node with the
00140 /// given name, an offset and a parent in the TBAA type DAG.
00141 MDNode *MDBuilder::createTBAAScalarTypeNode(StringRef Name, MDNode *Parent,
00142                                             uint64_t Offset) {
00143   ConstantInt *Off = ConstantInt::get(Type::getInt64Ty(Context), Offset);
00144   Value *Ops[3] = {createString(Name), Parent, Off};
00145   return MDNode::get(Context, Ops);
00146 }
00147 
00148 /// \brief Return metadata for a TBAA tag node with the given
00149 /// base type, access type and offset relative to the base type.
00150 MDNode *MDBuilder::createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType,
00151                                            uint64_t Offset) {
00152   Type *Int64 = Type::getInt64Ty(Context);
00153   Value *Ops[3] = {BaseType, AccessType, ConstantInt::get(Int64, Offset)};
00154   return MDNode::get(Context, Ops);
00155 }