Real-world scenario of x>y? abs(x-y+1):0 and its optimization?

I’ve been attending Open Source Promotion Plan 2023 for the last 6 weeks under the instruction of @Allen and nearly finished my project about improving the InstCombine optimization based on ⚙ D122013 [InstCombine] Fold abs of known negative operand when source is sub.

In this project, I added some pattern matching schemes for `x>y? abs(x-y+1): 0` and optimize out the `abs()` using the `isImpliedByDomCondition()` API. After the optimization, the ternary expression is replaced with `x>y? x-y+1: 0` and can generate only one AArch64 instruction `csinc`. I also try to match similar patterns such as `x<y? abs(x-y-1): 0`, `x>y? abs(2*(x-y)): 0` and optimize them based on arithmetic relationship.

While this optimization seems non-trivial, I can not figure out its real-world application. I try to search for the code pattern needs to be optimized in some large code bases such as Linux kernel using weggli, but there’s no matching result.

``````weggli --cpp "{\$x>\$y ? abs(\$x-\$y+1): 0}" .
``````

Can anybody tell me about the similar code pattern you’ve met? Or how to find the optimization scenario the right way? Thanks!

Hey, I once applied for this project too. And congratulations.
From my perspective, there is no much need for real-world motivation to fold that. If IR become “better” after the fold, it’s worthy to do.
Besides, there may be other equivalent forms like

``````if(x>y)
....
z = abs(expr)
``````

where expr may be a complex expression folded into x-y+1 after optimization, which would be indirect real-world application.

Thanks. I have finished this project two weeks before, and it turns out that this project really improves the IR by optimizing out the intrinsic `abs` function, see PR and test code for more details.

As for the equivalent form you have provided, this project can not handle it now because what have been implemented is a peephole optimization and only considers the basic value-flow inside ternary expression. In order for optimizing such case, we need a longer value-flow analysis, do you have any advice on that?