[PATCH] D142234: [ConstantRange] Handle Intrinsic::ctlz

Nikita Popov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 15 02:34:53 PST 2023


nikic added inline comments.


================
Comment at: llvm/test/Analysis/LazyValueAnalysis/lvi-for-ctlz.ll:1
+; RUN: opt < %s -passes=jump-threading -print-lvi-after-jump-threading -disable-output 2>&1 | FileCheck %s
+
----------------
antoniofrighetto wrote:
> antoniofrighetto wrote:
> > StephenFan wrote:
> > > antoniofrighetto wrote:
> > > > nikic wrote:
> > > > > nikic wrote:
> > > > > > Having an IR test is fine, but please do not test LVI debug output. Just check the resulting IR change using update_test_checks.py.
> > > > > I don't think that as written, these tests really test anything. There needs to be a comparison involving the ctlz that can be folded away, or similar.
> > > > I feel like a suitable test could be the following one:
> > > > ```
> > > > int lol(int b) {
> > > >     if (b < 65536) {
> > > >         int n = __builtin_clz(b);
> > > >         if (n < 8)
> > > >     	    return 0;
> > > >         else
> > > >             return 1;
> > > >    }
> > > >    return 2;
> > > > }
> > > > ```
> > > > which now could be simplified in a `return (b < 65536) ? 1 : 2;`. However, as far as I understand this, CVP needs to be extended as well in order to be able to obtain the above constant folding. Is that correct?
> > > Is the simplification to `return (b < 65536) ? 1:2;` correct? Since if `b` is negative, `clz` returns 0. 
> > True, sorry, I originally intended the first argument to be `unsigned`, whose optimization seems to occur successfully in this case.
> @nikic, perhaps a better example could be the following one? That would still require a change in CVP if I'm not mistaken though, correct?
> ```
> int test_ctlz(int b) {
>     if (b < 65536) {
>         int n = __builtin_clz(b);
>         if (n < 8 && n > 2)
>     	    return 0;
>         else
>             return 1;
>    }
>    return 2;
> }
> ```
Wouldn't something like this work as a test?
```
int test_ctlz(unsigned b) {
    if (b < 65536) {
        unsigned n = __builtin_clz(b);
        return n >= 16; // Fold to 1
   }
   return 2;
}
```


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

https://reviews.llvm.org/D142234



More information about the llvm-commits mailing list