[PATCH] Simplify RTL for loop exits without nesting

Roger Sayle roger@eyesopen.com
Thu Sep 19 20:08:00 GMT 2002


The following patch is the latest in a series to simplify the RTL
that's initially created for loops by GCC's front-ends.  The goal is
to both reduce the amount of RTL that is generated only to be thrown
away later, and to prevent LOOP notes being corrupted by CFG cleaning
prior to the loop pass.

[Once the LOOP notes are reliable, I can prevent GCSE from modifying
jumps to loop headers and thereby from creating irreducible loops].

The following patch avoids the "conditional branch over unconditional
jump" idiom that is currently used to implement loop exit tests.  It
turns out that over 99% of loop exits break out of the inner most loop
without leaving a block scope.  In these cases, we know that we won't
require a stmt.c "fixup" (to call destructors and/or clean-up the stack
frame).  Hence we can replace the "branch-over-jump" sequence with a
simple conditional branch.  A "sufficient" test is that the loop being
exited is the top of the nesting stack.  Fixups are only required when
leaving block scopes, which would appear above the target on the nesting
stack.

Whilst I was there I also tidied up an instance of "do_jump" to use
the higher-level "jumpif", which I find easier to read and matches
the "jumpifnot" call introduced by this patch.

The following patch has been tested with "make bootstrap" and "make
-k check", all languages except Ada and treelang, on i686-pc-linux-gnu
with no new regressions.

During the development of this patch, I evaluated a variant that just
called "abort()" whenever "whichloop != nesting_stack".  Interestingly
that patch survived a "make bootstrap" of all languages except Ada and
treelang, including libstdc++ and libjava.  Alas, the abort triggered
during "make -k check" in the gcj and f77 test suites.  So fixups are
extremely rare, but are still required by some non C-family front-ends.


Ok for the gcc-3_4-basic-improvements-branch?


2002-09-19  Roger Sayle  <roger@eyesopen.com>

	* stmt.c (expand_exit_loop_if_false): Expand a simple conditional
	jump, if this loop is the top of the current nesting stack.


Index: stmt.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/stmt.c,v
retrieving revision 1.269
diff -c -3 -p -r1.269 stmt.c
*** stmt.c	14 Aug 2002 10:04:48 -0000	1.269
--- stmt.c	12 Sep 2002 22:39:06 -0000
*************** expand_exit_loop_if_false (whichloop, co
*** 2699,2720 ****
       struct nesting *whichloop;
       tree cond;
  {
!   rtx label = gen_label_rtx ();
!   rtx last_insn;
    clear_last_expr ();

    if (whichloop == 0)
      whichloop = loop_stack;
    if (whichloop == 0)
      return 0;
    /* In order to handle fixups, we actually create a conditional jump
       around an unconditional branch to exit the loop.  If fixups are
       necessary, they go before the unconditional branch.  */

!   do_jump (cond, NULL_RTX, label);
!   last_insn = get_last_insn ();
!   if (GET_CODE (last_insn) == CODE_LABEL)
!     whichloop->data.loop.alt_end_label = last_insn;
    expand_goto_internal (NULL_TREE, whichloop->data.loop.end_label,
  			NULL_RTX);
    emit_label (label);
--- 2699,2725 ----
       struct nesting *whichloop;
       tree cond;
  {
!   rtx label;
    clear_last_expr ();

    if (whichloop == 0)
      whichloop = loop_stack;
    if (whichloop == 0)
      return 0;
+
+   /* Check if we definitely won't need a fixup.  */
+   if (whichloop == nesting_stack)
+     {
+       jumpifnot (cond, whichloop->data.loop.end_label);
+       return 1;
+     }
+
    /* In order to handle fixups, we actually create a conditional jump
       around an unconditional branch to exit the loop.  If fixups are
       necessary, they go before the unconditional branch.  */

!   label = gen_label_rtx ();
!   jumpif (cond, label);
    expand_goto_internal (NULL_TREE, whichloop->data.loop.end_label,
  			NULL_RTX);
    emit_label (label);

Roger
--
Roger Sayle,                         E-mail: roger@eyesopen.com
OpenEye Scientific Software,         WWW: http://www.eyesopen.com/
Suite 1107, 3600 Cerrillos Road,     Tel: (+1) 505-473-7385
Santa Fe, New Mexico, 87507.         Fax: (+1) 505-473-0833



More information about the Gcc-patches mailing list