public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* RFC: Two minor optimization patterns
@ 2015-01-13 22:47 Rasmus Villemoes
  2015-01-13 22:56 ` Andrew Pinski
  2015-01-14 13:14 ` RFC: Two minor optimization patterns Marc Glisse
  0 siblings, 2 replies; 21+ messages in thread
From: Rasmus Villemoes @ 2015-01-13 22:47 UTC (permalink / raw)
  To: gcc-patches

[My first attempt at submitting a patch for gcc, so please forgive me
if I'm not following the right protocol.]

Sometimes rounding a variable to the next even integer is written x += x
& 1. This usually means using an extra register (and hence at least an
extra mov instruction) compared to the equivalent x = (x + 1) & ~1. The
first pattern below tries to do this transformation.

While playing with various ways of rounding down, I noticed that gcc
already optimizes all of x-(x&3), x^(x&3) and x&~(x&3) to simply
x&~3. In fact, x&~(x&y) is rewritten as x&~y. However, the dual of this
is not handled, so I included the second pattern below.

I've tested the below in the sense that gcc compiles and that trivial
test cases get compiled as expected.

Rasmus



diff --git gcc/match.pd gcc/match.pd
index 81c4ee6..04a0bc4 100644
--- gcc/match.pd
+++ gcc/match.pd
@@ -262,6 +262,16 @@ along with GCC; see the file COPYING3.  If not see
  (abs tree_expr_nonnegative_p@0)
  @0)
 
+/* x + (x & 1) -> (x + 1) & ~1 */
+(simplify
+ (plus @0 (bit_and @0 integer_onep@1))
+ (bit_and (plus @0 @1) (bit_not @1)))
+
+/* x | ~(x | y) -> x | ~y */
+(simplify
+ (bit_ior @0 (bit_not (bit_ior @0 @1)))
+ (bit_ior @0 (bit_not @1)))
+
 
 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
    when profitable.

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

end of thread, other threads:[~2015-05-01 18:29 UTC | newest]

Thread overview: 21+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-01-13 22:47 RFC: Two minor optimization patterns Rasmus Villemoes
2015-01-13 22:56 ` Andrew Pinski
2015-01-14  9:52   ` Richard Biener
2015-01-14 12:45     ` Rasmus Villemoes
2015-01-14 14:01       ` Richard Biener
2015-01-21 10:50         ` [PATCH 0/4] A few " Rasmus Villemoes
2015-01-21 10:50           ` [PATCH 1/4] match.pd: Add x + (x & 1) -> (x + 1) & ~1 pattern Rasmus Villemoes
2015-04-30  9:34             ` Richard Biener
2015-05-01 18:26             ` Jeff Law
2015-01-21 10:55           ` [PATCH 2/4] match.pd: Add x & ~(x & y) -> x & ~y pattern Rasmus Villemoes
2015-05-01 18:29             ` Jeff Law
2015-01-21 10:58           ` [PATCH 3/4] match.pd: Add x | ~(x | y) -> x | " Rasmus Villemoes
2015-01-21 11:32             ` Marek Polacek
2015-01-21 11:17           ` [PATCH 4/4] match.pd: Add x + ((-x) & m) -> (x + m) & ~m pattern Rasmus Villemoes
2015-04-30  9:42             ` Richard Biener
2015-04-30 11:56               ` Marc Glisse
2015-04-30 12:25                 ` Richard Biener
2015-01-14 13:14 ` RFC: Two minor optimization patterns Marc Glisse
2015-01-14 13:58   ` Richard Biener
2015-01-14 14:31     ` Marc Glisse
2015-01-14 14:49       ` Richard Biener

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).