This is the first part in fixing PR target/120372.
The current code for canonicalize_comparison, uses gen_move_insn and rtx_cost 
to find
out the cost of generating a constant. This is ok in most cases except sometimes
the comparison instruction can handle different constants than a simple set
intruction can do. This changes to use rtx_cost directly with the outer being 
COMPARE
just like how prepare_cmp_insn handles that.

Note this is also a small speedup and small memory improvement because we are 
not creating
a move for the constant any more. Since we are not creating a psedu-register 
any more, this
also removes the check on that.

Also adds a dump so we can see why one choice was chosen over the other.

Build and tested for aarch64-linux-gnu.

gcc/ChangeLog:

        * expmed.cc (canonicalize_comparison): Use rtx_cost directly
        instead of gen_move_insn. Print out the choice if dump is enabled.

Signed-off-by: Andrew Pinski <quic_apin...@quicinc.com>
---
 gcc/expmed.cc | 23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)

diff --git a/gcc/expmed.cc b/gcc/expmed.cc
index 72dbafe5d9f..d5da199d033 100644
--- a/gcc/expmed.cc
+++ b/gcc/expmed.cc
@@ -6408,18 +6408,25 @@ canonicalize_comparison (machine_mode mode, enum 
rtx_code *code, rtx *imm)
   if (overflow)
     return;
 
-  /* The following creates a pseudo; if we cannot do that, bail out.  */
-  if (!can_create_pseudo_p ())
-    return;
-
-  rtx reg = gen_rtx_REG (mode, LAST_VIRTUAL_REGISTER + 1);
   rtx new_imm = immed_wide_int_const (imm_modif, mode);
 
-  rtx_insn *old_rtx = gen_move_insn (reg, *imm);
-  rtx_insn *new_rtx = gen_move_insn (reg, new_imm);
+  int old_cost = rtx_cost (*imm, mode, COMPARE, 0, true);
+  int new_cost = rtx_cost (new_imm, mode, COMPARE, 0, true);
+
+  if (dump_file && (dump_flags & TDF_DETAILS))
+    {
+      fprintf (dump_file, ";; cmp: %s, old cst: ",
+              GET_RTX_NAME (*code));
+      print_rtl (dump_file, *imm);
+      fprintf (dump_file, " new cst: ");
+      print_rtl (dump_file, new_imm);
+      fprintf (dump_file, "\n");
+      fprintf (dump_file, ";; old cst cost: %d, new cst cost: %d\n",
+              old_cost, new_cost);
+    }
 
   /* Update the immediate and the code.  */
-  if (insn_cost (old_rtx, true) > insn_cost (new_rtx, true))
+  if (old_cost > new_cost)
     {
       *code = equivalent_cmp_code (*code);
       *imm = new_imm;
-- 
2.43.0

Reply via email to