mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
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:
@@ -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];
|
||||
|
||||
Reference in New Issue
Block a user