LCOV - code coverage report
Current view: top level - lib/IR - MDBuilder.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 83 89 93.3 %
Date: 2017-09-14 15:23:50 Functions: 17 19 89.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===---- llvm/MDBuilder.cpp - Builder for LLVM metadata ------------------===//
       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             : #include "llvm/IR/MDBuilder.h"
      16             : #include "llvm/IR/Constants.h"
      17             : #include "llvm/IR/Metadata.h"
      18             : using namespace llvm;
      19             : 
      20       27845 : MDString *MDBuilder::createString(StringRef Str) {
      21       27845 :   return MDString::get(Context, Str);
      22             : }
      23             : 
      24       54919 : ConstantAsMetadata *MDBuilder::createConstant(Constant *C) {
      25       54919 :   return ConstantAsMetadata::get(C);
      26             : }
      27             : 
      28          17 : MDNode *MDBuilder::createFPMath(float Accuracy) {
      29          17 :   if (Accuracy == 0.0)
      30             :     return nullptr;
      31             :   assert(Accuracy > 0.0 && "Invalid fpmath accuracy!");
      32             :   auto *Op =
      33          16 :       createConstant(ConstantFP::get(Type::getFloatTy(Context), Accuracy));
      34          32 :   return MDNode::get(Context, Op);
      35             : }
      36             : 
      37        4267 : MDNode *MDBuilder::createBranchWeights(uint32_t TrueWeight,
      38             :                                        uint32_t FalseWeight) {
      39        8534 :   return createBranchWeights({TrueWeight, FalseWeight});
      40             : }
      41             : 
      42        4586 : MDNode *MDBuilder::createBranchWeights(ArrayRef<uint32_t> Weights) {
      43             :   assert(Weights.size() >= 1 && "Need at least one branch weights!");
      44             : 
      45       13758 :   SmallVector<Metadata *, 4> Vals(Weights.size() + 1);
      46        9172 :   Vals[0] = createString("branch_weights");
      47             : 
      48        4586 :   Type *Int32Ty = Type::getInt32Ty(Context);
      49       13894 :   for (unsigned i = 0, e = Weights.size(); i != e; ++i)
      50       27924 :     Vals[i + 1] = createConstant(ConstantInt::get(Int32Ty, Weights[i]));
      51             : 
      52       13758 :   return MDNode::get(Context, Vals);
      53             : }
      54             : 
      55           2 : MDNode *MDBuilder::createUnpredictable() {
      56           4 :   return MDNode::get(Context, None);
      57             : }
      58             : 
      59         348 : MDNode *MDBuilder::createFunctionEntryCount(
      60             :     uint64_t Count, const DenseSet<GlobalValue::GUID> *Imports) {
      61         348 :   Type *Int64Ty = Type::getInt64Ty(Context);
      62         696 :   SmallVector<Metadata *, 8> Ops;
      63         348 :   Ops.push_back(createString("function_entry_count"));
      64         348 :   Ops.push_back(createConstant(ConstantInt::get(Int64Ty, Count)));
      65         348 :   if (Imports) {
      66         345 :     SmallVector<GlobalValue::GUID, 2> OrderID(Imports->begin(), Imports->end());
      67         207 :     std::stable_sort(OrderID.begin(), OrderID.end(),
      68             :       [] (GlobalValue::GUID A, GlobalValue::GUID B) {
      69             :         return A < B;});
      70         213 :     for (auto ID : OrderID)
      71           6 :       Ops.push_back(createConstant(ConstantInt::get(Int64Ty, ID)));
      72             :   }
      73        1044 :   return MDNode::get(Context, Ops);
      74             : }
      75             : 
      76          15 : MDNode *MDBuilder::createFunctionSectionPrefix(StringRef Prefix) {
      77          60 :   return MDNode::get(Context,
      78          30 :                      {createString("function_section_prefix"),
      79          45 :                       createString(Prefix)});
      80             : }
      81             : 
      82        4104 : MDNode *MDBuilder::createRange(const APInt &Lo, const APInt &Hi) {
      83             :   assert(Lo.getBitWidth() == Hi.getBitWidth() && "Mismatched bitwidths!");
      84             : 
      85        4104 :   Type *Ty = IntegerType::get(Context, Lo.getBitWidth());
      86        4104 :   return createRange(ConstantInt::get(Ty, Lo), ConstantInt::get(Ty, Hi));
      87             : }
      88             : 
      89        4108 : MDNode *MDBuilder::createRange(Constant *Lo, Constant *Hi) {
      90             :   // If the range is everything then it is useless.
      91        4108 :   if (Hi == Lo)
      92             :     return nullptr;
      93             : 
      94             :   // Return the range [Lo, Hi).
      95       12312 :   return MDNode::get(Context, {createConstant(Lo), createConstant(Hi)});
      96             : }
      97             : 
      98        9206 : MDNode *MDBuilder::createAnonymousAARoot(StringRef Name, MDNode *Extra) {
      99             :   // To ensure uniqueness the root node is self-referential.
     100       27618 :   auto Dummy = MDNode::getTemporary(Context, None);
     101             : 
     102       27618 :   SmallVector<Metadata *, 3> Args(1, Dummy.get());
     103        9206 :   if (Extra)
     104        4843 :     Args.push_back(Extra);
     105        9206 :   if (!Name.empty())
     106        8892 :     Args.push_back(createString(Name));
     107       18412 :   MDNode *Root = MDNode::get(Context, Args);
     108             : 
     109             :   // At this point we have
     110             :   //   !0 = metadata !{}            <- dummy
     111             :   //   !1 = metadata !{metadata !0} <- root
     112             :   // Replace the dummy operand with the root node itself and delete the dummy.
     113        9206 :   Root->replaceOperandWith(0, Root);
     114             : 
     115             :   // We now have
     116             :   //   !1 = metadata !{metadata !1} <- self-referential root
     117       18412 :   return Root;
     118             : }
     119             : 
     120         559 : MDNode *MDBuilder::createTBAARoot(StringRef Name) {
     121        1118 :   return MDNode::get(Context, createString(Name));
     122             : }
     123             : 
     124             : /// \brief Return metadata for a non-root TBAA node with the given name,
     125             : /// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
     126           8 : MDNode *MDBuilder::createTBAANode(StringRef Name, MDNode *Parent,
     127             :                                   bool isConstant) {
     128           8 :   if (isConstant) {
     129           1 :     Constant *Flags = ConstantInt::get(Type::getInt64Ty(Context), 1);
     130           4 :     return MDNode::get(Context,
     131           3 :                        {createString(Name), Parent, createConstant(Flags)});
     132             :   }
     133          21 :   return MDNode::get(Context, {createString(Name), Parent});
     134             : }
     135             : 
     136           0 : MDNode *MDBuilder::createAliasScopeDomain(StringRef Name) {
     137           0 :   return MDNode::get(Context, createString(Name));
     138             : }
     139             : 
     140           0 : MDNode *MDBuilder::createAliasScope(StringRef Name, MDNode *Domain) {
     141           0 :   return MDNode::get(Context, {createString(Name), Domain});
     142             : }
     143             : 
     144             : /// \brief Return metadata for a tbaa.struct node with the given
     145             : /// struct field descriptions.
     146        2383 : MDNode *MDBuilder::createTBAAStructNode(ArrayRef<TBAAStructField> Fields) {
     147        7149 :   SmallVector<Metadata *, 4> Vals(Fields.size() * 3);
     148        2383 :   Type *Int64 = Type::getInt64Ty(Context);
     149        5164 :   for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
     150        5562 :     Vals[i * 3 + 0] = createConstant(ConstantInt::get(Int64, Fields[i].Offset));
     151        5562 :     Vals[i * 3 + 1] = createConstant(ConstantInt::get(Int64, Fields[i].Size));
     152        5562 :     Vals[i * 3 + 2] = Fields[i].TBAA;
     153             :   }
     154        7149 :   return MDNode::get(Context, Vals);
     155             : }
     156             : 
     157             : /// \brief Return metadata for a TBAA struct node in the type DAG
     158             : /// with the given name, a list of pairs (offset, field type in the type DAG).
     159        1852 : MDNode *MDBuilder::createTBAAStructTypeNode(
     160             :     StringRef Name, ArrayRef<std::pair<MDNode *, uint64_t>> Fields) {
     161        5556 :   SmallVector<Metadata *, 4> Ops(Fields.size() * 2 + 1);
     162        1852 :   Type *Int64 = Type::getInt64Ty(Context);
     163        1852 :   Ops[0] = createString(Name);
     164        7276 :   for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
     165       16272 :     Ops[i * 2 + 1] = Fields[i].first;
     166       10848 :     Ops[i * 2 + 2] = createConstant(ConstantInt::get(Int64, Fields[i].second));
     167             :   }
     168        5556 :   return MDNode::get(Context, Ops);
     169             : }
     170             : 
     171             : /// \brief Return metadata for a TBAA scalar type node with the
     172             : /// given name, an offset and a parent in the TBAA type DAG.
     173       11532 : MDNode *MDBuilder::createTBAAScalarTypeNode(StringRef Name, MDNode *Parent,
     174             :                                             uint64_t Offset) {
     175       11532 :   ConstantInt *Off = ConstantInt::get(Type::getInt64Ty(Context), Offset);
     176       46128 :   return MDNode::get(Context,
     177       46128 :                      {createString(Name), Parent, createConstant(Off)});
     178             : }
     179             : 
     180             : /// \brief Return metadata for a TBAA tag node with the given
     181             : /// base type, access type and offset relative to the base type.
     182       14202 : MDNode *MDBuilder::createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType,
     183             :                                            uint64_t Offset, bool IsConstant) {
     184       14202 :   IntegerType *Int64 = Type::getInt64Ty(Context);
     185       14202 :   ConstantInt *Off = ConstantInt::get(Int64, Offset);
     186       14202 :   if (IsConstant) {
     187           0 :     return MDNode::get(Context, {BaseType, AccessType, createConstant(Off),
     188           0 :                                  createConstant(ConstantInt::get(Int64, 1))});
     189             :   }
     190       42606 :   return MDNode::get(Context, {BaseType, AccessType, createConstant(Off)});
     191             : }

Generated by: LCOV version 1.13