diff --git a/llvm/include/llvm/Transforms/IPO/Attributor.h b/llvm/include/llvm/Transforms/IPO/Attributor.h --- a/llvm/include/llvm/Transforms/IPO/Attributor.h +++ b/llvm/include/llvm/Transforms/IPO/Attributor.h @@ -1218,12 +1218,13 @@ DepClassTy::NONE); } - /// Return the attribute of \p AAType for \p IRP if existing. This also allows - /// non-AA users lookup. + /// Return the attribute of \p AAType for \p IRP if existing and valid. This + /// also allows non-AA users lookup. template AAType *lookupAAFor(const IRPosition &IRP, const AbstractAttribute *QueryingAA = nullptr, - DepClassTy DepClass = DepClassTy::OPTIONAL) { + DepClassTy DepClass = DepClassTy::OPTIONAL, + bool AllowInvalidState = true) { static_assert(std::is_base_of::value, "Cannot query an attribute with a type not derived from " "'AbstractAttribute'!"); @@ -1240,6 +1241,10 @@ AA->getState().isValidState()) recordDependence(*AA, const_cast(*QueryingAA), DepClass); + + // Return nullptr if this attribute has an invalid state. + if (!AllowInvalidState && !AA->getState().isValidState()) + return nullptr; return AA; }