//===-- lib/Evaluate/fold-complex.cpp -------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// #include "fold-implementation.h" #include "fold-matmul.h" #include "fold-reduction.h" namespace Fortran::evaluate { template Expr> FoldIntrinsicFunction( FoldingContext &context, FunctionRef> &&funcRef) { using T = Type; using Part = typename T::Part; ActualArguments &args{funcRef.arguments()}; auto *intrinsic{std::get_if(&funcRef.proc().u)}; CHECK(intrinsic); std::string name{intrinsic->name}; if (name == "acos" || name == "acosh" || name == "asin" || name == "asinh" || name == "atan" || name == "atanh" || name == "cos" || name == "cosh" || name == "exp" || name == "log" || name == "sin" || name == "sinh" || name == "sqrt" || name == "tan" || name == "tanh") { if (auto callable{GetHostRuntimeWrapper(name)}) { return FoldElementalIntrinsic( context, std::move(funcRef), *callable); } else { context.messages().Say( "%s(complex(kind=%d)) cannot be folded on host"_warn_en_US, name, KIND); } } else if (name == "conjg") { return FoldElementalIntrinsic( context, std::move(funcRef), &Scalar::CONJG); } else if (name == "cmplx") { if (args.size() > 0 && args[0].has_value()) { if (auto *x{UnwrapExpr>(args[0])}) { // CMPLX(X [, KIND]) with complex X return Fold(context, ConvertToType(std::move(*x))); } else { if (args.size() >= 2 && args[1].has_value()) { // Do not fold CMPLX with an Y argument that may be absent at runtime // into a complex constructor so that lowering can deal with the // optional aspect (there is no optional aspect with the complex // constructor). if (MayBePassedAsAbsentOptional(*args[1]->UnwrapExpr())) { return Expr{std::move(funcRef)}; } } // CMPLX(X [, Y [, KIND]]) with non-complex X Expr re{std::move(*args[0].value().UnwrapExpr())}; Expr im{args.size() >= 2 && args[1].has_value() ? std::move(*args[1]->UnwrapExpr()) : AsGenericExpr(Constant{Scalar{}})}; return Fold(context, Expr{ ComplexConstructor{ToReal(context, std::move(re)), ToReal(context, std::move(im))}}); } } } else if (name == "dot_product") { return FoldDotProduct(context, std::move(funcRef)); } else if (name == "matmul") { return FoldMatmul(context, std::move(funcRef)); } else if (name == "product") { auto one{Scalar::FromInteger(value::Integer<8>{1}).value}; return FoldProduct(context, std::move(funcRef), Scalar{one}); } else if (name == "sum") { return FoldSum(context, std::move(funcRef)); } return Expr{std::move(funcRef)}; } template Expr> FoldOperation( FoldingContext &context, ComplexConstructor &&x) { if (auto array{ApplyElementwise(context, x)}) { return *array; } using Result = Type; if (auto folded{OperandsAreConstants(x)}) { return Expr{ Constant{Scalar{folded->first, folded->second}}}; } return Expr{std::move(x)}; } #ifdef _MSC_VER // disable bogus warning about missing definitions #pragma warning(disable : 4661) #endif FOR_EACH_COMPLEX_KIND(template class ExpressionBase, ) template class ExpressionBase; } // namespace Fortran::evaluate