Skip to content

Commit 41466a1

Browse files
authored
[SelectionDAG] Correct the implementation of m_AllOnes. (#90776)
Previously we used SpecificInt_match which created a 64 bit APInt containing all ones. This was then checked against other constants by using APInt::isSameValue. If the constnats have different bitwidths, APInt::isSameValue will zero extend the constant to make them match. This means for any constant less than 64 bits, m_AllOnes was guaranteed to fail since the zero extended value would not match all ones. I think would also incorrectly consider an i128 with 64 leading zeros and 64 trailing zeros as matching m_AllOnes. To avoid this, this patch adds a new matcher class that just calls isAllOnesOrAllOnesSplat.
1 parent c2d8926 commit 41466a1

File tree

2 files changed

+105
-387
lines changed

2 files changed

+105
-387
lines changed

llvm/include/llvm/CodeGen/SDPatternMatch.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,17 @@ inline SpecificInt_match m_SpecificInt(uint64_t V) {
716716

717717
inline SpecificInt_match m_Zero() { return m_SpecificInt(0U); }
718718
inline SpecificInt_match m_One() { return m_SpecificInt(1U); }
719-
inline SpecificInt_match m_AllOnes() { return m_SpecificInt(~0U); }
719+
720+
struct AllOnes_match {
721+
722+
AllOnes_match() = default;
723+
724+
template <typename MatchContext> bool match(const MatchContext &, SDValue N) {
725+
return isAllOnesOrAllOnesSplat(N);
726+
}
727+
};
728+
729+
inline AllOnes_match m_AllOnes() { return AllOnes_match(); }
720730

721731
/// Match true boolean value based on the information provided by
722732
/// TargetLowering.
@@ -766,7 +776,7 @@ inline BinaryOpc_match<SpecificInt_match, ValTy> m_Neg(const ValTy &V) {
766776

767777
/// Match a Not as a xor(v, -1) or xor(-1, v)
768778
template <typename ValTy>
769-
inline BinaryOpc_match<ValTy, SpecificInt_match, true> m_Not(const ValTy &V) {
779+
inline BinaryOpc_match<ValTy, AllOnes_match, true> m_Not(const ValTy &V) {
770780
return m_Xor(V, m_AllOnes());
771781
}
772782

0 commit comments

Comments
 (0)