LCOV - code coverage report
Current view: top level - lib/IR - AttributeImpl.h (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 36 36 100.0 %
Date: 2017-09-14 15:23:50 Functions: 8 11 72.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- AttributeImpl.h - Attribute Internals --------------------*- 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             : /// \file
      11             : /// \brief This file defines various helper methods and classes used by
      12             : /// LLVMContextImpl for creating and managing attributes.
      13             : ///
      14             : //===----------------------------------------------------------------------===//
      15             : 
      16             : #ifndef LLVM_LIB_IR_ATTRIBUTEIMPL_H
      17             : #define LLVM_LIB_IR_ATTRIBUTEIMPL_H
      18             : 
      19             : #include "llvm/ADT/ArrayRef.h"
      20             : #include "llvm/ADT/FoldingSet.h"
      21             : #include "llvm/ADT/StringRef.h"
      22             : #include "llvm/IR/Attributes.h"
      23             : #include "llvm/Support/TrailingObjects.h"
      24             : #include <cassert>
      25             : #include <cstddef>
      26             : #include <cstdint>
      27             : #include <string>
      28             : #include <utility>
      29             : 
      30             : namespace llvm {
      31             : 
      32             : class LLVMContext;
      33             : 
      34             : //===----------------------------------------------------------------------===//
      35             : /// \class
      36             : /// \brief This class represents a single, uniqued attribute. That attribute
      37             : /// could be a single enum, a tuple, or a string.
      38      156878 : class AttributeImpl : public FoldingSetNode {
      39             :   unsigned char KindID; ///< Holds the AttrEntryKind of the attribute
      40             : 
      41             : protected:
      42             :   enum AttrEntryKind {
      43             :     EnumAttrEntry,
      44             :     IntAttrEntry,
      45             :     StringAttrEntry
      46             :   };
      47             : 
      48      329958 :   AttributeImpl(AttrEntryKind KindID) : KindID(KindID) {}
      49             : 
      50             : public:
      51             :   // AttributesImpl is uniqued, these should not be available.
      52             :   AttributeImpl(const AttributeImpl &) = delete;
      53             :   AttributeImpl &operator=(const AttributeImpl &) = delete;
      54             : 
      55             :   virtual ~AttributeImpl();
      56             : 
      57             :   bool isEnumAttribute() const { return KindID == EnumAttrEntry; }
      58             :   bool isIntAttribute() const { return KindID == IntAttrEntry; }
      59             :   bool isStringAttribute() const { return KindID == StringAttrEntry; }
      60             : 
      61             :   bool hasAttribute(Attribute::AttrKind A) const;
      62             :   bool hasAttribute(StringRef Kind) const;
      63             : 
      64             :   Attribute::AttrKind getKindAsEnum() const;
      65             :   uint64_t getValueAsInt() const;
      66             : 
      67             :   StringRef getKindAsString() const;
      68             :   StringRef getValueAsString() const;
      69             : 
      70             :   /// \brief Used when sorting the attributes.
      71             :   bool operator<(const AttributeImpl &AI) const;
      72             : 
      73     9077406 :   void Profile(FoldingSetNodeID &ID) const {
      74     9077406 :     if (isEnumAttribute())
      75     2699746 :       Profile(ID, getKindAsEnum(), 0);
      76     6377660 :     else if (isIntAttribute())
      77      646173 :       Profile(ID, getKindAsEnum(), getValueAsInt());
      78             :     else
      79     5731487 :       Profile(ID, getKindAsString(), getValueAsString());
      80     9077406 :   }
      81             : 
      82             :   static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind,
      83             :                       uint64_t Val) {
      84     3345919 :     ID.AddInteger(Kind);
      85      646173 :     if (Val) ID.AddInteger(Val);
      86             :   }
      87             : 
      88             :   static void Profile(FoldingSetNodeID &ID, StringRef Kind, StringRef Values) {
      89     5731487 :     ID.AddString(Kind);
      90     5731487 :     if (!Values.empty()) ID.AddString(Values);
      91             :   }
      92             : };
      93             : 
      94             : //===----------------------------------------------------------------------===//
      95             : /// \class
      96             : /// \brief A set of classes that contain the value of the
      97             : /// attribute object. There are three main categories: enum attribute entries,
      98             : /// represented by Attribute::AttrKind; alignment attribute entries; and string
      99             : /// attribute enties, which are for target-dependent attributes.
     100             : 
     101       72425 : class EnumAttributeImpl : public AttributeImpl {
     102             :   virtual void anchor();
     103             : 
     104             :   Attribute::AttrKind Kind;
     105             : 
     106             : protected:
     107             :   EnumAttributeImpl(AttrEntryKind ID, Attribute::AttrKind Kind)
     108        6046 :       : AttributeImpl(ID), Kind(Kind) {}
     109             : 
     110             : public:
     111             :   EnumAttributeImpl(Attribute::AttrKind Kind)
     112      145534 :       : AttributeImpl(EnumAttrEntry), Kind(Kind) {}
     113             : 
     114             :   Attribute::AttrKind getEnumKind() const { return Kind; }
     115             : };
     116             : 
     117        4798 : class IntAttributeImpl : public EnumAttributeImpl {
     118             :   uint64_t Val;
     119             : 
     120             :   void anchor() override;
     121             : 
     122             : public:
     123             :   IntAttributeImpl(Attribute::AttrKind Kind, uint64_t Val)
     124        6046 :       : EnumAttributeImpl(IntAttrEntry, Kind), Val(Val) {
     125             :     assert((Kind == Attribute::Alignment || Kind == Attribute::StackAlignment ||
     126             :             Kind == Attribute::Dereferenceable ||
     127             :             Kind == Attribute::DereferenceableOrNull ||
     128             :             Kind == Attribute::AllocSize) &&
     129             :            "Wrong kind for int attribute!");
     130             :   }
     131             : 
     132             :   uint64_t getValue() const { return Val; }
     133             : };
     134             : 
     135      337812 : class StringAttributeImpl : public AttributeImpl {
     136             :   virtual void anchor();
     137             : 
     138             :   std::string Kind;
     139             :   std::string Val;
     140             : 
     141             : public:
     142       89189 :   StringAttributeImpl(StringRef Kind, StringRef Val = StringRef())
     143      356756 :       : AttributeImpl(StringAttrEntry), Kind(Kind), Val(Val) {}
     144             : 
     145   270083068 :   StringRef getStringKind() const { return Kind; }
     146    32173948 :   StringRef getStringValue() const { return Val; }
     147             : };
     148             : 
     149             : //===----------------------------------------------------------------------===//
     150             : /// \class
     151             : /// \brief This class represents a group of attributes that apply to one
     152             : /// element: function, return type, or parameter.
     153             : class AttributeSetNode final
     154             :     : public FoldingSetNode,
     155             :       private TrailingObjects<AttributeSetNode, Attribute> {
     156             :   friend TrailingObjects;
     157             : 
     158             :   /// Bitset with a bit for each available attribute Attribute::AttrKind.
     159             :   uint64_t AvailableAttrs;
     160             :   unsigned NumAttrs; ///< Number of attributes in this node.
     161             : 
     162             :   AttributeSetNode(ArrayRef<Attribute> Attrs);
     163             : 
     164             : public:
     165             :   // AttributesSetNode is uniqued, these should not be available.
     166             :   AttributeSetNode(const AttributeSetNode &) = delete;
     167             :   AttributeSetNode &operator=(const AttributeSetNode &) = delete;
     168             : 
     169       95871 :   void operator delete(void *p) { ::operator delete(p); }
     170             : 
     171             :   static AttributeSetNode *get(LLVMContext &C, const AttrBuilder &B);
     172             : 
     173             :   static AttributeSetNode *get(LLVMContext &C, ArrayRef<Attribute> Attrs);
     174             : 
     175             :   /// \brief Return the number of attributes this AttributeList contains.
     176             :   unsigned getNumAttributes() const { return NumAttrs; }
     177             : 
     178             :   bool hasAttribute(Attribute::AttrKind Kind) const {
     179   181190987 :     return AvailableAttrs & ((uint64_t)1) << Kind;
     180             :   }
     181             :   bool hasAttribute(StringRef Kind) const;
     182             :   bool hasAttributes() const { return NumAttrs != 0; }
     183             : 
     184             :   Attribute getAttribute(Attribute::AttrKind Kind) const;
     185             :   Attribute getAttribute(StringRef Kind) const;
     186             : 
     187             :   unsigned getAlignment() const;
     188             :   unsigned getStackAlignment() const;
     189             :   uint64_t getDereferenceableBytes() const;
     190             :   uint64_t getDereferenceableOrNullBytes() const;
     191             :   std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
     192             :   std::string getAsString(bool InAttrGrp) const;
     193             : 
     194             :   using iterator = const Attribute *;
     195             : 
     196    51657666 :   iterator begin() const { return getTrailingObjects<Attribute>(); }
     197    25730370 :   iterator end() const { return begin() + NumAttrs; }
     198             : 
     199     1607448 :   void Profile(FoldingSetNodeID &ID) const {
     200     6429792 :     Profile(ID, makeArrayRef(begin(), end()));
     201     1607448 :   }
     202             : 
     203             :   static void Profile(FoldingSetNodeID &ID, ArrayRef<Attribute> AttrList) {
     204    10958630 :     for (const auto &Attr : AttrList)
     205     7743734 :       Attr.Profile(ID);
     206             :   }
     207             : };
     208             : 
     209             : using IndexAttrPair = std::pair<unsigned, AttributeSet>;
     210             : 
     211             : //===----------------------------------------------------------------------===//
     212             : /// \class
     213             : /// \brief This class represents a set of attributes that apply to the function,
     214             : /// return type, and parameters.
     215             : class AttributeListImpl final
     216             :     : public FoldingSetNode,
     217             :       private TrailingObjects<AttributeListImpl, AttributeSet> {
     218             :   friend class AttributeList;
     219             :   friend TrailingObjects;
     220             : 
     221             : private:
     222             :   /// Bitset with a bit for each available attribute Attribute::AttrKind.
     223             :   uint64_t AvailableFunctionAttrs;
     224             :   LLVMContext &Context;
     225             :   unsigned NumAttrSets; ///< Number of entries in this set.
     226             : 
     227             :   // Helper fn for TrailingObjects class.
     228             :   size_t numTrailingObjects(OverloadToken<AttributeSet>) { return NumAttrSets; }
     229             : 
     230             : public:
     231             :   AttributeListImpl(LLVMContext &C, ArrayRef<AttributeSet> Sets);
     232             : 
     233             :   // AttributesSetImpt is uniqued, these should not be available.
     234             :   AttributeListImpl(const AttributeListImpl &) = delete;
     235             :   AttributeListImpl &operator=(const AttributeListImpl &) = delete;
     236             : 
     237      126126 :   void operator delete(void *p) { ::operator delete(p); }
     238             : 
     239             :   /// \brief Get the context that created this AttributeListImpl.
     240             :   LLVMContext &getContext() { return Context; }
     241             : 
     242             :   /// \brief Return true if the AttributeSet or the FunctionIndex has an
     243             :   /// enum attribute of the given kind.
     244             :   bool hasFnAttribute(Attribute::AttrKind Kind) const {
     245    83379100 :     return AvailableFunctionAttrs & ((uint64_t)1) << Kind;
     246             :   }
     247             : 
     248             :   using iterator = const AttributeSet *;
     249             : 
     250   241171112 :   iterator begin() const { return getTrailingObjects<AttributeSet>(); }
     251     2207680 :   iterator end() const { return begin() + NumAttrSets; }
     252             : 
     253             :   void Profile(FoldingSetNodeID &ID) const;
     254             :   static void Profile(FoldingSetNodeID &ID, ArrayRef<AttributeSet> Nodes);
     255             : 
     256             :   void dump() const;
     257             : };
     258             : 
     259             : } // end namespace llvm
     260             : 
     261             : #endif // LLVM_LIB_IR_ATTRIBUTEIMPL_H

Generated by: LCOV version 1.13