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