Home | History | Annotate | Download | only in CodeGen
      1 //===--- CGExprComplex.cpp - Emit LLVM Code for Complex Exprs -------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This contains code to emit Expr nodes with complex types as LLVM code.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #include "CodeGenFunction.h"
     15 #include "CodeGenModule.h"
     16 #include "clang/AST/ASTContext.h"
     17 #include "clang/AST/StmtVisitor.h"
     18 #include "llvm/ADT/STLExtras.h"
     19 #include "llvm/ADT/SmallString.h"
     20 #include "llvm/IR/Constants.h"
     21 #include "llvm/IR/Function.h"
     22 #include "llvm/IR/Instructions.h"
     23 #include "llvm/IR/MDBuilder.h"
     24 #include "llvm/IR/Metadata.h"
     25 #include <algorithm>
     26 using namespace clang;
     27 using namespace CodeGen;
     28 
     29 //===----------------------------------------------------------------------===//
     30 //                        Complex Expression Emitter
     31 //===----------------------------------------------------------------------===//
     32 
     33 typedef CodeGenFunction::ComplexPairTy ComplexPairTy;
     34 
     35 /// Return the complex type that we are meant to emit.
     36 static const ComplexType *getComplexType(QualType type) {
     37   type = type.getCanonicalType();
     38   if (const ComplexType *comp = dyn_cast<ComplexType>(type)) {
     39     return comp;
     40   } else {
     41     return cast<ComplexType>(cast<AtomicType>(type)->getValueType());
     42   }
     43 }
     44 
     45 namespace  {
     46 class ComplexExprEmitter
     47   : public StmtVisitor<ComplexExprEmitter, ComplexPairTy> {
     48   CodeGenFunction &CGF;
     49   CGBuilderTy &Builder;
     50   bool IgnoreReal;
     51   bool IgnoreImag;
     52 public:
     53   ComplexExprEmitter(CodeGenFunction &cgf, bool ir=false, bool ii=false)
     54     : CGF(cgf), Builder(CGF.Builder), IgnoreReal(ir), IgnoreImag(ii) {
     55   }
     56 
     57 
     58   //===--------------------------------------------------------------------===//
     59   //                               Utilities
     60   //===--------------------------------------------------------------------===//
     61 
     62   bool TestAndClearIgnoreReal() {
     63     bool I = IgnoreReal;
     64     IgnoreReal = false;
     65     return I;
     66   }
     67   bool TestAndClearIgnoreImag() {
     68     bool I = IgnoreImag;
     69     IgnoreImag = false;
     70     return I;
     71   }
     72 
     73   /// EmitLoadOfLValue - Given an expression with complex type that represents a
     74   /// value l-value, this method emits the address of the l-value, then loads
     75   /// and returns the result.
     76   ComplexPairTy EmitLoadOfLValue(const Expr *E) {
     77     return EmitLoadOfLValue(CGF.EmitLValue(E), E->getExprLoc());
     78   }
     79 
     80   ComplexPairTy EmitLoadOfLValue(LValue LV, SourceLocation Loc);
     81 
     82   /// EmitStoreOfComplex - Store the specified real/imag parts into the
     83   /// specified value pointer.
     84   void EmitStoreOfComplex(ComplexPairTy Val, LValue LV, bool isInit);
     85 
     86   /// Emit a cast from complex value Val to DestType.
     87   ComplexPairTy EmitComplexToComplexCast(ComplexPairTy Val, QualType SrcType,
     88                                          QualType DestType, SourceLocation Loc);
     89   /// Emit a cast from scalar value Val to DestType.
     90   ComplexPairTy EmitScalarToComplexCast(llvm::Value *Val, QualType SrcType,
     91                                         QualType DestType, SourceLocation Loc);
     92 
     93   //===--------------------------------------------------------------------===//
     94   //                            Visitor Methods
     95   //===--------------------------------------------------------------------===//
     96 
     97   ComplexPairTy Visit(Expr *E) {
     98     ApplyDebugLocation DL(CGF, E);
     99     return StmtVisitor<ComplexExprEmitter, ComplexPairTy>::Visit(E);
    100   }
    101 
    102   ComplexPairTy VisitStmt(Stmt *S) {
    103     S->dump(CGF.getContext().getSourceManager());
    104     llvm_unreachable("Stmt can't have complex result type!");
    105   }
    106   ComplexPairTy VisitExpr(Expr *S);
    107   ComplexPairTy VisitParenExpr(ParenExpr *PE) { return Visit(PE->getSubExpr());}
    108   ComplexPairTy VisitGenericSelectionExpr(GenericSelectionExpr *GE) {
    109     return Visit(GE->getResultExpr());
    110   }
    111   ComplexPairTy VisitImaginaryLiteral(const ImaginaryLiteral *IL);
    112   ComplexPairTy
    113   VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *PE) {
    114     return Visit(PE->getReplacement());
    115   }
    116 
    117   // l-values.
    118   ComplexPairTy VisitDeclRefExpr(DeclRefExpr *E) {
    119     if (CodeGenFunction::ConstantEmission result = CGF.tryEmitAsConstant(E)) {
    120       if (result.isReference())
    121         return EmitLoadOfLValue(result.getReferenceLValue(CGF, E),
    122                                 E->getExprLoc());
    123 
    124       llvm::Constant *pair = result.getValue();
    125       return ComplexPairTy(pair->getAggregateElement(0U),
    126                            pair->getAggregateElement(1U));
    127     }
    128     return EmitLoadOfLValue(E);
    129   }
    130   ComplexPairTy VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
    131     return EmitLoadOfLValue(E);
    132   }
    133   ComplexPairTy VisitObjCMessageExpr(ObjCMessageExpr *E) {
    134     return CGF.EmitObjCMessageExpr(E).getComplexVal();
    135   }
    136   ComplexPairTy VisitArraySubscriptExpr(Expr *E) { return EmitLoadOfLValue(E); }
    137   ComplexPairTy VisitMemberExpr(const Expr *E) { return EmitLoadOfLValue(E); }
    138   ComplexPairTy VisitOpaqueValueExpr(OpaqueValueExpr *E) {
    139     if (E->isGLValue())
    140       return EmitLoadOfLValue(CGF.getOpaqueLValueMapping(E), E->getExprLoc());
    141     return CGF.getOpaqueRValueMapping(E).getComplexVal();
    142   }
    143 
    144   ComplexPairTy VisitPseudoObjectExpr(PseudoObjectExpr *E) {
    145     return CGF.EmitPseudoObjectRValue(E).getComplexVal();
    146   }
    147 
    148   // FIXME: CompoundLiteralExpr
    149 
    150   ComplexPairTy EmitCast(CastKind CK, Expr *Op, QualType DestTy);
    151   ComplexPairTy VisitImplicitCastExpr(ImplicitCastExpr *E) {
    152     // Unlike for scalars, we don't have to worry about function->ptr demotion
    153     // here.
    154     return EmitCast(E->getCastKind(), E->getSubExpr(), E->getType());
    155   }
    156   ComplexPairTy VisitCastExpr(CastExpr *E) {
    157     if (const auto *ECE = dyn_cast<ExplicitCastExpr>(E))
    158       CGF.CGM.EmitExplicitCastExprType(ECE, &CGF);
    159     return EmitCast(E->getCastKind(), E->getSubExpr(), E->getType());
    160   }
    161   ComplexPairTy VisitCallExpr(const CallExpr *E);
    162   ComplexPairTy VisitStmtExpr(const StmtExpr *E);
    163 
    164   // Operators.
    165   ComplexPairTy VisitPrePostIncDec(const UnaryOperator *E,
    166                                    bool isInc, bool isPre) {
    167     LValue LV = CGF.EmitLValue(E->getSubExpr());
    168     return CGF.EmitComplexPrePostIncDec(E, LV, isInc, isPre);
    169   }
    170   ComplexPairTy VisitUnaryPostDec(const UnaryOperator *E) {
    171     return VisitPrePostIncDec(E, false, false);
    172   }
    173   ComplexPairTy VisitUnaryPostInc(const UnaryOperator *E) {
    174     return VisitPrePostIncDec(E, true, false);
    175   }
    176   ComplexPairTy VisitUnaryPreDec(const UnaryOperator *E) {
    177     return VisitPrePostIncDec(E, false, true);
    178   }
    179   ComplexPairTy VisitUnaryPreInc(const UnaryOperator *E) {
    180     return VisitPrePostIncDec(E, true, true);
    181   }
    182   ComplexPairTy VisitUnaryDeref(const Expr *E) { return EmitLoadOfLValue(E); }
    183   ComplexPairTy VisitUnaryPlus     (const UnaryOperator *E) {
    184     TestAndClearIgnoreReal();
    185     TestAndClearIgnoreImag();
    186     return Visit(E->getSubExpr());
    187   }
    188   ComplexPairTy VisitUnaryMinus    (const UnaryOperator *E);
    189   ComplexPairTy VisitUnaryNot      (const UnaryOperator *E);
    190   // LNot,Real,Imag never return complex.
    191   ComplexPairTy VisitUnaryExtension(const UnaryOperator *E) {
    192     return Visit(E->getSubExpr());
    193   }
    194   ComplexPairTy VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
    195     return Visit(DAE->getExpr());
    196   }
    197   ComplexPairTy VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) {
    198     CodeGenFunction::CXXDefaultInitExprScope Scope(CGF);
    199     return Visit(DIE->getExpr());
    200   }
    201   ComplexPairTy VisitExprWithCleanups(ExprWithCleanups *E) {
    202     CGF.enterFullExpression(E);
    203     CodeGenFunction::RunCleanupsScope Scope(CGF);
    204     return Visit(E->getSubExpr());
    205   }
    206   ComplexPairTy VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
    207     assert(E->getType()->isAnyComplexType() && "Expected complex type!");
    208     QualType Elem = E->getType()->castAs<ComplexType>()->getElementType();
    209     llvm::Constant *Null = llvm::Constant::getNullValue(CGF.ConvertType(Elem));
    210     return ComplexPairTy(Null, Null);
    211   }
    212   ComplexPairTy VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
    213     assert(E->getType()->isAnyComplexType() && "Expected complex type!");
    214     QualType Elem = E->getType()->castAs<ComplexType>()->getElementType();
    215     llvm::Constant *Null =
    216                        llvm::Constant::getNullValue(CGF.ConvertType(Elem));
    217     return ComplexPairTy(Null, Null);
    218   }
    219 
    220   struct BinOpInfo {
    221     ComplexPairTy LHS;
    222     ComplexPairTy RHS;
    223     QualType Ty;  // Computation Type.
    224   };
    225 
    226   BinOpInfo EmitBinOps(const BinaryOperator *E);
    227   LValue EmitCompoundAssignLValue(const CompoundAssignOperator *E,
    228                                   ComplexPairTy (ComplexExprEmitter::*Func)
    229                                   (const BinOpInfo &),
    230                                   RValue &Val);
    231   ComplexPairTy EmitCompoundAssign(const CompoundAssignOperator *E,
    232                                    ComplexPairTy (ComplexExprEmitter::*Func)
    233                                    (const BinOpInfo &));
    234 
    235   ComplexPairTy EmitBinAdd(const BinOpInfo &Op);
    236   ComplexPairTy EmitBinSub(const BinOpInfo &Op);
    237   ComplexPairTy EmitBinMul(const BinOpInfo &Op);
    238   ComplexPairTy EmitBinDiv(const BinOpInfo &Op);
    239 
    240   ComplexPairTy EmitComplexBinOpLibCall(StringRef LibCallName,
    241                                         const BinOpInfo &Op);
    242 
    243   ComplexPairTy VisitBinAdd(const BinaryOperator *E) {
    244     return EmitBinAdd(EmitBinOps(E));
    245   }
    246   ComplexPairTy VisitBinSub(const BinaryOperator *E) {
    247     return EmitBinSub(EmitBinOps(E));
    248   }
    249   ComplexPairTy VisitBinMul(const BinaryOperator *E) {
    250     return EmitBinMul(EmitBinOps(E));
    251   }
    252   ComplexPairTy VisitBinDiv(const BinaryOperator *E) {
    253     return EmitBinDiv(EmitBinOps(E));
    254   }
    255 
    256   // Compound assignments.
    257   ComplexPairTy VisitBinAddAssign(const CompoundAssignOperator *E) {
    258     return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinAdd);
    259   }
    260   ComplexPairTy VisitBinSubAssign(const CompoundAssignOperator *E) {
    261     return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinSub);
    262   }
    263   ComplexPairTy VisitBinMulAssign(const CompoundAssignOperator *E) {
    264     return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinMul);
    265   }
    266   ComplexPairTy VisitBinDivAssign(const CompoundAssignOperator *E) {
    267     return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinDiv);
    268   }
    269 
    270   // GCC rejects rem/and/or/xor for integer complex.
    271   // Logical and/or always return int, never complex.
    272 
    273   // No comparisons produce a complex result.
    274 
    275   LValue EmitBinAssignLValue(const BinaryOperator *E,
    276                              ComplexPairTy &Val);
    277   ComplexPairTy VisitBinAssign     (const BinaryOperator *E);
    278   ComplexPairTy VisitBinComma      (const BinaryOperator *E);
    279 
    280 
    281   ComplexPairTy
    282   VisitAbstractConditionalOperator(const AbstractConditionalOperator *CO);
    283   ComplexPairTy VisitChooseExpr(ChooseExpr *CE);
    284 
    285   ComplexPairTy VisitInitListExpr(InitListExpr *E);
    286 
    287   ComplexPairTy VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
    288     return EmitLoadOfLValue(E);
    289   }
    290 
    291   ComplexPairTy VisitVAArgExpr(VAArgExpr *E);
    292 
    293   ComplexPairTy VisitAtomicExpr(AtomicExpr *E) {
    294     return CGF.EmitAtomicExpr(E).getComplexVal();
    295   }
    296 };
    297 }  // end anonymous namespace.
    298 
    299 //===----------------------------------------------------------------------===//
    300 //                                Utilities
    301 //===----------------------------------------------------------------------===//
    302 
    303 Address CodeGenFunction::emitAddrOfRealComponent(Address addr,
    304                                                  QualType complexType) {
    305   CharUnits offset = CharUnits::Zero();
    306   return Builder.CreateStructGEP(addr, 0, offset, addr.getName() + ".realp");
    307 }
    308 
    309 Address CodeGenFunction::emitAddrOfImagComponent(Address addr,
    310                                                  QualType complexType) {
    311   QualType eltType = complexType->castAs<ComplexType>()->getElementType();
    312   CharUnits offset = getContext().getTypeSizeInChars(eltType);
    313   return Builder.CreateStructGEP(addr, 1, offset, addr.getName() + ".imagp");
    314 }
    315 
    316 /// EmitLoadOfLValue - Given an RValue reference for a complex, emit code to
    317 /// load the real and imaginary pieces, returning them as Real/Imag.
    318 ComplexPairTy ComplexExprEmitter::EmitLoadOfLValue(LValue lvalue,
    319                                                    SourceLocation loc) {
    320   assert(lvalue.isSimple() && "non-simple complex l-value?");
    321   if (lvalue.getType()->isAtomicType())
    322     return CGF.EmitAtomicLoad(lvalue, loc).getComplexVal();
    323 
    324   Address SrcPtr = lvalue.getAddress();
    325   bool isVolatile = lvalue.isVolatileQualified();
    326 
    327   llvm::Value *Real = nullptr, *Imag = nullptr;
    328 
    329   if (!IgnoreReal || isVolatile) {
    330     Address RealP = CGF.emitAddrOfRealComponent(SrcPtr, lvalue.getType());
    331     Real = Builder.CreateLoad(RealP, isVolatile, SrcPtr.getName() + ".real");
    332   }
    333 
    334   if (!IgnoreImag || isVolatile) {
    335     Address ImagP = CGF.emitAddrOfImagComponent(SrcPtr, lvalue.getType());
    336     Imag = Builder.CreateLoad(ImagP, isVolatile, SrcPtr.getName() + ".imag");
    337   }
    338 
    339   return ComplexPairTy(Real, Imag);
    340 }
    341 
    342 /// EmitStoreOfComplex - Store the specified real/imag parts into the
    343 /// specified value pointer.
    344 void ComplexExprEmitter::EmitStoreOfComplex(ComplexPairTy Val, LValue lvalue,
    345                                             bool isInit) {
    346   if (lvalue.getType()->isAtomicType() ||
    347       (!isInit && CGF.LValueIsSuitableForInlineAtomic(lvalue)))
    348     return CGF.EmitAtomicStore(RValue::getComplex(Val), lvalue, isInit);
    349 
    350   Address Ptr = lvalue.getAddress();
    351   Address RealPtr = CGF.emitAddrOfRealComponent(Ptr, lvalue.getType());
    352   Address ImagPtr = CGF.emitAddrOfImagComponent(Ptr, lvalue.getType());
    353 
    354   Builder.CreateStore(Val.first, RealPtr, lvalue.isVolatileQualified());
    355   Builder.CreateStore(Val.second, ImagPtr, lvalue.isVolatileQualified());
    356 }
    357 
    358 
    359 
    360 //===----------------------------------------------------------------------===//
    361 //                            Visitor Methods
    362 //===----------------------------------------------------------------------===//
    363 
    364 ComplexPairTy ComplexExprEmitter::VisitExpr(Expr *E) {
    365   CGF.ErrorUnsupported(E, "complex expression");
    366   llvm::Type *EltTy =
    367     CGF.ConvertType(getComplexType(E->getType())->getElementType());
    368   llvm::Value *U = llvm::UndefValue::get(EltTy);
    369   return ComplexPairTy(U, U);
    370 }
    371 
    372 ComplexPairTy ComplexExprEmitter::
    373 VisitImaginaryLiteral(const ImaginaryLiteral *IL) {
    374   llvm::Value *Imag = CGF.EmitScalarExpr(IL->getSubExpr());
    375   return ComplexPairTy(llvm::Constant::getNullValue(Imag->getType()), Imag);
    376 }
    377 
    378 
    379 ComplexPairTy ComplexExprEmitter::VisitCallExpr(const CallExpr *E) {
    380   if (E->getCallReturnType(CGF.getContext())->isReferenceType())
    381     return EmitLoadOfLValue(E);
    382 
    383   return CGF.EmitCallExpr(E).getComplexVal();
    384 }
    385 
    386 ComplexPairTy ComplexExprEmitter::VisitStmtExpr(const StmtExpr *E) {
    387   CodeGenFunction::StmtExprEvaluation eval(CGF);
    388   Address RetAlloca = CGF.EmitCompoundStmt(*E->getSubStmt(), true);
    389   assert(RetAlloca.isValid() && "Expected complex return value");
    390   return EmitLoadOfLValue(CGF.MakeAddrLValue(RetAlloca, E->getType()),
    391                           E->getExprLoc());
    392 }
    393 
    394 /// Emit a cast from complex value Val to DestType.
    395 ComplexPairTy ComplexExprEmitter::EmitComplexToComplexCast(ComplexPairTy Val,
    396                                                            QualType SrcType,
    397                                                            QualType DestType,
    398                                                            SourceLocation Loc) {
    399   // Get the src/dest element type.
    400   SrcType = SrcType->castAs<ComplexType>()->getElementType();
    401   DestType = DestType->castAs<ComplexType>()->getElementType();
    402 
    403   // C99 6.3.1.6: When a value of complex type is converted to another
    404   // complex type, both the real and imaginary parts follow the conversion
    405   // rules for the corresponding real types.
    406   Val.first = CGF.EmitScalarConversion(Val.first, SrcType, DestType, Loc);
    407   Val.second = CGF.EmitScalarConversion(Val.second, SrcType, DestType, Loc);
    408   return Val;
    409 }
    410 
    411 ComplexPairTy ComplexExprEmitter::EmitScalarToComplexCast(llvm::Value *Val,
    412                                                           QualType SrcType,
    413                                                           QualType DestType,
    414                                                           SourceLocation Loc) {
    415   // Convert the input element to the element type of the complex.
    416   DestType = DestType->castAs<ComplexType>()->getElementType();
    417   Val = CGF.EmitScalarConversion(Val, SrcType, DestType, Loc);
    418 
    419   // Return (realval, 0).
    420   return ComplexPairTy(Val, llvm::Constant::getNullValue(Val->getType()));
    421 }
    422 
    423 ComplexPairTy ComplexExprEmitter::EmitCast(CastKind CK, Expr *Op,
    424                                            QualType DestTy) {
    425   switch (CK) {
    426   case CK_Dependent: llvm_unreachable("dependent cast kind in IR gen!");
    427 
    428   // Atomic to non-atomic casts may be more than a no-op for some platforms and
    429   // for some types.
    430   case CK_AtomicToNonAtomic:
    431   case CK_NonAtomicToAtomic:
    432   case CK_NoOp:
    433   case CK_LValueToRValue:
    434   case CK_UserDefinedConversion:
    435     return Visit(Op);
    436 
    437   case CK_LValueBitCast: {
    438     LValue origLV = CGF.EmitLValue(Op);
    439     Address V = origLV.getAddress();
    440     V = Builder.CreateElementBitCast(V, CGF.ConvertType(DestTy));
    441     return EmitLoadOfLValue(CGF.MakeAddrLValue(V, DestTy), Op->getExprLoc());
    442   }
    443 
    444   case CK_BitCast:
    445   case CK_BaseToDerived:
    446   case CK_DerivedToBase:
    447   case CK_UncheckedDerivedToBase:
    448   case CK_Dynamic:
    449   case CK_ToUnion:
    450   case CK_ArrayToPointerDecay:
    451   case CK_FunctionToPointerDecay:
    452   case CK_NullToPointer:
    453   case CK_NullToMemberPointer:
    454   case CK_BaseToDerivedMemberPointer:
    455   case CK_DerivedToBaseMemberPointer:
    456   case CK_MemberPointerToBoolean:
    457   case CK_ReinterpretMemberPointer:
    458   case CK_ConstructorConversion:
    459   case CK_IntegralToPointer:
    460   case CK_PointerToIntegral:
    461   case CK_PointerToBoolean:
    462   case CK_ToVoid:
    463   case CK_VectorSplat:
    464   case CK_IntegralCast:
    465   case CK_BooleanToSignedIntegral:
    466   case CK_IntegralToBoolean:
    467   case CK_IntegralToFloating:
    468   case CK_FloatingToIntegral:
    469   case CK_FloatingToBoolean:
    470   case CK_FloatingCast:
    471   case CK_CPointerToObjCPointerCast:
    472   case CK_BlockPointerToObjCPointerCast:
    473   case CK_AnyPointerToBlockPointerCast:
    474   case CK_ObjCObjectLValueCast:
    475   case CK_FloatingComplexToReal:
    476   case CK_FloatingComplexToBoolean:
    477   case CK_IntegralComplexToReal:
    478   case CK_IntegralComplexToBoolean:
    479   case CK_ARCProduceObject:
    480   case CK_ARCConsumeObject:
    481   case CK_ARCReclaimReturnedObject:
    482   case CK_ARCExtendBlockObject:
    483   case CK_CopyAndAutoreleaseBlockObject:
    484   case CK_BuiltinFnToFnPtr:
    485   case CK_ZeroToOCLEvent:
    486   case CK_AddressSpaceConversion:
    487     llvm_unreachable("invalid cast kind for complex value");
    488 
    489   case CK_FloatingRealToComplex:
    490   case CK_IntegralRealToComplex:
    491     return EmitScalarToComplexCast(CGF.EmitScalarExpr(Op), Op->getType(),
    492                                    DestTy, Op->getExprLoc());
    493 
    494   case CK_FloatingComplexCast:
    495   case CK_FloatingComplexToIntegralComplex:
    496   case CK_IntegralComplexCast:
    497   case CK_IntegralComplexToFloatingComplex:
    498     return EmitComplexToComplexCast(Visit(Op), Op->getType(), DestTy,
    499                                     Op->getExprLoc());
    500   }
    501 
    502   llvm_unreachable("unknown cast resulting in complex value");
    503 }
    504 
    505 ComplexPairTy ComplexExprEmitter::VisitUnaryMinus(const UnaryOperator *E) {
    506   TestAndClearIgnoreReal();
    507   TestAndClearIgnoreImag();
    508   ComplexPairTy Op = Visit(E->getSubExpr());
    509 
    510   llvm::Value *ResR, *ResI;
    511   if (Op.first->getType()->isFloatingPointTy()) {
    512     ResR = Builder.CreateFNeg(Op.first,  "neg.r");
    513     ResI = Builder.CreateFNeg(Op.second, "neg.i");
    514   } else {
    515     ResR = Builder.CreateNeg(Op.first,  "neg.r");
    516     ResI = Builder.CreateNeg(Op.second, "neg.i");
    517   }
    518   return ComplexPairTy(ResR, ResI);
    519 }
    520 
    521 ComplexPairTy ComplexExprEmitter::VisitUnaryNot(const UnaryOperator *E) {
    522   TestAndClearIgnoreReal();
    523   TestAndClearIgnoreImag();
    524   // ~(a+ib) = a + i*-b
    525   ComplexPairTy Op = Visit(E->getSubExpr());
    526   llvm::Value *ResI;
    527   if (Op.second->getType()->isFloatingPointTy())
    528     ResI = Builder.CreateFNeg(Op.second, "conj.i");
    529   else
    530     ResI = Builder.CreateNeg(Op.second, "conj.i");
    531 
    532   return ComplexPairTy(Op.first, ResI);
    533 }
    534 
    535 ComplexPairTy ComplexExprEmitter::EmitBinAdd(const BinOpInfo &Op) {
    536   llvm::Value *ResR, *ResI;
    537 
    538   if (Op.LHS.first->getType()->isFloatingPointTy()) {
    539     ResR = Builder.CreateFAdd(Op.LHS.first,  Op.RHS.first,  "add.r");
    540     if (Op.LHS.second && Op.RHS.second)
    541       ResI = Builder.CreateFAdd(Op.LHS.second, Op.RHS.second, "add.i");
    542     else
    543       ResI = Op.LHS.second ? Op.LHS.second : Op.RHS.second;
    544     assert(ResI && "Only one operand may be real!");
    545   } else {
    546     ResR = Builder.CreateAdd(Op.LHS.first,  Op.RHS.first,  "add.r");
    547     assert(Op.LHS.second && Op.RHS.second &&
    548            "Both operands of integer complex operators must be complex!");
    549     ResI = Builder.CreateAdd(Op.LHS.second, Op.RHS.second, "add.i");
    550   }
    551   return ComplexPairTy(ResR, ResI);
    552 }
    553 
    554 ComplexPairTy ComplexExprEmitter::EmitBinSub(const BinOpInfo &Op) {
    555   llvm::Value *ResR, *ResI;
    556   if (Op.LHS.first->getType()->isFloatingPointTy()) {
    557     ResR = Builder.CreateFSub(Op.LHS.first, Op.RHS.first, "sub.r");
    558     if (Op.LHS.second && Op.RHS.second)
    559       ResI = Builder.CreateFSub(Op.LHS.second, Op.RHS.second, "sub.i");
    560     else
    561       ResI = Op.LHS.second ? Op.LHS.second
    562                            : Builder.CreateFNeg(Op.RHS.second, "sub.i");
    563     assert(ResI && "Only one operand may be real!");
    564   } else {
    565     ResR = Builder.CreateSub(Op.LHS.first, Op.RHS.first, "sub.r");
    566     assert(Op.LHS.second && Op.RHS.second &&
    567            "Both operands of integer complex operators must be complex!");
    568     ResI = Builder.CreateSub(Op.LHS.second, Op.RHS.second, "sub.i");
    569   }
    570   return ComplexPairTy(ResR, ResI);
    571 }
    572 
    573 /// \brief Emit a libcall for a binary operation on complex types.
    574 ComplexPairTy ComplexExprEmitter::EmitComplexBinOpLibCall(StringRef LibCallName,
    575                                                           const BinOpInfo &Op) {
    576   CallArgList Args;
    577   Args.add(RValue::get(Op.LHS.first),
    578            Op.Ty->castAs<ComplexType>()->getElementType());
    579   Args.add(RValue::get(Op.LHS.second),
    580            Op.Ty->castAs<ComplexType>()->getElementType());
    581   Args.add(RValue::get(Op.RHS.first),
    582            Op.Ty->castAs<ComplexType>()->getElementType());
    583   Args.add(RValue::get(Op.RHS.second),
    584            Op.Ty->castAs<ComplexType>()->getElementType());
    585 
    586   // We *must* use the full CG function call building logic here because the
    587   // complex type has special ABI handling. We also should not forget about
    588   // special calling convention which may be used for compiler builtins.
    589 
    590   // We create a function qualified type to state that this call does not have
    591   // any exceptions.
    592   FunctionProtoType::ExtProtoInfo EPI;
    593   EPI = EPI.withExceptionSpec(
    594       FunctionProtoType::ExceptionSpecInfo(EST_BasicNoexcept));
    595   SmallVector<QualType, 4> ArgsQTys(
    596       4, Op.Ty->castAs<ComplexType>()->getElementType());
    597   QualType FQTy = CGF.getContext().getFunctionType(Op.Ty, ArgsQTys, EPI);
    598   const CGFunctionInfo &FuncInfo = CGF.CGM.getTypes().arrangeFreeFunctionCall(
    599       Args, cast<FunctionType>(FQTy.getTypePtr()), false);
    600 
    601   llvm::FunctionType *FTy = CGF.CGM.getTypes().GetFunctionType(FuncInfo);
    602   llvm::Constant *Func = CGF.CGM.CreateBuiltinFunction(FTy, LibCallName);
    603   llvm::Instruction *Call;
    604 
    605   RValue Res = CGF.EmitCall(FuncInfo, Func, ReturnValueSlot(), Args,
    606                             FQTy->getAs<FunctionProtoType>(), &Call);
    607   cast<llvm::CallInst>(Call)->setCallingConv(CGF.CGM.getBuiltinCC());
    608   return Res.getComplexVal();
    609 }
    610 
    611 /// \brief Lookup the libcall name for a given floating point type complex
    612 /// multiply.
    613 static StringRef getComplexMultiplyLibCallName(llvm::Type *Ty) {
    614   switch (Ty->getTypeID()) {
    615   default:
    616     llvm_unreachable("Unsupported floating point type!");
    617   case llvm::Type::HalfTyID:
    618     return "__mulhc3";
    619   case llvm::Type::FloatTyID:
    620     return "__mulsc3";
    621   case llvm::Type::DoubleTyID:
    622     return "__muldc3";
    623   case llvm::Type::PPC_FP128TyID:
    624     return "__multc3";
    625   case llvm::Type::X86_FP80TyID:
    626     return "__mulxc3";
    627   case llvm::Type::FP128TyID:
    628     return "__multc3";
    629   }
    630 }
    631 
    632 // See C11 Annex G.5.1 for the semantics of multiplicative operators on complex
    633 // typed values.
    634 ComplexPairTy ComplexExprEmitter::EmitBinMul(const BinOpInfo &Op) {
    635   using llvm::Value;
    636   Value *ResR, *ResI;
    637   llvm::MDBuilder MDHelper(CGF.getLLVMContext());
    638 
    639   if (Op.LHS.first->getType()->isFloatingPointTy()) {
    640     // The general formulation is:
    641     // (a + ib) * (c + id) = (a * c - b * d) + i(a * d + b * c)
    642     //
    643     // But we can fold away components which would be zero due to a real
    644     // operand according to C11 Annex G.5.1p2.
    645     // FIXME: C11 also provides for imaginary types which would allow folding
    646     // still more of this within the type system.
    647 
    648     if (Op.LHS.second && Op.RHS.second) {
    649       // If both operands are complex, emit the core math directly, and then
    650       // test for NaNs. If we find NaNs in the result, we delegate to a libcall
    651       // to carefully re-compute the correct infinity representation if
    652       // possible. The expectation is that the presence of NaNs here is
    653       // *extremely* rare, and so the cost of the libcall is almost irrelevant.
    654       // This is good, because the libcall re-computes the core multiplication
    655       // exactly the same as we do here and re-tests for NaNs in order to be
    656       // a generic complex*complex libcall.
    657 
    658       // First compute the four products.
    659       Value *AC = Builder.CreateFMul(Op.LHS.first, Op.RHS.first, "mul_ac");
    660       Value *BD = Builder.CreateFMul(Op.LHS.second, Op.RHS.second, "mul_bd");
    661       Value *AD = Builder.CreateFMul(Op.LHS.first, Op.RHS.second, "mul_ad");
    662       Value *BC = Builder.CreateFMul(Op.LHS.second, Op.RHS.first, "mul_bc");
    663 
    664       // The real part is the difference of the first two, the imaginary part is
    665       // the sum of the second.
    666       ResR = Builder.CreateFSub(AC, BD, "mul_r");
    667       ResI = Builder.CreateFAdd(AD, BC, "mul_i");
    668 
    669       // Emit the test for the real part becoming NaN and create a branch to
    670       // handle it. We test for NaN by comparing the number to itself.
    671       Value *IsRNaN = Builder.CreateFCmpUNO(ResR, ResR, "isnan_cmp");
    672       llvm::BasicBlock *ContBB = CGF.createBasicBlock("complex_mul_cont");
    673       llvm::BasicBlock *INaNBB = CGF.createBasicBlock("complex_mul_imag_nan");
    674       llvm::Instruction *Branch = Builder.CreateCondBr(IsRNaN, INaNBB, ContBB);
    675       llvm::BasicBlock *OrigBB = Branch->getParent();
    676 
    677       // Give hint that we very much don't expect to see NaNs.
    678       // Value chosen to match UR_NONTAKEN_WEIGHT, see BranchProbabilityInfo.cpp
    679       llvm::MDNode *BrWeight = MDHelper.createBranchWeights(1, (1U << 20) - 1);
    680       Branch->setMetadata(llvm::LLVMContext::MD_prof, BrWeight);
    681 
    682       // Now test the imaginary part and create its branch.
    683       CGF.EmitBlock(INaNBB);
    684       Value *IsINaN = Builder.CreateFCmpUNO(ResI, ResI, "isnan_cmp");
    685       llvm::BasicBlock *LibCallBB = CGF.createBasicBlock("complex_mul_libcall");
    686       Branch = Builder.CreateCondBr(IsINaN, LibCallBB, ContBB);
    687       Branch->setMetadata(llvm::LLVMContext::MD_prof, BrWeight);
    688 
    689       // Now emit the libcall on this slowest of the slow paths.
    690       CGF.EmitBlock(LibCallBB);
    691       Value *LibCallR, *LibCallI;
    692       std::tie(LibCallR, LibCallI) = EmitComplexBinOpLibCall(
    693           getComplexMultiplyLibCallName(Op.LHS.first->getType()), Op);
    694       Builder.CreateBr(ContBB);
    695 
    696       // Finally continue execution by phi-ing together the different
    697       // computation paths.
    698       CGF.EmitBlock(ContBB);
    699       llvm::PHINode *RealPHI = Builder.CreatePHI(ResR->getType(), 3, "real_mul_phi");
    700       RealPHI->addIncoming(ResR, OrigBB);
    701       RealPHI->addIncoming(ResR, INaNBB);
    702       RealPHI->addIncoming(LibCallR, LibCallBB);
    703       llvm::PHINode *ImagPHI = Builder.CreatePHI(ResI->getType(), 3, "imag_mul_phi");
    704       ImagPHI->addIncoming(ResI, OrigBB);
    705       ImagPHI->addIncoming(ResI, INaNBB);
    706       ImagPHI->addIncoming(LibCallI, LibCallBB);
    707       return ComplexPairTy(RealPHI, ImagPHI);
    708     }
    709     assert((Op.LHS.second || Op.RHS.second) &&
    710            "At least one operand must be complex!");
    711 
    712     // If either of the operands is a real rather than a complex, the
    713     // imaginary component is ignored when computing the real component of the
    714     // result.
    715     ResR = Builder.CreateFMul(Op.LHS.first, Op.RHS.first, "mul.rl");
    716 
    717     ResI = Op.LHS.second
    718                ? Builder.CreateFMul(Op.LHS.second, Op.RHS.first, "mul.il")
    719                : Builder.CreateFMul(Op.LHS.first, Op.RHS.second, "mul.ir");
    720   } else {
    721     assert(Op.LHS.second && Op.RHS.second &&
    722            "Both operands of integer complex operators must be complex!");
    723     Value *ResRl = Builder.CreateMul(Op.LHS.first, Op.RHS.first, "mul.rl");
    724     Value *ResRr = Builder.CreateMul(Op.LHS.second, Op.RHS.second, "mul.rr");
    725     ResR = Builder.CreateSub(ResRl, ResRr, "mul.r");
    726 
    727     Value *ResIl = Builder.CreateMul(Op.LHS.second, Op.RHS.first, "mul.il");
    728     Value *ResIr = Builder.CreateMul(Op.LHS.first, Op.RHS.second, "mul.ir");
    729     ResI = Builder.CreateAdd(ResIl, ResIr, "mul.i");
    730   }
    731   return ComplexPairTy(ResR, ResI);
    732 }
    733 
    734 // See C11 Annex G.5.1 for the semantics of multiplicative operators on complex
    735 // typed values.
    736 ComplexPairTy ComplexExprEmitter::EmitBinDiv(const BinOpInfo &Op) {
    737   llvm::Value *LHSr = Op.LHS.first, *LHSi = Op.LHS.second;
    738   llvm::Value *RHSr = Op.RHS.first, *RHSi = Op.RHS.second;
    739 
    740 
    741   llvm::Value *DSTr, *DSTi;
    742   if (LHSr->getType()->isFloatingPointTy()) {
    743     // If we have a complex operand on the RHS, we delegate to a libcall to
    744     // handle all of the complexities and minimize underflow/overflow cases.
    745     //
    746     // FIXME: We would be able to avoid the libcall in many places if we
    747     // supported imaginary types in addition to complex types.
    748     if (RHSi) {
    749       BinOpInfo LibCallOp = Op;
    750       // If LHS was a real, supply a null imaginary part.
    751       if (!LHSi)
    752         LibCallOp.LHS.second = llvm::Constant::getNullValue(LHSr->getType());
    753 
    754       StringRef LibCallName;
    755       switch (LHSr->getType()->getTypeID()) {
    756       default:
    757         llvm_unreachable("Unsupported floating point type!");
    758       case llvm::Type::HalfTyID:
    759         return EmitComplexBinOpLibCall("__divhc3", LibCallOp);
    760       case llvm::Type::FloatTyID:
    761         return EmitComplexBinOpLibCall("__divsc3", LibCallOp);
    762       case llvm::Type::DoubleTyID:
    763         return EmitComplexBinOpLibCall("__divdc3", LibCallOp);
    764       case llvm::Type::PPC_FP128TyID:
    765         return EmitComplexBinOpLibCall("__divtc3", LibCallOp);
    766       case llvm::Type::X86_FP80TyID:
    767         return EmitComplexBinOpLibCall("__divxc3", LibCallOp);
    768       case llvm::Type::FP128TyID:
    769         return EmitComplexBinOpLibCall("__divtc3", LibCallOp);
    770       }
    771     }
    772     assert(LHSi && "Can have at most one non-complex operand!");
    773 
    774     DSTr = Builder.CreateFDiv(LHSr, RHSr);
    775     DSTi = Builder.CreateFDiv(LHSi, RHSr);
    776   } else {
    777     assert(Op.LHS.second && Op.RHS.second &&
    778            "Both operands of integer complex operators must be complex!");
    779     // (a+ib) / (c+id) = ((ac+bd)/(cc+dd)) + i((bc-ad)/(cc+dd))
    780     llvm::Value *Tmp1 = Builder.CreateMul(LHSr, RHSr); // a*c
    781     llvm::Value *Tmp2 = Builder.CreateMul(LHSi, RHSi); // b*d
    782     llvm::Value *Tmp3 = Builder.CreateAdd(Tmp1, Tmp2); // ac+bd
    783 
    784     llvm::Value *Tmp4 = Builder.CreateMul(RHSr, RHSr); // c*c
    785     llvm::Value *Tmp5 = Builder.CreateMul(RHSi, RHSi); // d*d
    786     llvm::Value *Tmp6 = Builder.CreateAdd(Tmp4, Tmp5); // cc+dd
    787 
    788     llvm::Value *Tmp7 = Builder.CreateMul(LHSi, RHSr); // b*c
    789     llvm::Value *Tmp8 = Builder.CreateMul(LHSr, RHSi); // a*d
    790     llvm::Value *Tmp9 = Builder.CreateSub(Tmp7, Tmp8); // bc-ad
    791 
    792     if (Op.Ty->castAs<ComplexType>()->getElementType()->isUnsignedIntegerType()) {
    793       DSTr = Builder.CreateUDiv(Tmp3, Tmp6);
    794       DSTi = Builder.CreateUDiv(Tmp9, Tmp6);
    795     } else {
    796       DSTr = Builder.CreateSDiv(Tmp3, Tmp6);
    797       DSTi = Builder.CreateSDiv(Tmp9, Tmp6);
    798     }
    799   }
    800 
    801   return ComplexPairTy(DSTr, DSTi);
    802 }
    803 
    804 ComplexExprEmitter::BinOpInfo
    805 ComplexExprEmitter::EmitBinOps(const BinaryOperator *E) {
    806   TestAndClearIgnoreReal();
    807   TestAndClearIgnoreImag();
    808   BinOpInfo Ops;
    809   if (E->getLHS()->getType()->isRealFloatingType())
    810     Ops.LHS = ComplexPairTy(CGF.EmitScalarExpr(E->getLHS()), nullptr);
    811   else
    812     Ops.LHS = Visit(E->getLHS());
    813   if (E->getRHS()->getType()->isRealFloatingType())
    814     Ops.RHS = ComplexPairTy(CGF.EmitScalarExpr(E->getRHS()), nullptr);
    815   else
    816     Ops.RHS = Visit(E->getRHS());
    817 
    818   Ops.Ty = E->getType();
    819   return Ops;
    820 }
    821 
    822 
    823 LValue ComplexExprEmitter::
    824 EmitCompoundAssignLValue(const CompoundAssignOperator *E,
    825           ComplexPairTy (ComplexExprEmitter::*Func)(const BinOpInfo&),
    826                          RValue &Val) {
    827   TestAndClearIgnoreReal();
    828   TestAndClearIgnoreImag();
    829   QualType LHSTy = E->getLHS()->getType();
    830   if (const AtomicType *AT = LHSTy->getAs<AtomicType>())
    831     LHSTy = AT->getValueType();
    832 
    833   BinOpInfo OpInfo;
    834 
    835   // Load the RHS and LHS operands.
    836   // __block variables need to have the rhs evaluated first, plus this should
    837   // improve codegen a little.
    838   OpInfo.Ty = E->getComputationResultType();
    839   QualType ComplexElementTy = cast<ComplexType>(OpInfo.Ty)->getElementType();
    840 
    841   // The RHS should have been converted to the computation type.
    842   if (E->getRHS()->getType()->isRealFloatingType()) {
    843     assert(
    844         CGF.getContext()
    845             .hasSameUnqualifiedType(ComplexElementTy, E->getRHS()->getType()));
    846     OpInfo.RHS = ComplexPairTy(CGF.EmitScalarExpr(E->getRHS()), nullptr);
    847   } else {
    848     assert(CGF.getContext()
    849                .hasSameUnqualifiedType(OpInfo.Ty, E->getRHS()->getType()));
    850     OpInfo.RHS = Visit(E->getRHS());
    851   }
    852 
    853   LValue LHS = CGF.EmitLValue(E->getLHS());
    854 
    855   // Load from the l-value and convert it.
    856   SourceLocation Loc = E->getExprLoc();
    857   if (LHSTy->isAnyComplexType()) {
    858     ComplexPairTy LHSVal = EmitLoadOfLValue(LHS, Loc);
    859     OpInfo.LHS = EmitComplexToComplexCast(LHSVal, LHSTy, OpInfo.Ty, Loc);
    860   } else {
    861     llvm::Value *LHSVal = CGF.EmitLoadOfScalar(LHS, Loc);
    862     // For floating point real operands we can directly pass the scalar form
    863     // to the binary operator emission and potentially get more efficient code.
    864     if (LHSTy->isRealFloatingType()) {
    865       if (!CGF.getContext().hasSameUnqualifiedType(ComplexElementTy, LHSTy))
    866         LHSVal = CGF.EmitScalarConversion(LHSVal, LHSTy, ComplexElementTy, Loc);
    867       OpInfo.LHS = ComplexPairTy(LHSVal, nullptr);
    868     } else {
    869       OpInfo.LHS = EmitScalarToComplexCast(LHSVal, LHSTy, OpInfo.Ty, Loc);
    870     }
    871   }
    872 
    873   // Expand the binary operator.
    874   ComplexPairTy Result = (this->*Func)(OpInfo);
    875 
    876   // Truncate the result and store it into the LHS lvalue.
    877   if (LHSTy->isAnyComplexType()) {
    878     ComplexPairTy ResVal =
    879         EmitComplexToComplexCast(Result, OpInfo.Ty, LHSTy, Loc);
    880     EmitStoreOfComplex(ResVal, LHS, /*isInit*/ false);
    881     Val = RValue::getComplex(ResVal);
    882   } else {
    883     llvm::Value *ResVal =
    884         CGF.EmitComplexToScalarConversion(Result, OpInfo.Ty, LHSTy, Loc);
    885     CGF.EmitStoreOfScalar(ResVal, LHS, /*isInit*/ false);
    886     Val = RValue::get(ResVal);
    887   }
    888 
    889   return LHS;
    890 }
    891 
    892 // Compound assignments.
    893 ComplexPairTy ComplexExprEmitter::
    894 EmitCompoundAssign(const CompoundAssignOperator *E,
    895                    ComplexPairTy (ComplexExprEmitter::*Func)(const BinOpInfo&)){
    896   RValue Val;
    897   LValue LV = EmitCompoundAssignLValue(E, Func, Val);
    898 
    899   // The result of an assignment in C is the assigned r-value.
    900   if (!CGF.getLangOpts().CPlusPlus)
    901     return Val.getComplexVal();
    902 
    903   // If the lvalue is non-volatile, return the computed value of the assignment.
    904   if (!LV.isVolatileQualified())
    905     return Val.getComplexVal();
    906 
    907   return EmitLoadOfLValue(LV, E->getExprLoc());
    908 }
    909 
    910 LValue ComplexExprEmitter::EmitBinAssignLValue(const BinaryOperator *E,
    911                                                ComplexPairTy &Val) {
    912   assert(CGF.getContext().hasSameUnqualifiedType(E->getLHS()->getType(),
    913                                                  E->getRHS()->getType()) &&
    914          "Invalid assignment");
    915   TestAndClearIgnoreReal();
    916   TestAndClearIgnoreImag();
    917 
    918   // Emit the RHS.  __block variables need the RHS evaluated first.
    919   Val = Visit(E->getRHS());
    920 
    921   // Compute the address to store into.
    922   LValue LHS = CGF.EmitLValue(E->getLHS());
    923 
    924   // Store the result value into the LHS lvalue.
    925   EmitStoreOfComplex(Val, LHS, /*isInit*/ false);
    926 
    927   return LHS;
    928 }
    929 
    930 ComplexPairTy ComplexExprEmitter::VisitBinAssign(const BinaryOperator *E) {
    931   ComplexPairTy Val;
    932   LValue LV = EmitBinAssignLValue(E, Val);
    933 
    934   // The result of an assignment in C is the assigned r-value.
    935   if (!CGF.getLangOpts().CPlusPlus)
    936     return Val;
    937 
    938   // If the lvalue is non-volatile, return the computed value of the assignment.
    939   if (!LV.isVolatileQualified())
    940     return Val;
    941 
    942   return EmitLoadOfLValue(LV, E->getExprLoc());
    943 }
    944 
    945 ComplexPairTy ComplexExprEmitter::VisitBinComma(const BinaryOperator *E) {
    946   CGF.EmitIgnoredExpr(E->getLHS());
    947   return Visit(E->getRHS());
    948 }
    949 
    950 ComplexPairTy ComplexExprEmitter::
    951 VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
    952   TestAndClearIgnoreReal();
    953   TestAndClearIgnoreImag();
    954   llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
    955   llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
    956   llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
    957 
    958   // Bind the common expression if necessary.
    959   CodeGenFunction::OpaqueValueMapping binding(CGF, E);
    960 
    961 
    962   CodeGenFunction::ConditionalEvaluation eval(CGF);
    963   CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock,
    964                            CGF.getProfileCount(E));
    965 
    966   eval.begin(CGF);
    967   CGF.EmitBlock(LHSBlock);
    968   CGF.incrementProfileCounter(E);
    969   ComplexPairTy LHS = Visit(E->getTrueExpr());
    970   LHSBlock = Builder.GetInsertBlock();
    971   CGF.EmitBranch(ContBlock);
    972   eval.end(CGF);
    973 
    974   eval.begin(CGF);
    975   CGF.EmitBlock(RHSBlock);
    976   ComplexPairTy RHS = Visit(E->getFalseExpr());
    977   RHSBlock = Builder.GetInsertBlock();
    978   CGF.EmitBlock(ContBlock);
    979   eval.end(CGF);
    980 
    981   // Create a PHI node for the real part.
    982   llvm::PHINode *RealPN = Builder.CreatePHI(LHS.first->getType(), 2, "cond.r");
    983   RealPN->addIncoming(LHS.first, LHSBlock);
    984   RealPN->addIncoming(RHS.first, RHSBlock);
    985 
    986   // Create a PHI node for the imaginary part.
    987   llvm::PHINode *ImagPN = Builder.CreatePHI(LHS.first->getType(), 2, "cond.i");
    988   ImagPN->addIncoming(LHS.second, LHSBlock);
    989   ImagPN->addIncoming(RHS.second, RHSBlock);
    990 
    991   return ComplexPairTy(RealPN, ImagPN);
    992 }
    993 
    994 ComplexPairTy ComplexExprEmitter::VisitChooseExpr(ChooseExpr *E) {
    995   return Visit(E->getChosenSubExpr());
    996 }
    997 
    998 ComplexPairTy ComplexExprEmitter::VisitInitListExpr(InitListExpr *E) {
    999     bool Ignore = TestAndClearIgnoreReal();
   1000     (void)Ignore;
   1001     assert (Ignore == false && "init list ignored");
   1002     Ignore = TestAndClearIgnoreImag();
   1003     (void)Ignore;
   1004     assert (Ignore == false && "init list ignored");
   1005 
   1006   if (E->getNumInits() == 2) {
   1007     llvm::Value *Real = CGF.EmitScalarExpr(E->getInit(0));
   1008     llvm::Value *Imag = CGF.EmitScalarExpr(E->getInit(1));
   1009     return ComplexPairTy(Real, Imag);
   1010   } else if (E->getNumInits() == 1) {
   1011     return Visit(E->getInit(0));
   1012   }
   1013 
   1014   // Empty init list intializes to null
   1015   assert(E->getNumInits() == 0 && "Unexpected number of inits");
   1016   QualType Ty = E->getType()->castAs<ComplexType>()->getElementType();
   1017   llvm::Type* LTy = CGF.ConvertType(Ty);
   1018   llvm::Value* zeroConstant = llvm::Constant::getNullValue(LTy);
   1019   return ComplexPairTy(zeroConstant, zeroConstant);
   1020 }
   1021 
   1022 ComplexPairTy ComplexExprEmitter::VisitVAArgExpr(VAArgExpr *E) {
   1023   Address ArgValue = Address::invalid();
   1024   Address ArgPtr = CGF.EmitVAArg(E, ArgValue);
   1025 
   1026   if (!ArgPtr.isValid()) {
   1027     CGF.ErrorUnsupported(E, "complex va_arg expression");
   1028     llvm::Type *EltTy =
   1029       CGF.ConvertType(E->getType()->castAs<ComplexType>()->getElementType());
   1030     llvm::Value *U = llvm::UndefValue::get(EltTy);
   1031     return ComplexPairTy(U, U);
   1032   }
   1033 
   1034   return EmitLoadOfLValue(CGF.MakeAddrLValue(ArgPtr, E->getType()),
   1035                           E->getExprLoc());
   1036 }
   1037 
   1038 //===----------------------------------------------------------------------===//
   1039 //                         Entry Point into this File
   1040 //===----------------------------------------------------------------------===//
   1041 
   1042 /// EmitComplexExpr - Emit the computation of the specified expression of
   1043 /// complex type, ignoring the result.
   1044 ComplexPairTy CodeGenFunction::EmitComplexExpr(const Expr *E, bool IgnoreReal,
   1045                                                bool IgnoreImag) {
   1046   assert(E && getComplexType(E->getType()) &&
   1047          "Invalid complex expression to emit");
   1048 
   1049   return ComplexExprEmitter(*this, IgnoreReal, IgnoreImag)
   1050       .Visit(const_cast<Expr *>(E));
   1051 }
   1052 
   1053 void CodeGenFunction::EmitComplexExprIntoLValue(const Expr *E, LValue dest,
   1054                                                 bool isInit) {
   1055   assert(E && getComplexType(E->getType()) &&
   1056          "Invalid complex expression to emit");
   1057   ComplexExprEmitter Emitter(*this);
   1058   ComplexPairTy Val = Emitter.Visit(const_cast<Expr*>(E));
   1059   Emitter.EmitStoreOfComplex(Val, dest, isInit);
   1060 }
   1061 
   1062 /// EmitStoreOfComplex - Store a complex number into the specified l-value.
   1063 void CodeGenFunction::EmitStoreOfComplex(ComplexPairTy V, LValue dest,
   1064                                          bool isInit) {
   1065   ComplexExprEmitter(*this).EmitStoreOfComplex(V, dest, isInit);
   1066 }
   1067 
   1068 /// EmitLoadOfComplex - Load a complex number from the specified address.
   1069 ComplexPairTy CodeGenFunction::EmitLoadOfComplex(LValue src,
   1070                                                  SourceLocation loc) {
   1071   return ComplexExprEmitter(*this).EmitLoadOfLValue(src, loc);
   1072 }
   1073 
   1074 LValue CodeGenFunction::EmitComplexAssignmentLValue(const BinaryOperator *E) {
   1075   assert(E->getOpcode() == BO_Assign);
   1076   ComplexPairTy Val; // ignored
   1077   return ComplexExprEmitter(*this).EmitBinAssignLValue(E, Val);
   1078 }
   1079 
   1080 typedef ComplexPairTy (ComplexExprEmitter::*CompoundFunc)(
   1081     const ComplexExprEmitter::BinOpInfo &);
   1082 
   1083 static CompoundFunc getComplexOp(BinaryOperatorKind Op) {
   1084   switch (Op) {
   1085   case BO_MulAssign: return &ComplexExprEmitter::EmitBinMul;
   1086   case BO_DivAssign: return &ComplexExprEmitter::EmitBinDiv;
   1087   case BO_SubAssign: return &ComplexExprEmitter::EmitBinSub;
   1088   case BO_AddAssign: return &ComplexExprEmitter::EmitBinAdd;
   1089   default:
   1090     llvm_unreachable("unexpected complex compound assignment");
   1091   }
   1092 }
   1093 
   1094 LValue CodeGenFunction::
   1095 EmitComplexCompoundAssignmentLValue(const CompoundAssignOperator *E) {
   1096   CompoundFunc Op = getComplexOp(E->getOpcode());
   1097   RValue Val;
   1098   return ComplexExprEmitter(*this).EmitCompoundAssignLValue(E, Op, Val);
   1099 }
   1100 
   1101 LValue CodeGenFunction::
   1102 EmitScalarCompoundAssignWithComplex(const CompoundAssignOperator *E,
   1103                                     llvm::Value *&Result) {
   1104   CompoundFunc Op = getComplexOp(E->getOpcode());
   1105   RValue Val;
   1106   LValue Ret = ComplexExprEmitter(*this).EmitCompoundAssignLValue(E, Op, Val);
   1107   Result = Val.getScalarVal();
   1108   return Ret;
   1109 }
   1110