LCOV - code coverage report
Current view: top level - lib/IR - MDBuilder.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 114 122 93.4 %
Date: 2018-07-13 00:08:38 Functions: 22 24 91.7 %
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/Function.h"
      18             : #include "llvm/IR/Metadata.h"
      19             : using namespace llvm;
      20             : 
      21       38962 : MDString *MDBuilder::createString(StringRef Str) {
      22       38962 :   return MDString::get(Context, Str);
      23             : }
      24             : 
      25       57765 : ConstantAsMetadata *MDBuilder::createConstant(Constant *C) {
      26       57765 :   return ConstantAsMetadata::get(C);
      27             : }
      28             : 
      29          17 : MDNode *MDBuilder::createFPMath(float Accuracy) {
      30          17 :   if (Accuracy == 0.0)
      31             :     return nullptr;
      32             :   assert(Accuracy > 0.0 && "Invalid fpmath accuracy!");
      33             :   auto *Op =
      34          16 :       createConstant(ConstantFP::get(Type::getFloatTy(Context), Accuracy));
      35          32 :   return MDNode::get(Context, Op);
      36             : }
      37             : 
      38        4912 : MDNode *MDBuilder::createBranchWeights(uint32_t TrueWeight,
      39             :                                        uint32_t FalseWeight) {
      40        9824 :   return createBranchWeights({TrueWeight, FalseWeight});
      41             : }
      42             : 
      43        5242 : MDNode *MDBuilder::createBranchWeights(ArrayRef<uint32_t> Weights) {
      44             :   assert(Weights.size() >= 1 && "Need at least one branch weights!");
      45             : 
      46       10484 :   SmallVector<Metadata *, 4> Vals(Weights.size() + 1);
      47        5242 :   Vals[0] = createString("branch_weights");
      48             : 
      49        5242 :   Type *Int32Ty = Type::getInt32Ty(Context);
      50       15859 :   for (unsigned i = 0, e = Weights.size(); i != e; ++i)
      51       31851 :     Vals[i + 1] = createConstant(ConstantInt::get(Int32Ty, Weights[i]));
      52             : 
      53       10484 :   return MDNode::get(Context, Vals);
      54             : }
      55             : 
      56           2 : MDNode *MDBuilder::createUnpredictable() {
      57           4 :   return MDNode::get(Context, None);
      58             : }
      59             : 
      60         470 : MDNode *MDBuilder::createFunctionEntryCount(
      61             :     uint64_t Count, bool Synthetic,
      62             :     const DenseSet<GlobalValue::GUID> *Imports) {
      63         470 :   Type *Int64Ty = Type::getInt64Ty(Context);
      64             :   SmallVector<Metadata *, 8> Ops;
      65         470 :   if (Synthetic)
      66          18 :     Ops.push_back(createString("synthetic_function_entry_count"));
      67             :   else
      68         452 :     Ops.push_back(createString("function_entry_count"));
      69         470 :   Ops.push_back(createConstant(ConstantInt::get(Int64Ty, Count)));
      70         470 :   if (Imports) {
      71             :     SmallVector<GlobalValue::GUID, 2> OrderID(Imports->begin(), Imports->end());
      72             :     std::stable_sort(OrderID.begin(), OrderID.end(),
      73             :       [] (GlobalValue::GUID A, GlobalValue::GUID B) {
      74             :         return A < B;});
      75          94 :     for (auto ID : OrderID)
      76           8 :       Ops.push_back(createConstant(ConstantInt::get(Int64Ty, ID)));
      77             :   }
      78         940 :   return MDNode::get(Context, Ops);
      79             : }
      80             : 
      81          19 : MDNode *MDBuilder::createFunctionSectionPrefix(StringRef Prefix) {
      82          57 :   return MDNode::get(Context,
      83          38 :                      {createString("function_section_prefix"),
      84          38 :                       createString(Prefix)});
      85             : }
      86             : 
      87        4867 : MDNode *MDBuilder::createRange(const APInt &Lo, const APInt &Hi) {
      88             :   assert(Lo.getBitWidth() == Hi.getBitWidth() && "Mismatched bitwidths!");
      89             : 
      90        4867 :   Type *Ty = IntegerType::get(Context, Lo.getBitWidth());
      91        4867 :   return createRange(ConstantInt::get(Ty, Lo), ConstantInt::get(Ty, Hi));
      92             : }
      93             : 
      94        4871 : MDNode *MDBuilder::createRange(Constant *Lo, Constant *Hi) {
      95             :   // If the range is everything then it is useless.
      96        4871 :   if (Hi == Lo)
      97             :     return nullptr;
      98             : 
      99             :   // Return the range [Lo, Hi).
     100        9734 :   return MDNode::get(Context, {createConstant(Lo), createConstant(Hi)});
     101             : }
     102             : 
     103           3 : MDNode *MDBuilder::createCallees(ArrayRef<Function *> Callees) {
     104             :   SmallVector<Metadata *, 4> Ops;
     105          15 :   for (Function *F : Callees)
     106           6 :     Ops.push_back(createConstant(F));
     107           6 :   return MDNode::get(Context, Ops);
     108             : }
     109             : 
     110       11823 : MDNode *MDBuilder::createAnonymousAARoot(StringRef Name, MDNode *Extra) {
     111             :   // To ensure uniqueness the root node is self-referential.
     112       11823 :   auto Dummy = MDNode::getTemporary(Context, None);
     113             : 
     114       23646 :   SmallVector<Metadata *, 3> Args(1, Dummy.get());
     115       11823 :   if (Extra)
     116        6823 :     Args.push_back(Extra);
     117       11823 :   if (!Name.empty())
     118       11403 :     Args.push_back(createString(Name));
     119       11823 :   MDNode *Root = MDNode::get(Context, Args);
     120             : 
     121             :   // At this point we have
     122             :   //   !0 = metadata !{}            <- dummy
     123             :   //   !1 = metadata !{metadata !0} <- root
     124             :   // Replace the dummy operand with the root node itself and delete the dummy.
     125       11823 :   Root->replaceOperandWith(0, Root);
     126             : 
     127             :   // We now have
     128             :   //   !1 = metadata !{metadata !1} <- self-referential root
     129       11823 :   return Root;
     130             : }
     131             : 
     132         623 : MDNode *MDBuilder::createTBAARoot(StringRef Name) {
     133        1246 :   return MDNode::get(Context, createString(Name));
     134             : }
     135             : 
     136             : /// Return metadata for a non-root TBAA node with the given name,
     137             : /// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
     138           8 : MDNode *MDBuilder::createTBAANode(StringRef Name, MDNode *Parent,
     139             :                                   bool isConstant) {
     140           8 :   if (isConstant) {
     141           1 :     Constant *Flags = ConstantInt::get(Type::getInt64Ty(Context), 1);
     142           3 :     return MDNode::get(Context,
     143           2 :                        {createString(Name), Parent, createConstant(Flags)});
     144             :   }
     145          14 :   return MDNode::get(Context, {createString(Name), Parent});
     146             : }
     147             : 
     148           0 : MDNode *MDBuilder::createAliasScopeDomain(StringRef Name) {
     149           0 :   return MDNode::get(Context, createString(Name));
     150             : }
     151             : 
     152           0 : MDNode *MDBuilder::createAliasScope(StringRef Name, MDNode *Domain) {
     153           0 :   return MDNode::get(Context, {createString(Name), Domain});
     154             : }
     155             : 
     156             : /// Return metadata for a tbaa.struct node with the given
     157             : /// struct field descriptions.
     158        2477 : MDNode *MDBuilder::createTBAAStructNode(ArrayRef<TBAAStructField> Fields) {
     159        4954 :   SmallVector<Metadata *, 4> Vals(Fields.size() * 3);
     160        2477 :   Type *Int64 = Type::getInt64Ty(Context);
     161        5382 :   for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
     162        8715 :     Vals[i * 3 + 0] = createConstant(ConstantInt::get(Int64, Fields[i].Offset));
     163        5810 :     Vals[i * 3 + 1] = createConstant(ConstantInt::get(Int64, Fields[i].Size));
     164        5810 :     Vals[i * 3 + 2] = Fields[i].Type;
     165             :   }
     166        4954 :   return MDNode::get(Context, Vals);
     167             : }
     168             : 
     169             : /// Return metadata for a TBAA struct node in the type DAG
     170             : /// with the given name, a list of pairs (offset, field type in the type DAG).
     171        4742 : MDNode *MDBuilder::createTBAAStructTypeNode(
     172             :     StringRef Name, ArrayRef<std::pair<MDNode *, uint64_t>> Fields) {
     173        9484 :   SmallVector<Metadata *, 4> Ops(Fields.size() * 2 + 1);
     174        4742 :   Type *Int64 = Type::getInt64Ty(Context);
     175        4742 :   Ops[0] = createString(Name);
     176       12747 :   for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
     177       24015 :     Ops[i * 2 + 1] = Fields[i].first;
     178       16010 :     Ops[i * 2 + 2] = createConstant(ConstantInt::get(Int64, Fields[i].second));
     179             :   }
     180        9484 :   return MDNode::get(Context, Ops);
     181             : }
     182             : 
     183             : /// Return metadata for a TBAA scalar type node with the
     184             : /// given name, an offset and a parent in the TBAA type DAG.
     185       16264 : MDNode *MDBuilder::createTBAAScalarTypeNode(StringRef Name, MDNode *Parent,
     186             :                                             uint64_t Offset) {
     187       16264 :   ConstantInt *Off = ConstantInt::get(Type::getInt64Ty(Context), Offset);
     188       48792 :   return MDNode::get(Context,
     189       48792 :                      {createString(Name), Parent, createConstant(Off)});
     190             : }
     191             : 
     192             : /// Return metadata for a TBAA tag node with the given
     193             : /// base type, access type and offset relative to the base type.
     194        6087 : MDNode *MDBuilder::createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType,
     195             :                                            uint64_t Offset, bool IsConstant) {
     196        6087 :   IntegerType *Int64 = Type::getInt64Ty(Context);
     197        6087 :   ConstantInt *Off = ConstantInt::get(Int64, Offset);
     198        6087 :   if (IsConstant) {
     199           0 :     return MDNode::get(Context, {BaseType, AccessType, createConstant(Off),
     200           0 :                                  createConstant(ConstantInt::get(Int64, 1))});
     201             :   }
     202       12174 :   return MDNode::get(Context, {BaseType, AccessType, createConstant(Off)});
     203             : }
     204             : 
     205         104 : MDNode *MDBuilder::createTBAATypeNode(MDNode *Parent, uint64_t Size,
     206             :                                       Metadata *Id,
     207             :                                       ArrayRef<TBAAStructField> Fields) {
     208         208 :   SmallVector<Metadata *, 4> Ops(3 + Fields.size() * 3);
     209         104 :   Type *Int64 = Type::getInt64Ty(Context);
     210         104 :   Ops[0] = Parent;
     211         208 :   Ops[1] = createConstant(ConstantInt::get(Int64, Size));
     212         104 :   Ops[2] = Id;
     213         181 :   for (unsigned I = 0, E = Fields.size(); I != E; ++I) {
     214         231 :     Ops[I * 3 + 3] = Fields[I].Type;
     215         154 :     Ops[I * 3 + 4] = createConstant(ConstantInt::get(Int64, Fields[I].Offset));
     216         154 :     Ops[I * 3 + 5] = createConstant(ConstantInt::get(Int64, Fields[I].Size));
     217             :   }
     218         208 :   return MDNode::get(Context, Ops);
     219             : }
     220             : 
     221          68 : MDNode *MDBuilder::createTBAAAccessTag(MDNode *BaseType, MDNode *AccessType,
     222             :                                        uint64_t Offset, uint64_t Size,
     223             :                                        bool IsImmutable) {
     224          68 :   IntegerType *Int64 = Type::getInt64Ty(Context);
     225          68 :   auto *OffsetNode = createConstant(ConstantInt::get(Int64, Offset));
     226          68 :   auto *SizeNode = createConstant(ConstantInt::get(Int64, Size));
     227          68 :   if (IsImmutable) {
     228           0 :     auto *ImmutabilityFlagNode = createConstant(ConstantInt::get(Int64, 1));
     229           0 :     return MDNode::get(Context, {BaseType, AccessType, OffsetNode, SizeNode,
     230             :                                  ImmutabilityFlagNode});
     231             :   }
     232         136 :   return MDNode::get(Context, {BaseType, AccessType, OffsetNode, SizeNode});
     233             : }
     234             : 
     235           4 : MDNode *MDBuilder::createMutableTBAAAccessTag(MDNode *Tag) {
     236             :   MDNode *BaseType = cast<MDNode>(Tag->getOperand(0));
     237             :   MDNode *AccessType = cast<MDNode>(Tag->getOperand(1));
     238             :   Metadata *OffsetNode = Tag->getOperand(2);
     239             :   uint64_t Offset = mdconst::extract<ConstantInt>(OffsetNode)->getZExtValue();
     240             : 
     241             :   bool NewFormat = isa<MDNode>(AccessType->getOperand(0));
     242             : 
     243             :   // See if the tag is already mutable.
     244             :   unsigned ImmutabilityFlagOp = NewFormat ? 4 : 3;
     245           4 :   if (Tag->getNumOperands() <= ImmutabilityFlagOp)
     246             :     return Tag;
     247             : 
     248             :   // If Tag is already mutable then return it.
     249             :   Metadata *ImmutabilityFlagNode = Tag->getOperand(ImmutabilityFlagOp);
     250           4 :   if (!mdconst::extract<ConstantInt>(ImmutabilityFlagNode)->getValue())
     251             :     return Tag;
     252             : 
     253             :   // Otherwise, create another node.
     254           4 :   if (!NewFormat)
     255           2 :     return createTBAAStructTagNode(BaseType, AccessType, Offset);
     256             : 
     257             :   Metadata *SizeNode = Tag->getOperand(3);
     258             :   uint64_t Size = mdconst::extract<ConstantInt>(SizeNode)->getZExtValue();
     259           2 :   return createTBAAAccessTag(BaseType, AccessType, Offset, Size);
     260             : }
     261             : 
     262          29 : MDNode *MDBuilder::createIrrLoopHeaderWeight(uint64_t Weight) {
     263          58 :   SmallVector<Metadata *, 2> Vals(2);
     264          29 :   Vals[0] = createString("loop_header_weight");
     265          58 :   Vals[1] = createConstant(ConstantInt::get(Type::getInt64Ty(Context), Weight));
     266          58 :   return MDNode::get(Context, Vals);
     267             : }

Generated by: LCOV version 1.13