LLVM API Documentation

MDBuilder.h
Go to the documentation of this file.
00001 //===---- llvm/MDBuilder.h - Builder for LLVM metadata ----------*- C++ -*-===//
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 #ifndef LLVM_IR_MDBUILDER_H
00016 #define LLVM_IR_MDBUILDER_H
00017 
00018 #include "llvm/IR/Constants.h"
00019 #include "llvm/IR/DerivedTypes.h"
00020 #include "llvm/IR/Metadata.h"
00021 
00022 namespace llvm {
00023 
00024 class APInt;
00025 class LLVMContext;
00026 
00027 class MDBuilder {
00028   LLVMContext &Context;
00029 
00030 public:
00031   MDBuilder(LLVMContext &context) : Context(context) {}
00032 
00033   /// \brief Return the given string as metadata.
00034   MDString *createString(StringRef Str) {
00035     return MDString::get(Context, Str);
00036   }
00037 
00038   //===------------------------------------------------------------------===//
00039   // FPMath metadata.
00040   //===------------------------------------------------------------------===//
00041 
00042   /// \brief Return metadata with the given settings.  The special value 0.0
00043   /// for the Accuracy parameter indicates the default (maximal precision)
00044   /// setting.
00045   MDNode *createFPMath(float Accuracy) {
00046     if (Accuracy == 0.0)
00047       return 0;
00048     assert(Accuracy > 0.0 && "Invalid fpmath accuracy!");
00049     Value *Op = ConstantFP::get(Type::getFloatTy(Context), Accuracy);
00050     return MDNode::get(Context, Op);
00051   }
00052 
00053   //===------------------------------------------------------------------===//
00054   // Prof metadata.
00055   //===------------------------------------------------------------------===//
00056 
00057   /// \brief Return metadata containing two branch weights.
00058   MDNode *createBranchWeights(uint32_t TrueWeight, uint32_t FalseWeight) {
00059     uint32_t Weights[] = { TrueWeight, FalseWeight };
00060     return createBranchWeights(Weights);
00061   }
00062 
00063   /// \brief Return metadata containing a number of branch weights.
00064   MDNode *createBranchWeights(ArrayRef<uint32_t> Weights) {
00065     assert(Weights.size() >= 2 && "Need at least two branch weights!");
00066 
00067     SmallVector<Value *, 4> Vals(Weights.size()+1);
00068     Vals[0] = createString("branch_weights");
00069 
00070     Type *Int32Ty = Type::getInt32Ty(Context);
00071     for (unsigned i = 0, e = Weights.size(); i != e; ++i)
00072       Vals[i+1] = ConstantInt::get(Int32Ty, Weights[i]);
00073 
00074     return MDNode::get(Context, Vals);
00075   }
00076 
00077   //===------------------------------------------------------------------===//
00078   // Range metadata.
00079   //===------------------------------------------------------------------===//
00080 
00081   /// \brief Return metadata describing the range [Lo, Hi).
00082   MDNode *createRange(const APInt &Lo, const APInt &Hi) {
00083     assert(Lo.getBitWidth() == Hi.getBitWidth() && "Mismatched bitwidths!");
00084     // If the range is everything then it is useless.
00085     if (Hi == Lo)
00086       return 0;
00087 
00088     // Return the range [Lo, Hi).
00089     Type *Ty = IntegerType::get(Context, Lo.getBitWidth());
00090     Value *Range[2] = { ConstantInt::get(Ty, Lo), ConstantInt::get(Ty, Hi) };
00091     return MDNode::get(Context, Range);
00092   }
00093 
00094 
00095   //===------------------------------------------------------------------===//
00096   // TBAA metadata.
00097   //===------------------------------------------------------------------===//
00098 
00099   /// \brief Return metadata appropriate for a TBAA root node.  Each returned
00100   /// node is distinct from all other metadata and will never be identified
00101   /// (uniqued) with anything else.
00102   MDNode *createAnonymousTBAARoot() {
00103     // To ensure uniqueness the root node is self-referential.
00104     MDNode *Dummy = MDNode::getTemporary(Context, ArrayRef<Value*>());
00105     MDNode *Root = MDNode::get(Context, Dummy);
00106     // At this point we have
00107     //   !0 = metadata !{}            <- dummy
00108     //   !1 = metadata !{metadata !0} <- root
00109     // Replace the dummy operand with the root node itself and delete the dummy.
00110     Root->replaceOperandWith(0, Root);
00111     MDNode::deleteTemporary(Dummy);
00112     // We now have
00113     //   !1 = metadata !{metadata !1} <- self-referential root
00114     return Root;
00115   }
00116 
00117   /// \brief Return metadata appropriate for a TBAA root node with the given
00118   /// name.  This may be identified (uniqued) with other roots with the same
00119   /// name.
00120   MDNode *createTBAARoot(StringRef Name) {
00121     return MDNode::get(Context, createString(Name));
00122   }
00123 
00124   /// \brief Return metadata for a non-root TBAA node with the given name,
00125   /// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
00126   MDNode *createTBAANode(StringRef Name, MDNode *Parent,
00127                          bool isConstant = false) {
00128     if (isConstant) {
00129       Constant *Flags = ConstantInt::get(Type::getInt64Ty(Context), 1);
00130       Value *Ops[3] = { createString(Name), Parent, Flags };
00131       return MDNode::get(Context, Ops);
00132     } else {
00133       Value *Ops[2] = { createString(Name), Parent };
00134       return MDNode::get(Context, Ops);
00135     }
00136   }
00137 
00138   struct TBAAStructField {
00139     uint64_t Offset;
00140     uint64_t Size;
00141     MDNode *TBAA;
00142     TBAAStructField(uint64_t Offset, uint64_t Size, MDNode *TBAA) :
00143       Offset(Offset), Size(Size), TBAA(TBAA) {}
00144   };
00145 
00146   /// \brief Return metadata for a tbaa.struct node with the given
00147   /// struct field descriptions.
00148   MDNode *createTBAAStructNode(ArrayRef<TBAAStructField> Fields) {
00149     SmallVector<Value *, 4> Vals(Fields.size() * 3);
00150     Type *Int64 = IntegerType::get(Context, 64);
00151     for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
00152       Vals[i * 3 + 0] = ConstantInt::get(Int64, Fields[i].Offset);
00153       Vals[i * 3 + 1] = ConstantInt::get(Int64, Fields[i].Size);
00154       Vals[i * 3 + 2] = Fields[i].TBAA;
00155     }
00156     return MDNode::get(Context, Vals);
00157   }
00158 
00159   /// \brief Return metadata for a TBAA struct node in the type DAG
00160   /// with the given name, a list of pairs (offset, field type in the type DAG).
00161   MDNode *createTBAAStructTypeNode(StringRef Name,
00162              ArrayRef<std::pair<MDNode*, uint64_t> > Fields) {
00163     SmallVector<Value *, 4> Ops(Fields.size() * 2 + 1);
00164     Type *Int64 = IntegerType::get(Context, 64);
00165     Ops[0] = createString(Name);
00166     for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
00167       Ops[i * 2 + 1] = Fields[i].first;
00168       Ops[i * 2 + 2] = ConstantInt::get(Int64, Fields[i].second);
00169     }
00170     return MDNode::get(Context, Ops);
00171   }
00172 
00173   /// \brief Return metadata for a TBAA scalar type node with the
00174   /// given name, an offset and a parent in the TBAA type DAG.
00175   MDNode *createTBAAScalarTypeNode(StringRef Name, MDNode *Parent,
00176                                    uint64_t Offset = 0) {
00177     SmallVector<Value *, 4> Ops(3);
00178     Type *Int64 = IntegerType::get(Context, 64);
00179     Ops[0] = createString(Name);
00180     Ops[1] = Parent;
00181     Ops[2] = ConstantInt::get(Int64, Offset);
00182     return MDNode::get(Context, Ops);
00183   }
00184 
00185   /// \brief Return metadata for a TBAA tag node with the given
00186   /// base type, access type and offset relative to the base type.
00187   MDNode *createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType,
00188                                   uint64_t Offset) {
00189     Type *Int64 = IntegerType::get(Context, 64);
00190     Value *Ops[3] = { BaseType, AccessType, ConstantInt::get(Int64, Offset) };
00191     return MDNode::get(Context, Ops);
00192   }
00193 
00194 };
00195 
00196 } // end namespace llvm
00197 
00198 #endif