bad loop optimization

bad loop optimization

void foo(int a) {
    while(a);
}
int main() {
    foo(1);
}

Compiler removes empty loop and there is no endless loop that i expect. MSVC and GCC works correctly.

Windows, ICC 13.1.

6 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

With Intel C++ compiler this is a well know issue but it is Not a problem. What optimization options did you use, /O1, /O2 or /O3?

With any level of optimization.

>>...Compiler removes empty loop and there is no endless loop that i expect...

Do you need to use a similar processing in a real application?

Jennifer J. (Intel)'s picture

Adding "#pragma intel optimization_level 0" before the function "foo()" will keep the loop. 

Or introduce a volatile variable like below: 

//#pragma intel optimization_level 0
void foo(int a) {
    volatile int b=a;
    while(a)
    ;
}

Okay, i forgot about volatile, thanks.

Login to leave a comment.