Skip to content

Commit

Permalink
middle-end: Simplify subtract where both arguments are being bitwise …
Browse files Browse the repository at this point in the history
…inverted.

This adds a match.pd rule that drops the bitwwise nots when both arguments to a
subtract is inverted. i.e. for:

float g(float a, float b)
{
  return ~(int)a - ~(int)b;
}

we instead generate

float g(float a, float b)
{
  return (int)b - (int)a;
}

We already do a limited version of this from the fold_binary fold functions but
this makes a more general version in match.pd that applies more often.

gcc/ChangeLog:

	* match.pd: New bit_not rule.

gcc/testsuite/ChangeLog:

	* gcc.dg/subnot.c: New test.
  • Loading branch information
TamarChristinaArm committed Aug 4, 2022
1 parent c832ec4 commit be58bf9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
5 changes: 5 additions & 0 deletions gcc/match.pd
Original file line number Diff line number Diff line change
Expand Up @@ -1308,6 +1308,11 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
(simplify
(bit_not (plus:c (bit_not @0) @1))
(minus @0 @1))
/* (~X - ~Y) -> Y - X. */
(simplify
(minus (bit_not @0) (bit_not @1))
(with { tree utype = unsigned_type_for (type); }
(convert (minus (convert:utype @1) (convert:utype @0)))))

/* ~(X - Y) -> ~X + Y. */
(simplify
Expand Down
9 changes: 9 additions & 0 deletions gcc/testsuite/gcc.dg/subnot.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* { dg-do compile } */
/* { dg-options "-O -fdump-tree-optimized" } */

float g(float a, float b)
{
return ~(int)a - ~(int)b;
}

/* { dg-final { scan-tree-dump-not "~" "optimized" } } */

0 comments on commit be58bf9

Please sign in to comment.