1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
/*========================== begin_copyright_notice ============================
Copyright (C) 2017-2021 Intel Corporation
SPDX-License-Identifier: MIT
============================= end_copyright_notice ===========================*/
#pragma once
#include "common/LLVMWarningsPush.hpp"
#include <llvm/IR/Value.h>
#include <llvm/IR/Constants.h>
#include <llvm/IR/Module.h>
#include <llvm/IR/Function.h>
#include <llvm/IR/Metadata.h>
#include <llvm/Support/Atomic.h>
#include "common/LLVMWarningsPop.hpp"
#include "MetaDataTraits.h"
#include "Probe/Assertion.h"
namespace IGC
{
template<class MDorNamedMD>
inline llvm::Metadata* getMDOpnd(MDorNamedMD* MD, unsigned idx)
{
IGC_ASSERT(0);
return nullptr;
}
template<>
inline llvm::Metadata* getMDOpnd<llvm::MDNode>(
llvm::MDNode* MD, unsigned idx)
{
return MD->getOperand(idx).get();
}
template<>
inline llvm::Metadata* getMDOpnd<llvm::NamedMDNode>(
llvm::NamedMDNode* MD, unsigned idx)
{
return MD->getOperand(idx);
}
template<>
inline llvm::Metadata* getMDOpnd<const llvm::MDNode>(
const llvm::MDNode* MD, unsigned idx)
{
return MD->getOperand(idx).get();
}
template<>
inline llvm::Metadata* getMDOpnd<const llvm::NamedMDNode>(
const llvm::NamedMDNode* MD, unsigned idx)
{
return MD->getOperand(idx);
}
///
// Iterator over the meta data nodes list. It is assumed that
// all the nodes are of the same type ( as specified by the T template parameter)
// Template parameters:
// T - type of the entry node
// N - type of the root(parent) node (supported types are MDNode and NamedMDNode )
// C - traits type (see the MDValueTraits )
//
template<class T, class N = llvm::MDNode, class C = MDValueTraits<T> >
class MetaDataIterator
{
public:
typedef typename C::value_type value_type;
typedef MetaDataIterator<T, N, C> _Myt;
///
// Ctor. Creates the sentinel iterator. Usually used as an end iterator
//
explicit MetaDataIterator(const N* pNode) :
m_pNode(pNode),
m_index(pNode->getNumOperands())
{
}
///
// Ctor. Create the iterator on given index
explicit MetaDataIterator(const N* pNode, unsigned int index) :
m_pNode(pNode),
m_index(index)
{
IGC_ASSERT(index <= pNode->getNumOperands());
}
llvm::Metadata* operator *()
{
IGC_ASSERT_MESSAGE(false == isNil(), "m_Index has to be 0");
return getMDOpnd(m_pNode, m_index);
}
///
// returns the current item in the list
value_type get()
{
IGC_ASSERT_MESSAGE(false == isNil(), "m_Index has to be 0");
return C::load(getMDOpnd(m_pNode, m_index));
}
_Myt& operator++()
{
if (!isNil())
++m_index;
return (*this);
}
_Myt operator++(int)
{
_Myt tmp = *this;
++* this;
return tmp;
}
bool operator == (const _Myt& rhs)
{
return m_pNode == rhs.m_pNode &&
m_index == rhs.m_index;
}
bool operator != (const _Myt& rhs)
{
return !this->operator==(rhs);
}
private:
bool isNil()
{
return m_index == m_pNode->getNumOperands();
}
private:
const N* m_pNode; // pointer to the parent node
unsigned int m_index;
};
} //namespace
|