2012-12-20 09:36:59 +08:00
|
|
|
//===-- AttributeImpl.h - Attribute Internals -------------------*- C++ -*-===//
|
2012-09-27 05:07:29 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2012-12-21 05:28:43 +08:00
|
|
|
///
|
|
|
|
/// \file
|
|
|
|
/// \brief This file defines various helper methods and classes used by
|
|
|
|
/// LLVMContextImpl for creating and managing attributes.
|
|
|
|
///
|
2012-09-27 05:07:29 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-08-14 00:26:38 +08:00
|
|
|
#ifndef LLVM_LIB_IR_ATTRIBUTEIMPL_H
|
|
|
|
#define LLVM_LIB_IR_ATTRIBUTEIMPL_H
|
2012-09-27 05:07:29 +08:00
|
|
|
|
2016-12-08 06:06:02 +08:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
2012-09-27 05:07:29 +08:00
|
|
|
#include "llvm/ADT/FoldingSet.h"
|
2016-12-08 06:06:02 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Attributes.h"
|
2016-12-08 06:06:02 +08:00
|
|
|
#include "llvm/Support/TrailingObjects.h"
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
2016-01-30 06:30:30 +08:00
|
|
|
#include <climits>
|
2016-12-08 06:06:02 +08:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
2016-01-30 06:35:29 +08:00
|
|
|
#include <string>
|
2016-12-08 06:06:02 +08:00
|
|
|
#include <utility>
|
2012-09-27 05:07:29 +08:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2012-12-20 07:55:43 +08:00
|
|
|
class LLVMContext;
|
|
|
|
|
2013-02-06 06:37:24 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// \class
|
2013-07-11 20:13:16 +08:00
|
|
|
/// \brief This class represents a single, uniqued attribute. That attribute
|
|
|
|
/// could be a single enum, a tuple, or a string.
|
2013-09-12 02:05:11 +08:00
|
|
|
class AttributeImpl : public FoldingSetNode {
|
2013-07-11 20:13:16 +08:00
|
|
|
unsigned char KindID; ///< Holds the AttrEntryKind of the attribute
|
|
|
|
|
2013-02-06 06:37:24 +08:00
|
|
|
protected:
|
|
|
|
enum AttrEntryKind {
|
|
|
|
EnumAttrEntry,
|
2014-07-18 14:51:55 +08:00
|
|
|
IntAttrEntry,
|
2013-02-06 06:37:24 +08:00
|
|
|
StringAttrEntry
|
|
|
|
};
|
|
|
|
|
2013-07-11 20:13:16 +08:00
|
|
|
AttributeImpl(AttrEntryKind KindID) : KindID(KindID) {}
|
2013-01-28 05:38:03 +08:00
|
|
|
|
2012-09-27 05:07:29 +08:00
|
|
|
public:
|
2016-12-08 06:06:02 +08:00
|
|
|
// AttributesImpl is uniqued, these should not be available.
|
|
|
|
AttributeImpl(const AttributeImpl &) = delete;
|
|
|
|
AttributeImpl &operator=(const AttributeImpl &) = delete;
|
|
|
|
|
2013-11-18 17:31:53 +08:00
|
|
|
virtual ~AttributeImpl();
|
|
|
|
|
2013-07-11 20:13:16 +08:00
|
|
|
bool isEnumAttribute() const { return KindID == EnumAttrEntry; }
|
2014-07-18 14:51:55 +08:00
|
|
|
bool isIntAttribute() const { return KindID == IntAttrEntry; }
|
2013-07-11 20:13:16 +08:00
|
|
|
bool isStringAttribute() const { return KindID == StringAttrEntry; }
|
2012-10-09 05:47:17 +08:00
|
|
|
|
2013-02-06 06:37:24 +08:00
|
|
|
bool hasAttribute(Attribute::AttrKind A) const;
|
|
|
|
bool hasAttribute(StringRef Kind) const;
|
2012-10-09 05:47:17 +08:00
|
|
|
|
2013-02-06 06:37:24 +08:00
|
|
|
Attribute::AttrKind getKindAsEnum() const;
|
|
|
|
uint64_t getValueAsInt() const;
|
2012-12-30 09:38:39 +08:00
|
|
|
|
2013-02-06 06:37:24 +08:00
|
|
|
StringRef getKindAsString() const;
|
|
|
|
StringRef getValueAsString() const;
|
2012-12-30 09:38:39 +08:00
|
|
|
|
2013-02-01 04:59:05 +08:00
|
|
|
/// \brief Used when sorting the attributes.
|
2013-01-24 08:06:56 +08:00
|
|
|
bool operator<(const AttributeImpl &AI) const;
|
|
|
|
|
2012-09-27 05:07:29 +08:00
|
|
|
void Profile(FoldingSetNodeID &ID) const {
|
2013-02-06 06:37:24 +08:00
|
|
|
if (isEnumAttribute())
|
|
|
|
Profile(ID, getKindAsEnum(), 0);
|
2014-07-18 14:51:55 +08:00
|
|
|
else if (isIntAttribute())
|
2013-02-06 06:37:24 +08:00
|
|
|
Profile(ID, getKindAsEnum(), getValueAsInt());
|
|
|
|
else
|
|
|
|
Profile(ID, getKindAsString(), getValueAsString());
|
|
|
|
}
|
|
|
|
static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind,
|
|
|
|
uint64_t Val) {
|
|
|
|
ID.AddInteger(Kind);
|
|
|
|
if (Val) ID.AddInteger(Val);
|
2012-09-27 05:07:29 +08:00
|
|
|
}
|
2013-02-06 06:37:24 +08:00
|
|
|
static void Profile(FoldingSetNodeID &ID, StringRef Kind, StringRef Values) {
|
|
|
|
ID.AddString(Kind);
|
2013-03-01 05:17:03 +08:00
|
|
|
if (!Values.empty()) ID.AddString(Values);
|
2013-01-29 08:34:06 +08:00
|
|
|
}
|
2012-09-27 05:07:29 +08:00
|
|
|
};
|
|
|
|
|
2013-07-11 20:13:16 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// \class
|
|
|
|
/// \brief A set of classes that contain the value of the
|
|
|
|
/// attribute object. There are three main categories: enum attribute entries,
|
|
|
|
/// represented by Attribute::AttrKind; alignment attribute entries; and string
|
|
|
|
/// attribute enties, which are for target-dependent attributes.
|
|
|
|
|
2013-09-12 02:05:11 +08:00
|
|
|
class EnumAttributeImpl : public AttributeImpl {
|
2013-11-19 08:57:56 +08:00
|
|
|
virtual void anchor();
|
2013-07-11 20:13:16 +08:00
|
|
|
Attribute::AttrKind Kind;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
EnumAttributeImpl(AttrEntryKind ID, Attribute::AttrKind Kind)
|
|
|
|
: AttributeImpl(ID), Kind(Kind) {}
|
|
|
|
|
|
|
|
public:
|
|
|
|
EnumAttributeImpl(Attribute::AttrKind Kind)
|
|
|
|
: AttributeImpl(EnumAttrEntry), Kind(Kind) {}
|
|
|
|
|
|
|
|
Attribute::AttrKind getEnumKind() const { return Kind; }
|
|
|
|
};
|
|
|
|
|
2014-07-18 14:51:55 +08:00
|
|
|
class IntAttributeImpl : public EnumAttributeImpl {
|
2014-03-05 14:35:38 +08:00
|
|
|
void anchor() override;
|
2014-07-18 14:51:55 +08:00
|
|
|
uint64_t Val;
|
2013-07-11 20:13:16 +08:00
|
|
|
|
|
|
|
public:
|
2014-07-18 14:51:55 +08:00
|
|
|
IntAttributeImpl(Attribute::AttrKind Kind, uint64_t Val)
|
|
|
|
: EnumAttributeImpl(IntAttrEntry, Kind), Val(Val) {
|
2015-04-17 04:29:50 +08:00
|
|
|
assert((Kind == Attribute::Alignment || Kind == Attribute::StackAlignment ||
|
|
|
|
Kind == Attribute::Dereferenceable ||
|
2016-04-12 09:05:35 +08:00
|
|
|
Kind == Attribute::DereferenceableOrNull ||
|
|
|
|
Kind == Attribute::AllocSize) &&
|
2015-04-17 04:29:50 +08:00
|
|
|
"Wrong kind for int attribute!");
|
2013-07-11 20:13:16 +08:00
|
|
|
}
|
|
|
|
|
2014-07-18 14:51:55 +08:00
|
|
|
uint64_t getValue() const { return Val; }
|
2013-07-11 20:13:16 +08:00
|
|
|
};
|
|
|
|
|
2013-09-12 02:05:11 +08:00
|
|
|
class StringAttributeImpl : public AttributeImpl {
|
2013-11-19 08:57:56 +08:00
|
|
|
virtual void anchor();
|
2013-07-11 20:13:16 +08:00
|
|
|
std::string Kind;
|
|
|
|
std::string Val;
|
|
|
|
|
|
|
|
public:
|
|
|
|
StringAttributeImpl(StringRef Kind, StringRef Val = StringRef())
|
|
|
|
: AttributeImpl(StringAttrEntry), Kind(Kind), Val(Val) {}
|
|
|
|
|
|
|
|
StringRef getStringKind() const { return Kind; }
|
|
|
|
StringRef getStringValue() const { return Val; }
|
|
|
|
};
|
|
|
|
|
2017-04-12 08:38:00 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// \class
|
|
|
|
/// \brief This class represents a group of attributes that apply to one
|
|
|
|
/// element: function, return type, or parameter.
|
|
|
|
class AttributeSetNode final
|
|
|
|
: public FoldingSetNode,
|
|
|
|
private TrailingObjects<AttributeSetNode, Attribute> {
|
|
|
|
friend TrailingObjects;
|
|
|
|
|
|
|
|
/// Bitset with a bit for each available attribute Attribute::AttrKind.
|
|
|
|
uint64_t AvailableAttrs;
|
|
|
|
unsigned NumAttrs; ///< Number of attributes in this node.
|
|
|
|
|
|
|
|
AttributeSetNode(ArrayRef<Attribute> Attrs);
|
|
|
|
|
|
|
|
public:
|
|
|
|
// AttributesSetNode is uniqued, these should not be available.
|
|
|
|
AttributeSetNode(const AttributeSetNode &) = delete;
|
|
|
|
AttributeSetNode &operator=(const AttributeSetNode &) = delete;
|
|
|
|
|
|
|
|
void operator delete(void *p) { ::operator delete(p); }
|
|
|
|
|
|
|
|
static AttributeSetNode *get(LLVMContext &C, const AttrBuilder &B);
|
|
|
|
|
|
|
|
static AttributeSetNode *get(LLVMContext &C, ArrayRef<Attribute> Attrs);
|
|
|
|
|
|
|
|
/// \brief Return the number of attributes this AttributeList contains.
|
|
|
|
unsigned getNumAttributes() const { return NumAttrs; }
|
|
|
|
|
|
|
|
bool hasAttribute(Attribute::AttrKind Kind) const {
|
|
|
|
return AvailableAttrs & ((uint64_t)1) << Kind;
|
|
|
|
}
|
|
|
|
bool hasAttribute(StringRef Kind) const;
|
|
|
|
bool hasAttributes() const { return NumAttrs != 0; }
|
|
|
|
|
|
|
|
Attribute getAttribute(Attribute::AttrKind Kind) const;
|
|
|
|
Attribute getAttribute(StringRef Kind) const;
|
|
|
|
|
|
|
|
unsigned getAlignment() const;
|
|
|
|
unsigned getStackAlignment() const;
|
|
|
|
uint64_t getDereferenceableBytes() const;
|
|
|
|
uint64_t getDereferenceableOrNullBytes() const;
|
|
|
|
std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
|
|
|
|
std::string getAsString(bool InAttrGrp) const;
|
|
|
|
|
|
|
|
typedef const Attribute *iterator;
|
|
|
|
iterator begin() const { return getTrailingObjects<Attribute>(); }
|
|
|
|
iterator end() const { return begin() + NumAttrs; }
|
|
|
|
|
|
|
|
void Profile(FoldingSetNodeID &ID) const {
|
|
|
|
Profile(ID, makeArrayRef(begin(), end()));
|
|
|
|
}
|
|
|
|
static void Profile(FoldingSetNodeID &ID, ArrayRef<Attribute> AttrList) {
|
|
|
|
for (const auto &Attr : AttrList)
|
|
|
|
Attr.Profile(ID);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::pair<unsigned, AttributeSet> IndexAttrPair;
|
2015-08-06 17:49:17 +08:00
|
|
|
|
2013-01-24 08:06:56 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// \class
|
|
|
|
/// \brief This class represents a set of attributes that apply to the function,
|
|
|
|
/// return type, and parameters.
|
Rename AttributeSet to AttributeList
Summary:
This class is a list of AttributeSetNodes corresponding the function
prototype of a call or function declaration. This class used to be
called ParamAttrListPtr, then AttrListPtr, then AttributeSet. It is
typically accessed by parameter and return value index, so
"AttributeList" seems like a more intuitive name.
Rename AttributeSetImpl to AttributeListImpl to follow suit.
It's useful to rename this class so that we can rename AttributeSetNode
to AttributeSet later. AttributeSet is the set of attributes that apply
to a single function, argument, or return value.
Reviewers: sanjoy, javed.absar, chandlerc, pete
Reviewed By: pete
Subscribers: pete, jholewinski, arsenm, dschuff, mehdi_amini, jfb, nhaehnle, sbc100, void, llvm-commits
Differential Revision: https://reviews.llvm.org/D31102
llvm-svn: 298393
2017-03-22 00:57:19 +08:00
|
|
|
class AttributeListImpl final
|
2015-08-06 06:57:34 +08:00
|
|
|
: public FoldingSetNode,
|
Rename AttributeSet to AttributeList
Summary:
This class is a list of AttributeSetNodes corresponding the function
prototype of a call or function declaration. This class used to be
called ParamAttrListPtr, then AttrListPtr, then AttributeSet. It is
typically accessed by parameter and return value index, so
"AttributeList" seems like a more intuitive name.
Rename AttributeSetImpl to AttributeListImpl to follow suit.
It's useful to rename this class so that we can rename AttributeSetNode
to AttributeSet later. AttributeSet is the set of attributes that apply
to a single function, argument, or return value.
Reviewers: sanjoy, javed.absar, chandlerc, pete
Reviewed By: pete
Subscribers: pete, jholewinski, arsenm, dschuff, mehdi_amini, jfb, nhaehnle, sbc100, void, llvm-commits
Differential Revision: https://reviews.llvm.org/D31102
llvm-svn: 298393
2017-03-22 00:57:19 +08:00
|
|
|
private TrailingObjects<AttributeListImpl, IndexAttrPair> {
|
|
|
|
friend class AttributeList;
|
2015-08-06 06:57:34 +08:00
|
|
|
friend TrailingObjects;
|
2015-06-17 09:21:20 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
LLVMContext &Context;
|
2016-06-15 06:04:16 +08:00
|
|
|
unsigned NumSlots; ///< Number of entries in this set.
|
2016-01-30 06:25:19 +08:00
|
|
|
/// Bitset with a bit for each available attribute Attribute::AttrKind.
|
|
|
|
uint64_t AvailableFunctionAttrs;
|
2013-07-11 20:13:16 +08:00
|
|
|
|
2015-08-06 06:57:34 +08:00
|
|
|
// Helper fn for TrailingObjects class.
|
2016-06-15 06:04:16 +08:00
|
|
|
size_t numTrailingObjects(OverloadToken<IndexAttrPair>) { return NumSlots; }
|
2015-08-06 06:57:34 +08:00
|
|
|
|
2013-07-11 20:13:16 +08:00
|
|
|
/// \brief Return a pointer to the IndexAttrPair for the specified slot.
|
2017-04-12 08:38:00 +08:00
|
|
|
const IndexAttrPair *getSlotPair(unsigned Slot) const {
|
2015-08-06 06:57:34 +08:00
|
|
|
return getTrailingObjects<IndexAttrPair>() + Slot;
|
2013-07-11 20:13:16 +08:00
|
|
|
}
|
2013-01-24 09:01:34 +08:00
|
|
|
|
2012-11-20 13:09:20 +08:00
|
|
|
public:
|
Rename AttributeSet to AttributeList
Summary:
This class is a list of AttributeSetNodes corresponding the function
prototype of a call or function declaration. This class used to be
called ParamAttrListPtr, then AttrListPtr, then AttributeSet. It is
typically accessed by parameter and return value index, so
"AttributeList" seems like a more intuitive name.
Rename AttributeSetImpl to AttributeListImpl to follow suit.
It's useful to rename this class so that we can rename AttributeSetNode
to AttributeSet later. AttributeSet is the set of attributes that apply
to a single function, argument, or return value.
Reviewers: sanjoy, javed.absar, chandlerc, pete
Reviewed By: pete
Subscribers: pete, jholewinski, arsenm, dschuff, mehdi_amini, jfb, nhaehnle, sbc100, void, llvm-commits
Differential Revision: https://reviews.llvm.org/D31102
llvm-svn: 298393
2017-03-22 00:57:19 +08:00
|
|
|
AttributeListImpl(LLVMContext &C,
|
2017-04-12 08:38:00 +08:00
|
|
|
ArrayRef<std::pair<unsigned, AttributeSet>> Slots);
|
2013-01-05 04:54:35 +08:00
|
|
|
|
2016-12-08 06:06:02 +08:00
|
|
|
// AttributesSetImpt is uniqued, these should not be available.
|
Rename AttributeSet to AttributeList
Summary:
This class is a list of AttributeSetNodes corresponding the function
prototype of a call or function declaration. This class used to be
called ParamAttrListPtr, then AttrListPtr, then AttributeSet. It is
typically accessed by parameter and return value index, so
"AttributeList" seems like a more intuitive name.
Rename AttributeSetImpl to AttributeListImpl to follow suit.
It's useful to rename this class so that we can rename AttributeSetNode
to AttributeSet later. AttributeSet is the set of attributes that apply
to a single function, argument, or return value.
Reviewers: sanjoy, javed.absar, chandlerc, pete
Reviewed By: pete
Subscribers: pete, jholewinski, arsenm, dschuff, mehdi_amini, jfb, nhaehnle, sbc100, void, llvm-commits
Differential Revision: https://reviews.llvm.org/D31102
llvm-svn: 298393
2017-03-22 00:57:19 +08:00
|
|
|
AttributeListImpl(const AttributeListImpl &) = delete;
|
|
|
|
AttributeListImpl &operator=(const AttributeListImpl &) = delete;
|
2016-12-08 06:06:02 +08:00
|
|
|
|
2016-02-09 10:09:16 +08:00
|
|
|
void operator delete(void *p) { ::operator delete(p); }
|
2016-02-09 09:03:42 +08:00
|
|
|
|
Rename AttributeSet to AttributeList
Summary:
This class is a list of AttributeSetNodes corresponding the function
prototype of a call or function declaration. This class used to be
called ParamAttrListPtr, then AttrListPtr, then AttributeSet. It is
typically accessed by parameter and return value index, so
"AttributeList" seems like a more intuitive name.
Rename AttributeSetImpl to AttributeListImpl to follow suit.
It's useful to rename this class so that we can rename AttributeSetNode
to AttributeSet later. AttributeSet is the set of attributes that apply
to a single function, argument, or return value.
Reviewers: sanjoy, javed.absar, chandlerc, pete
Reviewed By: pete
Subscribers: pete, jholewinski, arsenm, dschuff, mehdi_amini, jfb, nhaehnle, sbc100, void, llvm-commits
Differential Revision: https://reviews.llvm.org/D31102
llvm-svn: 298393
2017-03-22 00:57:19 +08:00
|
|
|
/// \brief Get the context that created this AttributeListImpl.
|
2013-01-05 04:54:35 +08:00
|
|
|
LLVMContext &getContext() { return Context; }
|
2013-01-28 05:32:11 +08:00
|
|
|
|
2016-06-15 06:04:16 +08:00
|
|
|
/// \brief Return the number of slots used in this attribute list. This is
|
|
|
|
/// the number of arguments that have an attribute set on them (including the
|
|
|
|
/// function itself).
|
|
|
|
unsigned getNumSlots() const { return NumSlots; }
|
2013-01-28 05:32:11 +08:00
|
|
|
|
|
|
|
/// \brief Get the index of the given "slot" in the AttrNodes list. This index
|
|
|
|
/// is the index of the return, parameter, or function object that the
|
|
|
|
/// attributes are applied to, not the index into the AttrNodes list where the
|
|
|
|
/// attributes reside.
|
2013-05-01 00:53:38 +08:00
|
|
|
unsigned getSlotIndex(unsigned Slot) const {
|
2017-04-12 08:38:00 +08:00
|
|
|
return getSlotPair(Slot)->first;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Retrieve the attribute set node for the given "slot" in the
|
|
|
|
/// AttrNode list.
|
2017-04-25 06:25:02 +08:00
|
|
|
AttributeSet getSlotAttributes(unsigned Slot) const {
|
2017-04-12 08:38:00 +08:00
|
|
|
return getSlotPair(Slot)->second;
|
2013-01-28 08:21:34 +08:00
|
|
|
}
|
2013-01-28 05:32:11 +08:00
|
|
|
|
2017-04-12 08:38:00 +08:00
|
|
|
/// \brief Return true if the AttributeSet or the FunctionIndex has an
|
2016-01-30 06:25:19 +08:00
|
|
|
/// enum attribute of the given kind.
|
|
|
|
bool hasFnAttribute(Attribute::AttrKind Kind) const {
|
|
|
|
return AvailableFunctionAttrs & ((uint64_t)1) << Kind;
|
|
|
|
}
|
|
|
|
|
2017-04-12 08:38:00 +08:00
|
|
|
typedef AttributeSet::iterator iterator;
|
2017-04-25 06:25:02 +08:00
|
|
|
iterator begin(unsigned Slot) const {
|
|
|
|
return getSlotAttributes(Slot).begin();
|
|
|
|
}
|
|
|
|
iterator end(unsigned Slot) const { return getSlotAttributes(Slot).end(); }
|
2013-01-28 08:21:34 +08:00
|
|
|
|
2017-04-11 08:16:00 +08:00
|
|
|
void Profile(FoldingSetNodeID &ID) const;
|
2013-01-24 09:01:34 +08:00
|
|
|
static void Profile(FoldingSetNodeID &ID,
|
2017-04-12 08:38:00 +08:00
|
|
|
ArrayRef<std::pair<unsigned, AttributeSet>> Nodes);
|
2013-01-28 07:41:29 +08:00
|
|
|
|
2013-08-03 06:34:30 +08:00
|
|
|
void dump() const;
|
2012-11-20 13:09:20 +08:00
|
|
|
};
|
|
|
|
|
2016-12-08 06:06:02 +08:00
|
|
|
} // end namespace llvm
|
2012-09-27 05:07:29 +08:00
|
|
|
|
2016-12-08 06:06:02 +08:00
|
|
|
#endif // LLVM_LIB_IR_ATTRIBUTEIMPL_H
|