xref: /netbsd-src/external/apache2/llvm/dist/clang/lib/CodeGen/CGExprComplex.cpp (revision e038c9c4676b0f19b1b7dd08a940c6ed64a6d5ae)
17330f729Sjoerg //===--- CGExprComplex.cpp - Emit LLVM Code for Complex Exprs -------------===//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg //
97330f729Sjoerg // This contains code to emit Expr nodes with complex types as LLVM code.
107330f729Sjoerg //
117330f729Sjoerg //===----------------------------------------------------------------------===//
127330f729Sjoerg 
13*e038c9c4Sjoerg #include "CGOpenMPRuntime.h"
147330f729Sjoerg #include "CodeGenFunction.h"
157330f729Sjoerg #include "CodeGenModule.h"
16*e038c9c4Sjoerg #include "ConstantEmitter.h"
177330f729Sjoerg #include "clang/AST/StmtVisitor.h"
187330f729Sjoerg #include "llvm/ADT/STLExtras.h"
197330f729Sjoerg #include "llvm/IR/Constants.h"
207330f729Sjoerg #include "llvm/IR/Instructions.h"
217330f729Sjoerg #include "llvm/IR/MDBuilder.h"
227330f729Sjoerg #include "llvm/IR/Metadata.h"
237330f729Sjoerg #include <algorithm>
247330f729Sjoerg using namespace clang;
257330f729Sjoerg using namespace CodeGen;
267330f729Sjoerg 
277330f729Sjoerg //===----------------------------------------------------------------------===//
287330f729Sjoerg //                        Complex Expression Emitter
297330f729Sjoerg //===----------------------------------------------------------------------===//
307330f729Sjoerg 
317330f729Sjoerg typedef CodeGenFunction::ComplexPairTy ComplexPairTy;
327330f729Sjoerg 
337330f729Sjoerg /// Return the complex type that we are meant to emit.
getComplexType(QualType type)347330f729Sjoerg static const ComplexType *getComplexType(QualType type) {
357330f729Sjoerg   type = type.getCanonicalType();
367330f729Sjoerg   if (const ComplexType *comp = dyn_cast<ComplexType>(type)) {
377330f729Sjoerg     return comp;
387330f729Sjoerg   } else {
397330f729Sjoerg     return cast<ComplexType>(cast<AtomicType>(type)->getValueType());
407330f729Sjoerg   }
417330f729Sjoerg }
427330f729Sjoerg 
437330f729Sjoerg namespace  {
447330f729Sjoerg class ComplexExprEmitter
457330f729Sjoerg   : public StmtVisitor<ComplexExprEmitter, ComplexPairTy> {
467330f729Sjoerg   CodeGenFunction &CGF;
477330f729Sjoerg   CGBuilderTy &Builder;
487330f729Sjoerg   bool IgnoreReal;
497330f729Sjoerg   bool IgnoreImag;
507330f729Sjoerg public:
ComplexExprEmitter(CodeGenFunction & cgf,bool ir=false,bool ii=false)517330f729Sjoerg   ComplexExprEmitter(CodeGenFunction &cgf, bool ir=false, bool ii=false)
527330f729Sjoerg     : CGF(cgf), Builder(CGF.Builder), IgnoreReal(ir), IgnoreImag(ii) {
537330f729Sjoerg   }
547330f729Sjoerg 
557330f729Sjoerg 
567330f729Sjoerg   //===--------------------------------------------------------------------===//
577330f729Sjoerg   //                               Utilities
587330f729Sjoerg   //===--------------------------------------------------------------------===//
597330f729Sjoerg 
TestAndClearIgnoreReal()607330f729Sjoerg   bool TestAndClearIgnoreReal() {
617330f729Sjoerg     bool I = IgnoreReal;
627330f729Sjoerg     IgnoreReal = false;
637330f729Sjoerg     return I;
647330f729Sjoerg   }
TestAndClearIgnoreImag()657330f729Sjoerg   bool TestAndClearIgnoreImag() {
667330f729Sjoerg     bool I = IgnoreImag;
677330f729Sjoerg     IgnoreImag = false;
687330f729Sjoerg     return I;
697330f729Sjoerg   }
707330f729Sjoerg 
717330f729Sjoerg   /// EmitLoadOfLValue - Given an expression with complex type that represents a
727330f729Sjoerg   /// value l-value, this method emits the address of the l-value, then loads
737330f729Sjoerg   /// and returns the result.
EmitLoadOfLValue(const Expr * E)747330f729Sjoerg   ComplexPairTy EmitLoadOfLValue(const Expr *E) {
757330f729Sjoerg     return EmitLoadOfLValue(CGF.EmitLValue(E), E->getExprLoc());
767330f729Sjoerg   }
777330f729Sjoerg 
787330f729Sjoerg   ComplexPairTy EmitLoadOfLValue(LValue LV, SourceLocation Loc);
797330f729Sjoerg 
807330f729Sjoerg   /// EmitStoreOfComplex - Store the specified real/imag parts into the
817330f729Sjoerg   /// specified value pointer.
827330f729Sjoerg   void EmitStoreOfComplex(ComplexPairTy Val, LValue LV, bool isInit);
837330f729Sjoerg 
847330f729Sjoerg   /// Emit a cast from complex value Val to DestType.
857330f729Sjoerg   ComplexPairTy EmitComplexToComplexCast(ComplexPairTy Val, QualType SrcType,
867330f729Sjoerg                                          QualType DestType, SourceLocation Loc);
877330f729Sjoerg   /// Emit a cast from scalar value Val to DestType.
887330f729Sjoerg   ComplexPairTy EmitScalarToComplexCast(llvm::Value *Val, QualType SrcType,
897330f729Sjoerg                                         QualType DestType, SourceLocation Loc);
907330f729Sjoerg 
917330f729Sjoerg   //===--------------------------------------------------------------------===//
927330f729Sjoerg   //                            Visitor Methods
937330f729Sjoerg   //===--------------------------------------------------------------------===//
947330f729Sjoerg 
Visit(Expr * E)957330f729Sjoerg   ComplexPairTy Visit(Expr *E) {
967330f729Sjoerg     ApplyDebugLocation DL(CGF, E);
977330f729Sjoerg     return StmtVisitor<ComplexExprEmitter, ComplexPairTy>::Visit(E);
987330f729Sjoerg   }
997330f729Sjoerg 
VisitStmt(Stmt * S)1007330f729Sjoerg   ComplexPairTy VisitStmt(Stmt *S) {
101*e038c9c4Sjoerg     S->dump(llvm::errs(), CGF.getContext());
1027330f729Sjoerg     llvm_unreachable("Stmt can't have complex result type!");
1037330f729Sjoerg   }
1047330f729Sjoerg   ComplexPairTy VisitExpr(Expr *S);
VisitConstantExpr(ConstantExpr * E)1057330f729Sjoerg   ComplexPairTy VisitConstantExpr(ConstantExpr *E) {
106*e038c9c4Sjoerg     if (llvm::Constant *Result = ConstantEmitter(CGF).tryEmitConstantExpr(E))
107*e038c9c4Sjoerg       return ComplexPairTy(Result->getAggregateElement(0U),
108*e038c9c4Sjoerg                            Result->getAggregateElement(1U));
1097330f729Sjoerg     return Visit(E->getSubExpr());
1107330f729Sjoerg   }
VisitParenExpr(ParenExpr * PE)1117330f729Sjoerg   ComplexPairTy VisitParenExpr(ParenExpr *PE) { return Visit(PE->getSubExpr());}
VisitGenericSelectionExpr(GenericSelectionExpr * GE)1127330f729Sjoerg   ComplexPairTy VisitGenericSelectionExpr(GenericSelectionExpr *GE) {
1137330f729Sjoerg     return Visit(GE->getResultExpr());
1147330f729Sjoerg   }
1157330f729Sjoerg   ComplexPairTy VisitImaginaryLiteral(const ImaginaryLiteral *IL);
1167330f729Sjoerg   ComplexPairTy
VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr * PE)1177330f729Sjoerg   VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *PE) {
1187330f729Sjoerg     return Visit(PE->getReplacement());
1197330f729Sjoerg   }
VisitCoawaitExpr(CoawaitExpr * S)1207330f729Sjoerg   ComplexPairTy VisitCoawaitExpr(CoawaitExpr *S) {
1217330f729Sjoerg     return CGF.EmitCoawaitExpr(*S).getComplexVal();
1227330f729Sjoerg   }
VisitCoyieldExpr(CoyieldExpr * S)1237330f729Sjoerg   ComplexPairTy VisitCoyieldExpr(CoyieldExpr *S) {
1247330f729Sjoerg     return CGF.EmitCoyieldExpr(*S).getComplexVal();
1257330f729Sjoerg   }
VisitUnaryCoawait(const UnaryOperator * E)1267330f729Sjoerg   ComplexPairTy VisitUnaryCoawait(const UnaryOperator *E) {
1277330f729Sjoerg     return Visit(E->getSubExpr());
1287330f729Sjoerg   }
1297330f729Sjoerg 
emitConstant(const CodeGenFunction::ConstantEmission & Constant,Expr * E)1307330f729Sjoerg   ComplexPairTy emitConstant(const CodeGenFunction::ConstantEmission &Constant,
1317330f729Sjoerg                              Expr *E) {
1327330f729Sjoerg     assert(Constant && "not a constant");
1337330f729Sjoerg     if (Constant.isReference())
1347330f729Sjoerg       return EmitLoadOfLValue(Constant.getReferenceLValue(CGF, E),
1357330f729Sjoerg                               E->getExprLoc());
1367330f729Sjoerg 
1377330f729Sjoerg     llvm::Constant *pair = Constant.getValue();
1387330f729Sjoerg     return ComplexPairTy(pair->getAggregateElement(0U),
1397330f729Sjoerg                          pair->getAggregateElement(1U));
1407330f729Sjoerg   }
1417330f729Sjoerg 
1427330f729Sjoerg   // l-values.
VisitDeclRefExpr(DeclRefExpr * E)1437330f729Sjoerg   ComplexPairTy VisitDeclRefExpr(DeclRefExpr *E) {
1447330f729Sjoerg     if (CodeGenFunction::ConstantEmission Constant = CGF.tryEmitAsConstant(E))
1457330f729Sjoerg       return emitConstant(Constant, E);
1467330f729Sjoerg     return EmitLoadOfLValue(E);
1477330f729Sjoerg   }
VisitObjCIvarRefExpr(ObjCIvarRefExpr * E)1487330f729Sjoerg   ComplexPairTy VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
1497330f729Sjoerg     return EmitLoadOfLValue(E);
1507330f729Sjoerg   }
VisitObjCMessageExpr(ObjCMessageExpr * E)1517330f729Sjoerg   ComplexPairTy VisitObjCMessageExpr(ObjCMessageExpr *E) {
1527330f729Sjoerg     return CGF.EmitObjCMessageExpr(E).getComplexVal();
1537330f729Sjoerg   }
VisitArraySubscriptExpr(Expr * E)1547330f729Sjoerg   ComplexPairTy VisitArraySubscriptExpr(Expr *E) { return EmitLoadOfLValue(E); }
VisitMemberExpr(MemberExpr * ME)1557330f729Sjoerg   ComplexPairTy VisitMemberExpr(MemberExpr *ME) {
1567330f729Sjoerg     if (CodeGenFunction::ConstantEmission Constant =
1577330f729Sjoerg             CGF.tryEmitAsConstant(ME)) {
1587330f729Sjoerg       CGF.EmitIgnoredExpr(ME->getBase());
1597330f729Sjoerg       return emitConstant(Constant, ME);
1607330f729Sjoerg     }
1617330f729Sjoerg     return EmitLoadOfLValue(ME);
1627330f729Sjoerg   }
VisitOpaqueValueExpr(OpaqueValueExpr * E)1637330f729Sjoerg   ComplexPairTy VisitOpaqueValueExpr(OpaqueValueExpr *E) {
1647330f729Sjoerg     if (E->isGLValue())
1657330f729Sjoerg       return EmitLoadOfLValue(CGF.getOrCreateOpaqueLValueMapping(E),
1667330f729Sjoerg                               E->getExprLoc());
1677330f729Sjoerg     return CGF.getOrCreateOpaqueRValueMapping(E).getComplexVal();
1687330f729Sjoerg   }
1697330f729Sjoerg 
VisitPseudoObjectExpr(PseudoObjectExpr * E)1707330f729Sjoerg   ComplexPairTy VisitPseudoObjectExpr(PseudoObjectExpr *E) {
1717330f729Sjoerg     return CGF.EmitPseudoObjectRValue(E).getComplexVal();
1727330f729Sjoerg   }
1737330f729Sjoerg 
1747330f729Sjoerg   // FIXME: CompoundLiteralExpr
1757330f729Sjoerg 
1767330f729Sjoerg   ComplexPairTy EmitCast(CastKind CK, Expr *Op, QualType DestTy);
VisitImplicitCastExpr(ImplicitCastExpr * E)1777330f729Sjoerg   ComplexPairTy VisitImplicitCastExpr(ImplicitCastExpr *E) {
1787330f729Sjoerg     // Unlike for scalars, we don't have to worry about function->ptr demotion
1797330f729Sjoerg     // here.
1807330f729Sjoerg     return EmitCast(E->getCastKind(), E->getSubExpr(), E->getType());
1817330f729Sjoerg   }
VisitCastExpr(CastExpr * E)1827330f729Sjoerg   ComplexPairTy VisitCastExpr(CastExpr *E) {
1837330f729Sjoerg     if (const auto *ECE = dyn_cast<ExplicitCastExpr>(E))
1847330f729Sjoerg       CGF.CGM.EmitExplicitCastExprType(ECE, &CGF);
1857330f729Sjoerg     return EmitCast(E->getCastKind(), E->getSubExpr(), E->getType());
1867330f729Sjoerg   }
1877330f729Sjoerg   ComplexPairTy VisitCallExpr(const CallExpr *E);
1887330f729Sjoerg   ComplexPairTy VisitStmtExpr(const StmtExpr *E);
1897330f729Sjoerg 
1907330f729Sjoerg   // Operators.
VisitPrePostIncDec(const UnaryOperator * E,bool isInc,bool isPre)1917330f729Sjoerg   ComplexPairTy VisitPrePostIncDec(const UnaryOperator *E,
1927330f729Sjoerg                                    bool isInc, bool isPre) {
1937330f729Sjoerg     LValue LV = CGF.EmitLValue(E->getSubExpr());
1947330f729Sjoerg     return CGF.EmitComplexPrePostIncDec(E, LV, isInc, isPre);
1957330f729Sjoerg   }
VisitUnaryPostDec(const UnaryOperator * E)1967330f729Sjoerg   ComplexPairTy VisitUnaryPostDec(const UnaryOperator *E) {
1977330f729Sjoerg     return VisitPrePostIncDec(E, false, false);
1987330f729Sjoerg   }
VisitUnaryPostInc(const UnaryOperator * E)1997330f729Sjoerg   ComplexPairTy VisitUnaryPostInc(const UnaryOperator *E) {
2007330f729Sjoerg     return VisitPrePostIncDec(E, true, false);
2017330f729Sjoerg   }
VisitUnaryPreDec(const UnaryOperator * E)2027330f729Sjoerg   ComplexPairTy VisitUnaryPreDec(const UnaryOperator *E) {
2037330f729Sjoerg     return VisitPrePostIncDec(E, false, true);
2047330f729Sjoerg   }
VisitUnaryPreInc(const UnaryOperator * E)2057330f729Sjoerg   ComplexPairTy VisitUnaryPreInc(const UnaryOperator *E) {
2067330f729Sjoerg     return VisitPrePostIncDec(E, true, true);
2077330f729Sjoerg   }
VisitUnaryDeref(const Expr * E)2087330f729Sjoerg   ComplexPairTy VisitUnaryDeref(const Expr *E) { return EmitLoadOfLValue(E); }
VisitUnaryPlus(const UnaryOperator * E)2097330f729Sjoerg   ComplexPairTy VisitUnaryPlus     (const UnaryOperator *E) {
2107330f729Sjoerg     TestAndClearIgnoreReal();
2117330f729Sjoerg     TestAndClearIgnoreImag();
2127330f729Sjoerg     return Visit(E->getSubExpr());
2137330f729Sjoerg   }
2147330f729Sjoerg   ComplexPairTy VisitUnaryMinus    (const UnaryOperator *E);
2157330f729Sjoerg   ComplexPairTy VisitUnaryNot      (const UnaryOperator *E);
2167330f729Sjoerg   // LNot,Real,Imag never return complex.
VisitUnaryExtension(const UnaryOperator * E)2177330f729Sjoerg   ComplexPairTy VisitUnaryExtension(const UnaryOperator *E) {
2187330f729Sjoerg     return Visit(E->getSubExpr());
2197330f729Sjoerg   }
VisitCXXDefaultArgExpr(CXXDefaultArgExpr * DAE)2207330f729Sjoerg   ComplexPairTy VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
2217330f729Sjoerg     CodeGenFunction::CXXDefaultArgExprScope Scope(CGF, DAE);
2227330f729Sjoerg     return Visit(DAE->getExpr());
2237330f729Sjoerg   }
VisitCXXDefaultInitExpr(CXXDefaultInitExpr * DIE)2247330f729Sjoerg   ComplexPairTy VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) {
2257330f729Sjoerg     CodeGenFunction::CXXDefaultInitExprScope Scope(CGF, DIE);
2267330f729Sjoerg     return Visit(DIE->getExpr());
2277330f729Sjoerg   }
VisitExprWithCleanups(ExprWithCleanups * E)2287330f729Sjoerg   ComplexPairTy VisitExprWithCleanups(ExprWithCleanups *E) {
2297330f729Sjoerg     CodeGenFunction::RunCleanupsScope Scope(CGF);
2307330f729Sjoerg     ComplexPairTy Vals = Visit(E->getSubExpr());
2317330f729Sjoerg     // Defend against dominance problems caused by jumps out of expression
2327330f729Sjoerg     // evaluation through the shared cleanup block.
2337330f729Sjoerg     Scope.ForceCleanup({&Vals.first, &Vals.second});
2347330f729Sjoerg     return Vals;
2357330f729Sjoerg   }
VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr * E)2367330f729Sjoerg   ComplexPairTy VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
2377330f729Sjoerg     assert(E->getType()->isAnyComplexType() && "Expected complex type!");
2387330f729Sjoerg     QualType Elem = E->getType()->castAs<ComplexType>()->getElementType();
2397330f729Sjoerg     llvm::Constant *Null = llvm::Constant::getNullValue(CGF.ConvertType(Elem));
2407330f729Sjoerg     return ComplexPairTy(Null, Null);
2417330f729Sjoerg   }
VisitImplicitValueInitExpr(ImplicitValueInitExpr * E)2427330f729Sjoerg   ComplexPairTy VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
2437330f729Sjoerg     assert(E->getType()->isAnyComplexType() && "Expected complex type!");
2447330f729Sjoerg     QualType Elem = E->getType()->castAs<ComplexType>()->getElementType();
2457330f729Sjoerg     llvm::Constant *Null =
2467330f729Sjoerg                        llvm::Constant::getNullValue(CGF.ConvertType(Elem));
2477330f729Sjoerg     return ComplexPairTy(Null, Null);
2487330f729Sjoerg   }
2497330f729Sjoerg 
2507330f729Sjoerg   struct BinOpInfo {
2517330f729Sjoerg     ComplexPairTy LHS;
2527330f729Sjoerg     ComplexPairTy RHS;
2537330f729Sjoerg     QualType Ty;  // Computation Type.
2547330f729Sjoerg   };
2557330f729Sjoerg 
2567330f729Sjoerg   BinOpInfo EmitBinOps(const BinaryOperator *E);
2577330f729Sjoerg   LValue EmitCompoundAssignLValue(const CompoundAssignOperator *E,
2587330f729Sjoerg                                   ComplexPairTy (ComplexExprEmitter::*Func)
2597330f729Sjoerg                                   (const BinOpInfo &),
2607330f729Sjoerg                                   RValue &Val);
2617330f729Sjoerg   ComplexPairTy EmitCompoundAssign(const CompoundAssignOperator *E,
2627330f729Sjoerg                                    ComplexPairTy (ComplexExprEmitter::*Func)
2637330f729Sjoerg                                    (const BinOpInfo &));
2647330f729Sjoerg 
2657330f729Sjoerg   ComplexPairTy EmitBinAdd(const BinOpInfo &Op);
2667330f729Sjoerg   ComplexPairTy EmitBinSub(const BinOpInfo &Op);
2677330f729Sjoerg   ComplexPairTy EmitBinMul(const BinOpInfo &Op);
2687330f729Sjoerg   ComplexPairTy EmitBinDiv(const BinOpInfo &Op);
2697330f729Sjoerg 
2707330f729Sjoerg   ComplexPairTy EmitComplexBinOpLibCall(StringRef LibCallName,
2717330f729Sjoerg                                         const BinOpInfo &Op);
2727330f729Sjoerg 
VisitBinAdd(const BinaryOperator * E)2737330f729Sjoerg   ComplexPairTy VisitBinAdd(const BinaryOperator *E) {
2747330f729Sjoerg     return EmitBinAdd(EmitBinOps(E));
2757330f729Sjoerg   }
VisitBinSub(const BinaryOperator * E)2767330f729Sjoerg   ComplexPairTy VisitBinSub(const BinaryOperator *E) {
2777330f729Sjoerg     return EmitBinSub(EmitBinOps(E));
2787330f729Sjoerg   }
VisitBinMul(const BinaryOperator * E)2797330f729Sjoerg   ComplexPairTy VisitBinMul(const BinaryOperator *E) {
2807330f729Sjoerg     return EmitBinMul(EmitBinOps(E));
2817330f729Sjoerg   }
VisitBinDiv(const BinaryOperator * E)2827330f729Sjoerg   ComplexPairTy VisitBinDiv(const BinaryOperator *E) {
2837330f729Sjoerg     return EmitBinDiv(EmitBinOps(E));
2847330f729Sjoerg   }
2857330f729Sjoerg 
VisitCXXRewrittenBinaryOperator(CXXRewrittenBinaryOperator * E)2867330f729Sjoerg   ComplexPairTy VisitCXXRewrittenBinaryOperator(CXXRewrittenBinaryOperator *E) {
2877330f729Sjoerg     return Visit(E->getSemanticForm());
2887330f729Sjoerg   }
2897330f729Sjoerg 
2907330f729Sjoerg   // Compound assignments.
VisitBinAddAssign(const CompoundAssignOperator * E)2917330f729Sjoerg   ComplexPairTy VisitBinAddAssign(const CompoundAssignOperator *E) {
2927330f729Sjoerg     return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinAdd);
2937330f729Sjoerg   }
VisitBinSubAssign(const CompoundAssignOperator * E)2947330f729Sjoerg   ComplexPairTy VisitBinSubAssign(const CompoundAssignOperator *E) {
2957330f729Sjoerg     return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinSub);
2967330f729Sjoerg   }
VisitBinMulAssign(const CompoundAssignOperator * E)2977330f729Sjoerg   ComplexPairTy VisitBinMulAssign(const CompoundAssignOperator *E) {
2987330f729Sjoerg     return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinMul);
2997330f729Sjoerg   }
VisitBinDivAssign(const CompoundAssignOperator * E)3007330f729Sjoerg   ComplexPairTy VisitBinDivAssign(const CompoundAssignOperator *E) {
3017330f729Sjoerg     return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinDiv);
3027330f729Sjoerg   }
3037330f729Sjoerg 
3047330f729Sjoerg   // GCC rejects rem/and/or/xor for integer complex.
3057330f729Sjoerg   // Logical and/or always return int, never complex.
3067330f729Sjoerg 
3077330f729Sjoerg   // No comparisons produce a complex result.
3087330f729Sjoerg 
3097330f729Sjoerg   LValue EmitBinAssignLValue(const BinaryOperator *E,
3107330f729Sjoerg                              ComplexPairTy &Val);
3117330f729Sjoerg   ComplexPairTy VisitBinAssign     (const BinaryOperator *E);
3127330f729Sjoerg   ComplexPairTy VisitBinComma      (const BinaryOperator *E);
3137330f729Sjoerg 
3147330f729Sjoerg 
3157330f729Sjoerg   ComplexPairTy
3167330f729Sjoerg   VisitAbstractConditionalOperator(const AbstractConditionalOperator *CO);
3177330f729Sjoerg   ComplexPairTy VisitChooseExpr(ChooseExpr *CE);
3187330f729Sjoerg 
3197330f729Sjoerg   ComplexPairTy VisitInitListExpr(InitListExpr *E);
3207330f729Sjoerg 
VisitCompoundLiteralExpr(CompoundLiteralExpr * E)3217330f729Sjoerg   ComplexPairTy VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
3227330f729Sjoerg     return EmitLoadOfLValue(E);
3237330f729Sjoerg   }
3247330f729Sjoerg 
3257330f729Sjoerg   ComplexPairTy VisitVAArgExpr(VAArgExpr *E);
3267330f729Sjoerg 
VisitAtomicExpr(AtomicExpr * E)3277330f729Sjoerg   ComplexPairTy VisitAtomicExpr(AtomicExpr *E) {
3287330f729Sjoerg     return CGF.EmitAtomicExpr(E).getComplexVal();
3297330f729Sjoerg   }
3307330f729Sjoerg };
3317330f729Sjoerg }  // end anonymous namespace.
3327330f729Sjoerg 
3337330f729Sjoerg //===----------------------------------------------------------------------===//
3347330f729Sjoerg //                                Utilities
3357330f729Sjoerg //===----------------------------------------------------------------------===//
3367330f729Sjoerg 
emitAddrOfRealComponent(Address addr,QualType complexType)3377330f729Sjoerg Address CodeGenFunction::emitAddrOfRealComponent(Address addr,
3387330f729Sjoerg                                                  QualType complexType) {
3397330f729Sjoerg   return Builder.CreateStructGEP(addr, 0, addr.getName() + ".realp");
3407330f729Sjoerg }
3417330f729Sjoerg 
emitAddrOfImagComponent(Address addr,QualType complexType)3427330f729Sjoerg Address CodeGenFunction::emitAddrOfImagComponent(Address addr,
3437330f729Sjoerg                                                  QualType complexType) {
3447330f729Sjoerg   return Builder.CreateStructGEP(addr, 1, addr.getName() + ".imagp");
3457330f729Sjoerg }
3467330f729Sjoerg 
3477330f729Sjoerg /// EmitLoadOfLValue - Given an RValue reference for a complex, emit code to
3487330f729Sjoerg /// load the real and imaginary pieces, returning them as Real/Imag.
EmitLoadOfLValue(LValue lvalue,SourceLocation loc)3497330f729Sjoerg ComplexPairTy ComplexExprEmitter::EmitLoadOfLValue(LValue lvalue,
3507330f729Sjoerg                                                    SourceLocation loc) {
3517330f729Sjoerg   assert(lvalue.isSimple() && "non-simple complex l-value?");
3527330f729Sjoerg   if (lvalue.getType()->isAtomicType())
3537330f729Sjoerg     return CGF.EmitAtomicLoad(lvalue, loc).getComplexVal();
3547330f729Sjoerg 
355*e038c9c4Sjoerg   Address SrcPtr = lvalue.getAddress(CGF);
3567330f729Sjoerg   bool isVolatile = lvalue.isVolatileQualified();
3577330f729Sjoerg 
3587330f729Sjoerg   llvm::Value *Real = nullptr, *Imag = nullptr;
3597330f729Sjoerg 
3607330f729Sjoerg   if (!IgnoreReal || isVolatile) {
3617330f729Sjoerg     Address RealP = CGF.emitAddrOfRealComponent(SrcPtr, lvalue.getType());
3627330f729Sjoerg     Real = Builder.CreateLoad(RealP, isVolatile, SrcPtr.getName() + ".real");
3637330f729Sjoerg   }
3647330f729Sjoerg 
3657330f729Sjoerg   if (!IgnoreImag || isVolatile) {
3667330f729Sjoerg     Address ImagP = CGF.emitAddrOfImagComponent(SrcPtr, lvalue.getType());
3677330f729Sjoerg     Imag = Builder.CreateLoad(ImagP, isVolatile, SrcPtr.getName() + ".imag");
3687330f729Sjoerg   }
3697330f729Sjoerg 
3707330f729Sjoerg   return ComplexPairTy(Real, Imag);
3717330f729Sjoerg }
3727330f729Sjoerg 
3737330f729Sjoerg /// EmitStoreOfComplex - Store the specified real/imag parts into the
3747330f729Sjoerg /// specified value pointer.
EmitStoreOfComplex(ComplexPairTy Val,LValue lvalue,bool isInit)3757330f729Sjoerg void ComplexExprEmitter::EmitStoreOfComplex(ComplexPairTy Val, LValue lvalue,
3767330f729Sjoerg                                             bool isInit) {
3777330f729Sjoerg   if (lvalue.getType()->isAtomicType() ||
3787330f729Sjoerg       (!isInit && CGF.LValueIsSuitableForInlineAtomic(lvalue)))
3797330f729Sjoerg     return CGF.EmitAtomicStore(RValue::getComplex(Val), lvalue, isInit);
3807330f729Sjoerg 
381*e038c9c4Sjoerg   Address Ptr = lvalue.getAddress(CGF);
3827330f729Sjoerg   Address RealPtr = CGF.emitAddrOfRealComponent(Ptr, lvalue.getType());
3837330f729Sjoerg   Address ImagPtr = CGF.emitAddrOfImagComponent(Ptr, lvalue.getType());
3847330f729Sjoerg 
3857330f729Sjoerg   Builder.CreateStore(Val.first, RealPtr, lvalue.isVolatileQualified());
3867330f729Sjoerg   Builder.CreateStore(Val.second, ImagPtr, lvalue.isVolatileQualified());
3877330f729Sjoerg }
3887330f729Sjoerg 
3897330f729Sjoerg 
3907330f729Sjoerg 
3917330f729Sjoerg //===----------------------------------------------------------------------===//
3927330f729Sjoerg //                            Visitor Methods
3937330f729Sjoerg //===----------------------------------------------------------------------===//
3947330f729Sjoerg 
VisitExpr(Expr * E)3957330f729Sjoerg ComplexPairTy ComplexExprEmitter::VisitExpr(Expr *E) {
3967330f729Sjoerg   CGF.ErrorUnsupported(E, "complex expression");
3977330f729Sjoerg   llvm::Type *EltTy =
3987330f729Sjoerg     CGF.ConvertType(getComplexType(E->getType())->getElementType());
3997330f729Sjoerg   llvm::Value *U = llvm::UndefValue::get(EltTy);
4007330f729Sjoerg   return ComplexPairTy(U, U);
4017330f729Sjoerg }
4027330f729Sjoerg 
4037330f729Sjoerg ComplexPairTy ComplexExprEmitter::
VisitImaginaryLiteral(const ImaginaryLiteral * IL)4047330f729Sjoerg VisitImaginaryLiteral(const ImaginaryLiteral *IL) {
4057330f729Sjoerg   llvm::Value *Imag = CGF.EmitScalarExpr(IL->getSubExpr());
4067330f729Sjoerg   return ComplexPairTy(llvm::Constant::getNullValue(Imag->getType()), Imag);
4077330f729Sjoerg }
4087330f729Sjoerg 
4097330f729Sjoerg 
VisitCallExpr(const CallExpr * E)4107330f729Sjoerg ComplexPairTy ComplexExprEmitter::VisitCallExpr(const CallExpr *E) {
4117330f729Sjoerg   if (E->getCallReturnType(CGF.getContext())->isReferenceType())
4127330f729Sjoerg     return EmitLoadOfLValue(E);
4137330f729Sjoerg 
4147330f729Sjoerg   return CGF.EmitCallExpr(E).getComplexVal();
4157330f729Sjoerg }
4167330f729Sjoerg 
VisitStmtExpr(const StmtExpr * E)4177330f729Sjoerg ComplexPairTy ComplexExprEmitter::VisitStmtExpr(const StmtExpr *E) {
4187330f729Sjoerg   CodeGenFunction::StmtExprEvaluation eval(CGF);
4197330f729Sjoerg   Address RetAlloca = CGF.EmitCompoundStmt(*E->getSubStmt(), true);
4207330f729Sjoerg   assert(RetAlloca.isValid() && "Expected complex return value");
4217330f729Sjoerg   return EmitLoadOfLValue(CGF.MakeAddrLValue(RetAlloca, E->getType()),
4227330f729Sjoerg                           E->getExprLoc());
4237330f729Sjoerg }
4247330f729Sjoerg 
4257330f729Sjoerg /// Emit a cast from complex value Val to DestType.
EmitComplexToComplexCast(ComplexPairTy Val,QualType SrcType,QualType DestType,SourceLocation Loc)4267330f729Sjoerg ComplexPairTy ComplexExprEmitter::EmitComplexToComplexCast(ComplexPairTy Val,
4277330f729Sjoerg                                                            QualType SrcType,
4287330f729Sjoerg                                                            QualType DestType,
4297330f729Sjoerg                                                            SourceLocation Loc) {
4307330f729Sjoerg   // Get the src/dest element type.
4317330f729Sjoerg   SrcType = SrcType->castAs<ComplexType>()->getElementType();
4327330f729Sjoerg   DestType = DestType->castAs<ComplexType>()->getElementType();
4337330f729Sjoerg 
4347330f729Sjoerg   // C99 6.3.1.6: When a value of complex type is converted to another
4357330f729Sjoerg   // complex type, both the real and imaginary parts follow the conversion
4367330f729Sjoerg   // rules for the corresponding real types.
437*e038c9c4Sjoerg   if (Val.first)
4387330f729Sjoerg     Val.first = CGF.EmitScalarConversion(Val.first, SrcType, DestType, Loc);
439*e038c9c4Sjoerg   if (Val.second)
4407330f729Sjoerg     Val.second = CGF.EmitScalarConversion(Val.second, SrcType, DestType, Loc);
4417330f729Sjoerg   return Val;
4427330f729Sjoerg }
4437330f729Sjoerg 
EmitScalarToComplexCast(llvm::Value * Val,QualType SrcType,QualType DestType,SourceLocation Loc)4447330f729Sjoerg ComplexPairTy ComplexExprEmitter::EmitScalarToComplexCast(llvm::Value *Val,
4457330f729Sjoerg                                                           QualType SrcType,
4467330f729Sjoerg                                                           QualType DestType,
4477330f729Sjoerg                                                           SourceLocation Loc) {
4487330f729Sjoerg   // Convert the input element to the element type of the complex.
4497330f729Sjoerg   DestType = DestType->castAs<ComplexType>()->getElementType();
4507330f729Sjoerg   Val = CGF.EmitScalarConversion(Val, SrcType, DestType, Loc);
4517330f729Sjoerg 
4527330f729Sjoerg   // Return (realval, 0).
4537330f729Sjoerg   return ComplexPairTy(Val, llvm::Constant::getNullValue(Val->getType()));
4547330f729Sjoerg }
4557330f729Sjoerg 
EmitCast(CastKind CK,Expr * Op,QualType DestTy)4567330f729Sjoerg ComplexPairTy ComplexExprEmitter::EmitCast(CastKind CK, Expr *Op,
4577330f729Sjoerg                                            QualType DestTy) {
4587330f729Sjoerg   switch (CK) {
4597330f729Sjoerg   case CK_Dependent: llvm_unreachable("dependent cast kind in IR gen!");
4607330f729Sjoerg 
4617330f729Sjoerg   // Atomic to non-atomic casts may be more than a no-op for some platforms and
4627330f729Sjoerg   // for some types.
4637330f729Sjoerg   case CK_AtomicToNonAtomic:
4647330f729Sjoerg   case CK_NonAtomicToAtomic:
4657330f729Sjoerg   case CK_NoOp:
4667330f729Sjoerg   case CK_LValueToRValue:
4677330f729Sjoerg   case CK_UserDefinedConversion:
4687330f729Sjoerg     return Visit(Op);
4697330f729Sjoerg 
4707330f729Sjoerg   case CK_LValueBitCast: {
4717330f729Sjoerg     LValue origLV = CGF.EmitLValue(Op);
472*e038c9c4Sjoerg     Address V = origLV.getAddress(CGF);
4737330f729Sjoerg     V = Builder.CreateElementBitCast(V, CGF.ConvertType(DestTy));
4747330f729Sjoerg     return EmitLoadOfLValue(CGF.MakeAddrLValue(V, DestTy), Op->getExprLoc());
4757330f729Sjoerg   }
4767330f729Sjoerg 
4777330f729Sjoerg   case CK_LValueToRValueBitCast: {
4787330f729Sjoerg     LValue SourceLVal = CGF.EmitLValue(Op);
479*e038c9c4Sjoerg     Address Addr = Builder.CreateElementBitCast(SourceLVal.getAddress(CGF),
4807330f729Sjoerg                                                 CGF.ConvertTypeForMem(DestTy));
4817330f729Sjoerg     LValue DestLV = CGF.MakeAddrLValue(Addr, DestTy);
4827330f729Sjoerg     DestLV.setTBAAInfo(TBAAAccessInfo::getMayAliasInfo());
4837330f729Sjoerg     return EmitLoadOfLValue(DestLV, Op->getExprLoc());
4847330f729Sjoerg   }
4857330f729Sjoerg 
4867330f729Sjoerg   case CK_BitCast:
4877330f729Sjoerg   case CK_BaseToDerived:
4887330f729Sjoerg   case CK_DerivedToBase:
4897330f729Sjoerg   case CK_UncheckedDerivedToBase:
4907330f729Sjoerg   case CK_Dynamic:
4917330f729Sjoerg   case CK_ToUnion:
4927330f729Sjoerg   case CK_ArrayToPointerDecay:
4937330f729Sjoerg   case CK_FunctionToPointerDecay:
4947330f729Sjoerg   case CK_NullToPointer:
4957330f729Sjoerg   case CK_NullToMemberPointer:
4967330f729Sjoerg   case CK_BaseToDerivedMemberPointer:
4977330f729Sjoerg   case CK_DerivedToBaseMemberPointer:
4987330f729Sjoerg   case CK_MemberPointerToBoolean:
4997330f729Sjoerg   case CK_ReinterpretMemberPointer:
5007330f729Sjoerg   case CK_ConstructorConversion:
5017330f729Sjoerg   case CK_IntegralToPointer:
5027330f729Sjoerg   case CK_PointerToIntegral:
5037330f729Sjoerg   case CK_PointerToBoolean:
5047330f729Sjoerg   case CK_ToVoid:
5057330f729Sjoerg   case CK_VectorSplat:
5067330f729Sjoerg   case CK_IntegralCast:
5077330f729Sjoerg   case CK_BooleanToSignedIntegral:
5087330f729Sjoerg   case CK_IntegralToBoolean:
5097330f729Sjoerg   case CK_IntegralToFloating:
5107330f729Sjoerg   case CK_FloatingToIntegral:
5117330f729Sjoerg   case CK_FloatingToBoolean:
5127330f729Sjoerg   case CK_FloatingCast:
5137330f729Sjoerg   case CK_CPointerToObjCPointerCast:
5147330f729Sjoerg   case CK_BlockPointerToObjCPointerCast:
5157330f729Sjoerg   case CK_AnyPointerToBlockPointerCast:
5167330f729Sjoerg   case CK_ObjCObjectLValueCast:
5177330f729Sjoerg   case CK_FloatingComplexToReal:
5187330f729Sjoerg   case CK_FloatingComplexToBoolean:
5197330f729Sjoerg   case CK_IntegralComplexToReal:
5207330f729Sjoerg   case CK_IntegralComplexToBoolean:
5217330f729Sjoerg   case CK_ARCProduceObject:
5227330f729Sjoerg   case CK_ARCConsumeObject:
5237330f729Sjoerg   case CK_ARCReclaimReturnedObject:
5247330f729Sjoerg   case CK_ARCExtendBlockObject:
5257330f729Sjoerg   case CK_CopyAndAutoreleaseBlockObject:
5267330f729Sjoerg   case CK_BuiltinFnToFnPtr:
5277330f729Sjoerg   case CK_ZeroToOCLOpaqueType:
5287330f729Sjoerg   case CK_AddressSpaceConversion:
5297330f729Sjoerg   case CK_IntToOCLSampler:
530*e038c9c4Sjoerg   case CK_FloatingToFixedPoint:
531*e038c9c4Sjoerg   case CK_FixedPointToFloating:
5327330f729Sjoerg   case CK_FixedPointCast:
5337330f729Sjoerg   case CK_FixedPointToBoolean:
5347330f729Sjoerg   case CK_FixedPointToIntegral:
5357330f729Sjoerg   case CK_IntegralToFixedPoint:
536*e038c9c4Sjoerg   case CK_MatrixCast:
5377330f729Sjoerg     llvm_unreachable("invalid cast kind for complex value");
5387330f729Sjoerg 
5397330f729Sjoerg   case CK_FloatingRealToComplex:
540*e038c9c4Sjoerg   case CK_IntegralRealToComplex: {
541*e038c9c4Sjoerg     CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, Op);
5427330f729Sjoerg     return EmitScalarToComplexCast(CGF.EmitScalarExpr(Op), Op->getType(),
5437330f729Sjoerg                                    DestTy, Op->getExprLoc());
544*e038c9c4Sjoerg   }
5457330f729Sjoerg 
5467330f729Sjoerg   case CK_FloatingComplexCast:
5477330f729Sjoerg   case CK_FloatingComplexToIntegralComplex:
5487330f729Sjoerg   case CK_IntegralComplexCast:
549*e038c9c4Sjoerg   case CK_IntegralComplexToFloatingComplex: {
550*e038c9c4Sjoerg     CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, Op);
5517330f729Sjoerg     return EmitComplexToComplexCast(Visit(Op), Op->getType(), DestTy,
5527330f729Sjoerg                                     Op->getExprLoc());
5537330f729Sjoerg   }
554*e038c9c4Sjoerg   }
5557330f729Sjoerg 
5567330f729Sjoerg   llvm_unreachable("unknown cast resulting in complex value");
5577330f729Sjoerg }
5587330f729Sjoerg 
VisitUnaryMinus(const UnaryOperator * E)5597330f729Sjoerg ComplexPairTy ComplexExprEmitter::VisitUnaryMinus(const UnaryOperator *E) {
5607330f729Sjoerg   TestAndClearIgnoreReal();
5617330f729Sjoerg   TestAndClearIgnoreImag();
5627330f729Sjoerg   ComplexPairTy Op = Visit(E->getSubExpr());
5637330f729Sjoerg 
5647330f729Sjoerg   llvm::Value *ResR, *ResI;
5657330f729Sjoerg   if (Op.first->getType()->isFloatingPointTy()) {
5667330f729Sjoerg     ResR = Builder.CreateFNeg(Op.first,  "neg.r");
5677330f729Sjoerg     ResI = Builder.CreateFNeg(Op.second, "neg.i");
5687330f729Sjoerg   } else {
5697330f729Sjoerg     ResR = Builder.CreateNeg(Op.first,  "neg.r");
5707330f729Sjoerg     ResI = Builder.CreateNeg(Op.second, "neg.i");
5717330f729Sjoerg   }
5727330f729Sjoerg   return ComplexPairTy(ResR, ResI);
5737330f729Sjoerg }
5747330f729Sjoerg 
VisitUnaryNot(const UnaryOperator * E)5757330f729Sjoerg ComplexPairTy ComplexExprEmitter::VisitUnaryNot(const UnaryOperator *E) {
5767330f729Sjoerg   TestAndClearIgnoreReal();
5777330f729Sjoerg   TestAndClearIgnoreImag();
5787330f729Sjoerg   // ~(a+ib) = a + i*-b
5797330f729Sjoerg   ComplexPairTy Op = Visit(E->getSubExpr());
5807330f729Sjoerg   llvm::Value *ResI;
5817330f729Sjoerg   if (Op.second->getType()->isFloatingPointTy())
5827330f729Sjoerg     ResI = Builder.CreateFNeg(Op.second, "conj.i");
5837330f729Sjoerg   else
5847330f729Sjoerg     ResI = Builder.CreateNeg(Op.second, "conj.i");
5857330f729Sjoerg 
5867330f729Sjoerg   return ComplexPairTy(Op.first, ResI);
5877330f729Sjoerg }
5887330f729Sjoerg 
EmitBinAdd(const BinOpInfo & Op)5897330f729Sjoerg ComplexPairTy ComplexExprEmitter::EmitBinAdd(const BinOpInfo &Op) {
5907330f729Sjoerg   llvm::Value *ResR, *ResI;
5917330f729Sjoerg 
5927330f729Sjoerg   if (Op.LHS.first->getType()->isFloatingPointTy()) {
5937330f729Sjoerg     ResR = Builder.CreateFAdd(Op.LHS.first,  Op.RHS.first,  "add.r");
5947330f729Sjoerg     if (Op.LHS.second && Op.RHS.second)
5957330f729Sjoerg       ResI = Builder.CreateFAdd(Op.LHS.second, Op.RHS.second, "add.i");
5967330f729Sjoerg     else
5977330f729Sjoerg       ResI = Op.LHS.second ? Op.LHS.second : Op.RHS.second;
5987330f729Sjoerg     assert(ResI && "Only one operand may be real!");
5997330f729Sjoerg   } else {
6007330f729Sjoerg     ResR = Builder.CreateAdd(Op.LHS.first,  Op.RHS.first,  "add.r");
6017330f729Sjoerg     assert(Op.LHS.second && Op.RHS.second &&
6027330f729Sjoerg            "Both operands of integer complex operators must be complex!");
6037330f729Sjoerg     ResI = Builder.CreateAdd(Op.LHS.second, Op.RHS.second, "add.i");
6047330f729Sjoerg   }
6057330f729Sjoerg   return ComplexPairTy(ResR, ResI);
6067330f729Sjoerg }
6077330f729Sjoerg 
EmitBinSub(const BinOpInfo & Op)6087330f729Sjoerg ComplexPairTy ComplexExprEmitter::EmitBinSub(const BinOpInfo &Op) {
6097330f729Sjoerg   llvm::Value *ResR, *ResI;
6107330f729Sjoerg   if (Op.LHS.first->getType()->isFloatingPointTy()) {
6117330f729Sjoerg     ResR = Builder.CreateFSub(Op.LHS.first, Op.RHS.first, "sub.r");
6127330f729Sjoerg     if (Op.LHS.second && Op.RHS.second)
6137330f729Sjoerg       ResI = Builder.CreateFSub(Op.LHS.second, Op.RHS.second, "sub.i");
6147330f729Sjoerg     else
6157330f729Sjoerg       ResI = Op.LHS.second ? Op.LHS.second
6167330f729Sjoerg                            : Builder.CreateFNeg(Op.RHS.second, "sub.i");
6177330f729Sjoerg     assert(ResI && "Only one operand may be real!");
6187330f729Sjoerg   } else {
6197330f729Sjoerg     ResR = Builder.CreateSub(Op.LHS.first, Op.RHS.first, "sub.r");
6207330f729Sjoerg     assert(Op.LHS.second && Op.RHS.second &&
6217330f729Sjoerg            "Both operands of integer complex operators must be complex!");
6227330f729Sjoerg     ResI = Builder.CreateSub(Op.LHS.second, Op.RHS.second, "sub.i");
6237330f729Sjoerg   }
6247330f729Sjoerg   return ComplexPairTy(ResR, ResI);
6257330f729Sjoerg }
6267330f729Sjoerg 
6277330f729Sjoerg /// Emit a libcall for a binary operation on complex types.
EmitComplexBinOpLibCall(StringRef LibCallName,const BinOpInfo & Op)6287330f729Sjoerg ComplexPairTy ComplexExprEmitter::EmitComplexBinOpLibCall(StringRef LibCallName,
6297330f729Sjoerg                                                           const BinOpInfo &Op) {
6307330f729Sjoerg   CallArgList Args;
6317330f729Sjoerg   Args.add(RValue::get(Op.LHS.first),
6327330f729Sjoerg            Op.Ty->castAs<ComplexType>()->getElementType());
6337330f729Sjoerg   Args.add(RValue::get(Op.LHS.second),
6347330f729Sjoerg            Op.Ty->castAs<ComplexType>()->getElementType());
6357330f729Sjoerg   Args.add(RValue::get(Op.RHS.first),
6367330f729Sjoerg            Op.Ty->castAs<ComplexType>()->getElementType());
6377330f729Sjoerg   Args.add(RValue::get(Op.RHS.second),
6387330f729Sjoerg            Op.Ty->castAs<ComplexType>()->getElementType());
6397330f729Sjoerg 
6407330f729Sjoerg   // We *must* use the full CG function call building logic here because the
6417330f729Sjoerg   // complex type has special ABI handling. We also should not forget about
6427330f729Sjoerg   // special calling convention which may be used for compiler builtins.
6437330f729Sjoerg 
6447330f729Sjoerg   // We create a function qualified type to state that this call does not have
6457330f729Sjoerg   // any exceptions.
6467330f729Sjoerg   FunctionProtoType::ExtProtoInfo EPI;
6477330f729Sjoerg   EPI = EPI.withExceptionSpec(
6487330f729Sjoerg       FunctionProtoType::ExceptionSpecInfo(EST_BasicNoexcept));
6497330f729Sjoerg   SmallVector<QualType, 4> ArgsQTys(
6507330f729Sjoerg       4, Op.Ty->castAs<ComplexType>()->getElementType());
6517330f729Sjoerg   QualType FQTy = CGF.getContext().getFunctionType(Op.Ty, ArgsQTys, EPI);
6527330f729Sjoerg   const CGFunctionInfo &FuncInfo = CGF.CGM.getTypes().arrangeFreeFunctionCall(
6537330f729Sjoerg       Args, cast<FunctionType>(FQTy.getTypePtr()), false);
6547330f729Sjoerg 
6557330f729Sjoerg   llvm::FunctionType *FTy = CGF.CGM.getTypes().GetFunctionType(FuncInfo);
6567330f729Sjoerg   llvm::FunctionCallee Func = CGF.CGM.CreateRuntimeFunction(
6577330f729Sjoerg       FTy, LibCallName, llvm::AttributeList(), true);
6587330f729Sjoerg   CGCallee Callee = CGCallee::forDirect(Func, FQTy->getAs<FunctionProtoType>());
6597330f729Sjoerg 
6607330f729Sjoerg   llvm::CallBase *Call;
6617330f729Sjoerg   RValue Res = CGF.EmitCall(FuncInfo, Callee, ReturnValueSlot(), Args, &Call);
6627330f729Sjoerg   Call->setCallingConv(CGF.CGM.getRuntimeCC());
6637330f729Sjoerg   return Res.getComplexVal();
6647330f729Sjoerg }
6657330f729Sjoerg 
6667330f729Sjoerg /// Lookup the libcall name for a given floating point type complex
6677330f729Sjoerg /// multiply.
getComplexMultiplyLibCallName(llvm::Type * Ty)6687330f729Sjoerg static StringRef getComplexMultiplyLibCallName(llvm::Type *Ty) {
6697330f729Sjoerg   switch (Ty->getTypeID()) {
6707330f729Sjoerg   default:
6717330f729Sjoerg     llvm_unreachable("Unsupported floating point type!");
6727330f729Sjoerg   case llvm::Type::HalfTyID:
6737330f729Sjoerg     return "__mulhc3";
6747330f729Sjoerg   case llvm::Type::FloatTyID:
6757330f729Sjoerg     return "__mulsc3";
6767330f729Sjoerg   case llvm::Type::DoubleTyID:
6777330f729Sjoerg     return "__muldc3";
6787330f729Sjoerg   case llvm::Type::PPC_FP128TyID:
6797330f729Sjoerg     return "__multc3";
6807330f729Sjoerg   case llvm::Type::X86_FP80TyID:
6817330f729Sjoerg     return "__mulxc3";
6827330f729Sjoerg   case llvm::Type::FP128TyID:
6837330f729Sjoerg     return "__multc3";
6847330f729Sjoerg   }
6857330f729Sjoerg }
6867330f729Sjoerg 
6877330f729Sjoerg // See C11 Annex G.5.1 for the semantics of multiplicative operators on complex
6887330f729Sjoerg // typed values.
EmitBinMul(const BinOpInfo & Op)6897330f729Sjoerg ComplexPairTy ComplexExprEmitter::EmitBinMul(const BinOpInfo &Op) {
6907330f729Sjoerg   using llvm::Value;
6917330f729Sjoerg   Value *ResR, *ResI;
6927330f729Sjoerg   llvm::MDBuilder MDHelper(CGF.getLLVMContext());
6937330f729Sjoerg 
6947330f729Sjoerg   if (Op.LHS.first->getType()->isFloatingPointTy()) {
6957330f729Sjoerg     // The general formulation is:
6967330f729Sjoerg     // (a + ib) * (c + id) = (a * c - b * d) + i(a * d + b * c)
6977330f729Sjoerg     //
6987330f729Sjoerg     // But we can fold away components which would be zero due to a real
6997330f729Sjoerg     // operand according to C11 Annex G.5.1p2.
7007330f729Sjoerg     // FIXME: C11 also provides for imaginary types which would allow folding
7017330f729Sjoerg     // still more of this within the type system.
7027330f729Sjoerg 
7037330f729Sjoerg     if (Op.LHS.second && Op.RHS.second) {
7047330f729Sjoerg       // If both operands are complex, emit the core math directly, and then
7057330f729Sjoerg       // test for NaNs. If we find NaNs in the result, we delegate to a libcall
7067330f729Sjoerg       // to carefully re-compute the correct infinity representation if
7077330f729Sjoerg       // possible. The expectation is that the presence of NaNs here is
7087330f729Sjoerg       // *extremely* rare, and so the cost of the libcall is almost irrelevant.
7097330f729Sjoerg       // This is good, because the libcall re-computes the core multiplication
7107330f729Sjoerg       // exactly the same as we do here and re-tests for NaNs in order to be
7117330f729Sjoerg       // a generic complex*complex libcall.
7127330f729Sjoerg 
7137330f729Sjoerg       // First compute the four products.
7147330f729Sjoerg       Value *AC = Builder.CreateFMul(Op.LHS.first, Op.RHS.first, "mul_ac");
7157330f729Sjoerg       Value *BD = Builder.CreateFMul(Op.LHS.second, Op.RHS.second, "mul_bd");
7167330f729Sjoerg       Value *AD = Builder.CreateFMul(Op.LHS.first, Op.RHS.second, "mul_ad");
7177330f729Sjoerg       Value *BC = Builder.CreateFMul(Op.LHS.second, Op.RHS.first, "mul_bc");
7187330f729Sjoerg 
7197330f729Sjoerg       // The real part is the difference of the first two, the imaginary part is
7207330f729Sjoerg       // the sum of the second.
7217330f729Sjoerg       ResR = Builder.CreateFSub(AC, BD, "mul_r");
7227330f729Sjoerg       ResI = Builder.CreateFAdd(AD, BC, "mul_i");
7237330f729Sjoerg 
7247330f729Sjoerg       // Emit the test for the real part becoming NaN and create a branch to
7257330f729Sjoerg       // handle it. We test for NaN by comparing the number to itself.
7267330f729Sjoerg       Value *IsRNaN = Builder.CreateFCmpUNO(ResR, ResR, "isnan_cmp");
7277330f729Sjoerg       llvm::BasicBlock *ContBB = CGF.createBasicBlock("complex_mul_cont");
7287330f729Sjoerg       llvm::BasicBlock *INaNBB = CGF.createBasicBlock("complex_mul_imag_nan");
7297330f729Sjoerg       llvm::Instruction *Branch = Builder.CreateCondBr(IsRNaN, INaNBB, ContBB);
7307330f729Sjoerg       llvm::BasicBlock *OrigBB = Branch->getParent();
7317330f729Sjoerg 
7327330f729Sjoerg       // Give hint that we very much don't expect to see NaNs.
7337330f729Sjoerg       // Value chosen to match UR_NONTAKEN_WEIGHT, see BranchProbabilityInfo.cpp
7347330f729Sjoerg       llvm::MDNode *BrWeight = MDHelper.createBranchWeights(1, (1U << 20) - 1);
7357330f729Sjoerg       Branch->setMetadata(llvm::LLVMContext::MD_prof, BrWeight);
7367330f729Sjoerg 
7377330f729Sjoerg       // Now test the imaginary part and create its branch.
7387330f729Sjoerg       CGF.EmitBlock(INaNBB);
7397330f729Sjoerg       Value *IsINaN = Builder.CreateFCmpUNO(ResI, ResI, "isnan_cmp");
7407330f729Sjoerg       llvm::BasicBlock *LibCallBB = CGF.createBasicBlock("complex_mul_libcall");
7417330f729Sjoerg       Branch = Builder.CreateCondBr(IsINaN, LibCallBB, ContBB);
7427330f729Sjoerg       Branch->setMetadata(llvm::LLVMContext::MD_prof, BrWeight);
7437330f729Sjoerg 
7447330f729Sjoerg       // Now emit the libcall on this slowest of the slow paths.
7457330f729Sjoerg       CGF.EmitBlock(LibCallBB);
7467330f729Sjoerg       Value *LibCallR, *LibCallI;
7477330f729Sjoerg       std::tie(LibCallR, LibCallI) = EmitComplexBinOpLibCall(
7487330f729Sjoerg           getComplexMultiplyLibCallName(Op.LHS.first->getType()), Op);
7497330f729Sjoerg       Builder.CreateBr(ContBB);
7507330f729Sjoerg 
7517330f729Sjoerg       // Finally continue execution by phi-ing together the different
7527330f729Sjoerg       // computation paths.
7537330f729Sjoerg       CGF.EmitBlock(ContBB);
7547330f729Sjoerg       llvm::PHINode *RealPHI = Builder.CreatePHI(ResR->getType(), 3, "real_mul_phi");
7557330f729Sjoerg       RealPHI->addIncoming(ResR, OrigBB);
7567330f729Sjoerg       RealPHI->addIncoming(ResR, INaNBB);
7577330f729Sjoerg       RealPHI->addIncoming(LibCallR, LibCallBB);
7587330f729Sjoerg       llvm::PHINode *ImagPHI = Builder.CreatePHI(ResI->getType(), 3, "imag_mul_phi");
7597330f729Sjoerg       ImagPHI->addIncoming(ResI, OrigBB);
7607330f729Sjoerg       ImagPHI->addIncoming(ResI, INaNBB);
7617330f729Sjoerg       ImagPHI->addIncoming(LibCallI, LibCallBB);
7627330f729Sjoerg       return ComplexPairTy(RealPHI, ImagPHI);
7637330f729Sjoerg     }
7647330f729Sjoerg     assert((Op.LHS.second || Op.RHS.second) &&
7657330f729Sjoerg            "At least one operand must be complex!");
7667330f729Sjoerg 
7677330f729Sjoerg     // If either of the operands is a real rather than a complex, the
7687330f729Sjoerg     // imaginary component is ignored when computing the real component of the
7697330f729Sjoerg     // result.
7707330f729Sjoerg     ResR = Builder.CreateFMul(Op.LHS.first, Op.RHS.first, "mul.rl");
7717330f729Sjoerg 
7727330f729Sjoerg     ResI = Op.LHS.second
7737330f729Sjoerg                ? Builder.CreateFMul(Op.LHS.second, Op.RHS.first, "mul.il")
7747330f729Sjoerg                : Builder.CreateFMul(Op.LHS.first, Op.RHS.second, "mul.ir");
7757330f729Sjoerg   } else {
7767330f729Sjoerg     assert(Op.LHS.second && Op.RHS.second &&
7777330f729Sjoerg            "Both operands of integer complex operators must be complex!");
7787330f729Sjoerg     Value *ResRl = Builder.CreateMul(Op.LHS.first, Op.RHS.first, "mul.rl");
7797330f729Sjoerg     Value *ResRr = Builder.CreateMul(Op.LHS.second, Op.RHS.second, "mul.rr");
7807330f729Sjoerg     ResR = Builder.CreateSub(ResRl, ResRr, "mul.r");
7817330f729Sjoerg 
7827330f729Sjoerg     Value *ResIl = Builder.CreateMul(Op.LHS.second, Op.RHS.first, "mul.il");
7837330f729Sjoerg     Value *ResIr = Builder.CreateMul(Op.LHS.first, Op.RHS.second, "mul.ir");
7847330f729Sjoerg     ResI = Builder.CreateAdd(ResIl, ResIr, "mul.i");
7857330f729Sjoerg   }
7867330f729Sjoerg   return ComplexPairTy(ResR, ResI);
7877330f729Sjoerg }
7887330f729Sjoerg 
7897330f729Sjoerg // See C11 Annex G.5.1 for the semantics of multiplicative operators on complex
7907330f729Sjoerg // typed values.
EmitBinDiv(const BinOpInfo & Op)7917330f729Sjoerg ComplexPairTy ComplexExprEmitter::EmitBinDiv(const BinOpInfo &Op) {
7927330f729Sjoerg   llvm::Value *LHSr = Op.LHS.first, *LHSi = Op.LHS.second;
7937330f729Sjoerg   llvm::Value *RHSr = Op.RHS.first, *RHSi = Op.RHS.second;
7947330f729Sjoerg 
7957330f729Sjoerg   llvm::Value *DSTr, *DSTi;
7967330f729Sjoerg   if (LHSr->getType()->isFloatingPointTy()) {
7977330f729Sjoerg     // If we have a complex operand on the RHS and FastMath is not allowed, we
7987330f729Sjoerg     // delegate to a libcall to handle all of the complexities and minimize
7997330f729Sjoerg     // underflow/overflow cases. When FastMath is allowed we construct the
8007330f729Sjoerg     // divide inline using the same algorithm as for integer operands.
8017330f729Sjoerg     //
8027330f729Sjoerg     // FIXME: We would be able to avoid the libcall in many places if we
8037330f729Sjoerg     // supported imaginary types in addition to complex types.
8047330f729Sjoerg     if (RHSi && !CGF.getLangOpts().FastMath) {
8057330f729Sjoerg       BinOpInfo LibCallOp = Op;
8067330f729Sjoerg       // If LHS was a real, supply a null imaginary part.
8077330f729Sjoerg       if (!LHSi)
8087330f729Sjoerg         LibCallOp.LHS.second = llvm::Constant::getNullValue(LHSr->getType());
8097330f729Sjoerg 
8107330f729Sjoerg       switch (LHSr->getType()->getTypeID()) {
8117330f729Sjoerg       default:
8127330f729Sjoerg         llvm_unreachable("Unsupported floating point type!");
8137330f729Sjoerg       case llvm::Type::HalfTyID:
8147330f729Sjoerg         return EmitComplexBinOpLibCall("__divhc3", LibCallOp);
8157330f729Sjoerg       case llvm::Type::FloatTyID:
8167330f729Sjoerg         return EmitComplexBinOpLibCall("__divsc3", LibCallOp);
8177330f729Sjoerg       case llvm::Type::DoubleTyID:
8187330f729Sjoerg         return EmitComplexBinOpLibCall("__divdc3", LibCallOp);
8197330f729Sjoerg       case llvm::Type::PPC_FP128TyID:
8207330f729Sjoerg         return EmitComplexBinOpLibCall("__divtc3", LibCallOp);
8217330f729Sjoerg       case llvm::Type::X86_FP80TyID:
8227330f729Sjoerg         return EmitComplexBinOpLibCall("__divxc3", LibCallOp);
8237330f729Sjoerg       case llvm::Type::FP128TyID:
8247330f729Sjoerg         return EmitComplexBinOpLibCall("__divtc3", LibCallOp);
8257330f729Sjoerg       }
8267330f729Sjoerg     } else if (RHSi) {
8277330f729Sjoerg       if (!LHSi)
8287330f729Sjoerg         LHSi = llvm::Constant::getNullValue(RHSi->getType());
8297330f729Sjoerg 
8307330f729Sjoerg       // (a+ib) / (c+id) = ((ac+bd)/(cc+dd)) + i((bc-ad)/(cc+dd))
8317330f729Sjoerg       llvm::Value *AC = Builder.CreateFMul(LHSr, RHSr); // a*c
8327330f729Sjoerg       llvm::Value *BD = Builder.CreateFMul(LHSi, RHSi); // b*d
8337330f729Sjoerg       llvm::Value *ACpBD = Builder.CreateFAdd(AC, BD); // ac+bd
8347330f729Sjoerg 
8357330f729Sjoerg       llvm::Value *CC = Builder.CreateFMul(RHSr, RHSr); // c*c
8367330f729Sjoerg       llvm::Value *DD = Builder.CreateFMul(RHSi, RHSi); // d*d
8377330f729Sjoerg       llvm::Value *CCpDD = Builder.CreateFAdd(CC, DD); // cc+dd
8387330f729Sjoerg 
8397330f729Sjoerg       llvm::Value *BC = Builder.CreateFMul(LHSi, RHSr); // b*c
8407330f729Sjoerg       llvm::Value *AD = Builder.CreateFMul(LHSr, RHSi); // a*d
8417330f729Sjoerg       llvm::Value *BCmAD = Builder.CreateFSub(BC, AD); // bc-ad
8427330f729Sjoerg 
8437330f729Sjoerg       DSTr = Builder.CreateFDiv(ACpBD, CCpDD);
8447330f729Sjoerg       DSTi = Builder.CreateFDiv(BCmAD, CCpDD);
8457330f729Sjoerg     } else {
8467330f729Sjoerg       assert(LHSi && "Can have at most one non-complex operand!");
8477330f729Sjoerg 
8487330f729Sjoerg       DSTr = Builder.CreateFDiv(LHSr, RHSr);
8497330f729Sjoerg       DSTi = Builder.CreateFDiv(LHSi, RHSr);
8507330f729Sjoerg     }
8517330f729Sjoerg   } else {
8527330f729Sjoerg     assert(Op.LHS.second && Op.RHS.second &&
8537330f729Sjoerg            "Both operands of integer complex operators must be complex!");
8547330f729Sjoerg     // (a+ib) / (c+id) = ((ac+bd)/(cc+dd)) + i((bc-ad)/(cc+dd))
8557330f729Sjoerg     llvm::Value *Tmp1 = Builder.CreateMul(LHSr, RHSr); // a*c
8567330f729Sjoerg     llvm::Value *Tmp2 = Builder.CreateMul(LHSi, RHSi); // b*d
8577330f729Sjoerg     llvm::Value *Tmp3 = Builder.CreateAdd(Tmp1, Tmp2); // ac+bd
8587330f729Sjoerg 
8597330f729Sjoerg     llvm::Value *Tmp4 = Builder.CreateMul(RHSr, RHSr); // c*c
8607330f729Sjoerg     llvm::Value *Tmp5 = Builder.CreateMul(RHSi, RHSi); // d*d
8617330f729Sjoerg     llvm::Value *Tmp6 = Builder.CreateAdd(Tmp4, Tmp5); // cc+dd
8627330f729Sjoerg 
8637330f729Sjoerg     llvm::Value *Tmp7 = Builder.CreateMul(LHSi, RHSr); // b*c
8647330f729Sjoerg     llvm::Value *Tmp8 = Builder.CreateMul(LHSr, RHSi); // a*d
8657330f729Sjoerg     llvm::Value *Tmp9 = Builder.CreateSub(Tmp7, Tmp8); // bc-ad
8667330f729Sjoerg 
8677330f729Sjoerg     if (Op.Ty->castAs<ComplexType>()->getElementType()->isUnsignedIntegerType()) {
8687330f729Sjoerg       DSTr = Builder.CreateUDiv(Tmp3, Tmp6);
8697330f729Sjoerg       DSTi = Builder.CreateUDiv(Tmp9, Tmp6);
8707330f729Sjoerg     } else {
8717330f729Sjoerg       DSTr = Builder.CreateSDiv(Tmp3, Tmp6);
8727330f729Sjoerg       DSTi = Builder.CreateSDiv(Tmp9, Tmp6);
8737330f729Sjoerg     }
8747330f729Sjoerg   }
8757330f729Sjoerg 
8767330f729Sjoerg   return ComplexPairTy(DSTr, DSTi);
8777330f729Sjoerg }
8787330f729Sjoerg 
8797330f729Sjoerg ComplexExprEmitter::BinOpInfo
EmitBinOps(const BinaryOperator * E)8807330f729Sjoerg ComplexExprEmitter::EmitBinOps(const BinaryOperator *E) {
8817330f729Sjoerg   TestAndClearIgnoreReal();
8827330f729Sjoerg   TestAndClearIgnoreImag();
8837330f729Sjoerg   BinOpInfo Ops;
8847330f729Sjoerg   if (E->getLHS()->getType()->isRealFloatingType())
8857330f729Sjoerg     Ops.LHS = ComplexPairTy(CGF.EmitScalarExpr(E->getLHS()), nullptr);
8867330f729Sjoerg   else
8877330f729Sjoerg     Ops.LHS = Visit(E->getLHS());
8887330f729Sjoerg   if (E->getRHS()->getType()->isRealFloatingType())
8897330f729Sjoerg     Ops.RHS = ComplexPairTy(CGF.EmitScalarExpr(E->getRHS()), nullptr);
8907330f729Sjoerg   else
8917330f729Sjoerg     Ops.RHS = Visit(E->getRHS());
8927330f729Sjoerg 
8937330f729Sjoerg   Ops.Ty = E->getType();
8947330f729Sjoerg   return Ops;
8957330f729Sjoerg }
8967330f729Sjoerg 
8977330f729Sjoerg 
8987330f729Sjoerg LValue ComplexExprEmitter::
EmitCompoundAssignLValue(const CompoundAssignOperator * E,ComplexPairTy (ComplexExprEmitter::* Func)(const BinOpInfo &),RValue & Val)8997330f729Sjoerg EmitCompoundAssignLValue(const CompoundAssignOperator *E,
9007330f729Sjoerg           ComplexPairTy (ComplexExprEmitter::*Func)(const BinOpInfo&),
9017330f729Sjoerg                          RValue &Val) {
9027330f729Sjoerg   TestAndClearIgnoreReal();
9037330f729Sjoerg   TestAndClearIgnoreImag();
9047330f729Sjoerg   QualType LHSTy = E->getLHS()->getType();
9057330f729Sjoerg   if (const AtomicType *AT = LHSTy->getAs<AtomicType>())
9067330f729Sjoerg     LHSTy = AT->getValueType();
9077330f729Sjoerg 
908*e038c9c4Sjoerg   CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, E);
9097330f729Sjoerg   BinOpInfo OpInfo;
9107330f729Sjoerg 
9117330f729Sjoerg   // Load the RHS and LHS operands.
9127330f729Sjoerg   // __block variables need to have the rhs evaluated first, plus this should
9137330f729Sjoerg   // improve codegen a little.
9147330f729Sjoerg   OpInfo.Ty = E->getComputationResultType();
9157330f729Sjoerg   QualType ComplexElementTy = cast<ComplexType>(OpInfo.Ty)->getElementType();
9167330f729Sjoerg 
9177330f729Sjoerg   // The RHS should have been converted to the computation type.
9187330f729Sjoerg   if (E->getRHS()->getType()->isRealFloatingType()) {
9197330f729Sjoerg     assert(
9207330f729Sjoerg         CGF.getContext()
9217330f729Sjoerg             .hasSameUnqualifiedType(ComplexElementTy, E->getRHS()->getType()));
9227330f729Sjoerg     OpInfo.RHS = ComplexPairTy(CGF.EmitScalarExpr(E->getRHS()), nullptr);
9237330f729Sjoerg   } else {
9247330f729Sjoerg     assert(CGF.getContext()
9257330f729Sjoerg                .hasSameUnqualifiedType(OpInfo.Ty, E->getRHS()->getType()));
9267330f729Sjoerg     OpInfo.RHS = Visit(E->getRHS());
9277330f729Sjoerg   }
9287330f729Sjoerg 
9297330f729Sjoerg   LValue LHS = CGF.EmitLValue(E->getLHS());
9307330f729Sjoerg 
9317330f729Sjoerg   // Load from the l-value and convert it.
9327330f729Sjoerg   SourceLocation Loc = E->getExprLoc();
9337330f729Sjoerg   if (LHSTy->isAnyComplexType()) {
9347330f729Sjoerg     ComplexPairTy LHSVal = EmitLoadOfLValue(LHS, Loc);
9357330f729Sjoerg     OpInfo.LHS = EmitComplexToComplexCast(LHSVal, LHSTy, OpInfo.Ty, Loc);
9367330f729Sjoerg   } else {
9377330f729Sjoerg     llvm::Value *LHSVal = CGF.EmitLoadOfScalar(LHS, Loc);
9387330f729Sjoerg     // For floating point real operands we can directly pass the scalar form
9397330f729Sjoerg     // to the binary operator emission and potentially get more efficient code.
9407330f729Sjoerg     if (LHSTy->isRealFloatingType()) {
9417330f729Sjoerg       if (!CGF.getContext().hasSameUnqualifiedType(ComplexElementTy, LHSTy))
9427330f729Sjoerg         LHSVal = CGF.EmitScalarConversion(LHSVal, LHSTy, ComplexElementTy, Loc);
9437330f729Sjoerg       OpInfo.LHS = ComplexPairTy(LHSVal, nullptr);
9447330f729Sjoerg     } else {
9457330f729Sjoerg       OpInfo.LHS = EmitScalarToComplexCast(LHSVal, LHSTy, OpInfo.Ty, Loc);
9467330f729Sjoerg     }
9477330f729Sjoerg   }
9487330f729Sjoerg 
9497330f729Sjoerg   // Expand the binary operator.
9507330f729Sjoerg   ComplexPairTy Result = (this->*Func)(OpInfo);
9517330f729Sjoerg 
9527330f729Sjoerg   // Truncate the result and store it into the LHS lvalue.
9537330f729Sjoerg   if (LHSTy->isAnyComplexType()) {
9547330f729Sjoerg     ComplexPairTy ResVal =
9557330f729Sjoerg         EmitComplexToComplexCast(Result, OpInfo.Ty, LHSTy, Loc);
9567330f729Sjoerg     EmitStoreOfComplex(ResVal, LHS, /*isInit*/ false);
9577330f729Sjoerg     Val = RValue::getComplex(ResVal);
9587330f729Sjoerg   } else {
9597330f729Sjoerg     llvm::Value *ResVal =
9607330f729Sjoerg         CGF.EmitComplexToScalarConversion(Result, OpInfo.Ty, LHSTy, Loc);
9617330f729Sjoerg     CGF.EmitStoreOfScalar(ResVal, LHS, /*isInit*/ false);
9627330f729Sjoerg     Val = RValue::get(ResVal);
9637330f729Sjoerg   }
9647330f729Sjoerg 
9657330f729Sjoerg   return LHS;
9667330f729Sjoerg }
9677330f729Sjoerg 
9687330f729Sjoerg // Compound assignments.
9697330f729Sjoerg ComplexPairTy ComplexExprEmitter::
EmitCompoundAssign(const CompoundAssignOperator * E,ComplexPairTy (ComplexExprEmitter::* Func)(const BinOpInfo &))9707330f729Sjoerg EmitCompoundAssign(const CompoundAssignOperator *E,
9717330f729Sjoerg                    ComplexPairTy (ComplexExprEmitter::*Func)(const BinOpInfo&)){
9727330f729Sjoerg   RValue Val;
9737330f729Sjoerg   LValue LV = EmitCompoundAssignLValue(E, Func, Val);
9747330f729Sjoerg 
9757330f729Sjoerg   // The result of an assignment in C is the assigned r-value.
9767330f729Sjoerg   if (!CGF.getLangOpts().CPlusPlus)
9777330f729Sjoerg     return Val.getComplexVal();
9787330f729Sjoerg 
9797330f729Sjoerg   // If the lvalue is non-volatile, return the computed value of the assignment.
9807330f729Sjoerg   if (!LV.isVolatileQualified())
9817330f729Sjoerg     return Val.getComplexVal();
9827330f729Sjoerg 
9837330f729Sjoerg   return EmitLoadOfLValue(LV, E->getExprLoc());
9847330f729Sjoerg }
9857330f729Sjoerg 
EmitBinAssignLValue(const BinaryOperator * E,ComplexPairTy & Val)9867330f729Sjoerg LValue ComplexExprEmitter::EmitBinAssignLValue(const BinaryOperator *E,
9877330f729Sjoerg                                                ComplexPairTy &Val) {
9887330f729Sjoerg   assert(CGF.getContext().hasSameUnqualifiedType(E->getLHS()->getType(),
9897330f729Sjoerg                                                  E->getRHS()->getType()) &&
9907330f729Sjoerg          "Invalid assignment");
9917330f729Sjoerg   TestAndClearIgnoreReal();
9927330f729Sjoerg   TestAndClearIgnoreImag();
9937330f729Sjoerg 
9947330f729Sjoerg   // Emit the RHS.  __block variables need the RHS evaluated first.
9957330f729Sjoerg   Val = Visit(E->getRHS());
9967330f729Sjoerg 
9977330f729Sjoerg   // Compute the address to store into.
9987330f729Sjoerg   LValue LHS = CGF.EmitLValue(E->getLHS());
9997330f729Sjoerg 
10007330f729Sjoerg   // Store the result value into the LHS lvalue.
10017330f729Sjoerg   EmitStoreOfComplex(Val, LHS, /*isInit*/ false);
10027330f729Sjoerg 
10037330f729Sjoerg   return LHS;
10047330f729Sjoerg }
10057330f729Sjoerg 
VisitBinAssign(const BinaryOperator * E)10067330f729Sjoerg ComplexPairTy ComplexExprEmitter::VisitBinAssign(const BinaryOperator *E) {
10077330f729Sjoerg   ComplexPairTy Val;
10087330f729Sjoerg   LValue LV = EmitBinAssignLValue(E, Val);
10097330f729Sjoerg 
10107330f729Sjoerg   // The result of an assignment in C is the assigned r-value.
10117330f729Sjoerg   if (!CGF.getLangOpts().CPlusPlus)
10127330f729Sjoerg     return Val;
10137330f729Sjoerg 
10147330f729Sjoerg   // If the lvalue is non-volatile, return the computed value of the assignment.
10157330f729Sjoerg   if (!LV.isVolatileQualified())
10167330f729Sjoerg     return Val;
10177330f729Sjoerg 
10187330f729Sjoerg   return EmitLoadOfLValue(LV, E->getExprLoc());
10197330f729Sjoerg }
10207330f729Sjoerg 
VisitBinComma(const BinaryOperator * E)10217330f729Sjoerg ComplexPairTy ComplexExprEmitter::VisitBinComma(const BinaryOperator *E) {
10227330f729Sjoerg   CGF.EmitIgnoredExpr(E->getLHS());
10237330f729Sjoerg   return Visit(E->getRHS());
10247330f729Sjoerg }
10257330f729Sjoerg 
10267330f729Sjoerg ComplexPairTy ComplexExprEmitter::
VisitAbstractConditionalOperator(const AbstractConditionalOperator * E)10277330f729Sjoerg VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
10287330f729Sjoerg   TestAndClearIgnoreReal();
10297330f729Sjoerg   TestAndClearIgnoreImag();
10307330f729Sjoerg   llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
10317330f729Sjoerg   llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
10327330f729Sjoerg   llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
10337330f729Sjoerg 
10347330f729Sjoerg   // Bind the common expression if necessary.
10357330f729Sjoerg   CodeGenFunction::OpaqueValueMapping binding(CGF, E);
10367330f729Sjoerg 
10377330f729Sjoerg 
10387330f729Sjoerg   CodeGenFunction::ConditionalEvaluation eval(CGF);
10397330f729Sjoerg   CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock,
10407330f729Sjoerg                            CGF.getProfileCount(E));
10417330f729Sjoerg 
10427330f729Sjoerg   eval.begin(CGF);
10437330f729Sjoerg   CGF.EmitBlock(LHSBlock);
10447330f729Sjoerg   CGF.incrementProfileCounter(E);
10457330f729Sjoerg   ComplexPairTy LHS = Visit(E->getTrueExpr());
10467330f729Sjoerg   LHSBlock = Builder.GetInsertBlock();
10477330f729Sjoerg   CGF.EmitBranch(ContBlock);
10487330f729Sjoerg   eval.end(CGF);
10497330f729Sjoerg 
10507330f729Sjoerg   eval.begin(CGF);
10517330f729Sjoerg   CGF.EmitBlock(RHSBlock);
10527330f729Sjoerg   ComplexPairTy RHS = Visit(E->getFalseExpr());
10537330f729Sjoerg   RHSBlock = Builder.GetInsertBlock();
10547330f729Sjoerg   CGF.EmitBlock(ContBlock);
10557330f729Sjoerg   eval.end(CGF);
10567330f729Sjoerg 
10577330f729Sjoerg   // Create a PHI node for the real part.
10587330f729Sjoerg   llvm::PHINode *RealPN = Builder.CreatePHI(LHS.first->getType(), 2, "cond.r");
10597330f729Sjoerg   RealPN->addIncoming(LHS.first, LHSBlock);
10607330f729Sjoerg   RealPN->addIncoming(RHS.first, RHSBlock);
10617330f729Sjoerg 
10627330f729Sjoerg   // Create a PHI node for the imaginary part.
10637330f729Sjoerg   llvm::PHINode *ImagPN = Builder.CreatePHI(LHS.first->getType(), 2, "cond.i");
10647330f729Sjoerg   ImagPN->addIncoming(LHS.second, LHSBlock);
10657330f729Sjoerg   ImagPN->addIncoming(RHS.second, RHSBlock);
10667330f729Sjoerg 
10677330f729Sjoerg   return ComplexPairTy(RealPN, ImagPN);
10687330f729Sjoerg }
10697330f729Sjoerg 
VisitChooseExpr(ChooseExpr * E)10707330f729Sjoerg ComplexPairTy ComplexExprEmitter::VisitChooseExpr(ChooseExpr *E) {
10717330f729Sjoerg   return Visit(E->getChosenSubExpr());
10727330f729Sjoerg }
10737330f729Sjoerg 
VisitInitListExpr(InitListExpr * E)10747330f729Sjoerg ComplexPairTy ComplexExprEmitter::VisitInitListExpr(InitListExpr *E) {
10757330f729Sjoerg     bool Ignore = TestAndClearIgnoreReal();
10767330f729Sjoerg     (void)Ignore;
10777330f729Sjoerg     assert (Ignore == false && "init list ignored");
10787330f729Sjoerg     Ignore = TestAndClearIgnoreImag();
10797330f729Sjoerg     (void)Ignore;
10807330f729Sjoerg     assert (Ignore == false && "init list ignored");
10817330f729Sjoerg 
10827330f729Sjoerg   if (E->getNumInits() == 2) {
10837330f729Sjoerg     llvm::Value *Real = CGF.EmitScalarExpr(E->getInit(0));
10847330f729Sjoerg     llvm::Value *Imag = CGF.EmitScalarExpr(E->getInit(1));
10857330f729Sjoerg     return ComplexPairTy(Real, Imag);
10867330f729Sjoerg   } else if (E->getNumInits() == 1) {
10877330f729Sjoerg     return Visit(E->getInit(0));
10887330f729Sjoerg   }
10897330f729Sjoerg 
10907330f729Sjoerg   // Empty init list initializes to null
10917330f729Sjoerg   assert(E->getNumInits() == 0 && "Unexpected number of inits");
10927330f729Sjoerg   QualType Ty = E->getType()->castAs<ComplexType>()->getElementType();
10937330f729Sjoerg   llvm::Type* LTy = CGF.ConvertType(Ty);
10947330f729Sjoerg   llvm::Value* zeroConstant = llvm::Constant::getNullValue(LTy);
10957330f729Sjoerg   return ComplexPairTy(zeroConstant, zeroConstant);
10967330f729Sjoerg }
10977330f729Sjoerg 
VisitVAArgExpr(VAArgExpr * E)10987330f729Sjoerg ComplexPairTy ComplexExprEmitter::VisitVAArgExpr(VAArgExpr *E) {
10997330f729Sjoerg   Address ArgValue = Address::invalid();
11007330f729Sjoerg   Address ArgPtr = CGF.EmitVAArg(E, ArgValue);
11017330f729Sjoerg 
11027330f729Sjoerg   if (!ArgPtr.isValid()) {
11037330f729Sjoerg     CGF.ErrorUnsupported(E, "complex va_arg expression");
11047330f729Sjoerg     llvm::Type *EltTy =
11057330f729Sjoerg       CGF.ConvertType(E->getType()->castAs<ComplexType>()->getElementType());
11067330f729Sjoerg     llvm::Value *U = llvm::UndefValue::get(EltTy);
11077330f729Sjoerg     return ComplexPairTy(U, U);
11087330f729Sjoerg   }
11097330f729Sjoerg 
11107330f729Sjoerg   return EmitLoadOfLValue(CGF.MakeAddrLValue(ArgPtr, E->getType()),
11117330f729Sjoerg                           E->getExprLoc());
11127330f729Sjoerg }
11137330f729Sjoerg 
11147330f729Sjoerg //===----------------------------------------------------------------------===//
11157330f729Sjoerg //                         Entry Point into this File
11167330f729Sjoerg //===----------------------------------------------------------------------===//
11177330f729Sjoerg 
11187330f729Sjoerg /// EmitComplexExpr - Emit the computation of the specified expression of
11197330f729Sjoerg /// complex type, ignoring the result.
EmitComplexExpr(const Expr * E,bool IgnoreReal,bool IgnoreImag)11207330f729Sjoerg ComplexPairTy CodeGenFunction::EmitComplexExpr(const Expr *E, bool IgnoreReal,
11217330f729Sjoerg                                                bool IgnoreImag) {
11227330f729Sjoerg   assert(E && getComplexType(E->getType()) &&
11237330f729Sjoerg          "Invalid complex expression to emit");
11247330f729Sjoerg 
11257330f729Sjoerg   return ComplexExprEmitter(*this, IgnoreReal, IgnoreImag)
11267330f729Sjoerg       .Visit(const_cast<Expr *>(E));
11277330f729Sjoerg }
11287330f729Sjoerg 
EmitComplexExprIntoLValue(const Expr * E,LValue dest,bool isInit)11297330f729Sjoerg void CodeGenFunction::EmitComplexExprIntoLValue(const Expr *E, LValue dest,
11307330f729Sjoerg                                                 bool isInit) {
11317330f729Sjoerg   assert(E && getComplexType(E->getType()) &&
11327330f729Sjoerg          "Invalid complex expression to emit");
11337330f729Sjoerg   ComplexExprEmitter Emitter(*this);
11347330f729Sjoerg   ComplexPairTy Val = Emitter.Visit(const_cast<Expr*>(E));
11357330f729Sjoerg   Emitter.EmitStoreOfComplex(Val, dest, isInit);
11367330f729Sjoerg }
11377330f729Sjoerg 
11387330f729Sjoerg /// EmitStoreOfComplex - Store a complex number into the specified l-value.
EmitStoreOfComplex(ComplexPairTy V,LValue dest,bool isInit)11397330f729Sjoerg void CodeGenFunction::EmitStoreOfComplex(ComplexPairTy V, LValue dest,
11407330f729Sjoerg                                          bool isInit) {
11417330f729Sjoerg   ComplexExprEmitter(*this).EmitStoreOfComplex(V, dest, isInit);
11427330f729Sjoerg }
11437330f729Sjoerg 
11447330f729Sjoerg /// EmitLoadOfComplex - Load a complex number from the specified address.
EmitLoadOfComplex(LValue src,SourceLocation loc)11457330f729Sjoerg ComplexPairTy CodeGenFunction::EmitLoadOfComplex(LValue src,
11467330f729Sjoerg                                                  SourceLocation loc) {
11477330f729Sjoerg   return ComplexExprEmitter(*this).EmitLoadOfLValue(src, loc);
11487330f729Sjoerg }
11497330f729Sjoerg 
EmitComplexAssignmentLValue(const BinaryOperator * E)11507330f729Sjoerg LValue CodeGenFunction::EmitComplexAssignmentLValue(const BinaryOperator *E) {
11517330f729Sjoerg   assert(E->getOpcode() == BO_Assign);
11527330f729Sjoerg   ComplexPairTy Val; // ignored
1153*e038c9c4Sjoerg   LValue LVal = ComplexExprEmitter(*this).EmitBinAssignLValue(E, Val);
1154*e038c9c4Sjoerg   if (getLangOpts().OpenMP)
1155*e038c9c4Sjoerg     CGM.getOpenMPRuntime().checkAndEmitLastprivateConditional(*this,
1156*e038c9c4Sjoerg                                                               E->getLHS());
1157*e038c9c4Sjoerg   return LVal;
11587330f729Sjoerg }
11597330f729Sjoerg 
11607330f729Sjoerg typedef ComplexPairTy (ComplexExprEmitter::*CompoundFunc)(
11617330f729Sjoerg     const ComplexExprEmitter::BinOpInfo &);
11627330f729Sjoerg 
getComplexOp(BinaryOperatorKind Op)11637330f729Sjoerg static CompoundFunc getComplexOp(BinaryOperatorKind Op) {
11647330f729Sjoerg   switch (Op) {
11657330f729Sjoerg   case BO_MulAssign: return &ComplexExprEmitter::EmitBinMul;
11667330f729Sjoerg   case BO_DivAssign: return &ComplexExprEmitter::EmitBinDiv;
11677330f729Sjoerg   case BO_SubAssign: return &ComplexExprEmitter::EmitBinSub;
11687330f729Sjoerg   case BO_AddAssign: return &ComplexExprEmitter::EmitBinAdd;
11697330f729Sjoerg   default:
11707330f729Sjoerg     llvm_unreachable("unexpected complex compound assignment");
11717330f729Sjoerg   }
11727330f729Sjoerg }
11737330f729Sjoerg 
11747330f729Sjoerg LValue CodeGenFunction::
EmitComplexCompoundAssignmentLValue(const CompoundAssignOperator * E)11757330f729Sjoerg EmitComplexCompoundAssignmentLValue(const CompoundAssignOperator *E) {
11767330f729Sjoerg   CompoundFunc Op = getComplexOp(E->getOpcode());
11777330f729Sjoerg   RValue Val;
11787330f729Sjoerg   return ComplexExprEmitter(*this).EmitCompoundAssignLValue(E, Op, Val);
11797330f729Sjoerg }
11807330f729Sjoerg 
11817330f729Sjoerg LValue CodeGenFunction::
EmitScalarCompoundAssignWithComplex(const CompoundAssignOperator * E,llvm::Value * & Result)11827330f729Sjoerg EmitScalarCompoundAssignWithComplex(const CompoundAssignOperator *E,
11837330f729Sjoerg                                     llvm::Value *&Result) {
11847330f729Sjoerg   CompoundFunc Op = getComplexOp(E->getOpcode());
11857330f729Sjoerg   RValue Val;
11867330f729Sjoerg   LValue Ret = ComplexExprEmitter(*this).EmitCompoundAssignLValue(E, Op, Val);
11877330f729Sjoerg   Result = Val.getScalarVal();
11887330f729Sjoerg   return Ret;
11897330f729Sjoerg }
1190