Optimizing away empty loop with step

Vladimir Panteleev thecybershadow.lists at gmail.com
Sat Jul 31 12:11:03 UTC 2021


On Friday, 30 July 2021 at 22:23:08 UTC, Iain Buclaw wrote:
> The actual compiler option that controls this is 
> `-ffinite-loops`.
> ```
> Assume that a loop with an exit will eventually take the exit 
> and not loop indefinitely. This allows the compiler to remove 
> loops that otherwise have no side-effects, not considering 
> eventual endless looping as such.
>
> This option is enabled by default at -O2 for C++ with 
> -std=c++11 or higher.
> ```
>
> You can add this to the GDC compiler window to observe the 
> optimization, or add `-fno-finite-loops` to the G++ compiler 
> window for the inverse.

I see, thanks for looking into it.

I wonder if there's a way to tell the compiler (using portable D) 
that it can assume that the loop will not be infinite. moon-child 
on IRC pointed out that this loop can be infinite in many cases, 
e.g. if `b` is `uint.max` and for any even value of `step` (i.e. 
`i` cannot take any value that is `>= b`).

(Maybe something like `__builtin_reachable` after the loop?)


More information about the digitalmars-d-ldc mailing list