[llvm-commits] [PATCH][FastMath, InstCombine] Fadd/Fsub optimizations

Shuxin Yang shuxin.llvm at gmail.com
Mon Dec 17 15:10:40 PST 2012


HI, Eli:


      I understand the idea.  Actually each individual is no-brainer, 
however, the # of combination is
big.  Take this case as example,  we need to clone similar code for 
fsub. In the case of (x+y) + (y-x),
what if the both (x+y) and (y-x) have multiple use, simplify this expr 
into 2*y does not save instruction
at all.

On the other hand, you change need to take two steps to simplify the 
expr, my change is able to handle in one shot.






> The following patch seems to do the trick to simplify (x+y)+(x-y).
> (Can't be committed as-is because it doesn't check for fast-math
> properly, and it doesn't handle (x+y)-(x-y) because we're missing an
> instcombine to turn it into (x+y)+(y-x), but it's enough to
> demonstrate the idea.)
>
> -Eli
>
> Index: lib/Analysis/InstructionSimplify.cpp
> ===================================================================
> --- lib/Analysis/InstructionSimplify.cpp	(revision 170375)
> +++ lib/Analysis/InstructionSimplify.cpp	(working copy)
> @@ -877,6 +877,14 @@
>         (FMF.noSignedZeros() || CannotBeNegativeZero(Op0)))
>       return Op0;
>
> +  // X + (Y - X) -> Y
> +  // (Y - X) + X -> Y
> +  // Eg: X + -X -> 0
> +  Value *Y = 0;
> +  if (match(Op1, m_FSub(m_Value(Y), m_Specific(Op0))) ||
> +      match(Op0, m_FSub(m_Value(Y), m_Specific(Op1))))
> +    return Y;
> +
>     // fadd [nnan ninf] X, (fsub [nnan ninf] 0, X) ==> 0
>     //   where nnan and ninf have to occur at least once somewhere in this
>     //   expression




More information about the llvm-commits mailing list