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

Fariborz Jahanian
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 

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 
This result in loop-count to be computed incorrectly.

I have not attempted to fix this bug so loop is optimized. Because I 
know how it can be done in its generality; i.e., when iv is modified in 
random fashion after exit test. Rather, I recognize the condition
and disallow the low-level optimization involving 
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

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 <>

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

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

int main()
          Kernel_GetFieldStruc( "120456789");


         2004-01-20      Fariborz Jahanian <>
                 * 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

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: patch_fsf_loopbug.txt
URL: <>
-------------- next part --------------

More information about the Gcc-patches mailing list