From: Andrew Pinski <pinskia@gmail.com>
To: gcc-patches@gcc.gnu.org
Cc: Andrew Pinski <pinskia@gmail.com>
Subject: [PATCHv2] Improve factor_out_conditional_operation for conversions and constants
Date: Tue, 24 Oct 2023 06:45:58 +0000 [thread overview]
Message-ID: <20231024064557.213050-1-pinskia@gmail.com> (raw)
In the case of a NOP conversion (precisions of the 2 types are equal),
factoring out the conversion can be done even if int_fits_type_p returns
false and even when the conversion is defined by a statement inside the
conditional. Since it is a NOP conversion there is no zero/sign extending
happening which is why it is ok to be done here; we were trying to prevent
an extra sign/zero extend from being moved away from definition which no-op
conversions are not.
Bootstrapped and tested on x86_64-linux-gnu with no regressions.
gcc/ChangeLog:
PR tree-optimization/104376
PR tree-optimization/101541
* tree-ssa-phiopt.cc (factor_out_conditional_operation):
Allow nop conversions even if it is defined by a statement
inside the conditional.
gcc/testsuite/ChangeLog:
PR tree-optimization/101541
* gcc.dg/tree-ssa/phi-opt-39.c: New test.
---
gcc/testsuite/gcc.dg/tree-ssa/phi-opt-39.c | 43 ++++++++++++++++++++++
gcc/tree-ssa-phiopt.cc | 16 ++++++--
2 files changed, 56 insertions(+), 3 deletions(-)
create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-39.c
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-39.c b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-39.c
new file mode 100644
index 00000000000..6b6006a96db
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-39.c
@@ -0,0 +1,43 @@
+/* { dg-options "-O2 -fdump-tree-phiopt" } */
+
+unsigned f0(int A)
+{
+// A == 0? A : -A same as -A
+ if (A == 0) return A;
+ return -A;
+}
+
+unsigned f1(int A)
+{
+// A != 0? A : -A same as A
+ if (A != 0) return A;
+ return -A;
+}
+unsigned f2(int A)
+{
+// A >= 0? A : -A same as abs (A)
+ if (A >= 0) return A;
+ return -A;
+}
+unsigned f3(int A)
+{
+// A > 0? A : -A same as abs (A)
+ if (A > 0) return A;
+ return -A;
+}
+unsigned f4(int A)
+{
+// A <= 0? A : -A same as -abs (A)
+ if (A <= 0) return A;
+ return -A;
+}
+unsigned f5(int A)
+{
+// A < 0? A : -A same as -abs (A)
+ if (A < 0) return A;
+ return -A;
+}
+
+/* f4 and f5 are not allowed to be optimized in early phi-opt. */
+/* { dg-final { scan-tree-dump-times "if" 2 "phiopt1" } } */
+/* { dg-final { scan-tree-dump-not "if" "phiopt2" } } */
diff --git a/gcc/tree-ssa-phiopt.cc b/gcc/tree-ssa-phiopt.cc
index 312a6f9082b..bb55a4fba33 100644
--- a/gcc/tree-ssa-phiopt.cc
+++ b/gcc/tree-ssa-phiopt.cc
@@ -310,7 +310,9 @@ factor_out_conditional_operation (edge e0, edge e1, gphi *phi,
return NULL;
/* If arg1 is an INTEGER_CST, fold it to new type. */
if (INTEGRAL_TYPE_P (TREE_TYPE (new_arg0))
- && int_fits_type_p (arg1, TREE_TYPE (new_arg0)))
+ && (int_fits_type_p (arg1, TREE_TYPE (new_arg0))
+ || (TYPE_PRECISION (TREE_TYPE (new_arg0))
+ == TYPE_PRECISION (TREE_TYPE (arg1)))))
{
if (gimple_assign_cast_p (arg0_def_stmt))
{
@@ -322,8 +324,12 @@ factor_out_conditional_operation (edge e0, edge e1, gphi *phi,
if arg0_def_stmt is the only non-debug stmt in
its basic block, because then it is possible this
could enable further optimizations (minmax replacement
- etc.). See PR71016. */
- if (new_arg0 != gimple_cond_lhs (cond_stmt)
+ etc.). See PR71016.
+ Note no-op conversions don't have this issue as
+ it will not generate any zero/sign extend in that case. */
+ if ((TYPE_PRECISION (TREE_TYPE (new_arg0))
+ != TYPE_PRECISION (TREE_TYPE (arg1)))
+ && new_arg0 != gimple_cond_lhs (cond_stmt)
&& new_arg0 != gimple_cond_rhs (cond_stmt)
&& gimple_bb (arg0_def_stmt) == e0->src)
{
@@ -354,6 +360,10 @@ factor_out_conditional_operation (edge e0, edge e1, gphi *phi,
return NULL;
}
new_arg1 = fold_convert (TREE_TYPE (new_arg0), arg1);
+
+ /* Drop the overlow that fold_convert might add. */
+ if (TREE_OVERFLOW (new_arg1))
+ new_arg1 = drop_tree_overflow (new_arg1);
}
else
return NULL;
--
2.34.1
next reply other threads:[~2023-10-24 6:45 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-10-24 6:45 Andrew Pinski [this message]
2023-10-24 7:14 ` Richard Biener
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20231024064557.213050-1-pinskia@gmail.com \
--to=pinskia@gmail.com \
--cc=gcc-patches@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).