Hi! The following patch avoids infinite recursion during generic folding. The (cmp (bswap @0) INTEGER_CST@1) simplification relies on (bswap @1) actually being simplified, if it is not simplified, we just move the bswap from one operand to the other and if @0 is also INTEGER_CST, we apply the same rule next.
The reason why bswap @1 isn't folded to INTEGER_CST is that the INTEGER_CST has TREE_OVERFLOW set on it and fold-const-call.cc predicate punts in such cases: static inline bool integer_cst_p (tree t) { return TREE_CODE (t) == INTEGER_CST && !TREE_OVERFLOW (t); } The patch uses ! modifier to ensure the bswap is simplified, but because ! is only supported in gimple-match, guards it also with #if GIMPLE. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2022-02-22 Jakub Jelinek <ja...@redhat.com> PR tree-optimization/104644 * match.pd (cmp (bswap @0) INTEGER_CST@1): Restrict optimization to GIMPLE only and use ! modifier on bswap. * gcc.dg/pr104644.c: New test. --- gcc/match.pd.jj 2022-02-18 12:38:06.075393091 +0100 +++ gcc/match.pd 2022-02-22 20:22:02.222022022 +0100 @@ -3959,10 +3959,13 @@ (define_operator_list SYNC_FETCH_AND_AND (cmp (bswap@2 @0) (bswap @1)) (with { tree ctype = TREE_TYPE (@2); } (cmp (convert:ctype @0) (convert:ctype @1)))) +#if GIMPLE (simplify (cmp (bswap @0) INTEGER_CST@1) (with { tree ctype = TREE_TYPE (@1); } - (cmp (convert:ctype @0) (bswap @1))))) + (cmp (convert:ctype @0) (bswap! @1)))) +#endif + ) /* (bswap(x) >> C1) & C2 can sometimes be simplified to (x >> C3) & C2. */ (simplify (bit_and (convert1? (rshift@0 (convert2? (bswap@4 @1)) INTEGER_CST@2)) --- gcc/testsuite/gcc.dg/pr104644.c.jj 2022-02-22 20:02:32.020408468 +0100 +++ gcc/testsuite/gcc.dg/pr104644.c 2022-02-22 20:02:04.609785996 +0100 @@ -0,0 +1,9 @@ +/* PR tree-optimization/104644 */ +/* { dg-do compile } */ +/* { dg-options "-Wno-overflow" } */ + +int +foo (void) +{ + return __builtin_bswap16 (1.31072e+5f) != (signed char) 1.31072e+5f; +} Jakub