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