[PATCH] D107148: [InstCombine] Fold two-value clamp patterns

Max Kazantsev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 7 22:23:33 PDT 2021


mkazantsev added a comment.

> Sounds like SCEV should be aware of this way to parse such a select-of-constants as min-max.

We can, theoretically, teach SCEV to recognize min and max in all kinds of arcane expressions including arithmetics, zexts, xors, shifts and whatsoever. It's not going to happen in reality, off couese.

In D107148#2916113 <https://reviews.llvm.org/D107148#2916113>, @lebedev.ri wrote:

> In D107148#2916097 <https://reviews.llvm.org/D107148#2916097>, @mkazantsev wrote:
>
>> The initial code
>>
>>   // s1 = (n < C1) ? n : C1
>>   // s2 = (s1 > C1 - 1) ? s1 : C1 - 1
>>
>> had a clear semantics of
>>
>>   s1 = min(n, C1)
>>   s2 = max(s1, C1 - 1)
>>
>> These pattern could be analyzed by other passes that understand semantics of min and max, such as SCEV using passes. If it participates in other min or max expression, there is a lot of ways how we can simplify it.
>>
>> This new form, despite it has fewer instructions, is completely arcane. Just looking at it, it's not so easy to understand what it actually means. And no pass will.
>>
>> I'd rather suggest to do changes like this as close to codegen as possible, for example in CodeGenPrepare.
>
> Sounds like SCEV should be aware of this way to parse such a select-of-constants as min-max.

@qiucf could you please add a test with `-analyze -scalar-evolution` to see if SCEV is able to recognize this as max/min? If not, please add a FIXME in this test, this is a good improvement to do.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D107148/new/

https://reviews.llvm.org/D107148



More information about the llvm-commits mailing list