blob: 8b4ead8b11176f8184b164284ea1434dcf3cc0e4 [file] [log] [blame]
Chris Lattner566b6ce2007-08-24 02:22:53 +00001//===--- CGExprComplex.cpp - Emit LLVM Code for Complex Exprs -------------===//
Chris Lattnerb6ef18a2007-08-21 05:54:00 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerb6ef18a2007-08-21 05:54:00 +00007//
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"
Daniel Dunbarde7fb842008-08-11 05:00:27 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/StmtVisitor.h"
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000018#include "llvm/Constants.h"
19#include "llvm/Function.h"
Chris Lattner6b5d0bf2007-08-26 22:47:40 +000020#include "llvm/ADT/SmallString.h"
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000021using namespace clang;
22using namespace CodeGen;
23
24//===----------------------------------------------------------------------===//
Chris Lattner05ba49c2007-08-21 05:54:53 +000025// Complex Expression Emitter
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000026//===----------------------------------------------------------------------===//
27
Chris Lattner58dee102007-08-21 16:57:55 +000028typedef CodeGenFunction::ComplexPairTy ComplexPairTy;
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000029
30namespace {
Benjamin Kramer85b45212009-11-28 19:45:26 +000031class ComplexExprEmitter
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000032 : public StmtVisitor<ComplexExprEmitter, ComplexPairTy> {
33 CodeGenFunction &CGF;
Daniel Dunbar45d196b2008-11-01 01:53:16 +000034 CGBuilderTy &Builder;
Mike Stump7f79f9b2009-05-29 15:46:01 +000035 // True is we should ignore the value of a
36 bool IgnoreReal;
37 bool IgnoreImag;
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000038public:
John McCallb418d742010-11-16 10:08:07 +000039 ComplexExprEmitter(CodeGenFunction &cgf, bool ir=false, bool ii=false)
40 : CGF(cgf), Builder(CGF.Builder), IgnoreReal(ir), IgnoreImag(ii) {
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000041 }
42
Mike Stumpdb52dcd2009-09-09 13:00:44 +000043
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000044 //===--------------------------------------------------------------------===//
45 // Utilities
46 //===--------------------------------------------------------------------===//
47
Mike Stump7f79f9b2009-05-29 15:46:01 +000048 bool TestAndClearIgnoreReal() {
49 bool I = IgnoreReal;
50 IgnoreReal = false;
51 return I;
52 }
53 bool TestAndClearIgnoreImag() {
54 bool I = IgnoreImag;
55 IgnoreImag = false;
56 return I;
57 }
Mike Stump7f79f9b2009-05-29 15:46:01 +000058
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000059 /// EmitLoadOfLValue - Given an expression with complex type that represents a
60 /// value l-value, this method emits the address of the l-value, then loads
61 /// and returns the result.
Chris Lattner46d7d9f2007-08-21 17:28:34 +000062 ComplexPairTy EmitLoadOfLValue(const Expr *E) {
John McCall0e800c92010-12-04 08:14:53 +000063 return EmitLoadOfLValue(CGF.EmitLValue(E));
64 }
65
66 ComplexPairTy EmitLoadOfLValue(LValue LV) {
Daniel Dunbar42f963d2009-06-10 04:38:50 +000067 if (LV.isSimple())
68 return EmitLoadOfComplex(LV.getAddress(), LV.isVolatileQualified());
69
John McCall119a1c62010-12-04 02:32:38 +000070 assert(LV.isPropertyRef() && "Unknown LValue type!");
71 return CGF.EmitLoadOfPropertyRefLValue(LV).getComplexVal();
Chris Lattner46d7d9f2007-08-21 17:28:34 +000072 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +000073
Chris Lattner46d7d9f2007-08-21 17:28:34 +000074 /// EmitLoadOfComplex - Given a pointer to a complex value, emit code to load
75 /// the real and imaginary pieces.
Chris Lattner76e80332007-08-21 18:02:02 +000076 ComplexPairTy EmitLoadOfComplex(llvm::Value *SrcPtr, bool isVolatile);
Mike Stumpdb52dcd2009-09-09 13:00:44 +000077
John McCall0e800c92010-12-04 08:14:53 +000078 /// EmitStoreThroughLValue - Given an l-value of complex type, store
79 /// a complex number into it.
80 void EmitStoreThroughLValue(ComplexPairTy Val, LValue LV) {
81 if (LV.isSimple())
82 return EmitStoreOfComplex(Val, LV.getAddress(), LV.isVolatileQualified());
83
84 assert(LV.isPropertyRef() && "Unknown LValue type!");
85 CGF.EmitStoreThroughPropertyRefLValue(RValue::getComplex(Val), LV);
86 }
87
Chris Lattner46d7d9f2007-08-21 17:28:34 +000088 /// EmitStoreOfComplex - Store the specified real/imag parts into the
89 /// specified value pointer.
Chris Lattner76e80332007-08-21 18:02:02 +000090 void EmitStoreOfComplex(ComplexPairTy Val, llvm::Value *ResPtr, bool isVol);
Mike Stumpdb52dcd2009-09-09 13:00:44 +000091
Chris Lattnerab340c22007-08-26 22:09:01 +000092 /// EmitComplexToComplexCast - Emit a cast from complex value Val to DestType.
93 ComplexPairTy EmitComplexToComplexCast(ComplexPairTy Val, QualType SrcType,
94 QualType DestType);
Mike Stumpdb52dcd2009-09-09 13:00:44 +000095
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000096 //===--------------------------------------------------------------------===//
97 // Visitor Methods
98 //===--------------------------------------------------------------------===//
Chris Lattner776c6492007-08-21 18:51:13 +000099
Fariborz Jahanianaa3b57e2010-09-20 23:50:22 +0000100 ComplexPairTy Visit(Expr *E) {
101 llvm::DenseMap<const Expr *, ComplexPairTy>::iterator I =
102 CGF.ConditionalSaveComplexExprs.find(E);
103 if (I != CGF.ConditionalSaveComplexExprs.end())
104 return I->second;
105
106 return StmtVisitor<ComplexExprEmitter, ComplexPairTy>::Visit(E);
107 }
108
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000109 ComplexPairTy VisitStmt(Stmt *S) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000110 S->dump(CGF.getContext().getSourceManager());
Chris Lattner776c6492007-08-21 18:51:13 +0000111 assert(0 && "Stmt can't have complex result type!");
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000112 return ComplexPairTy();
113 }
Chris Lattner776c6492007-08-21 18:51:13 +0000114 ComplexPairTy VisitExpr(Expr *S);
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000115 ComplexPairTy VisitParenExpr(ParenExpr *PE) { return Visit(PE->getSubExpr());}
Chris Lattnerd272ff02007-08-26 05:57:57 +0000116 ComplexPairTy VisitImaginaryLiteral(const ImaginaryLiteral *IL);
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000117
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000118 // l-values.
Chris Lattnerd272ff02007-08-26 05:57:57 +0000119 ComplexPairTy VisitDeclRefExpr(const Expr *E) { return EmitLoadOfLValue(E); }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000120 ComplexPairTy VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
Daniel Dunbar42f963d2009-06-10 04:38:50 +0000121 return EmitLoadOfLValue(E);
122 }
123 ComplexPairTy VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
John McCallf6a16482010-12-04 03:47:34 +0000124 assert(E->getObjectKind() == OK_Ordinary);
Daniel Dunbar42f963d2009-06-10 04:38:50 +0000125 return EmitLoadOfLValue(E);
126 }
Daniel Dunbar42f963d2009-06-10 04:38:50 +0000127 ComplexPairTy VisitObjCMessageExpr(ObjCMessageExpr *E) {
128 return CGF.EmitObjCMessageExpr(E).getComplexVal();
129 }
Chris Lattner8bcd7232007-08-21 18:03:58 +0000130 ComplexPairTy VisitArraySubscriptExpr(Expr *E) { return EmitLoadOfLValue(E); }
Chris Lattnerd272ff02007-08-26 05:57:57 +0000131 ComplexPairTy VisitMemberExpr(const Expr *E) { return EmitLoadOfLValue(E); }
John McCalle996ffd2011-02-16 08:02:54 +0000132 ComplexPairTy VisitOpaqueValueExpr(OpaqueValueExpr *E) {
John McCall56ca35d2011-02-17 10:25:35 +0000133 if (E->isGLValue())
134 return EmitLoadOfLValue(CGF.getOpaqueLValueMapping(E));
135 return CGF.getOpaqueRValueMapping(E).getComplexVal();
John McCalle996ffd2011-02-16 08:02:54 +0000136 }
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000137
Chris Lattnere73e8e22007-08-21 22:33:41 +0000138 // FIXME: CompoundLiteralExpr
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000139
Douglas Gregor77d439a2010-07-14 21:35:45 +0000140 ComplexPairTy EmitCast(CastExpr::CastKind CK, Expr *Op, QualType DestTy);
Chris Lattnerd272ff02007-08-26 05:57:57 +0000141 ComplexPairTy VisitImplicitCastExpr(ImplicitCastExpr *E) {
142 // Unlike for scalars, we don't have to worry about function->ptr demotion
143 // here.
Douglas Gregor77d439a2010-07-14 21:35:45 +0000144 return EmitCast(E->getCastKind(), E->getSubExpr(), E->getType());
Chris Lattnerd272ff02007-08-26 05:57:57 +0000145 }
146 ComplexPairTy VisitCastExpr(CastExpr *E) {
Douglas Gregor77d439a2010-07-14 21:35:45 +0000147 return EmitCast(E->getCastKind(), E->getSubExpr(), E->getType());
Chris Lattnerd272ff02007-08-26 05:57:57 +0000148 }
Chris Lattner419d25e2007-08-23 21:38:16 +0000149 ComplexPairTy VisitCallExpr(const CallExpr *E);
Chris Lattner7e20d092007-08-31 22:51:38 +0000150 ComplexPairTy VisitStmtExpr(const StmtExpr *E);
151
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000152 // Operators.
Chris Lattner3070f982007-08-21 22:25:29 +0000153 ComplexPairTy VisitPrePostIncDec(const UnaryOperator *E,
Chris Lattnerdd36d322010-01-09 21:40:03 +0000154 bool isInc, bool isPre) {
155 LValue LV = CGF.EmitLValue(E->getSubExpr());
156 return CGF.EmitComplexPrePostIncDec(E, LV, isInc, isPre);
157 }
Chris Lattner3070f982007-08-21 22:25:29 +0000158 ComplexPairTy VisitUnaryPostDec(const UnaryOperator *E) {
159 return VisitPrePostIncDec(E, false, false);
160 }
161 ComplexPairTy VisitUnaryPostInc(const UnaryOperator *E) {
162 return VisitPrePostIncDec(E, true, false);
163 }
164 ComplexPairTy VisitUnaryPreDec(const UnaryOperator *E) {
165 return VisitPrePostIncDec(E, false, true);
166 }
167 ComplexPairTy VisitUnaryPreInc(const UnaryOperator *E) {
168 return VisitPrePostIncDec(E, true, true);
169 }
170 ComplexPairTy VisitUnaryDeref(const Expr *E) { return EmitLoadOfLValue(E); }
Chris Lattnerfa2b9c92007-08-21 20:08:23 +0000171 ComplexPairTy VisitUnaryPlus (const UnaryOperator *E) {
Mike Stump7f79f9b2009-05-29 15:46:01 +0000172 TestAndClearIgnoreReal();
173 TestAndClearIgnoreImag();
Chris Lattnerfa2b9c92007-08-21 20:08:23 +0000174 return Visit(E->getSubExpr());
175 }
176 ComplexPairTy VisitUnaryMinus (const UnaryOperator *E);
Chris Lattnere98a11c2007-08-21 20:41:44 +0000177 ComplexPairTy VisitUnaryNot (const UnaryOperator *E);
Sebastian Redl05189992008-11-11 17:56:53 +0000178 // LNot,Real,Imag never return complex.
Chris Lattnere98a11c2007-08-21 20:41:44 +0000179 ComplexPairTy VisitUnaryExtension(const UnaryOperator *E) {
180 return Visit(E->getSubExpr());
181 }
Chris Lattner04421082008-04-08 04:40:51 +0000182 ComplexPairTy VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
183 return Visit(DAE->getExpr());
184 }
John McCall4765fa02010-12-06 08:20:24 +0000185 ComplexPairTy VisitExprWithCleanups(ExprWithCleanups *E) {
186 return CGF.EmitExprWithCleanups(E).getComplexVal();
Anders Carlsson4b76b4f2009-05-31 00:12:05 +0000187 }
Douglas Gregored8abf12010-07-08 06:14:04 +0000188 ComplexPairTy VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
Argyrios Kyrtzidis7267f782008-08-23 19:35:47 +0000189 assert(E->getType()->isAnyComplexType() && "Expected complex type!");
John McCall183700f2009-09-21 23:43:11 +0000190 QualType Elem = E->getType()->getAs<ComplexType>()->getElementType();
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000191 llvm::Constant *Null = llvm::Constant::getNullValue(CGF.ConvertType(Elem));
Argyrios Kyrtzidis7267f782008-08-23 19:35:47 +0000192 return ComplexPairTy(Null, Null);
193 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000194 ComplexPairTy VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
195 assert(E->getType()->isAnyComplexType() && "Expected complex type!");
John McCall183700f2009-09-21 23:43:11 +0000196 QualType Elem = E->getType()->getAs<ComplexType>()->getElementType();
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000197 llvm::Constant *Null =
Owen Andersonc9c88b42009-07-31 20:28:54 +0000198 llvm::Constant::getNullValue(CGF.ConvertType(Elem));
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000199 return ComplexPairTy(Null, Null);
200 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000201
Chris Lattnerab340c22007-08-26 22:09:01 +0000202 struct BinOpInfo {
203 ComplexPairTy LHS;
204 ComplexPairTy RHS;
205 QualType Ty; // Computation Type.
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000206 };
207
Chris Lattnerab340c22007-08-26 22:09:01 +0000208 BinOpInfo EmitBinOps(const BinaryOperator *E);
John McCallb418d742010-11-16 10:08:07 +0000209 LValue EmitCompoundAssignLValue(const CompoundAssignOperator *E,
210 ComplexPairTy (ComplexExprEmitter::*Func)
211 (const BinOpInfo &),
212 ComplexPairTy &Val);
Chris Lattnerab340c22007-08-26 22:09:01 +0000213 ComplexPairTy EmitCompoundAssign(const CompoundAssignOperator *E,
214 ComplexPairTy (ComplexExprEmitter::*Func)
215 (const BinOpInfo &));
216
217 ComplexPairTy EmitBinAdd(const BinOpInfo &Op);
218 ComplexPairTy EmitBinSub(const BinOpInfo &Op);
219 ComplexPairTy EmitBinMul(const BinOpInfo &Op);
220 ComplexPairTy EmitBinDiv(const BinOpInfo &Op);
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000221
Chris Lattnerab340c22007-08-26 22:09:01 +0000222 ComplexPairTy VisitBinAdd(const BinaryOperator *E) {
223 return EmitBinAdd(EmitBinOps(E));
224 }
225 ComplexPairTy VisitBinSub(const BinaryOperator *E) {
226 return EmitBinSub(EmitBinOps(E));
227 }
John McCallb418d742010-11-16 10:08:07 +0000228 ComplexPairTy VisitBinMul(const BinaryOperator *E) {
229 return EmitBinMul(EmitBinOps(E));
230 }
Chris Lattnerab340c22007-08-26 22:09:01 +0000231 ComplexPairTy VisitBinDiv(const BinaryOperator *E) {
232 return EmitBinDiv(EmitBinOps(E));
233 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000234
Chris Lattnerab340c22007-08-26 22:09:01 +0000235 // Compound assignments.
236 ComplexPairTy VisitBinAddAssign(const CompoundAssignOperator *E) {
237 return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinAdd);
238 }
239 ComplexPairTy VisitBinSubAssign(const CompoundAssignOperator *E) {
240 return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinSub);
241 }
242 ComplexPairTy VisitBinMulAssign(const CompoundAssignOperator *E) {
243 return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinMul);
244 }
245 ComplexPairTy VisitBinDivAssign(const CompoundAssignOperator *E) {
246 return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinDiv);
247 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000248
Chris Lattner612c40c2007-08-26 21:27:07 +0000249 // GCC rejects rem/and/or/xor for integer complex.
Chris Lattner4034edb2007-08-21 17:12:50 +0000250 // Logical and/or always return int, never complex.
Chris Lattner58dee102007-08-21 16:57:55 +0000251
252 // No comparisons produce a complex result.
John McCallb418d742010-11-16 10:08:07 +0000253
254 LValue EmitBinAssignLValue(const BinaryOperator *E,
255 ComplexPairTy &Val);
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000256 ComplexPairTy VisitBinAssign (const BinaryOperator *E);
Chris Lattner756a4d82007-08-21 17:15:50 +0000257 ComplexPairTy VisitBinComma (const BinaryOperator *E);
258
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000259
John McCall56ca35d2011-02-17 10:25:35 +0000260 ComplexPairTy
261 VisitAbstractConditionalOperator(const AbstractConditionalOperator *CO);
Chris Lattnerd0b12032007-08-24 02:18:47 +0000262 ComplexPairTy VisitChooseExpr(ChooseExpr *CE);
Eli Friedmanc4777f12008-05-13 23:11:35 +0000263
264 ComplexPairTy VisitInitListExpr(InitListExpr *E);
Daniel Dunbar4e484b82009-02-10 03:03:30 +0000265
266 ComplexPairTy VisitVAArgExpr(VAArgExpr *E);
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000267};
268} // end anonymous namespace.
269
270//===----------------------------------------------------------------------===//
271// Utilities
272//===----------------------------------------------------------------------===//
273
Chris Lattner46d7d9f2007-08-21 17:28:34 +0000274/// EmitLoadOfComplex - Given an RValue reference for a complex, emit code to
275/// load the real and imaginary pieces, returning them as Real/Imag.
Chris Lattner76e80332007-08-21 18:02:02 +0000276ComplexPairTy ComplexExprEmitter::EmitLoadOfComplex(llvm::Value *SrcPtr,
277 bool isVolatile) {
Mike Stump7f79f9b2009-05-29 15:46:01 +0000278 llvm::Value *Real=0, *Imag=0;
Chris Lattner6b5d0bf2007-08-26 22:47:40 +0000279
John McCallff624c12010-11-14 09:40:28 +0000280 if (!IgnoreReal || isVolatile) {
Benjamin Kramer56a44f72009-11-29 19:51:45 +0000281 llvm::Value *RealP = Builder.CreateStructGEP(SrcPtr, 0,
282 SrcPtr->getName() + ".realp");
283 Real = Builder.CreateLoad(RealP, isVolatile, SrcPtr->getName() + ".real");
Mike Stump7f79f9b2009-05-29 15:46:01 +0000284 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000285
John McCallff624c12010-11-14 09:40:28 +0000286 if (!IgnoreImag || isVolatile) {
Benjamin Kramer56a44f72009-11-29 19:51:45 +0000287 llvm::Value *ImagP = Builder.CreateStructGEP(SrcPtr, 1,
288 SrcPtr->getName() + ".imagp");
289 Imag = Builder.CreateLoad(ImagP, isVolatile, SrcPtr->getName() + ".imag");
Mike Stump7f79f9b2009-05-29 15:46:01 +0000290 }
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000291 return ComplexPairTy(Real, Imag);
292}
293
Chris Lattner46d7d9f2007-08-21 17:28:34 +0000294/// EmitStoreOfComplex - Store the specified real/imag parts into the
295/// specified value pointer.
Chris Lattner76e80332007-08-21 18:02:02 +0000296void ComplexExprEmitter::EmitStoreOfComplex(ComplexPairTy Val, llvm::Value *Ptr,
297 bool isVolatile) {
Chris Lattner36b6a0a2008-03-19 05:19:41 +0000298 llvm::Value *RealPtr = Builder.CreateStructGEP(Ptr, 0, "real");
299 llvm::Value *ImagPtr = Builder.CreateStructGEP(Ptr, 1, "imag");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000300
Chris Lattner76e80332007-08-21 18:02:02 +0000301 Builder.CreateStore(Val.first, RealPtr, isVolatile);
302 Builder.CreateStore(Val.second, ImagPtr, isVolatile);
Chris Lattner46d7d9f2007-08-21 17:28:34 +0000303}
304
305
306
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000307//===----------------------------------------------------------------------===//
308// Visitor Methods
309//===----------------------------------------------------------------------===//
310
Chris Lattner776c6492007-08-21 18:51:13 +0000311ComplexPairTy ComplexExprEmitter::VisitExpr(Expr *E) {
Daniel Dunbar488e9932008-08-16 00:56:44 +0000312 CGF.ErrorUnsupported(E, "complex expression");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000313 const llvm::Type *EltTy =
John McCall183700f2009-09-21 23:43:11 +0000314 CGF.ConvertType(E->getType()->getAs<ComplexType>()->getElementType());
Chris Lattner776c6492007-08-21 18:51:13 +0000315 llvm::Value *U = llvm::UndefValue::get(EltTy);
316 return ComplexPairTy(U, U);
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000317}
318
Chris Lattnerd272ff02007-08-26 05:57:57 +0000319ComplexPairTy ComplexExprEmitter::
320VisitImaginaryLiteral(const ImaginaryLiteral *IL) {
Chris Lattnerdb68f1b2007-08-26 03:51:12 +0000321 llvm::Value *Imag = CGF.EmitScalarExpr(IL->getSubExpr());
John McCall8d3d6c92011-01-13 02:03:06 +0000322 return ComplexPairTy(llvm::Constant::getNullValue(Imag->getType()), Imag);
Chris Lattnerdb68f1b2007-08-26 03:51:12 +0000323}
324
325
Chris Lattner419d25e2007-08-23 21:38:16 +0000326ComplexPairTy ComplexExprEmitter::VisitCallExpr(const CallExpr *E) {
Anders Carlssone9f2f452009-05-27 03:37:57 +0000327 if (E->getCallReturnType()->isReferenceType())
328 return EmitLoadOfLValue(E);
329
Chris Lattner9b655512007-08-31 22:49:20 +0000330 return CGF.EmitCallExpr(E).getComplexVal();
Chris Lattner419d25e2007-08-23 21:38:16 +0000331}
332
Chris Lattner7e20d092007-08-31 22:51:38 +0000333ComplexPairTy ComplexExprEmitter::VisitStmtExpr(const StmtExpr *E) {
John McCall150b4622011-01-26 04:00:11 +0000334 CodeGenFunction::StmtExprEvaluation eval(CGF);
335 return CGF.EmitCompoundStmt(*E->getSubStmt(), true).getComplexVal();
Chris Lattner7e20d092007-08-31 22:51:38 +0000336}
337
Chris Lattnerab340c22007-08-26 22:09:01 +0000338/// EmitComplexToComplexCast - Emit a cast from complex value Val to DestType.
339ComplexPairTy ComplexExprEmitter::EmitComplexToComplexCast(ComplexPairTy Val,
340 QualType SrcType,
341 QualType DestType) {
342 // Get the src/dest element type.
John McCall183700f2009-09-21 23:43:11 +0000343 SrcType = SrcType->getAs<ComplexType>()->getElementType();
344 DestType = DestType->getAs<ComplexType>()->getElementType();
Chris Lattnerd272ff02007-08-26 05:57:57 +0000345
Daniel Dunbar56f0d162009-01-29 06:44:03 +0000346 // C99 6.3.1.6: When a value of complex type is converted to another
Daniel Dunbare12d8e32009-01-26 18:02:34 +0000347 // complex type, both the real and imaginary parts follow the conversion
Chris Lattnerab340c22007-08-26 22:09:01 +0000348 // rules for the corresponding real types.
349 Val.first = CGF.EmitScalarConversion(Val.first, SrcType, DestType);
350 Val.second = CGF.EmitScalarConversion(Val.second, SrcType, DestType);
351 return Val;
352}
353
Douglas Gregor77d439a2010-07-14 21:35:45 +0000354ComplexPairTy ComplexExprEmitter::EmitCast(CastExpr::CastKind CK, Expr *Op,
355 QualType DestTy) {
John McCallf6a16482010-12-04 03:47:34 +0000356 switch (CK) {
357 case CK_GetObjCProperty: {
358 LValue LV = CGF.EmitLValue(Op);
359 assert(LV.isPropertyRef() && "Unknown LValue type!");
360 return CGF.EmitLoadOfPropertyRefLValue(LV).getComplexVal();
361 }
362
363 case CK_NoOp:
364 case CK_LValueToRValue:
365 return Visit(Op);
366
367 // TODO: do all of these
368 default:
369 break;
370 }
John McCall2bb5d002010-11-13 09:02:35 +0000371
Chris Lattnerd272ff02007-08-26 05:57:57 +0000372 // Two cases here: cast from (complex to complex) and (scalar to complex).
Chris Lattner9b2dc282008-04-04 16:54:41 +0000373 if (Op->getType()->isAnyComplexType())
Chris Lattnerab340c22007-08-26 22:09:01 +0000374 return EmitComplexToComplexCast(Visit(Op), Op->getType(), DestTy);
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000375
Douglas Gregor77d439a2010-07-14 21:35:45 +0000376 // FIXME: We should be looking at all of the cast kinds here, not
377 // cherry-picking the ones we have test cases for.
John McCall2de56d12010-08-25 11:45:40 +0000378 if (CK == CK_LValueBitCast) {
Douglas Gregor77d439a2010-07-14 21:35:45 +0000379 llvm::Value *V = CGF.EmitLValue(Op).getAddress();
380 V = Builder.CreateBitCast(V,
381 CGF.ConvertType(CGF.getContext().getPointerType(DestTy)));
382 // FIXME: Are the qualifiers correct here?
383 return EmitLoadOfComplex(V, DestTy.isVolatileQualified());
384 }
385
Chris Lattnerd272ff02007-08-26 05:57:57 +0000386 // C99 6.3.1.7: When a value of real type is converted to a complex type, the
Eli Friedmanc110c542009-03-23 04:08:46 +0000387 // real part of the complex result value is determined by the rules of
Chris Lattnerd272ff02007-08-26 05:57:57 +0000388 // conversion to the corresponding real type and the imaginary part of the
389 // complex result value is a positive zero or an unsigned zero.
390 llvm::Value *Elt = CGF.EmitScalarExpr(Op);
391
392 // Convert the input element to the element type of the complex.
John McCall183700f2009-09-21 23:43:11 +0000393 DestTy = DestTy->getAs<ComplexType>()->getElementType();
Chris Lattner3707b252007-08-26 06:48:56 +0000394 Elt = CGF.EmitScalarConversion(Elt, Op->getType(), DestTy);
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000395
Chris Lattnerd272ff02007-08-26 05:57:57 +0000396 // Return (realval, 0).
Owen Andersonc9c88b42009-07-31 20:28:54 +0000397 return ComplexPairTy(Elt, llvm::Constant::getNullValue(Elt->getType()));
Chris Lattnerd272ff02007-08-26 05:57:57 +0000398}
399
Chris Lattnerfa2b9c92007-08-21 20:08:23 +0000400ComplexPairTy ComplexExprEmitter::VisitUnaryMinus(const UnaryOperator *E) {
Mike Stump7f79f9b2009-05-29 15:46:01 +0000401 TestAndClearIgnoreReal();
402 TestAndClearIgnoreImag();
Chris Lattnerfa2b9c92007-08-21 20:08:23 +0000403 ComplexPairTy Op = Visit(E->getSubExpr());
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000404
Chris Lattner87415d22009-06-17 06:36:24 +0000405 llvm::Value *ResR, *ResI;
Duncan Sandsf177d9d2010-02-15 16:14:01 +0000406 if (Op.first->getType()->isFloatingPointTy()) {
Chris Lattner87415d22009-06-17 06:36:24 +0000407 ResR = Builder.CreateFNeg(Op.first, "neg.r");
408 ResI = Builder.CreateFNeg(Op.second, "neg.i");
409 } else {
410 ResR = Builder.CreateNeg(Op.first, "neg.r");
411 ResI = Builder.CreateNeg(Op.second, "neg.i");
412 }
Chris Lattnerfa2b9c92007-08-21 20:08:23 +0000413 return ComplexPairTy(ResR, ResI);
414}
415
Chris Lattnere98a11c2007-08-21 20:41:44 +0000416ComplexPairTy ComplexExprEmitter::VisitUnaryNot(const UnaryOperator *E) {
Mike Stump7f79f9b2009-05-29 15:46:01 +0000417 TestAndClearIgnoreReal();
418 TestAndClearIgnoreImag();
Chris Lattnere98a11c2007-08-21 20:41:44 +0000419 // ~(a+ib) = a + i*-b
420 ComplexPairTy Op = Visit(E->getSubExpr());
Chris Lattner87415d22009-06-17 06:36:24 +0000421 llvm::Value *ResI;
Duncan Sandsf177d9d2010-02-15 16:14:01 +0000422 if (Op.second->getType()->isFloatingPointTy())
Chris Lattner87415d22009-06-17 06:36:24 +0000423 ResI = Builder.CreateFNeg(Op.second, "conj.i");
424 else
425 ResI = Builder.CreateNeg(Op.second, "conj.i");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000426
Chris Lattnere98a11c2007-08-21 20:41:44 +0000427 return ComplexPairTy(Op.first, ResI);
428}
Chris Lattnerfa2b9c92007-08-21 20:08:23 +0000429
Chris Lattnerab340c22007-08-26 22:09:01 +0000430ComplexPairTy ComplexExprEmitter::EmitBinAdd(const BinOpInfo &Op) {
Chris Lattner87415d22009-06-17 06:36:24 +0000431 llvm::Value *ResR, *ResI;
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000432
Duncan Sandsf177d9d2010-02-15 16:14:01 +0000433 if (Op.LHS.first->getType()->isFloatingPointTy()) {
Chris Lattner87415d22009-06-17 06:36:24 +0000434 ResR = Builder.CreateFAdd(Op.LHS.first, Op.RHS.first, "add.r");
435 ResI = Builder.CreateFAdd(Op.LHS.second, Op.RHS.second, "add.i");
436 } else {
437 ResR = Builder.CreateAdd(Op.LHS.first, Op.RHS.first, "add.r");
438 ResI = Builder.CreateAdd(Op.LHS.second, Op.RHS.second, "add.i");
439 }
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000440 return ComplexPairTy(ResR, ResI);
441}
442
Chris Lattnerab340c22007-08-26 22:09:01 +0000443ComplexPairTy ComplexExprEmitter::EmitBinSub(const BinOpInfo &Op) {
Chris Lattner87415d22009-06-17 06:36:24 +0000444 llvm::Value *ResR, *ResI;
Duncan Sandsf177d9d2010-02-15 16:14:01 +0000445 if (Op.LHS.first->getType()->isFloatingPointTy()) {
Chris Lattner87415d22009-06-17 06:36:24 +0000446 ResR = Builder.CreateFSub(Op.LHS.first, Op.RHS.first, "sub.r");
447 ResI = Builder.CreateFSub(Op.LHS.second, Op.RHS.second, "sub.i");
448 } else {
449 ResR = Builder.CreateSub(Op.LHS.first, Op.RHS.first, "sub.r");
450 ResI = Builder.CreateSub(Op.LHS.second, Op.RHS.second, "sub.i");
451 }
Chris Lattner6bc1ade2007-08-23 23:46:33 +0000452 return ComplexPairTy(ResR, ResI);
453}
454
455
Chris Lattnerab340c22007-08-26 22:09:01 +0000456ComplexPairTy ComplexExprEmitter::EmitBinMul(const BinOpInfo &Op) {
Chris Lattner87415d22009-06-17 06:36:24 +0000457 using llvm::Value;
458 Value *ResR, *ResI;
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000459
Duncan Sandsf177d9d2010-02-15 16:14:01 +0000460 if (Op.LHS.first->getType()->isFloatingPointTy()) {
Chris Lattner87415d22009-06-17 06:36:24 +0000461 Value *ResRl = Builder.CreateFMul(Op.LHS.first, Op.RHS.first, "mul.rl");
462 Value *ResRr = Builder.CreateFMul(Op.LHS.second, Op.RHS.second,"mul.rr");
463 ResR = Builder.CreateFSub(ResRl, ResRr, "mul.r");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000464
Chris Lattner87415d22009-06-17 06:36:24 +0000465 Value *ResIl = Builder.CreateFMul(Op.LHS.second, Op.RHS.first, "mul.il");
466 Value *ResIr = Builder.CreateFMul(Op.LHS.first, Op.RHS.second, "mul.ir");
467 ResI = Builder.CreateFAdd(ResIl, ResIr, "mul.i");
468 } else {
469 Value *ResRl = Builder.CreateMul(Op.LHS.first, Op.RHS.first, "mul.rl");
470 Value *ResRr = Builder.CreateMul(Op.LHS.second, Op.RHS.second,"mul.rr");
471 ResR = Builder.CreateSub(ResRl, ResRr, "mul.r");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000472
Chris Lattner87415d22009-06-17 06:36:24 +0000473 Value *ResIl = Builder.CreateMul(Op.LHS.second, Op.RHS.first, "mul.il");
474 Value *ResIr = Builder.CreateMul(Op.LHS.first, Op.RHS.second, "mul.ir");
475 ResI = Builder.CreateAdd(ResIl, ResIr, "mul.i");
476 }
Chris Lattner2823c192007-08-21 16:34:16 +0000477 return ComplexPairTy(ResR, ResI);
478}
479
Chris Lattnerab340c22007-08-26 22:09:01 +0000480ComplexPairTy ComplexExprEmitter::EmitBinDiv(const BinOpInfo &Op) {
481 llvm::Value *LHSr = Op.LHS.first, *LHSi = Op.LHS.second;
482 llvm::Value *RHSr = Op.RHS.first, *RHSi = Op.RHS.second;
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000483
Chris Lattner3219c5d2007-08-26 21:24:19 +0000484
485 llvm::Value *DSTr, *DSTi;
Duncan Sandsf177d9d2010-02-15 16:14:01 +0000486 if (Op.LHS.first->getType()->isFloatingPointTy()) {
Chris Lattner87415d22009-06-17 06:36:24 +0000487 // (a+ib) / (c+id) = ((ac+bd)/(cc+dd)) + i((bc-ad)/(cc+dd))
488 llvm::Value *Tmp1 = Builder.CreateFMul(LHSr, RHSr, "tmp"); // a*c
489 llvm::Value *Tmp2 = Builder.CreateFMul(LHSi, RHSi, "tmp"); // b*d
490 llvm::Value *Tmp3 = Builder.CreateFAdd(Tmp1, Tmp2, "tmp"); // ac+bd
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000491
Chris Lattner87415d22009-06-17 06:36:24 +0000492 llvm::Value *Tmp4 = Builder.CreateFMul(RHSr, RHSr, "tmp"); // c*c
493 llvm::Value *Tmp5 = Builder.CreateFMul(RHSi, RHSi, "tmp"); // d*d
494 llvm::Value *Tmp6 = Builder.CreateFAdd(Tmp4, Tmp5, "tmp"); // cc+dd
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000495
Chris Lattner87415d22009-06-17 06:36:24 +0000496 llvm::Value *Tmp7 = Builder.CreateFMul(LHSi, RHSr, "tmp"); // b*c
497 llvm::Value *Tmp8 = Builder.CreateFMul(LHSr, RHSi, "tmp"); // a*d
498 llvm::Value *Tmp9 = Builder.CreateFSub(Tmp7, Tmp8, "tmp"); // bc-ad
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000499
Chris Lattner3219c5d2007-08-26 21:24:19 +0000500 DSTr = Builder.CreateFDiv(Tmp3, Tmp6, "tmp");
501 DSTi = Builder.CreateFDiv(Tmp9, Tmp6, "tmp");
502 } else {
Chris Lattner87415d22009-06-17 06:36:24 +0000503 // (a+ib) / (c+id) = ((ac+bd)/(cc+dd)) + i((bc-ad)/(cc+dd))
504 llvm::Value *Tmp1 = Builder.CreateMul(LHSr, RHSr, "tmp"); // a*c
505 llvm::Value *Tmp2 = Builder.CreateMul(LHSi, RHSi, "tmp"); // b*d
506 llvm::Value *Tmp3 = Builder.CreateAdd(Tmp1, Tmp2, "tmp"); // ac+bd
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000507
Chris Lattner87415d22009-06-17 06:36:24 +0000508 llvm::Value *Tmp4 = Builder.CreateMul(RHSr, RHSr, "tmp"); // c*c
509 llvm::Value *Tmp5 = Builder.CreateMul(RHSi, RHSi, "tmp"); // d*d
510 llvm::Value *Tmp6 = Builder.CreateAdd(Tmp4, Tmp5, "tmp"); // cc+dd
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000511
Chris Lattner87415d22009-06-17 06:36:24 +0000512 llvm::Value *Tmp7 = Builder.CreateMul(LHSi, RHSr, "tmp"); // b*c
513 llvm::Value *Tmp8 = Builder.CreateMul(LHSr, RHSi, "tmp"); // a*d
514 llvm::Value *Tmp9 = Builder.CreateSub(Tmp7, Tmp8, "tmp"); // bc-ad
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000515
John McCall183700f2009-09-21 23:43:11 +0000516 if (Op.Ty->getAs<ComplexType>()->getElementType()->isUnsignedIntegerType()) {
Chris Lattner3219c5d2007-08-26 21:24:19 +0000517 DSTr = Builder.CreateUDiv(Tmp3, Tmp6, "tmp");
518 DSTi = Builder.CreateUDiv(Tmp9, Tmp6, "tmp");
519 } else {
520 DSTr = Builder.CreateSDiv(Tmp3, Tmp6, "tmp");
521 DSTi = Builder.CreateSDiv(Tmp9, Tmp6, "tmp");
522 }
523 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000524
Chris Lattner3219c5d2007-08-26 21:24:19 +0000525 return ComplexPairTy(DSTr, DSTi);
526}
527
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000528ComplexExprEmitter::BinOpInfo
Chris Lattnerab340c22007-08-26 22:09:01 +0000529ComplexExprEmitter::EmitBinOps(const BinaryOperator *E) {
Mike Stump7f79f9b2009-05-29 15:46:01 +0000530 TestAndClearIgnoreReal();
531 TestAndClearIgnoreImag();
Chris Lattnerab340c22007-08-26 22:09:01 +0000532 BinOpInfo Ops;
533 Ops.LHS = Visit(E->getLHS());
534 Ops.RHS = Visit(E->getRHS());
535 Ops.Ty = E->getType();
536 return Ops;
537}
538
539
John McCallb418d742010-11-16 10:08:07 +0000540LValue ComplexExprEmitter::
541EmitCompoundAssignLValue(const CompoundAssignOperator *E,
542 ComplexPairTy (ComplexExprEmitter::*Func)(const BinOpInfo&),
543 ComplexPairTy &Val) {
Mike Stump7f79f9b2009-05-29 15:46:01 +0000544 TestAndClearIgnoreReal();
545 TestAndClearIgnoreImag();
Jeffrey Yasskindec09842011-01-18 02:00:16 +0000546 QualType LHSTy = E->getLHS()->getType();
Chris Lattnerab340c22007-08-26 22:09:01 +0000547
548 BinOpInfo OpInfo;
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000549
Mike Stump7f79f9b2009-05-29 15:46:01 +0000550 // Load the RHS and LHS operands.
551 // __block variables need to have the rhs evaluated first, plus this should
John McCall641ca7d2010-11-16 05:45:35 +0000552 // improve codegen a little.
Eli Friedmanab3a8522009-03-28 01:22:36 +0000553 OpInfo.Ty = E->getComputationResultType();
John McCall641ca7d2010-11-16 05:45:35 +0000554
555 // The RHS should have been converted to the computation type.
556 assert(OpInfo.Ty->isAnyComplexType());
557 assert(CGF.getContext().hasSameUnqualifiedType(OpInfo.Ty,
558 E->getRHS()->getType()));
559 OpInfo.RHS = Visit(E->getRHS());
Fariborz Jahaniandfb1eb22010-03-23 18:08:50 +0000560
Daniel Dunbar120bc772010-06-29 22:44:21 +0000561 LValue LHS = CGF.EmitLValue(E->getLHS());
John McCall0e800c92010-12-04 08:14:53 +0000562
563 // Load from the l-value.
564 ComplexPairTy LHSComplexPair = EmitLoadOfLValue(LHS);
Fariborz Jahaniandfb1eb22010-03-23 18:08:50 +0000565
Daniel Dunbar120bc772010-06-29 22:44:21 +0000566 OpInfo.LHS = EmitComplexToComplexCast(LHSComplexPair, LHSTy, OpInfo.Ty);
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000567
Chris Lattnerab340c22007-08-26 22:09:01 +0000568 // Expand the binary operator.
569 ComplexPairTy Result = (this->*Func)(OpInfo);
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000570
Chris Lattnerab340c22007-08-26 22:09:01 +0000571 // Truncate the result back to the LHS type.
572 Result = EmitComplexToComplexCast(Result, OpInfo.Ty, LHSTy);
John McCallb418d742010-11-16 10:08:07 +0000573 Val = Result;
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000574
Chris Lattnerab340c22007-08-26 22:09:01 +0000575 // Store the result value into the LHS lvalue.
John McCall0e800c92010-12-04 08:14:53 +0000576 EmitStoreThroughLValue(Result, LHS);
Daniel Dunbar120bc772010-06-29 22:44:21 +0000577
John McCallb418d742010-11-16 10:08:07 +0000578 return LHS;
Chris Lattnerab340c22007-08-26 22:09:01 +0000579}
Chris Lattner3219c5d2007-08-26 21:24:19 +0000580
John McCallb418d742010-11-16 10:08:07 +0000581// Compound assignments.
582ComplexPairTy ComplexExprEmitter::
583EmitCompoundAssign(const CompoundAssignOperator *E,
584 ComplexPairTy (ComplexExprEmitter::*Func)(const BinOpInfo&)){
585 ComplexPairTy Val;
586 LValue LV = EmitCompoundAssignLValue(E, Func, Val);
587
588 // The result of an assignment in C is the assigned r-value.
589 if (!CGF.getContext().getLangOptions().CPlusPlus)
590 return Val;
591
592 // Objective-C property assignment never reloads the value following a store.
John McCall119a1c62010-12-04 02:32:38 +0000593 if (LV.isPropertyRef())
John McCallb418d742010-11-16 10:08:07 +0000594 return Val;
595
596 // If the lvalue is non-volatile, return the computed value of the assignment.
597 if (!LV.isVolatileQualified())
598 return Val;
599
600 return EmitLoadOfComplex(LV.getAddress(), LV.isVolatileQualified());
601}
602
603LValue ComplexExprEmitter::EmitBinAssignLValue(const BinaryOperator *E,
604 ComplexPairTy &Val) {
Douglas Gregor63982352010-07-13 18:40:04 +0000605 assert(CGF.getContext().hasSameUnqualifiedType(E->getLHS()->getType(),
606 E->getRHS()->getType()) &&
Chris Lattner96196622008-07-26 22:37:01 +0000607 "Invalid assignment");
John McCallb418d742010-11-16 10:08:07 +0000608 TestAndClearIgnoreReal();
609 TestAndClearIgnoreImag();
610
John McCallcd940a12010-12-06 06:10:02 +0000611 // Emit the RHS. __block variables need the RHS evaluated first.
John McCallb418d742010-11-16 10:08:07 +0000612 Val = Visit(E->getRHS());
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000613
614 // Compute the address to store into.
615 LValue LHS = CGF.EmitLValue(E->getLHS());
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000616
Daniel Dunbar120bc772010-06-29 22:44:21 +0000617 // Store the result value into the LHS lvalue.
John McCall0e800c92010-12-04 08:14:53 +0000618 EmitStoreThroughLValue(Val, LHS);
Daniel Dunbar42f963d2009-06-10 04:38:50 +0000619
John McCallb418d742010-11-16 10:08:07 +0000620 return LHS;
621}
Daniel Dunbar120bc772010-06-29 22:44:21 +0000622
John McCallb418d742010-11-16 10:08:07 +0000623ComplexPairTy ComplexExprEmitter::VisitBinAssign(const BinaryOperator *E) {
624 ComplexPairTy Val;
625 LValue LV = EmitBinAssignLValue(E, Val);
626
627 // The result of an assignment in C is the assigned r-value.
628 if (!CGF.getContext().getLangOptions().CPlusPlus)
629 return Val;
630
631 // Objective-C property assignment never reloads the value following a store.
John McCall119a1c62010-12-04 02:32:38 +0000632 if (LV.isPropertyRef())
John McCallb418d742010-11-16 10:08:07 +0000633 return Val;
634
635 // If the lvalue is non-volatile, return the computed value of the assignment.
636 if (!LV.isVolatileQualified())
637 return Val;
638
639 return EmitLoadOfComplex(LV.getAddress(), LV.isVolatileQualified());
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000640}
641
Chris Lattner756a4d82007-08-21 17:15:50 +0000642ComplexPairTy ComplexExprEmitter::VisitBinComma(const BinaryOperator *E) {
John McCall2a416372010-12-05 02:00:02 +0000643 CGF.EmitIgnoredExpr(E->getLHS());
Chris Lattner756a4d82007-08-21 17:15:50 +0000644 return Visit(E->getRHS());
645}
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000646
647ComplexPairTy ComplexExprEmitter::
John McCall56ca35d2011-02-17 10:25:35 +0000648VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
Mike Stump7f79f9b2009-05-29 15:46:01 +0000649 TestAndClearIgnoreReal();
650 TestAndClearIgnoreImag();
Daniel Dunbar9615ecb2008-11-13 01:38:36 +0000651 llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
652 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
653 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000654
John McCall56ca35d2011-02-17 10:25:35 +0000655 // Bind the common expression if necessary.
656 CodeGenFunction::OpaqueValueMapping binding(CGF, E);
John McCall150b4622011-01-26 04:00:11 +0000657
John McCall56ca35d2011-02-17 10:25:35 +0000658 CodeGenFunction::ConditionalEvaluation eval(CGF);
659 CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock);
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000660
John McCall150b4622011-01-26 04:00:11 +0000661 eval.begin(CGF);
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000662 CGF.EmitBlock(LHSBlock);
Fariborz Jahanianaa3b57e2010-09-20 23:50:22 +0000663 ComplexPairTy LHS = Visit(E->getTrueExpr());
Chris Lattner5083a532007-08-21 17:39:38 +0000664 LHSBlock = Builder.GetInsertBlock();
Daniel Dunbard57a8712008-11-11 09:41:28 +0000665 CGF.EmitBranch(ContBlock);
John McCall150b4622011-01-26 04:00:11 +0000666 eval.end(CGF);
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000667
John McCall150b4622011-01-26 04:00:11 +0000668 eval.begin(CGF);
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000669 CGF.EmitBlock(RHSBlock);
John McCall56ca35d2011-02-17 10:25:35 +0000670 ComplexPairTy RHS = Visit(E->getFalseExpr());
Chris Lattner5083a532007-08-21 17:39:38 +0000671 RHSBlock = Builder.GetInsertBlock();
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000672 CGF.EmitBlock(ContBlock);
John McCall150b4622011-01-26 04:00:11 +0000673 eval.end(CGF);
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000674
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000675 // Create a PHI node for the real part.
Chris Lattner5083a532007-08-21 17:39:38 +0000676 llvm::PHINode *RealPN = Builder.CreatePHI(LHS.first->getType(), "cond.r");
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000677 RealPN->reserveOperandSpace(2);
678 RealPN->addIncoming(LHS.first, LHSBlock);
679 RealPN->addIncoming(RHS.first, RHSBlock);
680
681 // Create a PHI node for the imaginary part.
Chris Lattner5083a532007-08-21 17:39:38 +0000682 llvm::PHINode *ImagPN = Builder.CreatePHI(LHS.first->getType(), "cond.i");
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000683 ImagPN->reserveOperandSpace(2);
684 ImagPN->addIncoming(LHS.second, LHSBlock);
685 ImagPN->addIncoming(RHS.second, RHSBlock);
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000686
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000687 return ComplexPairTy(RealPN, ImagPN);
688}
689
Chris Lattnerd0b12032007-08-24 02:18:47 +0000690ComplexPairTy ComplexExprEmitter::VisitChooseExpr(ChooseExpr *E) {
Eli Friedman79769322009-03-04 05:52:32 +0000691 return Visit(E->getChosenSubExpr(CGF.getContext()));
Chris Lattnerd0b12032007-08-24 02:18:47 +0000692}
693
Eli Friedmanc4777f12008-05-13 23:11:35 +0000694ComplexPairTy ComplexExprEmitter::VisitInitListExpr(InitListExpr *E) {
Mike Stump7f79f9b2009-05-29 15:46:01 +0000695 bool Ignore = TestAndClearIgnoreReal();
696 (void)Ignore;
697 assert (Ignore == false && "init list ignored");
698 Ignore = TestAndClearIgnoreImag();
699 (void)Ignore;
700 assert (Ignore == false && "init list ignored");
Eli Friedmanc4777f12008-05-13 23:11:35 +0000701 if (E->getNumInits())
702 return Visit(E->getInit(0));
703
704 // Empty init list intializes to null
John McCall183700f2009-09-21 23:43:11 +0000705 QualType Ty = E->getType()->getAs<ComplexType>()->getElementType();
Eli Friedmanc4777f12008-05-13 23:11:35 +0000706 const llvm::Type* LTy = CGF.ConvertType(Ty);
Owen Andersonc9c88b42009-07-31 20:28:54 +0000707 llvm::Value* zeroConstant = llvm::Constant::getNullValue(LTy);
Eli Friedmanc4777f12008-05-13 23:11:35 +0000708 return ComplexPairTy(zeroConstant, zeroConstant);
709}
710
Daniel Dunbar4e484b82009-02-10 03:03:30 +0000711ComplexPairTy ComplexExprEmitter::VisitVAArgExpr(VAArgExpr *E) {
Daniel Dunbar07855702009-02-11 22:25:55 +0000712 llvm::Value *ArgValue = CGF.EmitVAListRef(E->getSubExpr());
Daniel Dunbar4e484b82009-02-10 03:03:30 +0000713 llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, E->getType());
714
715 if (!ArgPtr) {
716 CGF.ErrorUnsupported(E, "complex va_arg expression");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000717 const llvm::Type *EltTy =
John McCall183700f2009-09-21 23:43:11 +0000718 CGF.ConvertType(E->getType()->getAs<ComplexType>()->getElementType());
Daniel Dunbar4e484b82009-02-10 03:03:30 +0000719 llvm::Value *U = llvm::UndefValue::get(EltTy);
720 return ComplexPairTy(U, U);
721 }
722
723 // FIXME Volatility.
724 return EmitLoadOfComplex(ArgPtr, false);
725}
726
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000727//===----------------------------------------------------------------------===//
728// Entry Point into this File
729//===----------------------------------------------------------------------===//
730
731/// EmitComplexExpr - Emit the computation of the specified expression of
732/// complex type, ignoring the result.
Mike Stump7f79f9b2009-05-29 15:46:01 +0000733ComplexPairTy CodeGenFunction::EmitComplexExpr(const Expr *E, bool IgnoreReal,
John McCallb418d742010-11-16 10:08:07 +0000734 bool IgnoreImag) {
Chris Lattner9b2dc282008-04-04 16:54:41 +0000735 assert(E && E->getType()->isAnyComplexType() &&
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000736 "Invalid complex expression to emit");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000737
John McCallb418d742010-11-16 10:08:07 +0000738 return ComplexExprEmitter(*this, IgnoreReal, IgnoreImag)
Mike Stump7f79f9b2009-05-29 15:46:01 +0000739 .Visit(const_cast<Expr*>(E));
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000740}
741
Chris Lattner23b1cdb2007-08-23 23:43:33 +0000742/// EmitComplexExprIntoAddr - Emit the computation of the specified expression
743/// of complex type, storing into the specified Value*.
744void CodeGenFunction::EmitComplexExprIntoAddr(const Expr *E,
Chris Lattner190dbe22007-08-26 16:22:13 +0000745 llvm::Value *DestAddr,
746 bool DestIsVolatile) {
Chris Lattner9b2dc282008-04-04 16:54:41 +0000747 assert(E && E->getType()->isAnyComplexType() &&
Chris Lattner23b1cdb2007-08-23 23:43:33 +0000748 "Invalid complex expression to emit");
749 ComplexExprEmitter Emitter(*this);
750 ComplexPairTy Val = Emitter.Visit(const_cast<Expr*>(E));
Chris Lattner190dbe22007-08-26 16:22:13 +0000751 Emitter.EmitStoreOfComplex(Val, DestAddr, DestIsVolatile);
Chris Lattner23b1cdb2007-08-23 23:43:33 +0000752}
Chris Lattner9b655512007-08-31 22:49:20 +0000753
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000754/// StoreComplexToAddr - Store a complex number into the specified address.
755void CodeGenFunction::StoreComplexToAddr(ComplexPairTy V,
756 llvm::Value *DestAddr,
757 bool DestIsVolatile) {
758 ComplexExprEmitter(*this).EmitStoreOfComplex(V, DestAddr, DestIsVolatile);
759}
760
Chris Lattner9b655512007-08-31 22:49:20 +0000761/// LoadComplexFromAddr - Load a complex number from the specified address.
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000762ComplexPairTy CodeGenFunction::LoadComplexFromAddr(llvm::Value *SrcAddr,
Chris Lattner9b655512007-08-31 22:49:20 +0000763 bool SrcIsVolatile) {
764 return ComplexExprEmitter(*this).EmitLoadOfComplex(SrcAddr, SrcIsVolatile);
765}
John McCall83ce9d42010-11-16 23:07:28 +0000766
767LValue CodeGenFunction::EmitComplexAssignmentLValue(const BinaryOperator *E) {
John McCall2a416372010-12-05 02:00:02 +0000768 assert(E->getOpcode() == BO_Assign);
John McCall83ce9d42010-11-16 23:07:28 +0000769 ComplexPairTy Val; // ignored
John McCall2a416372010-12-05 02:00:02 +0000770 return ComplexExprEmitter(*this).EmitBinAssignLValue(E, Val);
771}
John McCall83ce9d42010-11-16 23:07:28 +0000772
John McCall2a416372010-12-05 02:00:02 +0000773LValue CodeGenFunction::
774EmitComplexCompoundAssignmentLValue(const CompoundAssignOperator *E) {
John McCall83ce9d42010-11-16 23:07:28 +0000775 ComplexPairTy(ComplexExprEmitter::*Op)(const ComplexExprEmitter::BinOpInfo &);
John McCall83ce9d42010-11-16 23:07:28 +0000776 switch (E->getOpcode()) {
John McCall83ce9d42010-11-16 23:07:28 +0000777 case BO_MulAssign: Op = &ComplexExprEmitter::EmitBinMul; break;
778 case BO_DivAssign: Op = &ComplexExprEmitter::EmitBinDiv; break;
779 case BO_SubAssign: Op = &ComplexExprEmitter::EmitBinSub; break;
780 case BO_AddAssign: Op = &ComplexExprEmitter::EmitBinAdd; break;
781
782 default:
783 llvm_unreachable("unexpected complex compound assignment");
784 Op = 0;
785 }
786
John McCall2a416372010-12-05 02:00:02 +0000787 ComplexPairTy Val; // ignored
788 return ComplexExprEmitter(*this).EmitCompoundAssignLValue(E, Op, Val);
John McCall83ce9d42010-11-16 23:07:28 +0000789}