PATCH - [RFC] patch to fix loop optimization bug in [tree-ssa] branch

Fariborz Jahanian fjahanian@apple.com
Wed Jan 21 19:47:00 GMT 2004


Following test case exposes a bug in loop optimization done in
routine doloop_optimize. This shows up in the tree-ssa branch, because
RTL generated in this branch, fits the pattern for a loop which can be
optimized. Bug is that the count of the loop iteration is incorrect.
This, in turn, is caused by computing an incorrect initial value for
the loop iv.  In this case, loop optimization logic does not compute
the initial of the iv correctly when there is post-increment after
the test condition.
Of course, this is just a simple test case and there may be a variety
of ways that iv may change after the expression in the exit test has 
been
evaluated.

Routine loop_bivs_init_find finds initial value of iv by scanning the
LOOP_BEG backward until the iv initialization is found. This value,
however, is not the correct initial_value because of post-increment 
operation.
This result in loop-count to be computed incorrectly.

I have not attempted to fix this bug so loop is optimized. Because I 
don't
know how it can be done in its generality; i.e., when iv is modified in 
a
random fashion after exit test. Rather, I recognize the condition
and disallow the low-level optimization involving 
decrement-loop-counter-and-branch
instruction. So, attached patch recognizes that the iv has been 
modified in the exit
block after the exit comparison and turns off this
optimization. Besides this patch; I see other possibilities:

1) Find the true initial value of iv and use it to optimize.

2) Find a better place for disallowing this optimization for this loop
    pattern.

3) Modify the loop RTL pattern;  so this loop is not valid
    to optimize.

Note that this bug does not show itself in gcc3.4 because different 
loop RTL
pattern is generated there.

- Fariborz Jahanian <fjahanian@apple.com>



extern void FOO();
void Kernel_GetFieldStruc (char *FieldName)
{
   unsigned long int StrSize = 0;
   int i = 0;
   StrSize = strlen (FieldName);

   while (FieldName[0] != '\0')
   {
         i=0;
         FOO();
         while (i++ < StrSize)           /* post-increment */
           ++FieldName;
   }
}

int main()
{
          Kernel_GetFieldStruc( "120456789");
}


ChangeLog:

         2004-01-20      Fariborz Jahanian <fjahanian@apple.com>
                 * cfgloop.h: Added exit_cond_side_effect to struct loop.
                 * doloop.c (doloop_valid_p): Check for 
exit_cond_side_effect and
                 do not do loop optimization with low-overhead 
instruction if it is set.
                 * loop.c (record_initial_inloop): Record insn which 
bumps iv in loop
                 exit test block.
                 (side_effect_condition): Check for condition which 
makes the special
                 low-overhead loop optimization invalid.
                 (check_on_exit_cond): Main routine which scans loops, 
finds exit test
                 block and makes low-overhead loop optimization invalid 
if certain
                 conditions are met.
                 (loop_bivs_init_find): This routine calls 
check_on_exit_cond for each
                 loop.

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: patch_fsf_loopbug.txt
URL: <http://gcc.gnu.org/pipermail/gcc-patches/attachments/20040121/dc77c07c/attachment.txt>
-------------- next part --------------



More information about the Gcc-patches mailing list