Add template <T> ValueBase::getSingleUserOfType.

Looping over all users and returning the only user of a specific type is a
common pattern in the compiler. This method just allows for the loop to be
eliminated.
This commit is contained in:
Michael Gottesman
2017-07-13 14:22:42 -07:00
parent 5f13ad03ca
commit 38cd78024b

View File

@@ -198,6 +198,9 @@ public:
/// otherwise.
inline Operand *getSingleUse() const;
template <class T>
inline T *getSingleUserOfType();
/// Pretty-print the value.
void dump() const;
void print(raw_ostream &OS) const;
@@ -527,6 +530,19 @@ inline Operand *ValueBase::getSingleUse() const {
return Op;
}
template <class T>
inline T *ValueBase::getSingleUserOfType() {
T *Result = nullptr;
for (auto *Op : getUses()) {
if (auto *Tmp = dyn_cast<T>(Op->getUser())) {
if (Result)
return nullptr;
Result = Tmp;
}
}
return Result;
}
/// A constant-size list of the operands of an instruction.
template <unsigned N> class FixedOperandList {
Operand Buffer[N];