17a51313dSChris Lattner //===--- CGExprComplex.cpp - Emit LLVM Code for Complex Exprs -------------===//
27a51313dSChris Lattner //
37a51313dSChris Lattner //                     The LLVM Compiler Infrastructure
47a51313dSChris Lattner //
57a51313dSChris Lattner // This file is distributed under the University of Illinois Open Source
67a51313dSChris Lattner // License. See LICENSE.TXT for details.
77a51313dSChris Lattner //
87a51313dSChris Lattner //===----------------------------------------------------------------------===//
97a51313dSChris Lattner //
107a51313dSChris Lattner // This contains code to emit Expr nodes with complex types as LLVM code.
117a51313dSChris Lattner //
127a51313dSChris Lattner //===----------------------------------------------------------------------===//
137a51313dSChris Lattner 
147a51313dSChris Lattner #include "CodeGenFunction.h"
157a51313dSChris Lattner #include "CodeGenModule.h"
16ad319a73SDaniel Dunbar #include "clang/AST/ASTContext.h"
17ad319a73SDaniel Dunbar #include "clang/AST/StmtVisitor.h"
180c4b230bSChandler Carruth #include "llvm/ADT/STLExtras.h"
193a02247dSChandler Carruth #include "llvm/ADT/SmallString.h"
20ffd5551bSChandler Carruth #include "llvm/IR/Constants.h"
21ffd5551bSChandler Carruth #include "llvm/IR/Function.h"
220c4b230bSChandler Carruth #include "llvm/IR/Instructions.h"
230c4b230bSChandler Carruth #include "llvm/IR/MDBuilder.h"
240c4b230bSChandler Carruth #include "llvm/IR/Metadata.h"
2527dcbb24SJF Bastien #include <algorithm>
267a51313dSChris Lattner using namespace clang;
277a51313dSChris Lattner using namespace CodeGen;
287a51313dSChris Lattner 
297a51313dSChris Lattner //===----------------------------------------------------------------------===//
307a51313dSChris Lattner //                        Complex Expression Emitter
317a51313dSChris Lattner //===----------------------------------------------------------------------===//
327a51313dSChris Lattner 
337a51313dSChris Lattner typedef CodeGenFunction::ComplexPairTy ComplexPairTy;
347a51313dSChris Lattner 
3547fb9508SJohn McCall /// Return the complex type that we are meant to emit.
3647fb9508SJohn McCall static const ComplexType *getComplexType(QualType type) {
3747fb9508SJohn McCall   type = type.getCanonicalType();
3847fb9508SJohn McCall   if (const ComplexType *comp = dyn_cast<ComplexType>(type)) {
3947fb9508SJohn McCall     return comp;
4047fb9508SJohn McCall   } else {
4147fb9508SJohn McCall     return cast<ComplexType>(cast<AtomicType>(type)->getValueType());
4247fb9508SJohn McCall   }
4347fb9508SJohn McCall }
4447fb9508SJohn McCall 
457a51313dSChris Lattner namespace  {
46337e3a5fSBenjamin Kramer class ComplexExprEmitter
477a51313dSChris Lattner   : public StmtVisitor<ComplexExprEmitter, ComplexPairTy> {
487a51313dSChris Lattner   CodeGenFunction &CGF;
49cb463859SDaniel Dunbar   CGBuilderTy &Builder;
50df0fe27bSMike Stump   bool IgnoreReal;
51df0fe27bSMike Stump   bool IgnoreImag;
527a51313dSChris Lattner public:
5307bb1966SJohn McCall   ComplexExprEmitter(CodeGenFunction &cgf, bool ir=false, bool ii=false)
5407bb1966SJohn McCall     : CGF(cgf), Builder(CGF.Builder), IgnoreReal(ir), IgnoreImag(ii) {
557a51313dSChris Lattner   }
567a51313dSChris Lattner 
577a51313dSChris Lattner 
587a51313dSChris Lattner   //===--------------------------------------------------------------------===//
597a51313dSChris Lattner   //                               Utilities
607a51313dSChris Lattner   //===--------------------------------------------------------------------===//
617a51313dSChris Lattner 
62df0fe27bSMike Stump   bool TestAndClearIgnoreReal() {
63df0fe27bSMike Stump     bool I = IgnoreReal;
64df0fe27bSMike Stump     IgnoreReal = false;
65df0fe27bSMike Stump     return I;
66df0fe27bSMike Stump   }
67df0fe27bSMike Stump   bool TestAndClearIgnoreImag() {
68df0fe27bSMike Stump     bool I = IgnoreImag;
69df0fe27bSMike Stump     IgnoreImag = false;
70df0fe27bSMike Stump     return I;
71df0fe27bSMike Stump   }
72df0fe27bSMike Stump 
737a51313dSChris Lattner   /// EmitLoadOfLValue - Given an expression with complex type that represents a
747a51313dSChris Lattner   /// value l-value, this method emits the address of the l-value, then loads
757a51313dSChris Lattner   /// and returns the result.
767a51313dSChris Lattner   ComplexPairTy EmitLoadOfLValue(const Expr *E) {
772d84e842SNick Lewycky     return EmitLoadOfLValue(CGF.EmitLValue(E), E->getExprLoc());
78e26a872bSJohn McCall   }
79e26a872bSJohn McCall 
802d84e842SNick Lewycky   ComplexPairTy EmitLoadOfLValue(LValue LV, SourceLocation Loc);
81e26a872bSJohn McCall 
827a51313dSChris Lattner   /// EmitStoreOfComplex - Store the specified real/imag parts into the
837a51313dSChris Lattner   /// specified value pointer.
8466e4197fSDavid Blaikie   void EmitStoreOfComplex(ComplexPairTy Val, LValue LV, bool isInit);
857a51313dSChris Lattner 
86*650d7f7dSFilipe Cabecinhas   /// Emit a cast from complex value Val to DestType.
877a51313dSChris Lattner   ComplexPairTy EmitComplexToComplexCast(ComplexPairTy Val, QualType SrcType,
887a51313dSChris Lattner                                          QualType DestType);
89*650d7f7dSFilipe Cabecinhas   /// Emit a cast from scalar value Val to DestType.
90f045007fSEli Friedman   ComplexPairTy EmitScalarToComplexCast(llvm::Value *Val, QualType SrcType,
91f045007fSEli Friedman                                         QualType DestType);
927a51313dSChris Lattner 
937a51313dSChris Lattner   //===--------------------------------------------------------------------===//
947a51313dSChris Lattner   //                            Visitor Methods
957a51313dSChris Lattner   //===--------------------------------------------------------------------===//
967a51313dSChris Lattner 
978162d4adSFariborz Jahanian   ComplexPairTy Visit(Expr *E) {
989b479666SDavid Blaikie     ApplyDebugLocation DL(CGF, E);
998162d4adSFariborz Jahanian     return StmtVisitor<ComplexExprEmitter, ComplexPairTy>::Visit(E);
1008162d4adSFariborz Jahanian   }
1018162d4adSFariborz Jahanian 
1027a51313dSChris Lattner   ComplexPairTy VisitStmt(Stmt *S) {
1037a51313dSChris Lattner     S->dump(CGF.getContext().getSourceManager());
10483d382b1SDavid Blaikie     llvm_unreachable("Stmt can't have complex result type!");
1057a51313dSChris Lattner   }
1067a51313dSChris Lattner   ComplexPairTy VisitExpr(Expr *S);
1077a51313dSChris Lattner   ComplexPairTy VisitParenExpr(ParenExpr *PE) { return Visit(PE->getSubExpr());}
10891147596SPeter Collingbourne   ComplexPairTy VisitGenericSelectionExpr(GenericSelectionExpr *GE) {
10991147596SPeter Collingbourne     return Visit(GE->getResultExpr());
11091147596SPeter Collingbourne   }
1117a51313dSChris Lattner   ComplexPairTy VisitImaginaryLiteral(const ImaginaryLiteral *IL);
1127c454bb8SJohn McCall   ComplexPairTy
1137c454bb8SJohn McCall   VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *PE) {
1147c454bb8SJohn McCall     return Visit(PE->getReplacement());
1157c454bb8SJohn McCall   }
1167a51313dSChris Lattner 
1177a51313dSChris Lattner   // l-values.
118113bee05SJohn McCall   ComplexPairTy VisitDeclRefExpr(DeclRefExpr *E) {
119113bee05SJohn McCall     if (CodeGenFunction::ConstantEmission result = CGF.tryEmitAsConstant(E)) {
12071335059SJohn McCall       if (result.isReference())
1212d84e842SNick Lewycky         return EmitLoadOfLValue(result.getReferenceLValue(CGF, E),
1222d84e842SNick Lewycky                                 E->getExprLoc());
12371335059SJohn McCall 
12464f23918SEli Friedman       llvm::Constant *pair = result.getValue();
12564f23918SEli Friedman       return ComplexPairTy(pair->getAggregateElement(0U),
12664f23918SEli Friedman                            pair->getAggregateElement(1U));
12771335059SJohn McCall     }
128113bee05SJohn McCall     return EmitLoadOfLValue(E);
12971335059SJohn McCall   }
13076d864c7SDaniel Dunbar   ComplexPairTy VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
13176d864c7SDaniel Dunbar     return EmitLoadOfLValue(E);
13276d864c7SDaniel Dunbar   }
13376d864c7SDaniel Dunbar   ComplexPairTy VisitObjCMessageExpr(ObjCMessageExpr *E) {
13476d864c7SDaniel Dunbar     return CGF.EmitObjCMessageExpr(E).getComplexVal();
13576d864c7SDaniel Dunbar   }
1367a51313dSChris Lattner   ComplexPairTy VisitArraySubscriptExpr(Expr *E) { return EmitLoadOfLValue(E); }
1377a51313dSChris Lattner   ComplexPairTy VisitMemberExpr(const Expr *E) { return EmitLoadOfLValue(E); }
1381bf5846aSJohn McCall   ComplexPairTy VisitOpaqueValueExpr(OpaqueValueExpr *E) {
139c07a0c7eSJohn McCall     if (E->isGLValue())
1402d84e842SNick Lewycky       return EmitLoadOfLValue(CGF.getOpaqueLValueMapping(E), E->getExprLoc());
141c07a0c7eSJohn McCall     return CGF.getOpaqueRValueMapping(E).getComplexVal();
1421bf5846aSJohn McCall   }
1437a51313dSChris Lattner 
144fe96e0b6SJohn McCall   ComplexPairTy VisitPseudoObjectExpr(PseudoObjectExpr *E) {
145fe96e0b6SJohn McCall     return CGF.EmitPseudoObjectRValue(E).getComplexVal();
146fe96e0b6SJohn McCall   }
147fe96e0b6SJohn McCall 
1487a51313dSChris Lattner   // FIXME: CompoundLiteralExpr
1497a51313dSChris Lattner 
1501a07fd18SCraig Topper   ComplexPairTy EmitCast(CastKind CK, Expr *Op, QualType DestTy);
1517a51313dSChris Lattner   ComplexPairTy VisitImplicitCastExpr(ImplicitCastExpr *E) {
1527a51313dSChris Lattner     // Unlike for scalars, we don't have to worry about function->ptr demotion
1537a51313dSChris Lattner     // here.
154c357f412SDouglas Gregor     return EmitCast(E->getCastKind(), E->getSubExpr(), E->getType());
1557a51313dSChris Lattner   }
1567a51313dSChris Lattner   ComplexPairTy VisitCastExpr(CastExpr *E) {
157c357f412SDouglas Gregor     return EmitCast(E->getCastKind(), E->getSubExpr(), E->getType());
1587a51313dSChris Lattner   }
1597a51313dSChris Lattner   ComplexPairTy VisitCallExpr(const CallExpr *E);
1607a51313dSChris Lattner   ComplexPairTy VisitStmtExpr(const StmtExpr *E);
1617a51313dSChris Lattner 
1627a51313dSChris Lattner   // Operators.
1637a51313dSChris Lattner   ComplexPairTy VisitPrePostIncDec(const UnaryOperator *E,
164116ce8f1SChris Lattner                                    bool isInc, bool isPre) {
165116ce8f1SChris Lattner     LValue LV = CGF.EmitLValue(E->getSubExpr());
166116ce8f1SChris Lattner     return CGF.EmitComplexPrePostIncDec(E, LV, isInc, isPre);
167116ce8f1SChris Lattner   }
1687a51313dSChris Lattner   ComplexPairTy VisitUnaryPostDec(const UnaryOperator *E) {
1697a51313dSChris Lattner     return VisitPrePostIncDec(E, false, false);
1707a51313dSChris Lattner   }
1717a51313dSChris Lattner   ComplexPairTy VisitUnaryPostInc(const UnaryOperator *E) {
1727a51313dSChris Lattner     return VisitPrePostIncDec(E, true, false);
1737a51313dSChris Lattner   }
1747a51313dSChris Lattner   ComplexPairTy VisitUnaryPreDec(const UnaryOperator *E) {
1757a51313dSChris Lattner     return VisitPrePostIncDec(E, false, true);
1767a51313dSChris Lattner   }
1777a51313dSChris Lattner   ComplexPairTy VisitUnaryPreInc(const UnaryOperator *E) {
1787a51313dSChris Lattner     return VisitPrePostIncDec(E, true, true);
1797a51313dSChris Lattner   }
1807a51313dSChris Lattner   ComplexPairTy VisitUnaryDeref(const Expr *E) { return EmitLoadOfLValue(E); }
1817a51313dSChris Lattner   ComplexPairTy VisitUnaryPlus     (const UnaryOperator *E) {
182df0fe27bSMike Stump     TestAndClearIgnoreReal();
183df0fe27bSMike Stump     TestAndClearIgnoreImag();
1847a51313dSChris Lattner     return Visit(E->getSubExpr());
1857a51313dSChris Lattner   }
1867a51313dSChris Lattner   ComplexPairTy VisitUnaryMinus    (const UnaryOperator *E);
1877a51313dSChris Lattner   ComplexPairTy VisitUnaryNot      (const UnaryOperator *E);
1886f28289aSSebastian Redl   // LNot,Real,Imag never return complex.
1897a51313dSChris Lattner   ComplexPairTy VisitUnaryExtension(const UnaryOperator *E) {
1907a51313dSChris Lattner     return Visit(E->getSubExpr());
1917a51313dSChris Lattner   }
192aa9c7aedSChris Lattner   ComplexPairTy VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
193aa9c7aedSChris Lattner     return Visit(DAE->getExpr());
194aa9c7aedSChris Lattner   }
195852c9db7SRichard Smith   ComplexPairTy VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) {
196852c9db7SRichard Smith     CodeGenFunction::CXXDefaultInitExprScope Scope(CGF);
197852c9db7SRichard Smith     return Visit(DIE->getExpr());
198852c9db7SRichard Smith   }
1995d413781SJohn McCall   ComplexPairTy VisitExprWithCleanups(ExprWithCleanups *E) {
20008ef4660SJohn McCall     CGF.enterFullExpression(E);
20108ef4660SJohn McCall     CodeGenFunction::RunCleanupsScope Scope(CGF);
20208ef4660SJohn McCall     return Visit(E->getSubExpr());
203c0092ad3SAnders Carlsson   }
204747eb784SDouglas Gregor   ComplexPairTy VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
205ce4528f0SArgyrios Kyrtzidis     assert(E->getType()->isAnyComplexType() && "Expected complex type!");
20647fb9508SJohn McCall     QualType Elem = E->getType()->castAs<ComplexType>()->getElementType();
2074a3999feSMike Stump     llvm::Constant *Null = llvm::Constant::getNullValue(CGF.ConvertType(Elem));
208ce4528f0SArgyrios Kyrtzidis     return ComplexPairTy(Null, Null);
209ce4528f0SArgyrios Kyrtzidis   }
2100202cb40SDouglas Gregor   ComplexPairTy VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
2110202cb40SDouglas Gregor     assert(E->getType()->isAnyComplexType() && "Expected complex type!");
21247fb9508SJohn McCall     QualType Elem = E->getType()->castAs<ComplexType>()->getElementType();
213ae86c19eSOwen Anderson     llvm::Constant *Null =
2140b75f23bSOwen Anderson                        llvm::Constant::getNullValue(CGF.ConvertType(Elem));
2150202cb40SDouglas Gregor     return ComplexPairTy(Null, Null);
2160202cb40SDouglas Gregor   }
2177a51313dSChris Lattner 
2187a51313dSChris Lattner   struct BinOpInfo {
2197a51313dSChris Lattner     ComplexPairTy LHS;
2207a51313dSChris Lattner     ComplexPairTy RHS;
2217a51313dSChris Lattner     QualType Ty;  // Computation Type.
2227a51313dSChris Lattner   };
2237a51313dSChris Lattner 
2247a51313dSChris Lattner   BinOpInfo EmitBinOps(const BinaryOperator *E);
22507bb1966SJohn McCall   LValue EmitCompoundAssignLValue(const CompoundAssignOperator *E,
22607bb1966SJohn McCall                                   ComplexPairTy (ComplexExprEmitter::*Func)
22707bb1966SJohn McCall                                   (const BinOpInfo &),
228f045007fSEli Friedman                                   RValue &Val);
2297a51313dSChris Lattner   ComplexPairTy EmitCompoundAssign(const CompoundAssignOperator *E,
2307a51313dSChris Lattner                                    ComplexPairTy (ComplexExprEmitter::*Func)
2317a51313dSChris Lattner                                    (const BinOpInfo &));
2327a51313dSChris Lattner 
2337a51313dSChris Lattner   ComplexPairTy EmitBinAdd(const BinOpInfo &Op);
2347a51313dSChris Lattner   ComplexPairTy EmitBinSub(const BinOpInfo &Op);
2357a51313dSChris Lattner   ComplexPairTy EmitBinMul(const BinOpInfo &Op);
2367a51313dSChris Lattner   ComplexPairTy EmitBinDiv(const BinOpInfo &Op);
2377a51313dSChris Lattner 
238a216cad0SChandler Carruth   ComplexPairTy EmitComplexBinOpLibCall(StringRef LibCallName,
239a216cad0SChandler Carruth                                         const BinOpInfo &Op);
240a216cad0SChandler Carruth 
2417a51313dSChris Lattner   ComplexPairTy VisitBinAdd(const BinaryOperator *E) {
2427a51313dSChris Lattner     return EmitBinAdd(EmitBinOps(E));
2437a51313dSChris Lattner   }
2447a51313dSChris Lattner   ComplexPairTy VisitBinSub(const BinaryOperator *E) {
2457a51313dSChris Lattner     return EmitBinSub(EmitBinOps(E));
2467a51313dSChris Lattner   }
24707bb1966SJohn McCall   ComplexPairTy VisitBinMul(const BinaryOperator *E) {
24807bb1966SJohn McCall     return EmitBinMul(EmitBinOps(E));
24907bb1966SJohn McCall   }
2507a51313dSChris Lattner   ComplexPairTy VisitBinDiv(const BinaryOperator *E) {
2517a51313dSChris Lattner     return EmitBinDiv(EmitBinOps(E));
2527a51313dSChris Lattner   }
2537a51313dSChris Lattner 
2547a51313dSChris Lattner   // Compound assignments.
2557a51313dSChris Lattner   ComplexPairTy VisitBinAddAssign(const CompoundAssignOperator *E) {
2567a51313dSChris Lattner     return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinAdd);
2577a51313dSChris Lattner   }
2587a51313dSChris Lattner   ComplexPairTy VisitBinSubAssign(const CompoundAssignOperator *E) {
2597a51313dSChris Lattner     return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinSub);
2607a51313dSChris Lattner   }
2617a51313dSChris Lattner   ComplexPairTy VisitBinMulAssign(const CompoundAssignOperator *E) {
2627a51313dSChris Lattner     return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinMul);
2637a51313dSChris Lattner   }
2647a51313dSChris Lattner   ComplexPairTy VisitBinDivAssign(const CompoundAssignOperator *E) {
2657a51313dSChris Lattner     return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinDiv);
2667a51313dSChris Lattner   }
2677a51313dSChris Lattner 
2687a51313dSChris Lattner   // GCC rejects rem/and/or/xor for integer complex.
2697a51313dSChris Lattner   // Logical and/or always return int, never complex.
2707a51313dSChris Lattner 
2717a51313dSChris Lattner   // No comparisons produce a complex result.
27207bb1966SJohn McCall 
27307bb1966SJohn McCall   LValue EmitBinAssignLValue(const BinaryOperator *E,
27407bb1966SJohn McCall                              ComplexPairTy &Val);
2757a51313dSChris Lattner   ComplexPairTy VisitBinAssign     (const BinaryOperator *E);
2767a51313dSChris Lattner   ComplexPairTy VisitBinComma      (const BinaryOperator *E);
2777a51313dSChris Lattner 
2787a51313dSChris Lattner 
279c07a0c7eSJohn McCall   ComplexPairTy
280c07a0c7eSJohn McCall   VisitAbstractConditionalOperator(const AbstractConditionalOperator *CO);
2817a51313dSChris Lattner   ComplexPairTy VisitChooseExpr(ChooseExpr *CE);
282dd7406e6SEli Friedman 
283dd7406e6SEli Friedman   ComplexPairTy VisitInitListExpr(InitListExpr *E);
28400079612SDaniel Dunbar 
285afa84ae8SEli Friedman   ComplexPairTy VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
286afa84ae8SEli Friedman     return EmitLoadOfLValue(E);
287afa84ae8SEli Friedman   }
288afa84ae8SEli Friedman 
28900079612SDaniel Dunbar   ComplexPairTy VisitVAArgExpr(VAArgExpr *E);
290df14b3a8SEli Friedman 
291df14b3a8SEli Friedman   ComplexPairTy VisitAtomicExpr(AtomicExpr *E) {
292df14b3a8SEli Friedman     return CGF.EmitAtomicExpr(E).getComplexVal();
293df14b3a8SEli Friedman   }
2947a51313dSChris Lattner };
2957a51313dSChris Lattner }  // end anonymous namespace.
2967a51313dSChris Lattner 
2977a51313dSChris Lattner //===----------------------------------------------------------------------===//
2987a51313dSChris Lattner //                                Utilities
2997a51313dSChris Lattner //===----------------------------------------------------------------------===//
3007a51313dSChris Lattner 
30147fb9508SJohn McCall /// EmitLoadOfLValue - Given an RValue reference for a complex, emit code to
3027a51313dSChris Lattner /// load the real and imaginary pieces, returning them as Real/Imag.
3032d84e842SNick Lewycky ComplexPairTy ComplexExprEmitter::EmitLoadOfLValue(LValue lvalue,
3042d84e842SNick Lewycky                                                    SourceLocation loc) {
30547fb9508SJohn McCall   assert(lvalue.isSimple() && "non-simple complex l-value?");
306a8ec7eb9SJohn McCall   if (lvalue.getType()->isAtomicType())
3072d84e842SNick Lewycky     return CGF.EmitAtomicLoad(lvalue, loc).getComplexVal();
308a8ec7eb9SJohn McCall 
30947fb9508SJohn McCall   llvm::Value *SrcPtr = lvalue.getAddress();
31047fb9508SJohn McCall   bool isVolatile = lvalue.isVolatileQualified();
31127dcbb24SJF Bastien   unsigned AlignR = lvalue.getAlignment().getQuantity();
31227dcbb24SJF Bastien   ASTContext &C = CGF.getContext();
31327dcbb24SJF Bastien   QualType ComplexTy = lvalue.getType();
31427dcbb24SJF Bastien   unsigned ComplexAlign = C.getTypeAlignInChars(ComplexTy).getQuantity();
31527dcbb24SJF Bastien   unsigned AlignI = std::min(AlignR, ComplexAlign);
31647fb9508SJohn McCall 
3178a13c418SCraig Topper   llvm::Value *Real=nullptr, *Imag=nullptr;
318df0fe27bSMike Stump 
31983fe49d1SJohn McCall   if (!IgnoreReal || isVolatile) {
3201ed728c4SDavid Blaikie     llvm::Value *RealP = Builder.CreateStructGEP(nullptr, SrcPtr, 0,
321ba9fd986SBenjamin Kramer                                                  SrcPtr->getName() + ".realp");
32227dcbb24SJF Bastien     Real = Builder.CreateAlignedLoad(RealP, AlignR, isVolatile,
32327dcbb24SJF Bastien                                      SrcPtr->getName() + ".real");
324df0fe27bSMike Stump   }
325df0fe27bSMike Stump 
32683fe49d1SJohn McCall   if (!IgnoreImag || isVolatile) {
3271ed728c4SDavid Blaikie     llvm::Value *ImagP = Builder.CreateStructGEP(nullptr, SrcPtr, 1,
328ba9fd986SBenjamin Kramer                                                  SrcPtr->getName() + ".imagp");
32927dcbb24SJF Bastien     Imag = Builder.CreateAlignedLoad(ImagP, AlignI, isVolatile,
33027dcbb24SJF Bastien                                      SrcPtr->getName() + ".imag");
331df0fe27bSMike Stump   }
3327a51313dSChris Lattner   return ComplexPairTy(Real, Imag);
3337a51313dSChris Lattner }
3347a51313dSChris Lattner 
3357a51313dSChris Lattner /// EmitStoreOfComplex - Store the specified real/imag parts into the
3367a51313dSChris Lattner /// specified value pointer.
337538deffdSDavid Blaikie void ComplexExprEmitter::EmitStoreOfComplex(ComplexPairTy Val, LValue lvalue,
33866e4197fSDavid Blaikie                                             bool isInit) {
339a5b195a1SDavid Majnemer   if (lvalue.getType()->isAtomicType() ||
340a5b195a1SDavid Majnemer       (!isInit && CGF.LValueIsSuitableForInlineAtomic(lvalue)))
341a8ec7eb9SJohn McCall     return CGF.EmitAtomicStore(RValue::getComplex(Val), lvalue, isInit);
342a8ec7eb9SJohn McCall 
34347fb9508SJohn McCall   llvm::Value *Ptr = lvalue.getAddress();
3441ed728c4SDavid Blaikie   llvm::Value *RealPtr = Builder.CreateStructGEP(nullptr, Ptr, 0, "real");
3451ed728c4SDavid Blaikie   llvm::Value *ImagPtr = Builder.CreateStructGEP(nullptr, Ptr, 1, "imag");
34627dcbb24SJF Bastien   unsigned AlignR = lvalue.getAlignment().getQuantity();
34727dcbb24SJF Bastien   ASTContext &C = CGF.getContext();
34827dcbb24SJF Bastien   QualType ComplexTy = lvalue.getType();
34927dcbb24SJF Bastien   unsigned ComplexAlign = C.getTypeAlignInChars(ComplexTy).getQuantity();
35027dcbb24SJF Bastien   unsigned AlignI = std::min(AlignR, ComplexAlign);
3517a51313dSChris Lattner 
35227dcbb24SJF Bastien   Builder.CreateAlignedStore(Val.first, RealPtr, AlignR,
35327dcbb24SJF Bastien                              lvalue.isVolatileQualified());
35427dcbb24SJF Bastien   Builder.CreateAlignedStore(Val.second, ImagPtr, AlignI,
35527dcbb24SJF Bastien                              lvalue.isVolatileQualified());
3567a51313dSChris Lattner }
3577a51313dSChris Lattner 
3587a51313dSChris Lattner 
3597a51313dSChris Lattner 
3607a51313dSChris Lattner //===----------------------------------------------------------------------===//
3617a51313dSChris Lattner //                            Visitor Methods
3627a51313dSChris Lattner //===----------------------------------------------------------------------===//
3637a51313dSChris Lattner 
3647a51313dSChris Lattner ComplexPairTy ComplexExprEmitter::VisitExpr(Expr *E) {
365b74711dfSFariborz Jahanian   CGF.ErrorUnsupported(E, "complex expression");
366b74711dfSFariborz Jahanian   llvm::Type *EltTy =
36747fb9508SJohn McCall     CGF.ConvertType(getComplexType(E->getType())->getElementType());
368b74711dfSFariborz Jahanian   llvm::Value *U = llvm::UndefValue::get(EltTy);
369b74711dfSFariborz Jahanian   return ComplexPairTy(U, U);
3707a51313dSChris Lattner }
3717a51313dSChris Lattner 
3727a51313dSChris Lattner ComplexPairTy ComplexExprEmitter::
3737a51313dSChris Lattner VisitImaginaryLiteral(const ImaginaryLiteral *IL) {
3747a51313dSChris Lattner   llvm::Value *Imag = CGF.EmitScalarExpr(IL->getSubExpr());
375294c2db4SJohn McCall   return ComplexPairTy(llvm::Constant::getNullValue(Imag->getType()), Imag);
3767a51313dSChris Lattner }
3777a51313dSChris Lattner 
3787a51313dSChris Lattner 
3797a51313dSChris Lattner ComplexPairTy ComplexExprEmitter::VisitCallExpr(const CallExpr *E) {
380ced8bdf7SDavid Majnemer   if (E->getCallReturnType(CGF.getContext())->isReferenceType())
381d8b7ae20SAnders Carlsson     return EmitLoadOfLValue(E);
382d8b7ae20SAnders Carlsson 
3837a51313dSChris Lattner   return CGF.EmitCallExpr(E).getComplexVal();
3847a51313dSChris Lattner }
3857a51313dSChris Lattner 
3867a51313dSChris Lattner ComplexPairTy ComplexExprEmitter::VisitStmtExpr(const StmtExpr *E) {
387ce1de617SJohn McCall   CodeGenFunction::StmtExprEvaluation eval(CGF);
3884871a46cSEli Friedman   llvm::Value *RetAlloca = CGF.EmitCompoundStmt(*E->getSubStmt(), true);
3894871a46cSEli Friedman   assert(RetAlloca && "Expected complex return value");
3902d84e842SNick Lewycky   return EmitLoadOfLValue(CGF.MakeAddrLValue(RetAlloca, E->getType()),
3912d84e842SNick Lewycky                           E->getExprLoc());
3927a51313dSChris Lattner }
3937a51313dSChris Lattner 
394*650d7f7dSFilipe Cabecinhas /// Emit a cast from complex value Val to DestType.
3957a51313dSChris Lattner ComplexPairTy ComplexExprEmitter::EmitComplexToComplexCast(ComplexPairTy Val,
3967a51313dSChris Lattner                                                            QualType SrcType,
3977a51313dSChris Lattner                                                            QualType DestType) {
3987a51313dSChris Lattner   // Get the src/dest element type.
39947fb9508SJohn McCall   SrcType = SrcType->castAs<ComplexType>()->getElementType();
40047fb9508SJohn McCall   DestType = DestType->castAs<ComplexType>()->getElementType();
4017a51313dSChris Lattner 
4027a51313dSChris Lattner   // C99 6.3.1.6: When a value of complex type is converted to another
4037a51313dSChris Lattner   // complex type, both the real and imaginary parts follow the conversion
4047a51313dSChris Lattner   // rules for the corresponding real types.
4057a51313dSChris Lattner   Val.first = CGF.EmitScalarConversion(Val.first, SrcType, DestType);
4067a51313dSChris Lattner   Val.second = CGF.EmitScalarConversion(Val.second, SrcType, DestType);
4077a51313dSChris Lattner   return Val;
4087a51313dSChris Lattner }
4097a51313dSChris Lattner 
410f045007fSEli Friedman ComplexPairTy ComplexExprEmitter::EmitScalarToComplexCast(llvm::Value *Val,
411f045007fSEli Friedman                                                           QualType SrcType,
412f045007fSEli Friedman                                                           QualType DestType) {
413f045007fSEli Friedman   // Convert the input element to the element type of the complex.
414f045007fSEli Friedman   DestType = DestType->castAs<ComplexType>()->getElementType();
415f045007fSEli Friedman   Val = CGF.EmitScalarConversion(Val, SrcType, DestType);
416f045007fSEli Friedman 
417f045007fSEli Friedman   // Return (realval, 0).
418f045007fSEli Friedman   return ComplexPairTy(Val, llvm::Constant::getNullValue(Val->getType()));
419f045007fSEli Friedman }
420f045007fSEli Friedman 
4211a07fd18SCraig Topper ComplexPairTy ComplexExprEmitter::EmitCast(CastKind CK, Expr *Op,
422c357f412SDouglas Gregor                                            QualType DestTy) {
42334376a68SJohn McCall   switch (CK) {
4245836852eSEli Friedman   case CK_Dependent: llvm_unreachable("dependent cast kind in IR gen!");
42534376a68SJohn McCall 
426fa35df62SDavid Chisnall   // Atomic to non-atomic casts may be more than a no-op for some platforms and
427fa35df62SDavid Chisnall   // for some types.
428fa35df62SDavid Chisnall   case CK_AtomicToNonAtomic:
429fa35df62SDavid Chisnall   case CK_NonAtomicToAtomic:
43034376a68SJohn McCall   case CK_NoOp:
43134376a68SJohn McCall   case CK_LValueToRValue:
4325836852eSEli Friedman   case CK_UserDefinedConversion:
43334376a68SJohn McCall     return Visit(Op);
43434376a68SJohn McCall 
4355836852eSEli Friedman   case CK_LValueBitCast: {
43647fb9508SJohn McCall     LValue origLV = CGF.EmitLValue(Op);
43747fb9508SJohn McCall     llvm::Value *V = origLV.getAddress();
438c357f412SDouglas Gregor     V = Builder.CreateBitCast(V,
439c357f412SDouglas Gregor                     CGF.ConvertType(CGF.getContext().getPointerType(DestTy)));
44047fb9508SJohn McCall     return EmitLoadOfLValue(CGF.MakeAddrLValue(V, DestTy,
4412d84e842SNick Lewycky                                                origLV.getAlignment()),
4422d84e842SNick Lewycky                             Op->getExprLoc());
443c357f412SDouglas Gregor   }
444c357f412SDouglas Gregor 
4455836852eSEli Friedman   case CK_BitCast:
4465836852eSEli Friedman   case CK_BaseToDerived:
4475836852eSEli Friedman   case CK_DerivedToBase:
4485836852eSEli Friedman   case CK_UncheckedDerivedToBase:
4495836852eSEli Friedman   case CK_Dynamic:
4505836852eSEli Friedman   case CK_ToUnion:
4515836852eSEli Friedman   case CK_ArrayToPointerDecay:
4525836852eSEli Friedman   case CK_FunctionToPointerDecay:
4535836852eSEli Friedman   case CK_NullToPointer:
4545836852eSEli Friedman   case CK_NullToMemberPointer:
4555836852eSEli Friedman   case CK_BaseToDerivedMemberPointer:
4565836852eSEli Friedman   case CK_DerivedToBaseMemberPointer:
4575836852eSEli Friedman   case CK_MemberPointerToBoolean:
458c62bb391SJohn McCall   case CK_ReinterpretMemberPointer:
4595836852eSEli Friedman   case CK_ConstructorConversion:
4605836852eSEli Friedman   case CK_IntegralToPointer:
4615836852eSEli Friedman   case CK_PointerToIntegral:
4625836852eSEli Friedman   case CK_PointerToBoolean:
4635836852eSEli Friedman   case CK_ToVoid:
4645836852eSEli Friedman   case CK_VectorSplat:
4655836852eSEli Friedman   case CK_IntegralCast:
4665836852eSEli Friedman   case CK_IntegralToBoolean:
4675836852eSEli Friedman   case CK_IntegralToFloating:
4685836852eSEli Friedman   case CK_FloatingToIntegral:
4695836852eSEli Friedman   case CK_FloatingToBoolean:
4705836852eSEli Friedman   case CK_FloatingCast:
4719320b87cSJohn McCall   case CK_CPointerToObjCPointerCast:
4729320b87cSJohn McCall   case CK_BlockPointerToObjCPointerCast:
4735836852eSEli Friedman   case CK_AnyPointerToBlockPointerCast:
4745836852eSEli Friedman   case CK_ObjCObjectLValueCast:
4755836852eSEli Friedman   case CK_FloatingComplexToReal:
4765836852eSEli Friedman   case CK_FloatingComplexToBoolean:
4775836852eSEli Friedman   case CK_IntegralComplexToReal:
4785836852eSEli Friedman   case CK_IntegralComplexToBoolean:
4792d637d2eSJohn McCall   case CK_ARCProduceObject:
4802d637d2eSJohn McCall   case CK_ARCConsumeObject:
4812d637d2eSJohn McCall   case CK_ARCReclaimReturnedObject:
4822d637d2eSJohn McCall   case CK_ARCExtendBlockObject:
483ed90df38SDouglas Gregor   case CK_CopyAndAutoreleaseBlockObject:
48434866c77SEli Friedman   case CK_BuiltinFnToFnPtr:
4851b4fb3e0SGuy Benyei   case CK_ZeroToOCLEvent:
486e1468322SDavid Tweed   case CK_AddressSpaceConversion:
4875836852eSEli Friedman     llvm_unreachable("invalid cast kind for complex value");
4885836852eSEli Friedman 
4895836852eSEli Friedman   case CK_FloatingRealToComplex:
490f045007fSEli Friedman   case CK_IntegralRealToComplex:
491f045007fSEli Friedman     return EmitScalarToComplexCast(CGF.EmitScalarExpr(Op),
492f045007fSEli Friedman                                    Op->getType(), DestTy);
4937a51313dSChris Lattner 
4945836852eSEli Friedman   case CK_FloatingComplexCast:
4955836852eSEli Friedman   case CK_FloatingComplexToIntegralComplex:
4965836852eSEli Friedman   case CK_IntegralComplexCast:
4975836852eSEli Friedman   case CK_IntegralComplexToFloatingComplex:
4985836852eSEli Friedman     return EmitComplexToComplexCast(Visit(Op), Op->getType(), DestTy);
4995836852eSEli Friedman   }
5005836852eSEli Friedman 
5015836852eSEli Friedman   llvm_unreachable("unknown cast resulting in complex value");
5025836852eSEli Friedman }
5035836852eSEli Friedman 
5047a51313dSChris Lattner ComplexPairTy ComplexExprEmitter::VisitUnaryMinus(const UnaryOperator *E) {
505df0fe27bSMike Stump   TestAndClearIgnoreReal();
506df0fe27bSMike Stump   TestAndClearIgnoreImag();
5077a51313dSChris Lattner   ComplexPairTy Op = Visit(E->getSubExpr());
50894dfae24SChris Lattner 
50994dfae24SChris Lattner   llvm::Value *ResR, *ResI;
510998f9d97SDuncan Sands   if (Op.first->getType()->isFloatingPointTy()) {
51194dfae24SChris Lattner     ResR = Builder.CreateFNeg(Op.first,  "neg.r");
51294dfae24SChris Lattner     ResI = Builder.CreateFNeg(Op.second, "neg.i");
51394dfae24SChris Lattner   } else {
51494dfae24SChris Lattner     ResR = Builder.CreateNeg(Op.first,  "neg.r");
51594dfae24SChris Lattner     ResI = Builder.CreateNeg(Op.second, "neg.i");
51694dfae24SChris Lattner   }
5177a51313dSChris Lattner   return ComplexPairTy(ResR, ResI);
5187a51313dSChris Lattner }
5197a51313dSChris Lattner 
5207a51313dSChris Lattner ComplexPairTy ComplexExprEmitter::VisitUnaryNot(const UnaryOperator *E) {
521df0fe27bSMike Stump   TestAndClearIgnoreReal();
522df0fe27bSMike Stump   TestAndClearIgnoreImag();
5237a51313dSChris Lattner   // ~(a+ib) = a + i*-b
5247a51313dSChris Lattner   ComplexPairTy Op = Visit(E->getSubExpr());
52594dfae24SChris Lattner   llvm::Value *ResI;
526998f9d97SDuncan Sands   if (Op.second->getType()->isFloatingPointTy())
52794dfae24SChris Lattner     ResI = Builder.CreateFNeg(Op.second, "conj.i");
52894dfae24SChris Lattner   else
52994dfae24SChris Lattner     ResI = Builder.CreateNeg(Op.second, "conj.i");
53094dfae24SChris Lattner 
5317a51313dSChris Lattner   return ComplexPairTy(Op.first, ResI);
5327a51313dSChris Lattner }
5337a51313dSChris Lattner 
5347a51313dSChris Lattner ComplexPairTy ComplexExprEmitter::EmitBinAdd(const BinOpInfo &Op) {
53594dfae24SChris Lattner   llvm::Value *ResR, *ResI;
53694dfae24SChris Lattner 
537998f9d97SDuncan Sands   if (Op.LHS.first->getType()->isFloatingPointTy()) {
53894dfae24SChris Lattner     ResR = Builder.CreateFAdd(Op.LHS.first,  Op.RHS.first,  "add.r");
539a216cad0SChandler Carruth     if (Op.LHS.second && Op.RHS.second)
54094dfae24SChris Lattner       ResI = Builder.CreateFAdd(Op.LHS.second, Op.RHS.second, "add.i");
541a216cad0SChandler Carruth     else
542a216cad0SChandler Carruth       ResI = Op.LHS.second ? Op.LHS.second : Op.RHS.second;
543a216cad0SChandler Carruth     assert(ResI && "Only one operand may be real!");
54494dfae24SChris Lattner   } else {
54594dfae24SChris Lattner     ResR = Builder.CreateAdd(Op.LHS.first,  Op.RHS.first,  "add.r");
546a216cad0SChandler Carruth     assert(Op.LHS.second && Op.RHS.second &&
547a216cad0SChandler Carruth            "Both operands of integer complex operators must be complex!");
54894dfae24SChris Lattner     ResI = Builder.CreateAdd(Op.LHS.second, Op.RHS.second, "add.i");
54994dfae24SChris Lattner   }
5507a51313dSChris Lattner   return ComplexPairTy(ResR, ResI);
5517a51313dSChris Lattner }
5527a51313dSChris Lattner 
5537a51313dSChris Lattner ComplexPairTy ComplexExprEmitter::EmitBinSub(const BinOpInfo &Op) {
55494dfae24SChris Lattner   llvm::Value *ResR, *ResI;
555998f9d97SDuncan Sands   if (Op.LHS.first->getType()->isFloatingPointTy()) {
55694dfae24SChris Lattner     ResR = Builder.CreateFSub(Op.LHS.first, Op.RHS.first, "sub.r");
557a216cad0SChandler Carruth     if (Op.LHS.second && Op.RHS.second)
55894dfae24SChris Lattner       ResI = Builder.CreateFSub(Op.LHS.second, Op.RHS.second, "sub.i");
559a216cad0SChandler Carruth     else
560a216cad0SChandler Carruth       ResI = Op.LHS.second ? Op.LHS.second
561a216cad0SChandler Carruth                            : Builder.CreateFNeg(Op.RHS.second, "sub.i");
562a216cad0SChandler Carruth     assert(ResI && "Only one operand may be real!");
56394dfae24SChris Lattner   } else {
56494dfae24SChris Lattner     ResR = Builder.CreateSub(Op.LHS.first, Op.RHS.first, "sub.r");
565a216cad0SChandler Carruth     assert(Op.LHS.second && Op.RHS.second &&
566a216cad0SChandler Carruth            "Both operands of integer complex operators must be complex!");
56794dfae24SChris Lattner     ResI = Builder.CreateSub(Op.LHS.second, Op.RHS.second, "sub.i");
56894dfae24SChris Lattner   }
5697a51313dSChris Lattner   return ComplexPairTy(ResR, ResI);
5707a51313dSChris Lattner }
5717a51313dSChris Lattner 
572a216cad0SChandler Carruth /// \brief Emit a libcall for a binary operation on complex types.
573a216cad0SChandler Carruth ComplexPairTy ComplexExprEmitter::EmitComplexBinOpLibCall(StringRef LibCallName,
574a216cad0SChandler Carruth                                                           const BinOpInfo &Op) {
575a216cad0SChandler Carruth   CallArgList Args;
576a216cad0SChandler Carruth   Args.add(RValue::get(Op.LHS.first),
577a216cad0SChandler Carruth            Op.Ty->castAs<ComplexType>()->getElementType());
578a216cad0SChandler Carruth   Args.add(RValue::get(Op.LHS.second),
579a216cad0SChandler Carruth            Op.Ty->castAs<ComplexType>()->getElementType());
580a216cad0SChandler Carruth   Args.add(RValue::get(Op.RHS.first),
581a216cad0SChandler Carruth            Op.Ty->castAs<ComplexType>()->getElementType());
582a216cad0SChandler Carruth   Args.add(RValue::get(Op.RHS.second),
583a216cad0SChandler Carruth            Op.Ty->castAs<ComplexType>()->getElementType());
5847a51313dSChris Lattner 
585a216cad0SChandler Carruth   // We *must* use the full CG function call building logic here because the
586d90dd797SAnton Korobeynikov   // complex type has special ABI handling. We also should not forget about
587d90dd797SAnton Korobeynikov   // special calling convention which may be used for compiler builtins.
588d90dd797SAnton Korobeynikov   const CGFunctionInfo &FuncInfo =
589d90dd797SAnton Korobeynikov     CGF.CGM.getTypes().arrangeFreeFunctionCall(
590d90dd797SAnton Korobeynikov       Op.Ty, Args, FunctionType::ExtInfo(/* No CC here - will be added later */),
591d90dd797SAnton Korobeynikov       RequiredArgs::All);
592a216cad0SChandler Carruth   llvm::FunctionType *FTy = CGF.CGM.getTypes().GetFunctionType(FuncInfo);
593d90dd797SAnton Korobeynikov   llvm::Constant *Func = CGF.CGM.CreateBuiltinFunction(FTy, LibCallName);
594d90dd797SAnton Korobeynikov   llvm::Instruction *Call;
595a216cad0SChandler Carruth 
596d90dd797SAnton Korobeynikov   RValue Res = CGF.EmitCall(FuncInfo, Func, ReturnValueSlot(), Args,
597d90dd797SAnton Korobeynikov                             nullptr, &Call);
598d90dd797SAnton Korobeynikov   cast<llvm::CallInst>(Call)->setCallingConv(CGF.CGM.getBuiltinCC());
599d90dd797SAnton Korobeynikov   cast<llvm::CallInst>(Call)->setDoesNotThrow();
600d90dd797SAnton Korobeynikov 
601d90dd797SAnton Korobeynikov   return Res.getComplexVal();
602a216cad0SChandler Carruth }
603a216cad0SChandler Carruth 
6040c4b230bSChandler Carruth /// \brief Lookup the libcall name for a given floating point type complex
6050c4b230bSChandler Carruth /// multiply.
6060c4b230bSChandler Carruth static StringRef getComplexMultiplyLibCallName(llvm::Type *Ty) {
6070c4b230bSChandler Carruth   switch (Ty->getTypeID()) {
6080c4b230bSChandler Carruth   default:
6090c4b230bSChandler Carruth     llvm_unreachable("Unsupported floating point type!");
6100c4b230bSChandler Carruth   case llvm::Type::HalfTyID:
6110c4b230bSChandler Carruth     return "__mulhc3";
6120c4b230bSChandler Carruth   case llvm::Type::FloatTyID:
6130c4b230bSChandler Carruth     return "__mulsc3";
6140c4b230bSChandler Carruth   case llvm::Type::DoubleTyID:
6150c4b230bSChandler Carruth     return "__muldc3";
6160c4b230bSChandler Carruth   case llvm::Type::PPC_FP128TyID:
6170c4b230bSChandler Carruth     return "__multc3";
6180c4b230bSChandler Carruth   case llvm::Type::X86_FP80TyID:
6190c4b230bSChandler Carruth     return "__mulxc3";
620444822bbSJiangning Liu   case llvm::Type::FP128TyID:
621444822bbSJiangning Liu     return "__multc3";
6220c4b230bSChandler Carruth   }
6230c4b230bSChandler Carruth }
6240c4b230bSChandler Carruth 
625a216cad0SChandler Carruth // See C11 Annex G.5.1 for the semantics of multiplicative operators on complex
626a216cad0SChandler Carruth // typed values.
6277a51313dSChris Lattner ComplexPairTy ComplexExprEmitter::EmitBinMul(const BinOpInfo &Op) {
62894dfae24SChris Lattner   using llvm::Value;
62994dfae24SChris Lattner   Value *ResR, *ResI;
6300c4b230bSChandler Carruth   llvm::MDBuilder MDHelper(CGF.getLLVMContext());
6317a51313dSChris Lattner 
632998f9d97SDuncan Sands   if (Op.LHS.first->getType()->isFloatingPointTy()) {
633a216cad0SChandler Carruth     // The general formulation is:
634a216cad0SChandler Carruth     // (a + ib) * (c + id) = (a * c - b * d) + i(a * d + b * c)
635a216cad0SChandler Carruth     //
636a216cad0SChandler Carruth     // But we can fold away components which would be zero due to a real
637a216cad0SChandler Carruth     // operand according to C11 Annex G.5.1p2.
638a216cad0SChandler Carruth     // FIXME: C11 also provides for imaginary types which would allow folding
639a216cad0SChandler Carruth     // still more of this within the type system.
64094dfae24SChris Lattner 
641a216cad0SChandler Carruth     if (Op.LHS.second && Op.RHS.second) {
6420c4b230bSChandler Carruth       // If both operands are complex, emit the core math directly, and then
6430c4b230bSChandler Carruth       // test for NaNs. If we find NaNs in the result, we delegate to a libcall
6440c4b230bSChandler Carruth       // to carefully re-compute the correct infinity representation if
6450c4b230bSChandler Carruth       // possible. The expectation is that the presence of NaNs here is
6460c4b230bSChandler Carruth       // *extremely* rare, and so the cost of the libcall is almost irrelevant.
6470c4b230bSChandler Carruth       // This is good, because the libcall re-computes the core multiplication
6480c4b230bSChandler Carruth       // exactly the same as we do here and re-tests for NaNs in order to be
6490c4b230bSChandler Carruth       // a generic complex*complex libcall.
6500c4b230bSChandler Carruth 
6510c4b230bSChandler Carruth       // First compute the four products.
6520c4b230bSChandler Carruth       Value *AC = Builder.CreateFMul(Op.LHS.first, Op.RHS.first, "mul_ac");
6530c4b230bSChandler Carruth       Value *BD = Builder.CreateFMul(Op.LHS.second, Op.RHS.second, "mul_bd");
6540c4b230bSChandler Carruth       Value *AD = Builder.CreateFMul(Op.LHS.first, Op.RHS.second, "mul_ad");
6550c4b230bSChandler Carruth       Value *BC = Builder.CreateFMul(Op.LHS.second, Op.RHS.first, "mul_bc");
6560c4b230bSChandler Carruth 
6570c4b230bSChandler Carruth       // The real part is the difference of the first two, the imaginary part is
6580c4b230bSChandler Carruth       // the sum of the second.
6590c4b230bSChandler Carruth       ResR = Builder.CreateFSub(AC, BD, "mul_r");
6600c4b230bSChandler Carruth       ResI = Builder.CreateFAdd(AD, BC, "mul_i");
6610c4b230bSChandler Carruth 
6620c4b230bSChandler Carruth       // Emit the test for the real part becoming NaN and create a branch to
6630c4b230bSChandler Carruth       // handle it. We test for NaN by comparing the number to itself.
6640c4b230bSChandler Carruth       Value *IsRNaN = Builder.CreateFCmpUNO(ResR, ResR, "isnan_cmp");
6650c4b230bSChandler Carruth       llvm::BasicBlock *ContBB = CGF.createBasicBlock("complex_mul_cont");
6660c4b230bSChandler Carruth       llvm::BasicBlock *INaNBB = CGF.createBasicBlock("complex_mul_imag_nan");
6670c4b230bSChandler Carruth       llvm::Instruction *Branch = Builder.CreateCondBr(IsRNaN, INaNBB, ContBB);
6680c4b230bSChandler Carruth       llvm::BasicBlock *OrigBB = Branch->getParent();
6690c4b230bSChandler Carruth 
6700c4b230bSChandler Carruth       // Give hint that we very much don't expect to see NaNs.
6710c4b230bSChandler Carruth       // Value chosen to match UR_NONTAKEN_WEIGHT, see BranchProbabilityInfo.cpp
6720c4b230bSChandler Carruth       llvm::MDNode *BrWeight = MDHelper.createBranchWeights(1, (1U << 20) - 1);
6730c4b230bSChandler Carruth       Branch->setMetadata(llvm::LLVMContext::MD_prof, BrWeight);
6740c4b230bSChandler Carruth 
6750c4b230bSChandler Carruth       // Now test the imaginary part and create its branch.
6760c4b230bSChandler Carruth       CGF.EmitBlock(INaNBB);
6770c4b230bSChandler Carruth       Value *IsINaN = Builder.CreateFCmpUNO(ResI, ResI, "isnan_cmp");
6780c4b230bSChandler Carruth       llvm::BasicBlock *LibCallBB = CGF.createBasicBlock("complex_mul_libcall");
6790c4b230bSChandler Carruth       Branch = Builder.CreateCondBr(IsINaN, LibCallBB, ContBB);
6800c4b230bSChandler Carruth       Branch->setMetadata(llvm::LLVMContext::MD_prof, BrWeight);
6810c4b230bSChandler Carruth 
6820c4b230bSChandler Carruth       // Now emit the libcall on this slowest of the slow paths.
6830c4b230bSChandler Carruth       CGF.EmitBlock(LibCallBB);
6840c4b230bSChandler Carruth       Value *LibCallR, *LibCallI;
6850c4b230bSChandler Carruth       std::tie(LibCallR, LibCallI) = EmitComplexBinOpLibCall(
6860c4b230bSChandler Carruth           getComplexMultiplyLibCallName(Op.LHS.first->getType()), Op);
6870c4b230bSChandler Carruth       Builder.CreateBr(ContBB);
6880c4b230bSChandler Carruth 
6890c4b230bSChandler Carruth       // Finally continue execution by phi-ing together the different
6900c4b230bSChandler Carruth       // computation paths.
6910c4b230bSChandler Carruth       CGF.EmitBlock(ContBB);
6920c4b230bSChandler Carruth       llvm::PHINode *RealPHI = Builder.CreatePHI(ResR->getType(), 3, "real_mul_phi");
6930c4b230bSChandler Carruth       RealPHI->addIncoming(ResR, OrigBB);
6940c4b230bSChandler Carruth       RealPHI->addIncoming(ResR, INaNBB);
6950c4b230bSChandler Carruth       RealPHI->addIncoming(LibCallR, LibCallBB);
6960c4b230bSChandler Carruth       llvm::PHINode *ImagPHI = Builder.CreatePHI(ResI->getType(), 3, "imag_mul_phi");
6970c4b230bSChandler Carruth       ImagPHI->addIncoming(ResI, OrigBB);
6980c4b230bSChandler Carruth       ImagPHI->addIncoming(ResI, INaNBB);
6990c4b230bSChandler Carruth       ImagPHI->addIncoming(LibCallI, LibCallBB);
7000c4b230bSChandler Carruth       return ComplexPairTy(RealPHI, ImagPHI);
701a216cad0SChandler Carruth     }
702a216cad0SChandler Carruth     assert((Op.LHS.second || Op.RHS.second) &&
703a216cad0SChandler Carruth            "At least one operand must be complex!");
704a216cad0SChandler Carruth 
705a216cad0SChandler Carruth     // If either of the operands is a real rather than a complex, the
706a216cad0SChandler Carruth     // imaginary component is ignored when computing the real component of the
707a216cad0SChandler Carruth     // result.
708a216cad0SChandler Carruth     ResR = Builder.CreateFMul(Op.LHS.first, Op.RHS.first, "mul.rl");
709a216cad0SChandler Carruth 
710a216cad0SChandler Carruth     ResI = Op.LHS.second
711a216cad0SChandler Carruth                ? Builder.CreateFMul(Op.LHS.second, Op.RHS.first, "mul.il")
712a216cad0SChandler Carruth                : Builder.CreateFMul(Op.LHS.first, Op.RHS.second, "mul.ir");
71394dfae24SChris Lattner   } else {
714a216cad0SChandler Carruth     assert(Op.LHS.second && Op.RHS.second &&
715a216cad0SChandler Carruth            "Both operands of integer complex operators must be complex!");
71694dfae24SChris Lattner     Value *ResRl = Builder.CreateMul(Op.LHS.first, Op.RHS.first, "mul.rl");
71794dfae24SChris Lattner     Value *ResRr = Builder.CreateMul(Op.LHS.second, Op.RHS.second, "mul.rr");
71894dfae24SChris Lattner     ResR = Builder.CreateSub(ResRl, ResRr, "mul.r");
71994dfae24SChris Lattner 
72094dfae24SChris Lattner     Value *ResIl = Builder.CreateMul(Op.LHS.second, Op.RHS.first, "mul.il");
72194dfae24SChris Lattner     Value *ResIr = Builder.CreateMul(Op.LHS.first, Op.RHS.second, "mul.ir");
72294dfae24SChris Lattner     ResI = Builder.CreateAdd(ResIl, ResIr, "mul.i");
72394dfae24SChris Lattner   }
7247a51313dSChris Lattner   return ComplexPairTy(ResR, ResI);
7257a51313dSChris Lattner }
7267a51313dSChris Lattner 
727a216cad0SChandler Carruth // See C11 Annex G.5.1 for the semantics of multiplicative operators on complex
728a216cad0SChandler Carruth // typed values.
7297a51313dSChris Lattner ComplexPairTy ComplexExprEmitter::EmitBinDiv(const BinOpInfo &Op) {
7307a51313dSChris Lattner   llvm::Value *LHSr = Op.LHS.first, *LHSi = Op.LHS.second;
7317a51313dSChris Lattner   llvm::Value *RHSr = Op.RHS.first, *RHSi = Op.RHS.second;
7327a51313dSChris Lattner 
73394dfae24SChris Lattner 
73494dfae24SChris Lattner   llvm::Value *DSTr, *DSTi;
735a216cad0SChandler Carruth   if (LHSr->getType()->isFloatingPointTy()) {
736a216cad0SChandler Carruth     // If we have a complex operand on the RHS, we delegate to a libcall to
737a216cad0SChandler Carruth     // handle all of the complexities and minimize underflow/overflow cases.
738a216cad0SChandler Carruth     //
739a216cad0SChandler Carruth     // FIXME: We would be able to avoid the libcall in many places if we
740a216cad0SChandler Carruth     // supported imaginary types in addition to complex types.
741a216cad0SChandler Carruth     if (RHSi) {
742a216cad0SChandler Carruth       BinOpInfo LibCallOp = Op;
743a216cad0SChandler Carruth       // If LHS was a real, supply a null imaginary part.
744a216cad0SChandler Carruth       if (!LHSi)
745a216cad0SChandler Carruth         LibCallOp.LHS.second = llvm::Constant::getNullValue(LHSr->getType());
74694dfae24SChris Lattner 
747a216cad0SChandler Carruth       StringRef LibCallName;
748a216cad0SChandler Carruth       switch (LHSr->getType()->getTypeID()) {
749a216cad0SChandler Carruth       default:
750a216cad0SChandler Carruth         llvm_unreachable("Unsupported floating point type!");
751a216cad0SChandler Carruth       case llvm::Type::HalfTyID:
752a216cad0SChandler Carruth         return EmitComplexBinOpLibCall("__divhc3", LibCallOp);
753a216cad0SChandler Carruth       case llvm::Type::FloatTyID:
754a216cad0SChandler Carruth         return EmitComplexBinOpLibCall("__divsc3", LibCallOp);
755a216cad0SChandler Carruth       case llvm::Type::DoubleTyID:
756a216cad0SChandler Carruth         return EmitComplexBinOpLibCall("__divdc3", LibCallOp);
757aa3e9f5aSJoerg Sonnenberger       case llvm::Type::PPC_FP128TyID:
758aa3e9f5aSJoerg Sonnenberger         return EmitComplexBinOpLibCall("__divtc3", LibCallOp);
759a216cad0SChandler Carruth       case llvm::Type::X86_FP80TyID:
760a216cad0SChandler Carruth         return EmitComplexBinOpLibCall("__divxc3", LibCallOp);
761444822bbSJiangning Liu       case llvm::Type::FP128TyID:
762444822bbSJiangning Liu         return EmitComplexBinOpLibCall("__divtc3", LibCallOp);
763a216cad0SChandler Carruth       }
764a216cad0SChandler Carruth     }
765a216cad0SChandler Carruth     assert(LHSi && "Can have at most one non-complex operand!");
76694dfae24SChris Lattner 
767a216cad0SChandler Carruth     DSTr = Builder.CreateFDiv(LHSr, RHSr);
768a216cad0SChandler Carruth     DSTi = Builder.CreateFDiv(LHSi, RHSr);
76994dfae24SChris Lattner   } else {
770a216cad0SChandler Carruth     assert(Op.LHS.second && Op.RHS.second &&
771a216cad0SChandler Carruth            "Both operands of integer complex operators must be complex!");
7727a51313dSChris Lattner     // (a+ib) / (c+id) = ((ac+bd)/(cc+dd)) + i((bc-ad)/(cc+dd))
77376399eb2SBenjamin Kramer     llvm::Value *Tmp1 = Builder.CreateMul(LHSr, RHSr); // a*c
77476399eb2SBenjamin Kramer     llvm::Value *Tmp2 = Builder.CreateMul(LHSi, RHSi); // b*d
77576399eb2SBenjamin Kramer     llvm::Value *Tmp3 = Builder.CreateAdd(Tmp1, Tmp2); // ac+bd
7767a51313dSChris Lattner 
77776399eb2SBenjamin Kramer     llvm::Value *Tmp4 = Builder.CreateMul(RHSr, RHSr); // c*c
77876399eb2SBenjamin Kramer     llvm::Value *Tmp5 = Builder.CreateMul(RHSi, RHSi); // d*d
77976399eb2SBenjamin Kramer     llvm::Value *Tmp6 = Builder.CreateAdd(Tmp4, Tmp5); // cc+dd
7807a51313dSChris Lattner 
78176399eb2SBenjamin Kramer     llvm::Value *Tmp7 = Builder.CreateMul(LHSi, RHSr); // b*c
78276399eb2SBenjamin Kramer     llvm::Value *Tmp8 = Builder.CreateMul(LHSr, RHSi); // a*d
78376399eb2SBenjamin Kramer     llvm::Value *Tmp9 = Builder.CreateSub(Tmp7, Tmp8); // bc-ad
7847a51313dSChris Lattner 
78547fb9508SJohn McCall     if (Op.Ty->castAs<ComplexType>()->getElementType()->isUnsignedIntegerType()) {
78676399eb2SBenjamin Kramer       DSTr = Builder.CreateUDiv(Tmp3, Tmp6);
78776399eb2SBenjamin Kramer       DSTi = Builder.CreateUDiv(Tmp9, Tmp6);
7887a51313dSChris Lattner     } else {
78976399eb2SBenjamin Kramer       DSTr = Builder.CreateSDiv(Tmp3, Tmp6);
79076399eb2SBenjamin Kramer       DSTi = Builder.CreateSDiv(Tmp9, Tmp6);
7917a51313dSChris Lattner     }
7927a51313dSChris Lattner   }
7937a51313dSChris Lattner 
7947a51313dSChris Lattner   return ComplexPairTy(DSTr, DSTi);
7957a51313dSChris Lattner }
7967a51313dSChris Lattner 
7977a51313dSChris Lattner ComplexExprEmitter::BinOpInfo
7987a51313dSChris Lattner ComplexExprEmitter::EmitBinOps(const BinaryOperator *E) {
799df0fe27bSMike Stump   TestAndClearIgnoreReal();
800df0fe27bSMike Stump   TestAndClearIgnoreImag();
8017a51313dSChris Lattner   BinOpInfo Ops;
802a216cad0SChandler Carruth   if (E->getLHS()->getType()->isRealFloatingType())
803a216cad0SChandler Carruth     Ops.LHS = ComplexPairTy(CGF.EmitScalarExpr(E->getLHS()), nullptr);
804a216cad0SChandler Carruth   else
8057a51313dSChris Lattner     Ops.LHS = Visit(E->getLHS());
806a216cad0SChandler Carruth   if (E->getRHS()->getType()->isRealFloatingType())
807a216cad0SChandler Carruth     Ops.RHS = ComplexPairTy(CGF.EmitScalarExpr(E->getRHS()), nullptr);
808a216cad0SChandler Carruth   else
8097a51313dSChris Lattner     Ops.RHS = Visit(E->getRHS());
810a216cad0SChandler Carruth 
8117a51313dSChris Lattner   Ops.Ty = E->getType();
8127a51313dSChris Lattner   return Ops;
8137a51313dSChris Lattner }
8147a51313dSChris Lattner 
8157a51313dSChris Lattner 
81607bb1966SJohn McCall LValue ComplexExprEmitter::
81707bb1966SJohn McCall EmitCompoundAssignLValue(const CompoundAssignOperator *E,
81807bb1966SJohn McCall           ComplexPairTy (ComplexExprEmitter::*Func)(const BinOpInfo&),
819f045007fSEli Friedman                          RValue &Val) {
820df0fe27bSMike Stump   TestAndClearIgnoreReal();
821df0fe27bSMike Stump   TestAndClearIgnoreImag();
8228dfa5f17SJeffrey Yasskin   QualType LHSTy = E->getLHS()->getType();
823ce27e42dSDavid Majnemer   if (const AtomicType *AT = LHSTy->getAs<AtomicType>())
824ce27e42dSDavid Majnemer     LHSTy = AT->getValueType();
8257a51313dSChris Lattner 
826df0fe27bSMike Stump   BinOpInfo OpInfo;
827df0fe27bSMike Stump 
828df0fe27bSMike Stump   // Load the RHS and LHS operands.
829df0fe27bSMike Stump   // __block variables need to have the rhs evaluated first, plus this should
830fa8edb11SJohn McCall   // improve codegen a little.
831df0fe27bSMike Stump   OpInfo.Ty = E->getComputationResultType();
832a216cad0SChandler Carruth   QualType ComplexElementTy = cast<ComplexType>(OpInfo.Ty)->getElementType();
833fa8edb11SJohn McCall 
834fa8edb11SJohn McCall   // The RHS should have been converted to the computation type.
835a216cad0SChandler Carruth   if (E->getRHS()->getType()->isRealFloatingType()) {
836a216cad0SChandler Carruth     assert(
837a216cad0SChandler Carruth         CGF.getContext()
838a216cad0SChandler Carruth             .hasSameUnqualifiedType(ComplexElementTy, E->getRHS()->getType()));
839a216cad0SChandler Carruth     OpInfo.RHS = ComplexPairTy(CGF.EmitScalarExpr(E->getRHS()), nullptr);
840a216cad0SChandler Carruth   } else {
841a216cad0SChandler Carruth     assert(CGF.getContext()
842a216cad0SChandler Carruth                .hasSameUnqualifiedType(OpInfo.Ty, E->getRHS()->getType()));
843fa8edb11SJohn McCall     OpInfo.RHS = Visit(E->getRHS());
844a216cad0SChandler Carruth   }
845df0fe27bSMike Stump 
8468c94ffe7SDaniel Dunbar   LValue LHS = CGF.EmitLValue(E->getLHS());
847e26a872bSJohn McCall 
848f045007fSEli Friedman   // Load from the l-value and convert it.
849f045007fSEli Friedman   if (LHSTy->isAnyComplexType()) {
8502d84e842SNick Lewycky     ComplexPairTy LHSVal = EmitLoadOfLValue(LHS, E->getExprLoc());
851f045007fSEli Friedman     OpInfo.LHS = EmitComplexToComplexCast(LHSVal, LHSTy, OpInfo.Ty);
852f045007fSEli Friedman   } else {
8532d84e842SNick Lewycky     llvm::Value *LHSVal = CGF.EmitLoadOfScalar(LHS, E->getExprLoc());
854a216cad0SChandler Carruth     // For floating point real operands we can directly pass the scalar form
855a216cad0SChandler Carruth     // to the binary operator emission and potentially get more efficient code.
856a216cad0SChandler Carruth     if (LHSTy->isRealFloatingType()) {
857a216cad0SChandler Carruth       if (!CGF.getContext().hasSameUnqualifiedType(ComplexElementTy, LHSTy))
858a216cad0SChandler Carruth         LHSVal = CGF.EmitScalarConversion(LHSVal, LHSTy, ComplexElementTy);
859a216cad0SChandler Carruth       OpInfo.LHS = ComplexPairTy(LHSVal, nullptr);
860a216cad0SChandler Carruth     } else {
861f045007fSEli Friedman       OpInfo.LHS = EmitScalarToComplexCast(LHSVal, LHSTy, OpInfo.Ty);
862f045007fSEli Friedman     }
863a216cad0SChandler Carruth   }
8647a51313dSChris Lattner 
8657a51313dSChris Lattner   // Expand the binary operator.
8667a51313dSChris Lattner   ComplexPairTy Result = (this->*Func)(OpInfo);
8677a51313dSChris Lattner 
868f045007fSEli Friedman   // Truncate the result and store it into the LHS lvalue.
869f045007fSEli Friedman   if (LHSTy->isAnyComplexType()) {
870f045007fSEli Friedman     ComplexPairTy ResVal = EmitComplexToComplexCast(Result, OpInfo.Ty, LHSTy);
87166e4197fSDavid Blaikie     EmitStoreOfComplex(ResVal, LHS, /*isInit*/ false);
872f045007fSEli Friedman     Val = RValue::getComplex(ResVal);
873f045007fSEli Friedman   } else {
874f045007fSEli Friedman     llvm::Value *ResVal =
875f045007fSEli Friedman         CGF.EmitComplexToScalarConversion(Result, OpInfo.Ty, LHSTy);
876f045007fSEli Friedman     CGF.EmitStoreOfScalar(ResVal, LHS, /*isInit*/ false);
877f045007fSEli Friedman     Val = RValue::get(ResVal);
878f045007fSEli Friedman   }
8798c94ffe7SDaniel Dunbar 
88007bb1966SJohn McCall   return LHS;
8817a51313dSChris Lattner }
8827a51313dSChris Lattner 
88307bb1966SJohn McCall // Compound assignments.
88407bb1966SJohn McCall ComplexPairTy ComplexExprEmitter::
88507bb1966SJohn McCall EmitCompoundAssign(const CompoundAssignOperator *E,
88607bb1966SJohn McCall                    ComplexPairTy (ComplexExprEmitter::*Func)(const BinOpInfo&)){
887f045007fSEli Friedman   RValue Val;
88807bb1966SJohn McCall   LValue LV = EmitCompoundAssignLValue(E, Func, Val);
88907bb1966SJohn McCall 
89007bb1966SJohn McCall   // The result of an assignment in C is the assigned r-value.
8919c6890a7SRichard Smith   if (!CGF.getLangOpts().CPlusPlus)
892f045007fSEli Friedman     return Val.getComplexVal();
89307bb1966SJohn McCall 
89407bb1966SJohn McCall   // If the lvalue is non-volatile, return the computed value of the assignment.
89507bb1966SJohn McCall   if (!LV.isVolatileQualified())
896f045007fSEli Friedman     return Val.getComplexVal();
89707bb1966SJohn McCall 
8982d84e842SNick Lewycky   return EmitLoadOfLValue(LV, E->getExprLoc());
89907bb1966SJohn McCall }
90007bb1966SJohn McCall 
90107bb1966SJohn McCall LValue ComplexExprEmitter::EmitBinAssignLValue(const BinaryOperator *E,
90207bb1966SJohn McCall                                                ComplexPairTy &Val) {
903a8a089bfSDouglas Gregor   assert(CGF.getContext().hasSameUnqualifiedType(E->getLHS()->getType(),
904a8a089bfSDouglas Gregor                                                  E->getRHS()->getType()) &&
9050f398c44SChris Lattner          "Invalid assignment");
90607bb1966SJohn McCall   TestAndClearIgnoreReal();
90707bb1966SJohn McCall   TestAndClearIgnoreImag();
90807bb1966SJohn McCall 
909d0a30016SJohn McCall   // Emit the RHS.  __block variables need the RHS evaluated first.
91007bb1966SJohn McCall   Val = Visit(E->getRHS());
9117a51313dSChris Lattner 
9127a51313dSChris Lattner   // Compute the address to store into.
9137a51313dSChris Lattner   LValue LHS = CGF.EmitLValue(E->getLHS());
9147a51313dSChris Lattner 
9158c94ffe7SDaniel Dunbar   // Store the result value into the LHS lvalue.
91666e4197fSDavid Blaikie   EmitStoreOfComplex(Val, LHS, /*isInit*/ false);
91776d864c7SDaniel Dunbar 
91807bb1966SJohn McCall   return LHS;
91907bb1966SJohn McCall }
9208c94ffe7SDaniel Dunbar 
92107bb1966SJohn McCall ComplexPairTy ComplexExprEmitter::VisitBinAssign(const BinaryOperator *E) {
92207bb1966SJohn McCall   ComplexPairTy Val;
92307bb1966SJohn McCall   LValue LV = EmitBinAssignLValue(E, Val);
92407bb1966SJohn McCall 
92507bb1966SJohn McCall   // The result of an assignment in C is the assigned r-value.
9269c6890a7SRichard Smith   if (!CGF.getLangOpts().CPlusPlus)
92776d864c7SDaniel Dunbar     return Val;
92807bb1966SJohn McCall 
92907bb1966SJohn McCall   // If the lvalue is non-volatile, return the computed value of the assignment.
93007bb1966SJohn McCall   if (!LV.isVolatileQualified())
93107bb1966SJohn McCall     return Val;
93207bb1966SJohn McCall 
9332d84e842SNick Lewycky   return EmitLoadOfLValue(LV, E->getExprLoc());
93476d864c7SDaniel Dunbar }
93576d864c7SDaniel Dunbar 
9367a51313dSChris Lattner ComplexPairTy ComplexExprEmitter::VisitBinComma(const BinaryOperator *E) {
937a2342eb8SJohn McCall   CGF.EmitIgnoredExpr(E->getLHS());
9387a51313dSChris Lattner   return Visit(E->getRHS());
9397a51313dSChris Lattner }
9407a51313dSChris Lattner 
9417a51313dSChris Lattner ComplexPairTy ComplexExprEmitter::
942c07a0c7eSJohn McCall VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
943df0fe27bSMike Stump   TestAndClearIgnoreReal();
944df0fe27bSMike Stump   TestAndClearIgnoreImag();
945a612e79bSDaniel Dunbar   llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
946a612e79bSDaniel Dunbar   llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
947a612e79bSDaniel Dunbar   llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
9487a51313dSChris Lattner 
949c07a0c7eSJohn McCall   // Bind the common expression if necessary.
95048fd89adSEli Friedman   CodeGenFunction::OpaqueValueMapping binding(CGF, E);
951ce1de617SJohn McCall 
95266242d6cSJustin Bogner 
953c07a0c7eSJohn McCall   CodeGenFunction::ConditionalEvaluation eval(CGF);
95466242d6cSJustin Bogner   CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock,
95566242d6cSJustin Bogner                            CGF.getProfileCount(E));
9567a51313dSChris Lattner 
957ce1de617SJohn McCall   eval.begin(CGF);
9587a51313dSChris Lattner   CGF.EmitBlock(LHSBlock);
95966242d6cSJustin Bogner   CGF.incrementProfileCounter(E);
9608162d4adSFariborz Jahanian   ComplexPairTy LHS = Visit(E->getTrueExpr());
9617a51313dSChris Lattner   LHSBlock = Builder.GetInsertBlock();
962c56e6764SDaniel Dunbar   CGF.EmitBranch(ContBlock);
963ce1de617SJohn McCall   eval.end(CGF);
9647a51313dSChris Lattner 
965ce1de617SJohn McCall   eval.begin(CGF);
9667a51313dSChris Lattner   CGF.EmitBlock(RHSBlock);
967c07a0c7eSJohn McCall   ComplexPairTy RHS = Visit(E->getFalseExpr());
9687a51313dSChris Lattner   RHSBlock = Builder.GetInsertBlock();
9697a51313dSChris Lattner   CGF.EmitBlock(ContBlock);
970ce1de617SJohn McCall   eval.end(CGF);
9717a51313dSChris Lattner 
9727a51313dSChris Lattner   // Create a PHI node for the real part.
97320c0f02cSJay Foad   llvm::PHINode *RealPN = Builder.CreatePHI(LHS.first->getType(), 2, "cond.r");
9747a51313dSChris Lattner   RealPN->addIncoming(LHS.first, LHSBlock);
9757a51313dSChris Lattner   RealPN->addIncoming(RHS.first, RHSBlock);
9767a51313dSChris Lattner 
9777a51313dSChris Lattner   // Create a PHI node for the imaginary part.
97820c0f02cSJay Foad   llvm::PHINode *ImagPN = Builder.CreatePHI(LHS.first->getType(), 2, "cond.i");
9797a51313dSChris Lattner   ImagPN->addIncoming(LHS.second, LHSBlock);
9807a51313dSChris Lattner   ImagPN->addIncoming(RHS.second, RHSBlock);
9817a51313dSChris Lattner 
9827a51313dSChris Lattner   return ComplexPairTy(RealPN, ImagPN);
9837a51313dSChris Lattner }
9847a51313dSChris Lattner 
9857a51313dSChris Lattner ComplexPairTy ComplexExprEmitter::VisitChooseExpr(ChooseExpr *E) {
98675807f23SEli Friedman   return Visit(E->getChosenSubExpr());
9877a51313dSChris Lattner }
9887a51313dSChris Lattner 
989dd7406e6SEli Friedman ComplexPairTy ComplexExprEmitter::VisitInitListExpr(InitListExpr *E) {
990df0fe27bSMike Stump     bool Ignore = TestAndClearIgnoreReal();
991df0fe27bSMike Stump     (void)Ignore;
992df0fe27bSMike Stump     assert (Ignore == false && "init list ignored");
993df0fe27bSMike Stump     Ignore = TestAndClearIgnoreImag();
994df0fe27bSMike Stump     (void)Ignore;
995df0fe27bSMike Stump     assert (Ignore == false && "init list ignored");
9966b9c41eaSEli Friedman 
9976b9c41eaSEli Friedman   if (E->getNumInits() == 2) {
9986b9c41eaSEli Friedman     llvm::Value *Real = CGF.EmitScalarExpr(E->getInit(0));
9996b9c41eaSEli Friedman     llvm::Value *Imag = CGF.EmitScalarExpr(E->getInit(1));
10006b9c41eaSEli Friedman     return ComplexPairTy(Real, Imag);
10016b9c41eaSEli Friedman   } else if (E->getNumInits() == 1) {
1002dd7406e6SEli Friedman     return Visit(E->getInit(0));
10036b9c41eaSEli Friedman   }
1004dd7406e6SEli Friedman 
1005dd7406e6SEli Friedman   // Empty init list intializes to null
10066b9c41eaSEli Friedman   assert(E->getNumInits() == 0 && "Unexpected number of inits");
100747fb9508SJohn McCall   QualType Ty = E->getType()->castAs<ComplexType>()->getElementType();
10082192fe50SChris Lattner   llvm::Type* LTy = CGF.ConvertType(Ty);
10090b75f23bSOwen Anderson   llvm::Value* zeroConstant = llvm::Constant::getNullValue(LTy);
1010dd7406e6SEli Friedman   return ComplexPairTy(zeroConstant, zeroConstant);
1011dd7406e6SEli Friedman }
1012dd7406e6SEli Friedman 
101300079612SDaniel Dunbar ComplexPairTy ComplexExprEmitter::VisitVAArgExpr(VAArgExpr *E) {
1014e9fcadd2SDaniel Dunbar   llvm::Value *ArgValue = CGF.EmitVAListRef(E->getSubExpr());
101500079612SDaniel Dunbar   llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, E->getType());
101600079612SDaniel Dunbar 
101700079612SDaniel Dunbar   if (!ArgPtr) {
101800079612SDaniel Dunbar     CGF.ErrorUnsupported(E, "complex va_arg expression");
10192192fe50SChris Lattner     llvm::Type *EltTy =
102047fb9508SJohn McCall       CGF.ConvertType(E->getType()->castAs<ComplexType>()->getElementType());
102100079612SDaniel Dunbar     llvm::Value *U = llvm::UndefValue::get(EltTy);
102200079612SDaniel Dunbar     return ComplexPairTy(U, U);
102300079612SDaniel Dunbar   }
102400079612SDaniel Dunbar 
10252d84e842SNick Lewycky   return EmitLoadOfLValue(CGF.MakeNaturalAlignAddrLValue(ArgPtr, E->getType()),
10262d84e842SNick Lewycky                           E->getExprLoc());
102700079612SDaniel Dunbar }
102800079612SDaniel Dunbar 
10297a51313dSChris Lattner //===----------------------------------------------------------------------===//
10307a51313dSChris Lattner //                         Entry Point into this File
10317a51313dSChris Lattner //===----------------------------------------------------------------------===//
10327a51313dSChris Lattner 
10337a51313dSChris Lattner /// EmitComplexExpr - Emit the computation of the specified expression of
10347a51313dSChris Lattner /// complex type, ignoring the result.
1035df0fe27bSMike Stump ComplexPairTy CodeGenFunction::EmitComplexExpr(const Expr *E, bool IgnoreReal,
103607bb1966SJohn McCall                                                bool IgnoreImag) {
103747fb9508SJohn McCall   assert(E && getComplexType(E->getType()) &&
10387a51313dSChris Lattner          "Invalid complex expression to emit");
10397a51313dSChris Lattner 
104038b25914SDavid Blaikie   return ComplexExprEmitter(*this, IgnoreReal, IgnoreImag)
1041df0fe27bSMike Stump       .Visit(const_cast<Expr *>(E));
10427a51313dSChris Lattner }
10437a51313dSChris Lattner 
104447fb9508SJohn McCall void CodeGenFunction::EmitComplexExprIntoLValue(const Expr *E, LValue dest,
104566e4197fSDavid Blaikie                                                 bool isInit) {
104647fb9508SJohn McCall   assert(E && getComplexType(E->getType()) &&
10477a51313dSChris Lattner          "Invalid complex expression to emit");
10487a51313dSChris Lattner   ComplexExprEmitter Emitter(*this);
10497a51313dSChris Lattner   ComplexPairTy Val = Emitter.Visit(const_cast<Expr*>(E));
105066e4197fSDavid Blaikie   Emitter.EmitStoreOfComplex(Val, dest, isInit);
10517a51313dSChris Lattner }
10527a51313dSChris Lattner 
105347fb9508SJohn McCall /// EmitStoreOfComplex - Store a complex number into the specified l-value.
105447fb9508SJohn McCall void CodeGenFunction::EmitStoreOfComplex(ComplexPairTy V, LValue dest,
105566e4197fSDavid Blaikie                                          bool isInit) {
105666e4197fSDavid Blaikie   ComplexExprEmitter(*this).EmitStoreOfComplex(V, dest, isInit);
10574b8c6db9SDaniel Dunbar }
10584b8c6db9SDaniel Dunbar 
105947fb9508SJohn McCall /// EmitLoadOfComplex - Load a complex number from the specified address.
10602d84e842SNick Lewycky ComplexPairTy CodeGenFunction::EmitLoadOfComplex(LValue src,
10612d84e842SNick Lewycky                                                  SourceLocation loc) {
10622d84e842SNick Lewycky   return ComplexExprEmitter(*this).EmitLoadOfLValue(src, loc);
10637a51313dSChris Lattner }
10644f29b49dSJohn McCall 
10654f29b49dSJohn McCall LValue CodeGenFunction::EmitComplexAssignmentLValue(const BinaryOperator *E) {
1066a2342eb8SJohn McCall   assert(E->getOpcode() == BO_Assign);
10674f29b49dSJohn McCall   ComplexPairTy Val; // ignored
10684f29b49dSJohn McCall   return ComplexExprEmitter(*this).EmitBinAssignLValue(E, Val);
1069a2342eb8SJohn McCall }
10704f29b49dSJohn McCall 
1071f045007fSEli Friedman typedef ComplexPairTy (ComplexExprEmitter::*CompoundFunc)(
1072f045007fSEli Friedman     const ComplexExprEmitter::BinOpInfo &);
10734f29b49dSJohn McCall 
1074f045007fSEli Friedman static CompoundFunc getComplexOp(BinaryOperatorKind Op) {
1075f045007fSEli Friedman   switch (Op) {
1076f045007fSEli Friedman   case BO_MulAssign: return &ComplexExprEmitter::EmitBinMul;
1077f045007fSEli Friedman   case BO_DivAssign: return &ComplexExprEmitter::EmitBinDiv;
1078f045007fSEli Friedman   case BO_SubAssign: return &ComplexExprEmitter::EmitBinSub;
1079f045007fSEli Friedman   case BO_AddAssign: return &ComplexExprEmitter::EmitBinAdd;
10804f29b49dSJohn McCall   default:
10814f29b49dSJohn McCall     llvm_unreachable("unexpected complex compound assignment");
10824f29b49dSJohn McCall   }
1083f045007fSEli Friedman }
10844f29b49dSJohn McCall 
1085f045007fSEli Friedman LValue CodeGenFunction::
1086f045007fSEli Friedman EmitComplexCompoundAssignmentLValue(const CompoundAssignOperator *E) {
1087f045007fSEli Friedman   CompoundFunc Op = getComplexOp(E->getOpcode());
1088f045007fSEli Friedman   RValue Val;
1089a2342eb8SJohn McCall   return ComplexExprEmitter(*this).EmitCompoundAssignLValue(E, Op, Val);
10904f29b49dSJohn McCall }
1091f045007fSEli Friedman 
1092f045007fSEli Friedman LValue CodeGenFunction::
1093527473dfSRichard Smith EmitScalarCompoundAssignWithComplex(const CompoundAssignOperator *E,
1094f045007fSEli Friedman                                     llvm::Value *&Result) {
1095f045007fSEli Friedman   CompoundFunc Op = getComplexOp(E->getOpcode());
1096f045007fSEli Friedman   RValue Val;
1097f045007fSEli Friedman   LValue Ret = ComplexExprEmitter(*this).EmitCompoundAssignLValue(E, Op, Val);
1098f045007fSEli Friedman   Result = Val.getScalarVal();
1099f045007fSEli Friedman   return Ret;
1100f045007fSEli Friedman }
1101