public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Need help with match.pd crash
@ 2022-10-06 22:57 Michael Collison
  2022-10-06 23:18 ` Andrew Pinski
  2022-10-06 23:22 ` Jakub Jelinek
  0 siblings, 2 replies; 3+ messages in thread
From: Michael Collison @ 2022-10-06 22:57 UTC (permalink / raw)
  To: gcc

I am trying to improve code generation for coremark to match a recent 
improvement that was made in LLVM.

I added the following transformation to match.pd which attempts to 
replace a branch with straight line code:

/* (cond (and (x , 0x1) == 0), y, (z ^ y) ) -> (-(and (x , 0x1)) & z ) ^ 
y */
(simplify
     (cond (eq (bit_and @0 integer_onep@1)
                  integer_zerop)
          @2
          (bit_xor:c @3 @2))
         (bit_xor (bit_and (negate (bit_and @0 @1)) @3) @2))

I get a internal error, but in stepping through the debugger I can see 
the pattern matches, but fails when when it tries to further simplify 
and match another pattern in match.pd:

/* x & C -> x if we know that x & ~C == 0.  */
#if GIMPLE
(simplify
  (bit_and SSA_NAME@0 INTEGER_CST@1)
  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
   @0))
#endif

The crash occurs in wi::bit_and_not. Before digging further I want to 
ask if there is a problem with the way I wrote the transformation?




^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Need help with match.pd crash
  2022-10-06 22:57 Need help with match.pd crash Michael Collison
@ 2022-10-06 23:18 ` Andrew Pinski
  2022-10-06 23:22 ` Jakub Jelinek
  1 sibling, 0 replies; 3+ messages in thread
From: Andrew Pinski @ 2022-10-06 23:18 UTC (permalink / raw)
  To: Michael Collison; +Cc: gcc

On Thu, Oct 6, 2022 at 4:00 PM Michael Collison <collison@rivosinc.com> wrote:
>
> I am trying to improve code generation for coremark to match a recent
> improvement that was made in LLVM.
>
> I added the following transformation to match.pd which attempts to
> replace a branch with straight line code:
>
> /* (cond (and (x , 0x1) == 0), y, (z ^ y) ) -> (-(and (x , 0x1)) & z ) ^
> y */
> (simplify
>      (cond (eq (bit_and @0 integer_onep@1)
>                   integer_zerop)
>           @2
>           (bit_xor:c @3 @2))
>          (bit_xor (bit_and (negate (bit_and @0 @1)) @3) @2))
>

I suspect you are missing a convert as @0/@1 might be a different type
from type (@2/@3's type).
Try:
(simplify
     (cond (eq (bit_and @0 integer_onep@1)
                  integer_zerop)
          @2
          (bit_xor:c @3 @2))
         (bit_xor (bit_and (negate (convert:type (bit_and @0 @1))) @3) @2))

But I am not 100% sure that match.pd (phiopt since that is where the
transformation is really happening) is the right place for this. I
suspect a better place is rtl level inside either simplify-rtl.cc
and/or inside ifcvt.cc.
Because on some targets has conditional moves which might be better
than doing an and/neg/and/xor.
AARCH64 and MIPS are good examples of that (I know because I spent
time making sure GCC produces the conditional moves for coremarks for
those targets inside crc8).

Thanks,
Andrew Pinski

> I get a internal error, but in stepping through the debugger I can see
> the pattern matches, but fails when when it tries to further simplify
> and match another pattern in match.pd:
>
> /* x & C -> x if we know that x & ~C == 0.  */
> #if GIMPLE
> (simplify
>   (bit_and SSA_NAME@0 INTEGER_CST@1)
>   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
>        && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
>    @0))
> #endif
>
> The crash occurs in wi::bit_and_not. Before digging further I want to
> ask if there is a problem with the way I wrote the transformation?
>
>
>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Need help with match.pd crash
  2022-10-06 22:57 Need help with match.pd crash Michael Collison
  2022-10-06 23:18 ` Andrew Pinski
@ 2022-10-06 23:22 ` Jakub Jelinek
  1 sibling, 0 replies; 3+ messages in thread
From: Jakub Jelinek @ 2022-10-06 23:22 UTC (permalink / raw)
  To: Michael Collison; +Cc: gcc

On Thu, Oct 06, 2022 at 06:57:40PM -0400, Michael Collison wrote:
> I am trying to improve code generation for coremark to match a recent
> improvement that was made in LLVM.
> 
> I added the following transformation to match.pd which attempts to replace a
> branch with straight line code:
> 
> /* (cond (and (x , 0x1) == 0), y, (z ^ y) ) -> (-(and (x , 0x1)) & z ) ^ y
> */
> (simplify
>     (cond (eq (bit_and @0 integer_onep@1)
>                  integer_zerop)
>          @2
>          (bit_xor:c @3 @2))
>         (bit_xor (bit_and (negate (bit_and @0 @1)) @3) @2))
> 
> I get a internal error, but in stepping through the debugger I can see the
> pattern matches, but fails when when it tries to further simplify and match
> another pattern in match.pd:
> 
> /* x & C -> x if we know that x & ~C == 0.  */
> #if GIMPLE
> (simplify
>  (bit_and SSA_NAME@0 INTEGER_CST@1)
>  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
>       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
>   @0))
> #endif
> 
> The crash occurs in wi::bit_and_not. Before digging further I want to ask if
> there is a problem with the way I wrote the transformation?

Yes.  The way you wrote it, @0 and @1 (and the zero) will have the same type
(or compatible) and @2 and @3 too, but the replacement expression relies
on all of @0, @1, @2 and @3 to have compatible types.
If you have
int x;
long long y, z;
..
(x & 1) == 0 ? y : z ^ y
then
(-(x & 1) & z) ^ y
is invalid in GIMPLE.
It can be even more incompatible, e.g. y and z could be integral vectors
while x could be scalar integer, etc.
If both TREE_TYPE (@0) and type (aka TREE_TYPE (@2)/TREE_TYPE (@3)) are
scalar, then perhaps you could just add (convert? ...) around bit_and,
otherwise I think you'd better require TREE_TYPE (@0) and type to be
compatible types.
On the other side, you probably should handle also
(x & 1) != 0 ? z ^ y : y

	Jakub


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-10-06 23:22 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-06 22:57 Need help with match.pd crash Michael Collison
2022-10-06 23:18 ` Andrew Pinski
2022-10-06 23:22 ` Jakub Jelinek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).