Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22016

Hint the C compiler (GCC or Clang) of possible variable value/range

$
0
0

In the following code, only one comparison will be done, because the compiler knows the conditions are exclusive and we will always enter the second condition as bar will be necessary > 32:

int foo(int bar) {
    if (bar <= 64)
        return 1;
    if (bar > 32) {
        printf("Too many elements");
    }
    return 0;
}

Now, imagine I know bar is always higher than 64. Because of the input of the system, configuration, or else. How can I hint the compiler to do no comparison at all, like if the if (bar <= 64) return was compiled, except it actually isn't kept in the final ASM.

Something like:

int foo(int bar) {
    @precond(bar > 64);
    if (bar > 32) {
        printf("Too many elements");
    }
    return 0;
}

Is my only solution to write eg a LLVM pass?


Viewing all articles
Browse latest Browse all 22016

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>