]> granicus.if.org Git - clang/commit
[complex] Teach Clang to preserve different-type operands to arithmetic
authorChandler Carruth <chandlerc@gmail.com>
Sat, 11 Oct 2014 00:57:18 +0000 (00:57 +0000)
committerChandler Carruth <chandlerc@gmail.com>
Sat, 11 Oct 2014 00:57:18 +0000 (00:57 +0000)
commit0eb6c46c27992f0aa833e3c4d6f702ab853b48b8
treed1caa30b3d67af5945ab4d784b838a84bb56192f
parentcd98c92e39cbc0661897533da2d046ab26a347c2
[complex] Teach Clang to preserve different-type operands to arithmetic
operators where one type is a C complex type, and to emit both the
efficient and correct implementation for complex arithmetic according to
C11 Annex G using this extra information.

For both multiply and divide the old code was writing a long-hand
reduced version of the math without any of the special handling of inf
and NaN recommended by the standard here. Instead of putting more
complexity here, this change does what GCC does which is to emit
a libcall for the fully general case.

However, the old code also failed to do the proper minimization of the
set of operations when there was a mixed complex and real operation. In
those cases, C provides a spec for much more minimal operations that are
valid. Clang now emits the exact suggested operations. This change isn't
*just* about performance though, without minimizing these operations, we
again lose the correct handling of infinities and NaNs. It is critical
that this happen in the frontend based on assymetric type operands to
complex math operations.

The performance implications of this change aren't trivial either. I've
run a set of benchmarks in Eigen, an open source mathematics library
that makes heavy use of complex. While a few have slowed down due to the
libcall being introduce, most sped up and some by a huge amount: up to
100% and 140%.

In order to make all of this work, also match the algorithm in the
constant evaluator to the one in the runtime library. Currently it is
a broken port of the simplifications from C's Annex G to the long-hand
formulation of the algorithm.

Splitting this patch up is very hard because none of this works without
the AST change to preserve non-complex operands. Sorry for the enormous
change.

Follow-up changes will include support for sinking the libcalls onto
cold paths in common cases and fastmath improvements to allow more
aggressive backend folding.

Differential Revision: http://reviews.llvm.org/D5698

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@219557 91177308-0d34-0410-b5e6-96231b3b80d8
lib/AST/ExprConstant.cpp
lib/CodeGen/CGExprComplex.cpp
lib/Sema/SemaExpr.cpp
test/CodeGen/complex-math.c [new file with mode: 0644]
test/SemaCXX/complex-folding.cpp [new file with mode: 0644]