<div dir="ltr"><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Sep 22, 2020 at 8:52 PM Roman Lebedev <<a href="mailto:lebedev.ri@gmail.com">lebedev.ri@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On Tue, Sep 22, 2020 at 9:47 PM Nikita Popov <<a href="mailto:nikita.ppv@gmail.com" target="_blank">nikita.ppv@gmail.com</a>> wrote:<br>
><br>
> On Tue, Sep 22, 2020 at 8:38 PM Roman Lebedev via llvm-commits <<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a>> wrote:<br>
>><br>
>><br>
>> Author: Roman Lebedev<br>
>> Date: 2020-09-22T21:37:29+03:00<br>
>> New Revision: b38d897e802664034c7e6e4654328256ed370a61<br>
>><br>
>> URL: <a href="https://github.com/llvm/llvm-project/commit/b38d897e802664034c7e6e4654328256ed370a61" rel="noreferrer" target="_blank">https://github.com/llvm/llvm-project/commit/b38d897e802664034c7e6e4654328256ed370a61</a><br>
>> DIFF: <a href="https://github.com/llvm/llvm-project/commit/b38d897e802664034c7e6e4654328256ed370a61.diff" rel="noreferrer" target="_blank">https://github.com/llvm/llvm-project/commit/b38d897e802664034c7e6e4654328256ed370a61.diff</a><br>
>><br>
>> LOG: [ConstantRange] binaryXor(): special-case binary complement case - the result is precise<br>
>><br>
>> Use the fact that `~X` is equivalent to `-1 - X`, which gives us<br>
>> fully-precise answer, and we only need to special-handle the wrapped case.<br>
>><br>
>> This fires ~16k times for vanilla llvm test-suite + RawSpeed.<br>
>><br>
>> Added:<br>
>><br>
>><br>
>> Modified:<br>
>>     llvm/include/llvm/IR/ConstantRange.h<br>
>>     llvm/lib/IR/ConstantRange.cpp<br>
>>     llvm/unittests/IR/ConstantRangeTest.cpp<br>
>><br>
>> Removed:<br>
>><br>
>><br>
>><br>
>> ################################################################################<br>
>> diff  --git a/llvm/include/llvm/IR/ConstantRange.h b/llvm/include/llvm/IR/ConstantRange.h<br>
>> index 318532b24e83..494a14a10cdb 100644<br>
>> --- a/llvm/include/llvm/IR/ConstantRange.h<br>
>> +++ b/llvm/include/llvm/IR/ConstantRange.h<br>
>> @@ -409,6 +409,11 @@ class LLVM_NODISCARD ConstantRange {<br>
>>    /// value in \p Other.<br>
>>    ConstantRange srem(const ConstantRange &Other) const;<br>
>><br>
>> +  /// Return a new range representing the possible values resulting from<br>
>> +  /// a binary-xor of a value in this range by an all-one value,<br>
>> +  /// aka bitwise complement operation.<br>
>> +  ConstantRange binaryNot() const;<br>
>> +<br>
>>    /// Return a new range representing the possible values resulting<br>
>>    /// from a binary-and of a value in this range by a value in \p Other.<br>
>>    ConstantRange binaryAnd(const ConstantRange &Other) const;<br>
>><br>
>> diff  --git a/llvm/lib/IR/ConstantRange.cpp b/llvm/lib/IR/ConstantRange.cpp<br>
>> index 396c39b5b3a8..7b8dd66b993c 100644<br>
>> --- a/llvm/lib/IR/ConstantRange.cpp<br>
>> +++ b/llvm/lib/IR/ConstantRange.cpp<br>
>> @@ -1240,6 +1240,16 @@ ConstantRange ConstantRange::srem(const ConstantRange &RHS) const {<br>
>>    return ConstantRange(std::move(Lower), std::move(Upper));<br>
>>  }<br>
>><br>
>> +ConstantRange ConstantRange::binaryNot() const {<br>
>> +  if (isEmptySet())<br>
>> +    return getEmpty();<br>
>> +<br>
>> +  if (isWrappedSet())<br>
>> +    return getFull();<br>
><br>
><br>
> Probably don't need those checks, as you're forwarding to the sub() implementation anyway?<br>
It is needed if we want to give the right answer.<br></blockquote><div><br></div><div>Would you mind elaborating on that? It's not immediately obvious to me why the X ^ -1 to -1 - X transform would not hold for wrapped ranges.</div><div><br></div><div>Possibly it's just a testing issue? TestUnsignedUnaryOpExhaustive tests for the unsigned envelope, so it will report false positives if the result is a (correct) wrapped range. Unfortunately, I don't think we have a helper for the general case right now.<br></div><div><br></div><div>Nikita<br></div></div></div>