GCC: is there a way to tell a compiler what range of number I expect? For instance there's a uint8_t in range [2, 17]. I expect the compiler will optimize code for that range and consider out-of-range values as undefined. #cpp
-
-
Why not shorten it to 0/(x >= 2 && x <= 17);
-
Because I like the idiom for "undefined" that you can also drop in case labels, etc.
End of conversation
New conversation -
-
-
this is
@volatile_void's preferred idiom! -
Specifically the 0/0? Or just the [insert UB here] aspect?
-
well he may not have used that one exactly, I don't remember
End of conversation
New conversation -
-
-
If you do it like that, the compiler will almost always optimize it out before it can be used for optimization. GCC has special rules to preserve branches to __builtin_unreachable longer than usual. Clang has __builtin_assume to tell the compiler not to remove it right away.
-
You can slow down code by using __builtin_assume (or the GCC idiom) because it ends up preserving the code through the initial optimizations to avoid invalidating the guarantees it gets from it. Preserving it can hinder other optimizations even though it gets removed at the end.
-
SUPER tricky to nail the phase ordering here...
-
https://llvm.org/docs/LangRef.html#llvm-assume-intrinsic … https://clang.llvm.org/docs/LanguageExtensions.html#builtin-assume … The same kind of issue happens with the branch metadata from __builtin_expect. Clang lowers it from the intrinsic to branch metadata very early on (the early function passes, before interprocedural optimization) & loses it easily.
-
Can output a bunch of fancy alias analysis metadata, etc. but the compiler likely isn't going to cripple optimizations to keep it around, so a lot of it will end up invalidated by other transformations before getting to where it could make a big difference.
-
It's a bit amusing that the warning against using it in only in the LLVM IR reference and not there for Clang __builtin_assume. It can also ridiculously slow down compile time if you put it in something used everywhere. Remember that happening with Rc<T> in Rust.
End of conversation
New conversation -
-
-
__builtin_unreachable is probably a nicer way than 0/0
-
But it's not in the C standard and depends on your compiler, whereas 0/0 works everywhere.
-
Well he asked gcc...
End of conversation
New conversation -
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.