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>
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         if (args.size() >= 2 && args[1].has_value()) {
45           // Do not fold CMPLX with an Y argument that may be absent at runtime
46           // into a complex constructor so that lowering can deal with the
47           // optional aspect (there is no optional aspect with the complex
48           // constructor).
49           if (MayBePassedAsAbsentOptional(*args[1]->UnwrapExpr(), context)) {
50             return Expr<T>{std::move(funcRef)};
51           }
52         }
53         // CMPLX(X [, Y [, KIND]]) with non-complex X
54         Expr<SomeType> re{std::move(*args[0].value().UnwrapExpr())};
55         Expr<SomeType> im{args.size() >= 2 && args[1].has_value()
56                 ? std::move(*args[1]->UnwrapExpr())
57                 : AsGenericExpr(Constant<Part>{Scalar<Part>{}})};
58         return Fold(context,
59             Expr<T>{
60                 ComplexConstructor<KIND>{ToReal<KIND>(context, std::move(re)),
61                     ToReal<KIND>(context, std::move(im))}});
62       }
63     }
64   } else if (name == "merge") {
65     return FoldMerge<T>(context, std::move(funcRef));
66   } else if (name == "product") {
67     auto one{Scalar<Part>::FromInteger(value::Integer<8>{1}).value};
68     return FoldProduct<T>(context, std::move(funcRef), Scalar<T>{one});
69   } else if (name == "sum") {
70     return FoldSum<T>(context, std::move(funcRef));
71   }
72   // TODO: dot_product, matmul, transfer
73   return Expr<T>{std::move(funcRef)};
74 }
75 
76 template <int KIND>
77 Expr<Type<TypeCategory::Complex, KIND>> FoldOperation(
78     FoldingContext &context, ComplexConstructor<KIND> &&x) {
79   if (auto array{ApplyElementwise(context, x)}) {
80     return *array;
81   }
82   using Result = Type<TypeCategory::Complex, KIND>;
83   if (auto folded{OperandsAreConstants(x)}) {
84     return Expr<Result>{
85         Constant<Result>{Scalar<Result>{folded->first, folded->second}}};
86   }
87   return Expr<Result>{std::move(x)};
88 }
89 
90 #ifdef _MSC_VER // disable bogus warning about missing definitions
91 #pragma warning(disable : 4661)
92 #endif
93 FOR_EACH_COMPLEX_KIND(template class ExpressionBase, )
94 template class ExpressionBase<SomeComplex>;
95 } // namespace Fortran::evaluate
96