1 //===-- ComplexExpr.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 "flang/Lower/ComplexExpr.h"
10 #include "flang/Lower/ConvertType.h"
11
12 //===----------------------------------------------------------------------===//
13 // ComplexExprHelper implementation
14 //===----------------------------------------------------------------------===//
15
16 mlir::Type
getComplexPartType(mlir::Type complexType)17 Fortran::lower::ComplexExprHelper::getComplexPartType(mlir::Type complexType) {
18 return Fortran::lower::convertReal(
19 builder.getContext(), complexType.cast<fir::CplxType>().getFKind());
20 }
21
22 mlir::Type
getComplexPartType(mlir::Value cplx)23 Fortran::lower::ComplexExprHelper::getComplexPartType(mlir::Value cplx) {
24 return getComplexPartType(cplx.getType());
25 }
26
createComplex(fir::KindTy kind,mlir::Value real,mlir::Value imag)27 mlir::Value Fortran::lower::ComplexExprHelper::createComplex(fir::KindTy kind,
28 mlir::Value real,
29 mlir::Value imag) {
30 auto complexTy = fir::CplxType::get(builder.getContext(), kind);
31 mlir::Value und = builder.create<fir::UndefOp>(loc, complexTy);
32 return insert<Part::Imag>(insert<Part::Real>(und, real), imag);
33 }
34
createComplex(mlir::Type cplxTy,mlir::Value real,mlir::Value imag)35 mlir::Value Fortran::lower::ComplexExprHelper::createComplex(mlir::Type cplxTy,
36 mlir::Value real,
37 mlir::Value imag) {
38 mlir::Value und = builder.create<fir::UndefOp>(loc, cplxTy);
39 return insert<Part::Imag>(insert<Part::Real>(und, real), imag);
40 }
41
createComplexCompare(mlir::Value cplx1,mlir::Value cplx2,bool eq)42 mlir::Value Fortran::lower::ComplexExprHelper::createComplexCompare(
43 mlir::Value cplx1, mlir::Value cplx2, bool eq) {
44 auto real1 = extract<Part::Real>(cplx1);
45 auto real2 = extract<Part::Real>(cplx2);
46 auto imag1 = extract<Part::Imag>(cplx1);
47 auto imag2 = extract<Part::Imag>(cplx2);
48
49 mlir::CmpFPredicate predicate =
50 eq ? mlir::CmpFPredicate::UEQ : mlir::CmpFPredicate::UNE;
51 mlir::Value realCmp =
52 builder.create<mlir::CmpFOp>(loc, predicate, real1, real2);
53 mlir::Value imagCmp =
54 builder.create<mlir::CmpFOp>(loc, predicate, imag1, imag2);
55
56 return eq ? builder.create<mlir::AndOp>(loc, realCmp, imagCmp).getResult()
57 : builder.create<mlir::OrOp>(loc, realCmp, imagCmp).getResult();
58 }
59