Skip to content

Commit

Permalink
[SelectionDAG] Correct the implementation of m_AllOnes. (#90776)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
topperc committed May 1, 2024
1 parent c2d8926 commit 41466a1
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 387 deletions.
14 changes: 12 additions & 2 deletions llvm/include/llvm/CodeGen/SDPatternMatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,17 @@ inline SpecificInt_match m_SpecificInt(uint64_t V) {

inline SpecificInt_match m_Zero() { return m_SpecificInt(0U); }
inline SpecificInt_match m_One() { return m_SpecificInt(1U); }
inline SpecificInt_match m_AllOnes() { return m_SpecificInt(~0U); }

struct AllOnes_match {

AllOnes_match() = default;

template <typename MatchContext> bool match(const MatchContext &, SDValue N) {
return isAllOnesOrAllOnesSplat(N);
}
};

inline AllOnes_match m_AllOnes() { return AllOnes_match(); }

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

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

Expand Down
Loading

0 comments on commit 41466a1

Please sign in to comment.