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

Eli Friedman eli.friedman at gmail.com
Mon Dec 17 14:29:20 PST 2012


On Fri, Dec 14, 2012 at 10:45 AM, Shuxin Yang <shuxin.llvm at gmail.com> wrote:
> Hi, Eli:
>
>   I'm sorry. I have to took back what I said. I try to make you happy by
> returning
> back to pattern-match approach:-). Unfortunately it is so messy, I have to
> give
> up again.
>
>   It seems that you have certain degree of misunderstanding of what I'm
> doing and
> which code can be "reused" by my work. The functions that you mentioned can
> be
> reused are following:
>
>   1. InstCombiner::SimplifyAssociateOrCommutative()
>     It is already automatically reused two weeks ago (due to my change to
>     Instruction::isAssociative())
>   2. InstCombiner::SimplifyUsingDistributiveLaws()
>     I don't need distrubution at all. At very least at this time.
>
>   3. dyn_castFoldableMul
>     This is your misunderstanding because of my typo in the mail (1.5 vs
> 1*5).
>
>   It seems that you left out two functions that can really be "re-used": the
> InstCombine::{visitAdd, visitSub}. They are pattern-match based, can be
> "re-used" by cloning-and-modify!  We can quntify the complexity by examing
> the LOCs
> they have: the visitAdd and visitSub and 262 and 156 LOCs respectively, not
> including
> helper routines...
>
>   I could be wrong. But if you can show me your implementation of
> (x+y)+/-(x-y)
> within 10 lines code by reusing existing code. I would certainly like to
> change
> my mind a bit.

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