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