-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Branchless code generation for ternaries #32632
Comments
Regarding |
But might be quite easy to implement (the non-LEA one):
To
Same for |
Hopefully this same optimization could be applied if this were written as: if(!condition) ++x; |
These are different, right? The latter statement has an expression with side effects on Do we have anything to query a source tree semantically, like Coccigrep can do in C? |
In the absence of a compelling real-world code example, I'm going to close this. |
Might be an opportunity to improve codegen for ternaries where both the expr-if-true and expr-if-false are constants.
I played a little bit with Compiler Explorer today to get a feeling of how GCC and Clang generate the code for this. Here's what I found from looking at the assembly output, with the accompanying C code for reference.
Method 1: sete/setne
Is generated as, by both Clang and GCC:
Playing a bit with Compiler Explorer, it seems that if both expressions in the ternary are apart by 1, then it can reuse the result of "cmp" as part of the expression. ("sete" is generated if the ternary is inverted, as expected: "a == 5 ? 3 : 2".)
Clang seems to go a bit further with this method before switching to use conditional moves as with "m3" below, by issuing an additional "add" instruction if values are apart by 2, for example.
I suspect a simplified version of this codegen could be used for things like
Convert.ToInt32(bool b)
too.Method 2: lea
Is generated as, by GCC:
Clang generates similar code but changes the "+1" inside the LEA to an "add
eax, 1" before the "ret".
This is a more generic extension to the previous method, and seems to be preferred to the next method, when the operands would fit the constraints of the LEA instruction arguments.
Method 3: cmov
Is generated as, by both Clang and GCC (order varies a bit):
Conditional moves seems to be generated when the above conditions do not hold.
category:cq
theme:optimization
skill-level:expert
cost:medium
The text was updated successfully, but these errors were encountered: