Re: [PATCH] csa, postreload: Improve csa after recent cselib changes [PR94516]

2020-05-05 Thread Jeff Law via Gcc-patches
On Tue, 2020-05-05 at 09:38 +0200, Jakub Jelinek wrote:
> Hi!
> 
> On Wed, Apr 22, 2020 at 12:02:12PM -0600, Jeff Law via Gcc-patches wrote:
> > > Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?
> > > 
> > > 2020-04-20  Jakub Jelinek  
> > > 
> > >   PR rtl-optimization/94516
> > >   * postreload.c (reload_cse_simplify): When replacing sp = sp + const
> > >   with sp = reg, add REG_EQUAL note with sp + const.
> > >   * combine-stack-adj.c (try_apply_stack_adjustment): Change return
> > >   type from int to bool.  Add LIVE and OTHER_INSN arguments.  Undo
> > >   postreload sp = sp + const to sp = reg optimization if needed and
> > >   possible.
> > >   (combine_stack_adjustments_for_block): Add LIVE argument.  Handle
> > >   reg = sp insn with sp + const REG_EQUAL note.  Adjust
> > >   try_apply_stack_adjustment caller, call
> > >   df_simulate_initialize_forwards and df_simulate_one_insn_forwards.
> > >   (combine_stack_adjustments): Allocate and free LIVE bitmap,
> > >   adjust combine_stack_adjustments_for_block caller.
> > I'd probably defer to gcc-11 since this is "just" a missed optimization and
> > we're
> > getting real close to cutting an RC and I'd hate to introduce new 
> > instability
> > at
> > this point.
> 
> Is the patch now ok for stage1?
Yes, of course.
jeff



Re: [PATCH] csa, postreload: Improve csa after recent cselib changes [PR94516]

2020-05-05 Thread Jakub Jelinek via Gcc-patches
Hi!

On Wed, Apr 22, 2020 at 12:02:12PM -0600, Jeff Law via Gcc-patches wrote:
> > Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?
> > 
> > 2020-04-20  Jakub Jelinek  
> > 
> > PR rtl-optimization/94516
> > * postreload.c (reload_cse_simplify): When replacing sp = sp + const
> > with sp = reg, add REG_EQUAL note with sp + const.
> > * combine-stack-adj.c (try_apply_stack_adjustment): Change return
> > type from int to bool.  Add LIVE and OTHER_INSN arguments.  Undo
> > postreload sp = sp + const to sp = reg optimization if needed and
> > possible.
> > (combine_stack_adjustments_for_block): Add LIVE argument.  Handle
> > reg = sp insn with sp + const REG_EQUAL note.  Adjust
> > try_apply_stack_adjustment caller, call
> > df_simulate_initialize_forwards and df_simulate_one_insn_forwards.
> > (combine_stack_adjustments): Allocate and free LIVE bitmap,
> > adjust combine_stack_adjustments_for_block caller.
> I'd probably defer to gcc-11 since this is "just" a missed optimization and 
> we're
> getting real close to cutting an RC and I'd hate to introduce new instability 
> at
> this point.

Is the patch now ok for stage1?

Jakub



Re: [PATCH] csa, postreload: Improve csa after recent cselib changes [PR94516]

2020-04-22 Thread Jeff Law via Gcc-patches
On Tue, 2020-04-21 at 01:00 +0200, Jakub Jelinek wrote:
> Hi!
> 
> This patch addresses a missed optimization caused by the cselib changes.
> Already in the past postreload could replace sp = sp + const_int with
> sp = regxy if regxy already has the right value, but with the cselib
> changes it happens several times more often.  It can result in smaller
> code, so it seems undesirable to prevent such optimizations, but
> unfortunately it can get into the way of stack adjustment coalescing,
> where e.g. if we used to have sp = sp + 32; sp = sp - 8;, previously
> we'd turn that into sp = sp + 24;, but now postreload optimizes
> into sp = r12; sp = sp - 8; and csa gives up.
> 
> The patch just adds a REG_EQUAL note when changing sp = sp + const into
> sp = reg, where we remember it was actually a stack adjustment by certain
> constant, and the combine-stack-adj changes than make use of those REG_EQUAL
> notes, together with LR tracking (csa did enable the note problem, just
> didn't simulate each insn) so that we can add the needed clobbers etc.
> (taken from the other stack adjustment insn).
> 
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?
> 
> 2020-04-20  Jakub Jelinek  
> 
>   PR rtl-optimization/94516
>   * postreload.c (reload_cse_simplify): When replacing sp = sp + const
>   with sp = reg, add REG_EQUAL note with sp + const.
>   * combine-stack-adj.c (try_apply_stack_adjustment): Change return
>   type from int to bool.  Add LIVE and OTHER_INSN arguments.  Undo
>   postreload sp = sp + const to sp = reg optimization if needed and
>   possible.
>   (combine_stack_adjustments_for_block): Add LIVE argument.  Handle
>   reg = sp insn with sp + const REG_EQUAL note.  Adjust
>   try_apply_stack_adjustment caller, call
>   df_simulate_initialize_forwards and df_simulate_one_insn_forwards.
>   (combine_stack_adjustments): Allocate and free LIVE bitmap,
>   adjust combine_stack_adjustments_for_block caller.
I'd probably defer to gcc-11 since this is "just" a missed optimization and 
we're
getting real close to cutting an RC and I'd hate to introduce new instability at
this point.

Jeff
> 



[PATCH] csa, postreload: Improve csa after recent cselib changes [PR94516]

2020-04-20 Thread Jakub Jelinek via Gcc-patches
Hi!

This patch addresses a missed optimization caused by the cselib changes.
Already in the past postreload could replace sp = sp + const_int with
sp = regxy if regxy already has the right value, but with the cselib
changes it happens several times more often.  It can result in smaller
code, so it seems undesirable to prevent such optimizations, but
unfortunately it can get into the way of stack adjustment coalescing,
where e.g. if we used to have sp = sp + 32; sp = sp - 8;, previously
we'd turn that into sp = sp + 24;, but now postreload optimizes
into sp = r12; sp = sp - 8; and csa gives up.

The patch just adds a REG_EQUAL note when changing sp = sp + const into
sp = reg, where we remember it was actually a stack adjustment by certain
constant, and the combine-stack-adj changes than make use of those REG_EQUAL
notes, together with LR tracking (csa did enable the note problem, just
didn't simulate each insn) so that we can add the needed clobbers etc.
(taken from the other stack adjustment insn).

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

2020-04-20  Jakub Jelinek  

PR rtl-optimization/94516
* postreload.c (reload_cse_simplify): When replacing sp = sp + const
with sp = reg, add REG_EQUAL note with sp + const.
* combine-stack-adj.c (try_apply_stack_adjustment): Change return
type from int to bool.  Add LIVE and OTHER_INSN arguments.  Undo
postreload sp = sp + const to sp = reg optimization if needed and
possible.
(combine_stack_adjustments_for_block): Add LIVE argument.  Handle
reg = sp insn with sp + const REG_EQUAL note.  Adjust
try_apply_stack_adjustment caller, call
df_simulate_initialize_forwards and df_simulate_one_insn_forwards.
(combine_stack_adjustments): Allocate and free LIVE bitmap,
adjust combine_stack_adjustments_for_block caller.

--- gcc/postreload.c.jj 2020-04-19 12:10:19.042877054 +0200
+++ gcc/postreload.c2020-04-20 12:16:01.672848537 +0200
@@ -97,6 +97,16 @@ reload_cse_simplify (rtx_insn *insn, rtx
   if (NO_FUNCTION_CSE && CALL_P (insn))
 return false;
 
+  /* Remember if this insn has been sp += const_int.  */
+  rtx sp_set = set_for_reg_notes (insn);
+  rtx sp_addend = NULL_RTX;
+  if (sp_set
+  && SET_DEST (sp_set) == stack_pointer_rtx
+  && GET_CODE (SET_SRC (sp_set)) == PLUS
+  && XEXP (SET_SRC (sp_set), 0) == stack_pointer_rtx
+  && CONST_INT_P (XEXP (SET_SRC (sp_set), 1)))
+sp_addend = XEXP (SET_SRC (sp_set), 1);
+
   if (GET_CODE (body) == SET)
 {
   int count = 0;
@@ -180,6 +190,15 @@ reload_cse_simplify (rtx_insn *insn, rtx
reload_cse_simplify_operands (insn, testreg);
 }
 
+  /* If sp += const_int insn is changed into sp = reg;, add REG_EQUAL
+ note so that the stack_adjustments pass can undo it if beneficial.  */
+  if (sp_addend
+  && SET_DEST (sp_set) == stack_pointer_rtx
+  && REG_P (SET_SRC (sp_set)))
+set_dst_reg_note (insn, REG_EQUAL,
+ gen_rtx_PLUS (Pmode, stack_pointer_rtx,
+   sp_addend), stack_pointer_rtx);
+
 done:
   return (EDGE_COUNT (insn_bb->succs) != insn_bb_succs);
 }
--- gcc/combine-stack-adj.c.jj  2020-01-12 11:54:36.221416305 +0100
+++ gcc/combine-stack-adj.c 2020-04-20 14:52:47.294323991 +0200
@@ -70,9 +70,10 @@ static rtx single_set_for_csa (rtx_insn
 static void free_csa_reflist (struct csa_reflist *);
 static struct csa_reflist *record_one_stack_ref (rtx_insn *, rtx *,
 struct csa_reflist *);
-static int try_apply_stack_adjustment (rtx_insn *, struct csa_reflist *,
-  HOST_WIDE_INT, HOST_WIDE_INT);
-static void combine_stack_adjustments_for_block (basic_block);
+static bool try_apply_stack_adjustment (rtx_insn *, struct csa_reflist *,
+   HOST_WIDE_INT, HOST_WIDE_INT,
+   bitmap, rtx_insn *);
+static void combine_stack_adjustments_for_block (basic_block, bitmap);
 
 
 /* Main entry point for stack adjustment combination.  */
@@ -81,9 +82,12 @@ static void
 combine_stack_adjustments (void)
 {
   basic_block bb;
+  bitmap live = BITMAP_ALLOC (_obstack);
 
   FOR_EACH_BB_FN (bb, cfun)
-combine_stack_adjustments_for_block (bb);
+combine_stack_adjustments_for_block (bb, live);
+
+  BITMAP_FREE (live);
 }
 
 /* Recognize a MEM of the form (sp) or (plus sp const).  */
@@ -219,18 +223,60 @@ no_unhandled_cfa (rtx_insn *insn)
as each of the memories and stack references in REFLIST.  Return true
on success.  */
 
-static int
+static bool
 try_apply_stack_adjustment (rtx_insn *insn, struct csa_reflist *reflist,
-   HOST_WIDE_INT new_adjust, HOST_WIDE_INT delta)
+   HOST_WIDE_INT new_adjust, HOST_WIDE_INT delta,
+   bitmap live, rtx_insn *other_insn)
 {
   struct