[llvm-commits] CVS: llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Chris Lattner clattner at apple.com
Tue Jul 3 16:47:12 PDT 2007


On Jun 29, 2007, at 1:10 AM, Duncan Sands wrote:

> Hi,
>
>>>>> It seems like folding undef/X to undef isn't safe either though,
>
> here is my understanding of how to fold undef.  I hope it clarifies
> this confusing area.  Of course, I could be confused myself but I
> hope not :)
>
> (1) When is it OK to fold "y=foo(undef)" to "y=undef"?  I claim that
> it is OK if and only if foo is surjective, i.e. if for each possible
> value for y there exists a value for x such that y=foo(x).   
> "Surjective"
> is sometimes called "onto".
>
> Before I explain why I think this, an example:
> 	y=(undef == z)
> The possible values of y are 0 and 1 because the result of == is
> an i1.  Surjectivity means: can I get (undef==z) to produce each of
> 0 and 1 by plugging in different values for undef?  Obviously I can,
> so in this case I can fold to "y=undef".

Yep, I agree.

> (2) What to do when foo is not surjective?  Choose some value for  
> undef
> and fold to "y=foo(value_chosen)".  In general foo will involve some
> other variables, so the trick is to find a constant value for y  
> that is
> always obtainable no matter what those other variables are (while  
> it is
> logically correct to replace y with a function of those other  
> variables,
> which is what foo(0) will give in general for example, it is more  
> efficient
> to use a constant value if possible).
>
> Example: folding "y=undef udiv x".  This could be folded to 0 or to 1,
> since 0 is what you get by substituting undef=0, and 1 is what you get
> by substituting undef=x.  (If x=0 then in both cases you get 0/0 which
> is, I hear, undefined so you can choose it to be 0 or 1 as you like).
> Of course you could also fold it to "1 div x" or "intmax div x" or
> "(x*x) div x" if you really felt like it, but 0 and 1 are the only
> constants that can always be obtained regardless of the value of x,
> so they are the most efficient choices.

Yes.

Duncan, this whole write-up is very helpful.  Can you find some place  
to put this so that we can find it again in the future?  How about  
the programmer's manual?

-Chris





More information about the llvm-commits mailing list