https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83661

            Bug ID: 83661
           Summary: sincos does not handle sin(2x)
           Product: gcc
           Version: 8.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: prathamesh3492 at gcc dot gnu.org
  Target Milestone: ---

Hi,
For the following test-case:

double f(double x)
{
  return __builtin_sin(2*x) + __builtin_sin(x);
}

optimzied dump with -O2 -funsafe-math-optimizations -ffast-math shows:
;; Function f (f, funcdef_no=0, decl_uid=1952, cgraph_uid=0, symbol_order=0)

  <bb 2> [local count: 1073741825]:
  _1 = __builtin_sin (x_4(D));
  _2 = x_4(D) * 2.0e+0;
  _3 = __builtin_sin (_2);
  _5 = _1 + _3;
  return _5;

Would it be a good idea to enhance sincos pass to recognize the
identity sin(2*x) = 2*sin(x)*cos(x) and thus eliminate one call
to __builtin_sin ?

Writing 2*sin(x)*cos(x) explicitly in the source yields following optimized
dump:

  <bb 2> [local count: 1073741825]:
  sincostmp_8 = __builtin_cexpi (x_5(D));
  _1 = IMAGPART_EXPR <sincostmp_8>;
  _2 = REALPART_EXPR <sincostmp_8>;
  _3 = _1 * _2;
  _4 = _3 * 2.0e+0;
  _6 = _2 + _4;
  return _6;

I agree in general that adding math identities like sin(x)**2 + cos(x)**2 = 1
isn't a good idea since user would almost always write the "optimized" version
in practice. However for the above case, would the transform make sense ?

Thanks,
Prathamesh

Reply via email to