binaryninja-api icon indicating copy to clipboard operation
binaryninja-api copied to clipboard

Provide some simplification of common routines like MIN/MAX

Open emesare opened this issue 8 months ago • 1 comments

Created on behalf of @VisualEhrmanntraut

Optimiser for min, max expressions:

TYPE val;
if (Z > Y) {
  val = Y
} else {
  val = Z
}
// after
TYPE val = MIN(Y, Z);```
```// before 
TYPE val;
if (Z < Y) {
  val = Y
} else {
  val = Z
}
// after
TYPE val = MAX(Y, Z);

This should be done after all IL simplifications like constant propagation so that the analysis has a chance to optimize the MIN/MAX code out entirely. This really is not about any particular architecture plugin either, one can reasonably expect this operation to be performed by a set of instructions rather than any one instruction, were it the latter you might compromise and lift as an intrinsic, however even that is a little disappointing as you will stonewall certain analysis regarding the input and output registers.

Putting this as an HLIL issue because I believe that is the only place this optimization can be made considering the first sentence above.

Slack Message

emesare avatar Jun 11 '25 16:06 emesare

could be done either as a synthetic builtin or just on the tokenization layer like the new ternary operator stuff. Probably we'd probably go with builtin_max rather than MAX

plafosse avatar Jun 11 '25 16:06 plafosse

Also likely want to do https://github.com/Vector35/binaryninja-api/issues/3847 as well, other tools have this same concept in their IR so I don't think its that off base.

Keyword: "Generic Intrinsic"

emesare avatar Jul 30 '25 17:07 emesare