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.
This optimization has already been done in GCC, click here for a demo.
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!