This is the mail archive of the mailing list for the GCC project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: -funsafe-loop-optimizations


> > several times some people sounded opinion that in addition to improving
> > analyses in loop optimizer, we might also add a flag that would enable
> > the compiler to assume that the compiled code does not contain loops
> > behaving in "weird" way (loops whose control induction variables
> > overflows, infinite loops with nontrivial exit conditions).
> Are these optimizations permitted by the C standard, the Ada standard
> etc.?

in general no; they may alter semantics of the program, if there happens
to be such a "weird" loop in the program.

> If not, the documentation should mention this fact, and it should
> provide proper documentation.  With the current documentation, I have
> no idea whether it's safe to use this option for my code or not.

Almost surely yes.  There are several patterns that might get
misscompiled with this flag, but none of them is likely to occur
in "normal" code (some of the constants in the examples need
to be replaced by variables with a given value, since with the explicit
constants loop optimizer recognizes that the loop is "weird" and will
not try to optimize it even with the -funsafe-loop-optimizations flag):

1) You write empty loops that may be infinite, although they have
   nontrivial exit condition. E.g.

   for (i = 1; i != 3; i *= 2)

2) You write loops whose control induction variable overflows, e.g.

   for (i = 0; i != 1; i+=2)

   for (i = 0; i <= MAX_UNSIGNED; i++)

   for (i = 0; i < MAX_UNSIGNED - 2; i += 17)

In particular programs that might get misscompiled with
-funsafe-loop-optimizations are usually not portable,
since they depend on exact sizes of types.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]