public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] [tree-optimization] Fix for PR97223
@ 2020-10-24  0:19 Eugene Rozenfeld
  2020-10-27  9:23 ` Richard Biener
  0 siblings, 1 reply; 5+ messages in thread
From: Eugene Rozenfeld @ 2020-10-24  0:19 UTC (permalink / raw)
  To: gcc-patches

[-- Attachment #1: Type: text/plain, Size: 533 bytes --]

This patch adds a pattern for folding 
                x < (short) ((unsigned short)x + const)
to
	 x <= SHORT_MAX - const
(and similarly for other integral types) if const is not 0.
as described in PR97223.

For example, without this patch the x86_64-pc-linux code generated for this function

bool f(char x)
{
    return x < (char)(x + 12);
}

is

lea    eax,[rdi+0xc]
cmp    al,dil
setg   al
ret  

With the patch the code is 

cmp    dil,0x73
setle  al
ret    

Tested on x86_64-pc-linux.

Eugene


[-- Attachment #2: 0001-Add-a-tree-optimization-described-in-PR97223.patch --]
[-- Type: application/octet-stream, Size: 1923 bytes --]

From bc5fca4cbafae6b6bbf55787af1d2e5d1538649b Mon Sep 17 00:00:00 2001
From: Eugene Rozenfeld <erozen@microsoft.com>
Date: Fri, 23 Oct 2020 16:47:01 -0700
Subject: [PATCH] Add a tree optimization described in PR97223.
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Convert
x < (short) ((unsigned short)x + const)
to
x <= SHORT_MAX – const
(and similarly for other integral types) if const is not 0.

For example, without this patch the x86_64-pc-linux code generated for this function

bool f(char x)
{
    return x < (char)(x + 12);
}

is

lea    eax,[rdi+0xc]
cmp    al,dil
setg   al
ret

With the patch the code is

cmp    dil,0x73
setle  al
ret
---
 gcc/match.pd | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/gcc/match.pd b/gcc/match.pd
index 17ba04100c7..bc5bed626ec 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -4954,6 +4954,22 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
 			        wi::max_value (prec, UNSIGNED)
 				- wi::to_wide (@1)); })))))
 
+/* Similar to the previous pattern but with additional casts. */
+(for cmp (lt le ge gt)
+     out (gt gt le le)
+ (simplify
+  (cmp:c (convert@3 (plus@2 (convert@4 @0) INTEGER_CST@1)) @0)
+  (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
+       && types_match (TREE_TYPE (@0), TREE_TYPE (@3))
+       && types_match (TREE_TYPE (@4), unsigned_type_for (TREE_TYPE (@0)))
+       && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@4))
+       && wi::to_wide (@1) != 0
+       && single_use (@2))
+   (with { unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
+    (out @0 { wide_int_to_tree (TREE_TYPE (@0),
+			        wi::max_value (prec, SIGNED)
+				- wi::to_wide (@1)); })))))
+
 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
    However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c
    expects the long form, so we restrict the transformation for now.  */
-- 
2.17.1


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

end of thread, other threads:[~2020-11-06  3:47 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-24  0:19 [PATCH] [tree-optimization] Fix for PR97223 Eugene Rozenfeld
2020-10-27  9:23 ` Richard Biener
2020-10-29 19:45   ` [EXTERNAL] " Eugene Rozenfeld
2020-10-30  8:24     ` Richard Biener
2020-11-06  3:46     ` Jeff Law

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