llvm.org GIT mirror llvm / bce7b42
[Constant] Add 'isElementWiseEqual()' method Promoting it from InstCombine's tryToReuseConstantFromSelectInComparison(). Return true if this constant and a constant 'Y' are element-wise equal. This is identical to just comparing the pointers, with the exception that for vectors, if only one of the constants has an `undef` element in some lane, the constants still match. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@369842 91177308-0d34-0410-b5e6-96231b3b80d8 Roman Lebedev 24 days ago
3 changed file(s) with 23 addition(s) and 16 deletion(s). Raw diff Collapse all Expand all
8484 /// Return true if this is a floating-point NaN constant or a vector
8585 /// floating-point constant with all NaN elements.
8686 bool isNaN() const;
87
88 /// Return true if this constant and a constant 'Y' are element-wise equal.
89 /// This is identical to just comparing the pointers, with the exception that
90 /// for vectors, if only one of the constants has an `undef` element in some
91 /// lane, the constants still match.
92 bool isElementWiseEqual(Value *Y) const;
8793
8894 /// Return true if this is a vector constant that includes any undefined
8995 /// elements.
2121 #include "llvm/IR/Instructions.h"
2222 #include "llvm/IR/Module.h"
2323 #include "llvm/IR/Operator.h"
24 #include "llvm/IR/PatternMatch.h"
2425 #include "llvm/Support/Debug.h"
2526 #include "llvm/Support/ErrorHandling.h"
2627 #include "llvm/Support/ManagedStatic.h"
247248 return false;
248249 }
249250 return true;
251 }
252
253 bool Constant::isElementWiseEqual(Value *Y) const {
254 // Are they fully identical?
255 if (this == Y)
256 return true;
257 // They may still be identical element-wise (if they have `undef`s).
258 auto *Cy = dyn_cast(Y);
259 if (!Cy)
260 return false;
261 return PatternMatch::match(ConstantExpr::getICmp(ICmpInst::Predicate::ICMP_EQ,
262 const_cast(this),
263 Cy),
264 PatternMatch::m_One());
250265 }
251266
252267 bool Constant::containsUndefElement() const {
12951295
12961296 // FIXME: are there any magic icmp predicate+constant pairs we must not touch?
12971297
1298 auto ConstantsAreElementWiseEqual = [](Constant *Cx, Value *Y) {
1299 // Are they fully identical?
1300 if (Cx == Y)
1301 return true;
1302 // They may still be identical element-wise (if they have `undef`s).
1303 auto *Cy = dyn_cast(Y);
1304 if (!Cy)
1305 return false;
1306 return match(ConstantExpr::getICmp(ICmpInst::Predicate::ICMP_EQ, Cx, Cy),
1307 m_One());
1308 };
1309
13101298 Value *SelVal0, *SelVal1; // We do not care which one is from where.
13111299 match(&Sel, m_Select(m_Value(), m_Value(SelVal0), m_Value(SelVal1)));
13121300 // At least one of these values we are selecting between must be a constant
13161304 return nullptr;
13171305
13181306 // Does this constant C match any of the `select` values?
1319 auto MatchesSelectValue = [ConstantsAreElementWiseEqual, SelVal0,
1320 SelVal1](Constant *C) {
1321 return ConstantsAreElementWiseEqual(C, SelVal0) ||
1322 ConstantsAreElementWiseEqual(C, SelVal1);
1307 auto MatchesSelectValue = [SelVal0, SelVal1](Constant *C) {
1308 return C->isElementWiseEqual(SelVal0) || C->isElementWiseEqual(SelVal1);
13231309 };
13241310
13251311 // If C0 *already* matches true/false value of select, we are done.