diff --git a/llvm/include/llvm/Transforms/IPO/Attributor.h b/llvm/include/llvm/Transforms/IPO/Attributor.h index 81ee6d5b762f02..59ba92c0c3d4d0 100644 --- a/llvm/include/llvm/Transforms/IPO/Attributor.h +++ b/llvm/include/llvm/Transforms/IPO/Attributor.h @@ -116,6 +116,7 @@ #include "llvm/Analysis/TargetLibraryInfo.h" #include "llvm/IR/AbstractCallSite.h" #include "llvm/IR/ConstantRange.h" +#include "llvm/IR/Constants.h" #include "llvm/IR/InstIterator.h" #include "llvm/IR/PassManager.h" #include "llvm/Support/Allocator.h" @@ -4279,13 +4280,14 @@ struct AAValueConstantRange /// Return an assumed constant for the associated value a program point \p /// CtxI. - Optional - getAssumedConstantInt(Attributor &A, - const Instruction *CtxI = nullptr) const { + Optional + getAssumedConstant(Attributor &A, const Instruction *CtxI = nullptr) const { ConstantRange RangeV = getAssumedConstantRange(A, CtxI); - if (auto *C = RangeV.getSingleElement()) - return cast( - ConstantInt::get(getAssociatedValue().getType(), *C)); + if (auto *C = RangeV.getSingleElement()) { + Type *Ty = getAssociatedValue().getType(); + return cast_or_null( + AA::getWithType(*ConstantInt::get(Ty->getContext(), *C), *Ty)); + } if (RangeV.isEmptySet()) return llvm::None; return nullptr; @@ -4522,18 +4524,19 @@ struct AAPotentialConstantValues Attributor &A); /// Return assumed constant for the associated value - Optional - getAssumedConstantInt(Attributor &A, - const Instruction *CtxI = nullptr) const { + Optional + getAssumedConstant(Attributor &A, const Instruction *CtxI = nullptr) const { if (!isValidState()) return nullptr; - if (getAssumedSet().size() == 1) - return cast(ConstantInt::get(getAssociatedValue().getType(), - *(getAssumedSet().begin()))); + if (getAssumedSet().size() == 1) { + Type *Ty = getAssociatedValue().getType(); + return cast_or_null(AA::getWithType( + *ConstantInt::get(Ty->getContext(), *(getAssumedSet().begin())), + *Ty)); + } if (getAssumedSet().size() == 0) { if (undefIsContained()) - return cast( - ConstantInt::get(getAssociatedValue().getType(), 0)); + return UndefValue::get(getAssociatedValue().getType()); return llvm::None; } diff --git a/llvm/lib/Transforms/IPO/AttributorAttributes.cpp b/llvm/lib/Transforms/IPO/AttributorAttributes.cpp index 58f407db5d0145..ba2fb1e325039f 100644 --- a/llvm/lib/Transforms/IPO/AttributorAttributes.cpp +++ b/llvm/lib/Transforms/IPO/AttributorAttributes.cpp @@ -5527,7 +5527,7 @@ struct AAValueSimplifyImpl : AAValueSimplify { const auto &AA = A.getAAFor(*this, getIRPosition(), DepClassTy::NONE); - Optional COpt = AA.getAssumedConstantInt(A); + Optional COpt = AA.getAssumedConstant(A); if (!COpt.hasValue()) { SimplifiedAssociatedValue = llvm::None;