1 //===-- lib/Evaluate/fold-complex.cpp -------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "fold-implementation.h"
10 #include "fold-reduction.h"
11 
12 namespace Fortran::evaluate {
13 
14 template <int KIND>
FoldIntrinsicFunction(FoldingContext & context,FunctionRef<Type<TypeCategory::Complex,KIND>> && funcRef)15 Expr<Type<TypeCategory::Complex, KIND>> FoldIntrinsicFunction(
16     FoldingContext &context,
17     FunctionRef<Type<TypeCategory::Complex, KIND>> &&funcRef) {
18   using T = Type<TypeCategory::Complex, KIND>;
19   using Part = typename T::Part;
20   ActualArguments &args{funcRef.arguments()};
21   auto *intrinsic{std::get_if<SpecificIntrinsic>(&funcRef.proc().u)};
22   CHECK(intrinsic);
23   std::string name{intrinsic->name};
24   if (name == "acos" || name == "acosh" || name == "asin" || name == "asinh" ||
25       name == "atan" || name == "atanh" || name == "cos" || name == "cosh" ||
26       name == "exp" || name == "log" || name == "sin" || name == "sinh" ||
27       name == "sqrt" || name == "tan" || name == "tanh") {
28     if (auto callable{GetHostRuntimeWrapper<T, T>(name)}) {
29       return FoldElementalIntrinsic<T, T>(
30           context, std::move(funcRef), *callable);
31     } else {
32       context.messages().Say(
33           "%s(complex(kind=%d)) cannot be folded on host"_en_US, name, KIND);
34     }
35   } else if (name == "conjg") {
36     return FoldElementalIntrinsic<T, T>(
37         context, std::move(funcRef), &Scalar<T>::CONJG);
38   } else if (name == "cmplx") {
39     if (args.size() > 0 && args[0].has_value()) {
40       if (auto *x{UnwrapExpr<Expr<SomeComplex>>(args[0])}) {
41         // CMPLX(X [, KIND]) with complex X
42         return Fold(context, ConvertToType<T>(std::move(*x)));
43       } else {
44         // CMPLX(X [, Y [, KIND]]) with non-complex X
45         Expr<SomeType> re{std::move(*args[0].value().UnwrapExpr())};
46         Expr<SomeType> im{args.size() >= 2 && args[1].has_value()
47                 ? std::move(*args[1]->UnwrapExpr())
48                 : AsGenericExpr(Constant<Part>{Scalar<Part>{}})};
49         return Fold(context,
50             Expr<T>{
51                 ComplexConstructor<KIND>{ToReal<KIND>(context, std::move(re)),
52                     ToReal<KIND>(context, std::move(im))}});
53       }
54     }
55   } else if (name == "merge") {
56     return FoldMerge<T>(context, std::move(funcRef));
57   } else if (name == "product") {
58     auto one{Scalar<Part>::FromInteger(value::Integer<8>{1}).value};
59     return FoldProduct<T>(context, std::move(funcRef), Scalar<T>{one});
60   } else if (name == "sum") {
61     return FoldSum<T>(context, std::move(funcRef));
62   }
63   // TODO: dot_product, matmul, transfer
64   return Expr<T>{std::move(funcRef)};
65 }
66 
67 template <int KIND>
FoldOperation(FoldingContext & context,ComplexConstructor<KIND> && x)68 Expr<Type<TypeCategory::Complex, KIND>> FoldOperation(
69     FoldingContext &context, ComplexConstructor<KIND> &&x) {
70   if (auto array{ApplyElementwise(context, x)}) {
71     return *array;
72   }
73   using Result = Type<TypeCategory::Complex, KIND>;
74   if (auto folded{OperandsAreConstants(x)}) {
75     return Expr<Result>{
76         Constant<Result>{Scalar<Result>{folded->first, folded->second}}};
77   }
78   return Expr<Result>{std::move(x)};
79 }
80 
81 FOR_EACH_COMPLEX_KIND(template class ExpressionBase, )
82 template class ExpressionBase<SomeComplex>;
83 } // namespace Fortran::evaluate
84