diff options
author | Sanjay Patel <spatel@rotateright.com> | 2020-01-16 16:20:48 -0500 |
---|---|---|
committer | Sanjay Patel <spatel@rotateright.com> | 2020-01-16 16:49:16 -0500 |
commit | 52b44902d059e68c6b5553c1d043f768c516064a (patch) | |
tree | 22aceae492196b45701652029d7a3b3b97fa94c1 /llvm/lib/IR/Constants.cpp | |
parent | 8b321929483ee3c4070a10c457733c1bddd10b51 (diff) | |
download | llvm-52b44902d059e68c6b5553c1d043f768c516064a.zip llvm-52b44902d059e68c6b5553c1d043f768c516064a.tar.gz llvm-52b44902d059e68c6b5553c1d043f768c516064a.tar.bz2 |
[IR] fix crash in Constant::isElementWiseEqual() with FP types
We lifted this code from InstCombine for general usage in:
rL369842
...but it's not safe as-is. There are no existing users that can
trigger this bug, but I discovered it via crashing several
regression tests when trying to use it for select folding in
InstSimplify.
ICmp requires (vector) integer types, so give up on anything that's
not integer or FP (pointers and ?) then bitcast the constants
before trying the match. That matches the definition of "equal or
undef" that I was looking for. If someone wants an FP-aware version
of equality (deal with NaN, -0.0), that could be a different mode
or different function.
Differential Revision: https://reviews.llvm.org/D72784
Diffstat (limited to 'llvm/lib/IR/Constants.cpp')
-rw-r--r-- | llvm/lib/IR/Constants.cpp | 15 |
1 files changed, 10 insertions, 5 deletions
diff --git a/llvm/lib/IR/Constants.cpp b/llvm/lib/IR/Constants.cpp index 054375aa..93d406f 100644 --- a/llvm/lib/IR/Constants.cpp +++ b/llvm/lib/IR/Constants.cpp @@ -286,12 +286,17 @@ bool Constant::isElementWiseEqual(Value *Y) const { if (!isa<Constant>(Y) || !Ty->isVectorTy() || Ty != Y->getType()) return false; + // TODO: Compare pointer constants? + if (!(Ty->getVectorElementType()->isIntegerTy() || + Ty->getVectorElementType()->isFloatingPointTy())) + return false; + // They may still be identical element-wise (if they have `undef`s). - // FIXME: This crashes on FP vector constants. - return match(ConstantExpr::getICmp(ICmpInst::Predicate::ICMP_EQ, - const_cast<Constant *>(this), - cast<Constant>(Y)), - m_One()); + // Bitcast to integer to allow exact bitwise comparison for all types. + Type *IntTy = VectorType::getInteger(cast<VectorType>(Ty)); + Constant *C0 = ConstantExpr::getBitCast(const_cast<Constant *>(this), IntTy); + Constant *C1 = ConstantExpr::getBitCast(cast<Constant>(Y), IntTy); + return match(ConstantExpr::getICmp(ICmpInst::ICMP_EQ, C0, C1), m_One()); } bool Constant::containsUndefElement() const { |