[PATCH] SelectionDAG: mimic InstCombine when comparing to undef

Mehdi Amini mehdi.amini at apple.com
Fri Feb 13 02:02:45 PST 2015


> On Feb 12, 2015, at 11:42 PM, Owen Anderson <owen at apple.com> wrote:
> 
>> 
>> On Feb 12, 2015, at 11:36 PM, Mehdi Amini <mehdi.amini at apple.com> wrote:
>> 
>> 
>>> On Feb 12, 2015, at 9:25 PM, hfinkel at anl.gov wrote:
>>> 
>>>> I haven't put a test yet, and it breaks the validation, but before fixing this I seek opinion on NaN handling here. InstCombine does not care and I don't really see why the same IR would be handled differently in the DAG than in InstCombine.
>>> 
>>> 
>>> I don't know either, but that sounds wrong. This depends on the predicate, I think, for fcmp, but NaN != NaN (or anything else), and so we can't arbitrarily fold fcmp pred x, undef to true unless we know that x cannot be NaN (or we're generically allowing unsafe floating-point transformations).
>> 
>> 
>> I’m not sure I can invalidate the reasoning for InstCombine, I need help :)
>> 
>> // fcmp pred X, undef -> undef
>> 
>> If we don’t know anything for X, then the result of the comparison can be either true or false. It seems to me completely valid to return “undef”.
>> 
>> The select will then decide that on “undef” it will always pick the first choice (IIRC).
> 
> Consider the specific case of “%0 = fcmp oeq %x, undef; %1 = select %0, %a, %b”
> If we optimize then fcmp to undef, then later stages can legitimately optimize the entire sequence to "%1 = %a”
> 
> Now, imagine that %x is dynamically NaN.  We have now produce a dynamic contradiction: no matter what value we chose to represent the undef, the final result could never have been %a in this situation.


Ok, so the question is, considering 
  fcmp oeq %x, undef
we can fold to “undef” only if for *any* possible %x value, there exists a value for “undef" that give true and another value for “undef" that gives false. Right?

I think we can’t fold ordered comparisons, since if %x is NaN the answer is always false whatever “undef” is.
For unordered comparison, it is the same problem: if %x is NaN the answer is always true whatever “undef” is.

Conclusion: InstCombine can’t fold this. 

For the DAG we have a flag to assume there is no NaN and another flag to assume no infinity, I assume I can use these flags to allows folding?

Thanks,

Mehdi






More information about the llvm-commits mailing list