Signed overflow optimization hazards in the kernel
Signed overflow optimization hazards in the kernel
Posted Aug 16, 2012 23:10 UTC (Thu) by cmccabe (guest, #60281)In reply to: Signed overflow optimization hazards in the kernel by cmccabe
Parent article: Signed overflow optimization hazards in the kernel
> int i;
> for (i = 0; i < 5; i++) {
> [expression not involving i]
> }
I don't see how -fnowrapv will prevent you from unrolling the loop. You know that starting at 0 and adding 1 will get you to 5 before it will get you to overflow.
I guess you could come up with a scenario where you don't know the initial value of the counter, but you do have a constant positive increment and a set upper limit. So something like this:
> for (i = function_from_another_translation_unit(); i < 5; i++) {
> [expression not involving i]
> }
Even in this case, though, you still know that either you'll run the loop no times, or there will be no overflow. You have to get to something like this to start seeing a problem:
> for (i = function_from_another_translation_unit(); i < 0x7ffffffe; i += 2) {
> [expression not involving i]
> }
This is pretty exotic scenario, though. If i starts off as 0x7fffffff and -fwrapv is enabled, the loop will never terminate, whereas with -fnowrapv it's undefined. To me, this feels like buggy code in the first place, so I don't really care if it's not optimized.
Am I missing something here? Is there a good example of a real-world scenario where -fnowrapv helps well-written code?
