blob: b2228f094c173980829d37f3fecba8634c640532 [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"
Stephen Hines176edba2014-12-01 14:53:08 -080018#include "llvm/ADT/STLExtras.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000019#include "llvm/ADT/SmallString.h"
Chandler Carruth3b844ba2013-01-02 11:45:17 +000020#include "llvm/IR/Constants.h"
21#include "llvm/IR/Function.h"
Stephen Hines176edba2014-12-01 14:53:08 -080022#include "llvm/IR/Instructions.h"
23#include "llvm/IR/MDBuilder.h"
24#include "llvm/IR/Metadata.h"
JF Bastienab96e562013-07-17 05:57:42 +000025#include <algorithm>
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000026using namespace clang;
27using namespace CodeGen;
28
29//===----------------------------------------------------------------------===//
Chris Lattner05ba49c2007-08-21 05:54:53 +000030// Complex Expression Emitter
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000031//===----------------------------------------------------------------------===//
32
Chris Lattner58dee102007-08-21 16:57:55 +000033typedef CodeGenFunction::ComplexPairTy ComplexPairTy;
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000034
John McCall9d232c82013-03-07 21:37:08 +000035/// Return the complex type that we are meant to emit.
36static const ComplexType *getComplexType(QualType type) {
37 type = type.getCanonicalType();
38 if (const ComplexType *comp = dyn_cast<ComplexType>(type)) {
39 return comp;
40 } else {
41 return cast<ComplexType>(cast<AtomicType>(type)->getValueType());
42 }
43}
44
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000045namespace {
Benjamin Kramer85b45212009-11-28 19:45:26 +000046class ComplexExprEmitter
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000047 : public StmtVisitor<ComplexExprEmitter, ComplexPairTy> {
48 CodeGenFunction &CGF;
Daniel Dunbar45d196b2008-11-01 01:53:16 +000049 CGBuilderTy &Builder;
Mike Stump7f79f9b2009-05-29 15:46:01 +000050 bool IgnoreReal;
51 bool IgnoreImag;
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000052public:
John McCallb418d742010-11-16 10:08:07 +000053 ComplexExprEmitter(CodeGenFunction &cgf, bool ir=false, bool ii=false)
54 : CGF(cgf), Builder(CGF.Builder), IgnoreReal(ir), IgnoreImag(ii) {
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000055 }
56
Mike Stumpdb52dcd2009-09-09 13:00:44 +000057
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000058 //===--------------------------------------------------------------------===//
59 // Utilities
60 //===--------------------------------------------------------------------===//
61
Mike Stump7f79f9b2009-05-29 15:46:01 +000062 bool TestAndClearIgnoreReal() {
63 bool I = IgnoreReal;
64 IgnoreReal = false;
65 return I;
66 }
67 bool TestAndClearIgnoreImag() {
68 bool I = IgnoreImag;
69 IgnoreImag = false;
70 return I;
71 }
Mike Stump7f79f9b2009-05-29 15:46:01 +000072
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000073 /// EmitLoadOfLValue - Given an expression with complex type that represents a
74 /// value l-value, this method emits the address of the l-value, then loads
75 /// and returns the result.
Chris Lattner46d7d9f2007-08-21 17:28:34 +000076 ComplexPairTy EmitLoadOfLValue(const Expr *E) {
Nick Lewycky4ee7dc22013-10-02 02:29:49 +000077 return EmitLoadOfLValue(CGF.EmitLValue(E), E->getExprLoc());
John McCall0e800c92010-12-04 08:14:53 +000078 }
79
Nick Lewycky4ee7dc22013-10-02 02:29:49 +000080 ComplexPairTy EmitLoadOfLValue(LValue LV, SourceLocation Loc);
John McCall0e800c92010-12-04 08:14:53 +000081
Chris Lattner46d7d9f2007-08-21 17:28:34 +000082 /// EmitStoreOfComplex - Store the specified real/imag parts into the
83 /// specified value pointer.
John McCall9d232c82013-03-07 21:37:08 +000084 void EmitStoreOfComplex(ComplexPairTy Val, LValue LV, bool isInit);
Mike Stumpdb52dcd2009-09-09 13:00:44 +000085
Chris Lattnerab340c22007-08-26 22:09:01 +000086 /// EmitComplexToComplexCast - Emit a cast from complex value Val to DestType.
87 ComplexPairTy EmitComplexToComplexCast(ComplexPairTy Val, QualType SrcType,
88 QualType DestType);
Eli Friedman0934e182013-06-12 01:40:06 +000089 /// EmitComplexToComplexCast - Emit a cast from scalar value Val to DestType.
90 ComplexPairTy EmitScalarToComplexCast(llvm::Value *Val, QualType SrcType,
91 QualType DestType);
Mike Stumpdb52dcd2009-09-09 13:00:44 +000092
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000093 //===--------------------------------------------------------------------===//
94 // Visitor Methods
95 //===--------------------------------------------------------------------===//
Chris Lattner776c6492007-08-21 18:51:13 +000096
Fariborz Jahanianaa3b57e2010-09-20 23:50:22 +000097 ComplexPairTy Visit(Expr *E) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -070098 ApplyDebugLocation DL(CGF, E);
John McCallcf0b2d82011-02-17 19:02:56 +000099 return StmtVisitor<ComplexExprEmitter, ComplexPairTy>::Visit(E);
Fariborz Jahanianaa3b57e2010-09-20 23:50:22 +0000100 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700101
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000102 ComplexPairTy VisitStmt(Stmt *S) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000103 S->dump(CGF.getContext().getSourceManager());
David Blaikieb219cfc2011-09-23 05:06:16 +0000104 llvm_unreachable("Stmt can't have complex result type!");
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000105 }
Chris Lattner776c6492007-08-21 18:51:13 +0000106 ComplexPairTy VisitExpr(Expr *S);
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000107 ComplexPairTy VisitParenExpr(ParenExpr *PE) { return Visit(PE->getSubExpr());}
Peter Collingbournef111d932011-04-15 00:35:48 +0000108 ComplexPairTy VisitGenericSelectionExpr(GenericSelectionExpr *GE) {
109 return Visit(GE->getResultExpr());
110 }
Chris Lattnerd272ff02007-08-26 05:57:57 +0000111 ComplexPairTy VisitImaginaryLiteral(const ImaginaryLiteral *IL);
John McCall91a57552011-07-15 05:09:51 +0000112 ComplexPairTy
113 VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *PE) {
114 return Visit(PE->getReplacement());
115 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000116
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000117 // l-values.
John McCallf4b88a42012-03-10 09:33:50 +0000118 ComplexPairTy VisitDeclRefExpr(DeclRefExpr *E) {
119 if (CodeGenFunction::ConstantEmission result = CGF.tryEmitAsConstant(E)) {
John McCalldd2ecee2012-03-10 03:05:10 +0000120 if (result.isReference())
Nick Lewycky4ee7dc22013-10-02 02:29:49 +0000121 return EmitLoadOfLValue(result.getReferenceLValue(CGF, E),
122 E->getExprLoc());
John McCalldd2ecee2012-03-10 03:05:10 +0000123
Eli Friedmanbe6d9132013-07-16 20:19:04 +0000124 llvm::Constant *pair = result.getValue();
125 return ComplexPairTy(pair->getAggregateElement(0U),
126 pair->getAggregateElement(1U));
John McCalldd2ecee2012-03-10 03:05:10 +0000127 }
John McCallf4b88a42012-03-10 09:33:50 +0000128 return EmitLoadOfLValue(E);
John McCalldd2ecee2012-03-10 03:05:10 +0000129 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000130 ComplexPairTy VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
Daniel Dunbar42f963d2009-06-10 04:38:50 +0000131 return EmitLoadOfLValue(E);
132 }
Daniel Dunbar42f963d2009-06-10 04:38:50 +0000133 ComplexPairTy VisitObjCMessageExpr(ObjCMessageExpr *E) {
134 return CGF.EmitObjCMessageExpr(E).getComplexVal();
135 }
Chris Lattner8bcd7232007-08-21 18:03:58 +0000136 ComplexPairTy VisitArraySubscriptExpr(Expr *E) { return EmitLoadOfLValue(E); }
Chris Lattnerd272ff02007-08-26 05:57:57 +0000137 ComplexPairTy VisitMemberExpr(const Expr *E) { return EmitLoadOfLValue(E); }
John McCalle996ffd2011-02-16 08:02:54 +0000138 ComplexPairTy VisitOpaqueValueExpr(OpaqueValueExpr *E) {
John McCall56ca35d2011-02-17 10:25:35 +0000139 if (E->isGLValue())
Nick Lewycky4ee7dc22013-10-02 02:29:49 +0000140 return EmitLoadOfLValue(CGF.getOpaqueLValueMapping(E), E->getExprLoc());
John McCall56ca35d2011-02-17 10:25:35 +0000141 return CGF.getOpaqueRValueMapping(E).getComplexVal();
John McCalle996ffd2011-02-16 08:02:54 +0000142 }
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000143
John McCall4b9c2d22011-11-06 09:01:30 +0000144 ComplexPairTy VisitPseudoObjectExpr(PseudoObjectExpr *E) {
145 return CGF.EmitPseudoObjectRValue(E).getComplexVal();
146 }
147
Chris Lattnere73e8e22007-08-21 22:33:41 +0000148 // FIXME: CompoundLiteralExpr
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000149
Stephen Hines176edba2014-12-01 14:53:08 -0800150 ComplexPairTy EmitCast(CastKind CK, Expr *Op, QualType DestTy);
Chris Lattnerd272ff02007-08-26 05:57:57 +0000151 ComplexPairTy VisitImplicitCastExpr(ImplicitCastExpr *E) {
152 // Unlike for scalars, we don't have to worry about function->ptr demotion
153 // here.
Douglas Gregor77d439a2010-07-14 21:35:45 +0000154 return EmitCast(E->getCastKind(), E->getSubExpr(), E->getType());
Chris Lattnerd272ff02007-08-26 05:57:57 +0000155 }
156 ComplexPairTy VisitCastExpr(CastExpr *E) {
Douglas Gregor77d439a2010-07-14 21:35:45 +0000157 return EmitCast(E->getCastKind(), E->getSubExpr(), E->getType());
Chris Lattnerd272ff02007-08-26 05:57:57 +0000158 }
Chris Lattner419d25e2007-08-23 21:38:16 +0000159 ComplexPairTy VisitCallExpr(const CallExpr *E);
Chris Lattner7e20d092007-08-31 22:51:38 +0000160 ComplexPairTy VisitStmtExpr(const StmtExpr *E);
161
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000162 // Operators.
Chris Lattner3070f982007-08-21 22:25:29 +0000163 ComplexPairTy VisitPrePostIncDec(const UnaryOperator *E,
Chris Lattnerdd36d322010-01-09 21:40:03 +0000164 bool isInc, bool isPre) {
165 LValue LV = CGF.EmitLValue(E->getSubExpr());
166 return CGF.EmitComplexPrePostIncDec(E, LV, isInc, isPre);
167 }
Chris Lattner3070f982007-08-21 22:25:29 +0000168 ComplexPairTy VisitUnaryPostDec(const UnaryOperator *E) {
169 return VisitPrePostIncDec(E, false, false);
170 }
171 ComplexPairTy VisitUnaryPostInc(const UnaryOperator *E) {
172 return VisitPrePostIncDec(E, true, false);
173 }
174 ComplexPairTy VisitUnaryPreDec(const UnaryOperator *E) {
175 return VisitPrePostIncDec(E, false, true);
176 }
177 ComplexPairTy VisitUnaryPreInc(const UnaryOperator *E) {
178 return VisitPrePostIncDec(E, true, true);
179 }
180 ComplexPairTy VisitUnaryDeref(const Expr *E) { return EmitLoadOfLValue(E); }
Chris Lattnerfa2b9c92007-08-21 20:08:23 +0000181 ComplexPairTy VisitUnaryPlus (const UnaryOperator *E) {
Mike Stump7f79f9b2009-05-29 15:46:01 +0000182 TestAndClearIgnoreReal();
183 TestAndClearIgnoreImag();
Chris Lattnerfa2b9c92007-08-21 20:08:23 +0000184 return Visit(E->getSubExpr());
185 }
186 ComplexPairTy VisitUnaryMinus (const UnaryOperator *E);
Chris Lattnere98a11c2007-08-21 20:41:44 +0000187 ComplexPairTy VisitUnaryNot (const UnaryOperator *E);
Sebastian Redl05189992008-11-11 17:56:53 +0000188 // LNot,Real,Imag never return complex.
Chris Lattnere98a11c2007-08-21 20:41:44 +0000189 ComplexPairTy VisitUnaryExtension(const UnaryOperator *E) {
190 return Visit(E->getSubExpr());
191 }
Chris Lattner04421082008-04-08 04:40:51 +0000192 ComplexPairTy VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
193 return Visit(DAE->getExpr());
194 }
Richard Smithc3bf52c2013-04-20 22:23:05 +0000195 ComplexPairTy VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) {
196 CodeGenFunction::CXXDefaultInitExprScope Scope(CGF);
197 return Visit(DIE->getExpr());
198 }
John McCall4765fa02010-12-06 08:20:24 +0000199 ComplexPairTy VisitExprWithCleanups(ExprWithCleanups *E) {
John McCall1a343eb2011-11-10 08:15:53 +0000200 CGF.enterFullExpression(E);
201 CodeGenFunction::RunCleanupsScope Scope(CGF);
202 return Visit(E->getSubExpr());
Anders Carlsson4b76b4f2009-05-31 00:12:05 +0000203 }
Douglas Gregored8abf12010-07-08 06:14:04 +0000204 ComplexPairTy VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
Argyrios Kyrtzidis7267f782008-08-23 19:35:47 +0000205 assert(E->getType()->isAnyComplexType() && "Expected complex type!");
John McCall9d232c82013-03-07 21:37:08 +0000206 QualType Elem = E->getType()->castAs<ComplexType>()->getElementType();
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000207 llvm::Constant *Null = llvm::Constant::getNullValue(CGF.ConvertType(Elem));
Argyrios Kyrtzidis7267f782008-08-23 19:35:47 +0000208 return ComplexPairTy(Null, Null);
209 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000210 ComplexPairTy VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
211 assert(E->getType()->isAnyComplexType() && "Expected complex type!");
John McCall9d232c82013-03-07 21:37:08 +0000212 QualType Elem = E->getType()->castAs<ComplexType>()->getElementType();
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000213 llvm::Constant *Null =
Owen Andersonc9c88b42009-07-31 20:28:54 +0000214 llvm::Constant::getNullValue(CGF.ConvertType(Elem));
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000215 return ComplexPairTy(Null, Null);
216 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000217
Chris Lattnerab340c22007-08-26 22:09:01 +0000218 struct BinOpInfo {
219 ComplexPairTy LHS;
220 ComplexPairTy RHS;
221 QualType Ty; // Computation Type.
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000222 };
223
Chris Lattnerab340c22007-08-26 22:09:01 +0000224 BinOpInfo EmitBinOps(const BinaryOperator *E);
John McCallb418d742010-11-16 10:08:07 +0000225 LValue EmitCompoundAssignLValue(const CompoundAssignOperator *E,
226 ComplexPairTy (ComplexExprEmitter::*Func)
227 (const BinOpInfo &),
Eli Friedman0934e182013-06-12 01:40:06 +0000228 RValue &Val);
Chris Lattnerab340c22007-08-26 22:09:01 +0000229 ComplexPairTy EmitCompoundAssign(const CompoundAssignOperator *E,
230 ComplexPairTy (ComplexExprEmitter::*Func)
231 (const BinOpInfo &));
232
233 ComplexPairTy EmitBinAdd(const BinOpInfo &Op);
234 ComplexPairTy EmitBinSub(const BinOpInfo &Op);
235 ComplexPairTy EmitBinMul(const BinOpInfo &Op);
236 ComplexPairTy EmitBinDiv(const BinOpInfo &Op);
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000237
Stephen Hines176edba2014-12-01 14:53:08 -0800238 ComplexPairTy EmitComplexBinOpLibCall(StringRef LibCallName,
239 const BinOpInfo &Op);
240
Chris Lattnerab340c22007-08-26 22:09:01 +0000241 ComplexPairTy VisitBinAdd(const BinaryOperator *E) {
242 return EmitBinAdd(EmitBinOps(E));
243 }
244 ComplexPairTy VisitBinSub(const BinaryOperator *E) {
245 return EmitBinSub(EmitBinOps(E));
246 }
John McCallb418d742010-11-16 10:08:07 +0000247 ComplexPairTy VisitBinMul(const BinaryOperator *E) {
248 return EmitBinMul(EmitBinOps(E));
249 }
Chris Lattnerab340c22007-08-26 22:09:01 +0000250 ComplexPairTy VisitBinDiv(const BinaryOperator *E) {
251 return EmitBinDiv(EmitBinOps(E));
252 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000253
Chris Lattnerab340c22007-08-26 22:09:01 +0000254 // Compound assignments.
255 ComplexPairTy VisitBinAddAssign(const CompoundAssignOperator *E) {
256 return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinAdd);
257 }
258 ComplexPairTy VisitBinSubAssign(const CompoundAssignOperator *E) {
259 return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinSub);
260 }
261 ComplexPairTy VisitBinMulAssign(const CompoundAssignOperator *E) {
262 return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinMul);
263 }
264 ComplexPairTy VisitBinDivAssign(const CompoundAssignOperator *E) {
265 return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinDiv);
266 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000267
Chris Lattner612c40c2007-08-26 21:27:07 +0000268 // GCC rejects rem/and/or/xor for integer complex.
Chris Lattner4034edb2007-08-21 17:12:50 +0000269 // Logical and/or always return int, never complex.
Chris Lattner58dee102007-08-21 16:57:55 +0000270
271 // No comparisons produce a complex result.
John McCallb418d742010-11-16 10:08:07 +0000272
273 LValue EmitBinAssignLValue(const BinaryOperator *E,
274 ComplexPairTy &Val);
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000275 ComplexPairTy VisitBinAssign (const BinaryOperator *E);
Chris Lattner756a4d82007-08-21 17:15:50 +0000276 ComplexPairTy VisitBinComma (const BinaryOperator *E);
277
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000278
John McCall56ca35d2011-02-17 10:25:35 +0000279 ComplexPairTy
280 VisitAbstractConditionalOperator(const AbstractConditionalOperator *CO);
Chris Lattnerd0b12032007-08-24 02:18:47 +0000281 ComplexPairTy VisitChooseExpr(ChooseExpr *CE);
Eli Friedmanc4777f12008-05-13 23:11:35 +0000282
283 ComplexPairTy VisitInitListExpr(InitListExpr *E);
Daniel Dunbar4e484b82009-02-10 03:03:30 +0000284
Eli Friedmanfce47152012-02-27 20:26:13 +0000285 ComplexPairTy VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
286 return EmitLoadOfLValue(E);
287 }
288
Daniel Dunbar4e484b82009-02-10 03:03:30 +0000289 ComplexPairTy VisitVAArgExpr(VAArgExpr *E);
Eli Friedman276b0612011-10-11 02:20:01 +0000290
291 ComplexPairTy VisitAtomicExpr(AtomicExpr *E) {
292 return CGF.EmitAtomicExpr(E).getComplexVal();
293 }
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000294};
295} // end anonymous namespace.
296
297//===----------------------------------------------------------------------===//
298// Utilities
299//===----------------------------------------------------------------------===//
300
John McCall9d232c82013-03-07 21:37:08 +0000301/// EmitLoadOfLValue - Given an RValue reference for a complex, emit code to
Chris Lattner46d7d9f2007-08-21 17:28:34 +0000302/// load the real and imaginary pieces, returning them as Real/Imag.
Nick Lewycky4ee7dc22013-10-02 02:29:49 +0000303ComplexPairTy ComplexExprEmitter::EmitLoadOfLValue(LValue lvalue,
304 SourceLocation loc) {
John McCall9d232c82013-03-07 21:37:08 +0000305 assert(lvalue.isSimple() && "non-simple complex l-value?");
John McCall9eda3ab2013-03-07 21:37:17 +0000306 if (lvalue.getType()->isAtomicType())
Nick Lewycky4ee7dc22013-10-02 02:29:49 +0000307 return CGF.EmitAtomicLoad(lvalue, loc).getComplexVal();
John McCall9eda3ab2013-03-07 21:37:17 +0000308
John McCall9d232c82013-03-07 21:37:08 +0000309 llvm::Value *SrcPtr = lvalue.getAddress();
310 bool isVolatile = lvalue.isVolatileQualified();
JF Bastienab96e562013-07-17 05:57:42 +0000311 unsigned AlignR = lvalue.getAlignment().getQuantity();
312 ASTContext &C = CGF.getContext();
313 QualType ComplexTy = lvalue.getType();
314 unsigned ComplexAlign = C.getTypeAlignInChars(ComplexTy).getQuantity();
315 unsigned AlignI = std::min(AlignR, ComplexAlign);
John McCall9d232c82013-03-07 21:37:08 +0000316
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700317 llvm::Value *Real=nullptr, *Imag=nullptr;
Chris Lattner6b5d0bf2007-08-26 22:47:40 +0000318
John McCallff624c12010-11-14 09:40:28 +0000319 if (!IgnoreReal || isVolatile) {
Benjamin Kramer56a44f72009-11-29 19:51:45 +0000320 llvm::Value *RealP = Builder.CreateStructGEP(SrcPtr, 0,
321 SrcPtr->getName() + ".realp");
JF Bastienab96e562013-07-17 05:57:42 +0000322 Real = Builder.CreateAlignedLoad(RealP, AlignR, isVolatile,
323 SrcPtr->getName() + ".real");
Mike Stump7f79f9b2009-05-29 15:46:01 +0000324 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000325
John McCallff624c12010-11-14 09:40:28 +0000326 if (!IgnoreImag || isVolatile) {
Benjamin Kramer56a44f72009-11-29 19:51:45 +0000327 llvm::Value *ImagP = Builder.CreateStructGEP(SrcPtr, 1,
328 SrcPtr->getName() + ".imagp");
JF Bastienab96e562013-07-17 05:57:42 +0000329 Imag = Builder.CreateAlignedLoad(ImagP, AlignI, isVolatile,
330 SrcPtr->getName() + ".imag");
Mike Stump7f79f9b2009-05-29 15:46:01 +0000331 }
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000332 return ComplexPairTy(Real, Imag);
333}
334
Chris Lattner46d7d9f2007-08-21 17:28:34 +0000335/// EmitStoreOfComplex - Store the specified real/imag parts into the
336/// specified value pointer.
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700337void ComplexExprEmitter::EmitStoreOfComplex(ComplexPairTy Val, LValue lvalue,
John McCall9d232c82013-03-07 21:37:08 +0000338 bool isInit) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700339 if (lvalue.getType()->isAtomicType() ||
340 (!isInit && CGF.LValueIsSuitableForInlineAtomic(lvalue)))
John McCall9eda3ab2013-03-07 21:37:17 +0000341 return CGF.EmitAtomicStore(RValue::getComplex(Val), lvalue, isInit);
342
John McCall9d232c82013-03-07 21:37:08 +0000343 llvm::Value *Ptr = lvalue.getAddress();
Chris Lattner36b6a0a2008-03-19 05:19:41 +0000344 llvm::Value *RealPtr = Builder.CreateStructGEP(Ptr, 0, "real");
345 llvm::Value *ImagPtr = Builder.CreateStructGEP(Ptr, 1, "imag");
JF Bastienab96e562013-07-17 05:57:42 +0000346 unsigned AlignR = lvalue.getAlignment().getQuantity();
347 ASTContext &C = CGF.getContext();
348 QualType ComplexTy = lvalue.getType();
349 unsigned ComplexAlign = C.getTypeAlignInChars(ComplexTy).getQuantity();
350 unsigned AlignI = std::min(AlignR, ComplexAlign);
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000351
JF Bastienab96e562013-07-17 05:57:42 +0000352 Builder.CreateAlignedStore(Val.first, RealPtr, AlignR,
353 lvalue.isVolatileQualified());
354 Builder.CreateAlignedStore(Val.second, ImagPtr, AlignI,
355 lvalue.isVolatileQualified());
Chris Lattner46d7d9f2007-08-21 17:28:34 +0000356}
357
358
359
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000360//===----------------------------------------------------------------------===//
361// Visitor Methods
362//===----------------------------------------------------------------------===//
363
Chris Lattner776c6492007-08-21 18:51:13 +0000364ComplexPairTy ComplexExprEmitter::VisitExpr(Expr *E) {
Fariborz Jahanian776b7902011-09-02 20:03:16 +0000365 CGF.ErrorUnsupported(E, "complex expression");
366 llvm::Type *EltTy =
John McCall9d232c82013-03-07 21:37:08 +0000367 CGF.ConvertType(getComplexType(E->getType())->getElementType());
Fariborz Jahanian776b7902011-09-02 20:03:16 +0000368 llvm::Value *U = llvm::UndefValue::get(EltTy);
369 return ComplexPairTy(U, U);
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000370}
371
Chris Lattnerd272ff02007-08-26 05:57:57 +0000372ComplexPairTy ComplexExprEmitter::
373VisitImaginaryLiteral(const ImaginaryLiteral *IL) {
Chris Lattnerdb68f1b2007-08-26 03:51:12 +0000374 llvm::Value *Imag = CGF.EmitScalarExpr(IL->getSubExpr());
John McCall8d3d6c92011-01-13 02:03:06 +0000375 return ComplexPairTy(llvm::Constant::getNullValue(Imag->getType()), Imag);
Chris Lattnerdb68f1b2007-08-26 03:51:12 +0000376}
377
378
Chris Lattner419d25e2007-08-23 21:38:16 +0000379ComplexPairTy ComplexExprEmitter::VisitCallExpr(const CallExpr *E) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700380 if (E->getCallReturnType(CGF.getContext())->isReferenceType())
Anders Carlssone9f2f452009-05-27 03:37:57 +0000381 return EmitLoadOfLValue(E);
382
Chris Lattner9b655512007-08-31 22:49:20 +0000383 return CGF.EmitCallExpr(E).getComplexVal();
Chris Lattner419d25e2007-08-23 21:38:16 +0000384}
385
Chris Lattner7e20d092007-08-31 22:51:38 +0000386ComplexPairTy ComplexExprEmitter::VisitStmtExpr(const StmtExpr *E) {
John McCall150b4622011-01-26 04:00:11 +0000387 CodeGenFunction::StmtExprEvaluation eval(CGF);
Eli Friedman2ac2fa72013-06-10 22:04:49 +0000388 llvm::Value *RetAlloca = CGF.EmitCompoundStmt(*E->getSubStmt(), true);
389 assert(RetAlloca && "Expected complex return value");
Nick Lewycky4ee7dc22013-10-02 02:29:49 +0000390 return EmitLoadOfLValue(CGF.MakeAddrLValue(RetAlloca, E->getType()),
391 E->getExprLoc());
Chris Lattner7e20d092007-08-31 22:51:38 +0000392}
393
Chris Lattnerab340c22007-08-26 22:09:01 +0000394/// EmitComplexToComplexCast - Emit a cast from complex value Val to DestType.
395ComplexPairTy ComplexExprEmitter::EmitComplexToComplexCast(ComplexPairTy Val,
396 QualType SrcType,
397 QualType DestType) {
398 // Get the src/dest element type.
John McCall9d232c82013-03-07 21:37:08 +0000399 SrcType = SrcType->castAs<ComplexType>()->getElementType();
400 DestType = DestType->castAs<ComplexType>()->getElementType();
Chris Lattnerd272ff02007-08-26 05:57:57 +0000401
Daniel Dunbar56f0d162009-01-29 06:44:03 +0000402 // C99 6.3.1.6: When a value of complex type is converted to another
Daniel Dunbare12d8e32009-01-26 18:02:34 +0000403 // complex type, both the real and imaginary parts follow the conversion
Chris Lattnerab340c22007-08-26 22:09:01 +0000404 // rules for the corresponding real types.
405 Val.first = CGF.EmitScalarConversion(Val.first, SrcType, DestType);
406 Val.second = CGF.EmitScalarConversion(Val.second, SrcType, DestType);
407 return Val;
408}
409
Eli Friedman0934e182013-06-12 01:40:06 +0000410ComplexPairTy ComplexExprEmitter::EmitScalarToComplexCast(llvm::Value *Val,
411 QualType SrcType,
412 QualType DestType) {
413 // Convert the input element to the element type of the complex.
414 DestType = DestType->castAs<ComplexType>()->getElementType();
415 Val = CGF.EmitScalarConversion(Val, SrcType, DestType);
416
417 // Return (realval, 0).
418 return ComplexPairTy(Val, llvm::Constant::getNullValue(Val->getType()));
419}
420
Stephen Hines176edba2014-12-01 14:53:08 -0800421ComplexPairTy ComplexExprEmitter::EmitCast(CastKind CK, Expr *Op,
Douglas Gregor77d439a2010-07-14 21:35:45 +0000422 QualType DestTy) {
John McCallf6a16482010-12-04 03:47:34 +0000423 switch (CK) {
Eli Friedman65949422011-06-25 02:58:47 +0000424 case CK_Dependent: llvm_unreachable("dependent cast kind in IR gen!");
John McCallf6a16482010-12-04 03:47:34 +0000425
David Chisnall7a7ee302012-01-16 17:27:18 +0000426 // Atomic to non-atomic casts may be more than a no-op for some platforms and
427 // for some types.
428 case CK_AtomicToNonAtomic:
429 case CK_NonAtomicToAtomic:
John McCallf6a16482010-12-04 03:47:34 +0000430 case CK_NoOp:
431 case CK_LValueToRValue:
Eli Friedman65949422011-06-25 02:58:47 +0000432 case CK_UserDefinedConversion:
John McCallf6a16482010-12-04 03:47:34 +0000433 return Visit(Op);
434
Eli Friedman65949422011-06-25 02:58:47 +0000435 case CK_LValueBitCast: {
John McCall9d232c82013-03-07 21:37:08 +0000436 LValue origLV = CGF.EmitLValue(Op);
437 llvm::Value *V = origLV.getAddress();
Stephen Hines651f13c2014-04-23 16:59:28 -0700438 V = Builder.CreateBitCast(V,
Eli Friedman65949422011-06-25 02:58:47 +0000439 CGF.ConvertType(CGF.getContext().getPointerType(DestTy)));
John McCall9d232c82013-03-07 21:37:08 +0000440 return EmitLoadOfLValue(CGF.MakeAddrLValue(V, DestTy,
Nick Lewycky4ee7dc22013-10-02 02:29:49 +0000441 origLV.getAlignment()),
442 Op->getExprLoc());
Douglas Gregor77d439a2010-07-14 21:35:45 +0000443 }
Chris Lattnerd272ff02007-08-26 05:57:57 +0000444
Eli Friedman65949422011-06-25 02:58:47 +0000445 case CK_BitCast:
446 case CK_BaseToDerived:
447 case CK_DerivedToBase:
448 case CK_UncheckedDerivedToBase:
449 case CK_Dynamic:
450 case CK_ToUnion:
451 case CK_ArrayToPointerDecay:
452 case CK_FunctionToPointerDecay:
453 case CK_NullToPointer:
454 case CK_NullToMemberPointer:
455 case CK_BaseToDerivedMemberPointer:
456 case CK_DerivedToBaseMemberPointer:
457 case CK_MemberPointerToBoolean:
John McCall4d4e5c12012-02-15 01:22:51 +0000458 case CK_ReinterpretMemberPointer:
Eli Friedman65949422011-06-25 02:58:47 +0000459 case CK_ConstructorConversion:
460 case CK_IntegralToPointer:
461 case CK_PointerToIntegral:
462 case CK_PointerToBoolean:
463 case CK_ToVoid:
464 case CK_VectorSplat:
465 case CK_IntegralCast:
466 case CK_IntegralToBoolean:
467 case CK_IntegralToFloating:
468 case CK_FloatingToIntegral:
469 case CK_FloatingToBoolean:
470 case CK_FloatingCast:
John McCall1d9b3b22011-09-09 05:25:32 +0000471 case CK_CPointerToObjCPointerCast:
472 case CK_BlockPointerToObjCPointerCast:
Eli Friedman65949422011-06-25 02:58:47 +0000473 case CK_AnyPointerToBlockPointerCast:
474 case CK_ObjCObjectLValueCast:
475 case CK_FloatingComplexToReal:
476 case CK_FloatingComplexToBoolean:
477 case CK_IntegralComplexToReal:
478 case CK_IntegralComplexToBoolean:
John McCall33e56f32011-09-10 06:18:15 +0000479 case CK_ARCProduceObject:
480 case CK_ARCConsumeObject:
481 case CK_ARCReclaimReturnedObject:
482 case CK_ARCExtendBlockObject:
Douglas Gregorac1303e2012-02-22 05:02:47 +0000483 case CK_CopyAndAutoreleaseBlockObject:
Eli Friedmana6c66ce2012-08-31 00:14:07 +0000484 case CK_BuiltinFnToFnPtr:
Guy Benyeie6b9d802013-01-20 12:31:11 +0000485 case CK_ZeroToOCLEvent:
Stephen Hines651f13c2014-04-23 16:59:28 -0700486 case CK_AddressSpaceConversion:
Eli Friedman65949422011-06-25 02:58:47 +0000487 llvm_unreachable("invalid cast kind for complex value");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000488
Eli Friedman65949422011-06-25 02:58:47 +0000489 case CK_FloatingRealToComplex:
Eli Friedman0934e182013-06-12 01:40:06 +0000490 case CK_IntegralRealToComplex:
491 return EmitScalarToComplexCast(CGF.EmitScalarExpr(Op),
492 Op->getType(), DestTy);
Eli Friedman65949422011-06-25 02:58:47 +0000493
494 case CK_FloatingComplexCast:
495 case CK_FloatingComplexToIntegralComplex:
496 case CK_IntegralComplexCast:
497 case CK_IntegralComplexToFloatingComplex:
498 return EmitComplexToComplexCast(Visit(Op), Op->getType(), DestTy);
499 }
500
501 llvm_unreachable("unknown cast resulting in complex value");
Chris Lattnerd272ff02007-08-26 05:57:57 +0000502}
503
Chris Lattnerfa2b9c92007-08-21 20:08:23 +0000504ComplexPairTy ComplexExprEmitter::VisitUnaryMinus(const UnaryOperator *E) {
Mike Stump7f79f9b2009-05-29 15:46:01 +0000505 TestAndClearIgnoreReal();
506 TestAndClearIgnoreImag();
Chris Lattnerfa2b9c92007-08-21 20:08:23 +0000507 ComplexPairTy Op = Visit(E->getSubExpr());
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000508
Chris Lattner87415d22009-06-17 06:36:24 +0000509 llvm::Value *ResR, *ResI;
Duncan Sandsf177d9d2010-02-15 16:14:01 +0000510 if (Op.first->getType()->isFloatingPointTy()) {
Chris Lattner87415d22009-06-17 06:36:24 +0000511 ResR = Builder.CreateFNeg(Op.first, "neg.r");
512 ResI = Builder.CreateFNeg(Op.second, "neg.i");
513 } else {
514 ResR = Builder.CreateNeg(Op.first, "neg.r");
515 ResI = Builder.CreateNeg(Op.second, "neg.i");
516 }
Chris Lattnerfa2b9c92007-08-21 20:08:23 +0000517 return ComplexPairTy(ResR, ResI);
518}
519
Chris Lattnere98a11c2007-08-21 20:41:44 +0000520ComplexPairTy ComplexExprEmitter::VisitUnaryNot(const UnaryOperator *E) {
Mike Stump7f79f9b2009-05-29 15:46:01 +0000521 TestAndClearIgnoreReal();
522 TestAndClearIgnoreImag();
Chris Lattnere98a11c2007-08-21 20:41:44 +0000523 // ~(a+ib) = a + i*-b
524 ComplexPairTy Op = Visit(E->getSubExpr());
Chris Lattner87415d22009-06-17 06:36:24 +0000525 llvm::Value *ResI;
Duncan Sandsf177d9d2010-02-15 16:14:01 +0000526 if (Op.second->getType()->isFloatingPointTy())
Chris Lattner87415d22009-06-17 06:36:24 +0000527 ResI = Builder.CreateFNeg(Op.second, "conj.i");
528 else
529 ResI = Builder.CreateNeg(Op.second, "conj.i");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000530
Chris Lattnere98a11c2007-08-21 20:41:44 +0000531 return ComplexPairTy(Op.first, ResI);
532}
Chris Lattnerfa2b9c92007-08-21 20:08:23 +0000533
Chris Lattnerab340c22007-08-26 22:09:01 +0000534ComplexPairTy ComplexExprEmitter::EmitBinAdd(const BinOpInfo &Op) {
Chris Lattner87415d22009-06-17 06:36:24 +0000535 llvm::Value *ResR, *ResI;
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000536
Duncan Sandsf177d9d2010-02-15 16:14:01 +0000537 if (Op.LHS.first->getType()->isFloatingPointTy()) {
Chris Lattner87415d22009-06-17 06:36:24 +0000538 ResR = Builder.CreateFAdd(Op.LHS.first, Op.RHS.first, "add.r");
Stephen Hines176edba2014-12-01 14:53:08 -0800539 if (Op.LHS.second && Op.RHS.second)
540 ResI = Builder.CreateFAdd(Op.LHS.second, Op.RHS.second, "add.i");
541 else
542 ResI = Op.LHS.second ? Op.LHS.second : Op.RHS.second;
543 assert(ResI && "Only one operand may be real!");
Chris Lattner87415d22009-06-17 06:36:24 +0000544 } else {
545 ResR = Builder.CreateAdd(Op.LHS.first, Op.RHS.first, "add.r");
Stephen Hines176edba2014-12-01 14:53:08 -0800546 assert(Op.LHS.second && Op.RHS.second &&
547 "Both operands of integer complex operators must be complex!");
Chris Lattner87415d22009-06-17 06:36:24 +0000548 ResI = Builder.CreateAdd(Op.LHS.second, Op.RHS.second, "add.i");
549 }
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000550 return ComplexPairTy(ResR, ResI);
551}
552
Chris Lattnerab340c22007-08-26 22:09:01 +0000553ComplexPairTy ComplexExprEmitter::EmitBinSub(const BinOpInfo &Op) {
Chris Lattner87415d22009-06-17 06:36:24 +0000554 llvm::Value *ResR, *ResI;
Duncan Sandsf177d9d2010-02-15 16:14:01 +0000555 if (Op.LHS.first->getType()->isFloatingPointTy()) {
Stephen Hines176edba2014-12-01 14:53:08 -0800556 ResR = Builder.CreateFSub(Op.LHS.first, Op.RHS.first, "sub.r");
557 if (Op.LHS.second && Op.RHS.second)
558 ResI = Builder.CreateFSub(Op.LHS.second, Op.RHS.second, "sub.i");
559 else
560 ResI = Op.LHS.second ? Op.LHS.second
561 : Builder.CreateFNeg(Op.RHS.second, "sub.i");
562 assert(ResI && "Only one operand may be real!");
Chris Lattner87415d22009-06-17 06:36:24 +0000563 } else {
Stephen Hines176edba2014-12-01 14:53:08 -0800564 ResR = Builder.CreateSub(Op.LHS.first, Op.RHS.first, "sub.r");
565 assert(Op.LHS.second && Op.RHS.second &&
566 "Both operands of integer complex operators must be complex!");
Chris Lattner87415d22009-06-17 06:36:24 +0000567 ResI = Builder.CreateSub(Op.LHS.second, Op.RHS.second, "sub.i");
568 }
Chris Lattner6bc1ade2007-08-23 23:46:33 +0000569 return ComplexPairTy(ResR, ResI);
570}
571
Stephen Hines176edba2014-12-01 14:53:08 -0800572/// \brief Emit a libcall for a binary operation on complex types.
573ComplexPairTy ComplexExprEmitter::EmitComplexBinOpLibCall(StringRef LibCallName,
574 const BinOpInfo &Op) {
575 CallArgList Args;
576 Args.add(RValue::get(Op.LHS.first),
577 Op.Ty->castAs<ComplexType>()->getElementType());
578 Args.add(RValue::get(Op.LHS.second),
579 Op.Ty->castAs<ComplexType>()->getElementType());
580 Args.add(RValue::get(Op.RHS.first),
581 Op.Ty->castAs<ComplexType>()->getElementType());
582 Args.add(RValue::get(Op.RHS.second),
583 Op.Ty->castAs<ComplexType>()->getElementType());
Chris Lattner6bc1ade2007-08-23 23:46:33 +0000584
Stephen Hines176edba2014-12-01 14:53:08 -0800585 // We *must* use the full CG function call building logic here because the
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700586 // complex type has special ABI handling. We also should not forget about
587 // special calling convention which may be used for compiler builtins.
588 const CGFunctionInfo &FuncInfo =
589 CGF.CGM.getTypes().arrangeFreeFunctionCall(
590 Op.Ty, Args, FunctionType::ExtInfo(/* No CC here - will be added later */),
591 RequiredArgs::All);
Stephen Hines176edba2014-12-01 14:53:08 -0800592 llvm::FunctionType *FTy = CGF.CGM.getTypes().GetFunctionType(FuncInfo);
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700593 llvm::Constant *Func = CGF.CGM.CreateBuiltinFunction(FTy, LibCallName);
594 llvm::Instruction *Call;
Stephen Hines176edba2014-12-01 14:53:08 -0800595
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700596 RValue Res = CGF.EmitCall(FuncInfo, Func, ReturnValueSlot(), Args,
597 nullptr, &Call);
598 cast<llvm::CallInst>(Call)->setCallingConv(CGF.CGM.getBuiltinCC());
599 cast<llvm::CallInst>(Call)->setDoesNotThrow();
600
601 return Res.getComplexVal();
Stephen Hines176edba2014-12-01 14:53:08 -0800602}
603
604/// \brief Lookup the libcall name for a given floating point type complex
605/// multiply.
606static StringRef getComplexMultiplyLibCallName(llvm::Type *Ty) {
607 switch (Ty->getTypeID()) {
608 default:
609 llvm_unreachable("Unsupported floating point type!");
610 case llvm::Type::HalfTyID:
611 return "__mulhc3";
612 case llvm::Type::FloatTyID:
613 return "__mulsc3";
614 case llvm::Type::DoubleTyID:
615 return "__muldc3";
616 case llvm::Type::PPC_FP128TyID:
617 return "__multc3";
618 case llvm::Type::X86_FP80TyID:
619 return "__mulxc3";
620 case llvm::Type::FP128TyID:
621 return "__multc3";
622 }
623}
624
625// See C11 Annex G.5.1 for the semantics of multiplicative operators on complex
626// typed values.
Chris Lattnerab340c22007-08-26 22:09:01 +0000627ComplexPairTy ComplexExprEmitter::EmitBinMul(const BinOpInfo &Op) {
Chris Lattner87415d22009-06-17 06:36:24 +0000628 using llvm::Value;
629 Value *ResR, *ResI;
Stephen Hines176edba2014-12-01 14:53:08 -0800630 llvm::MDBuilder MDHelper(CGF.getLLVMContext());
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000631
Duncan Sandsf177d9d2010-02-15 16:14:01 +0000632 if (Op.LHS.first->getType()->isFloatingPointTy()) {
Stephen Hines176edba2014-12-01 14:53:08 -0800633 // The general formulation is:
634 // (a + ib) * (c + id) = (a * c - b * d) + i(a * d + b * c)
635 //
636 // But we can fold away components which would be zero due to a real
637 // operand according to C11 Annex G.5.1p2.
638 // FIXME: C11 also provides for imaginary types which would allow folding
639 // still more of this within the type system.
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000640
Stephen Hines176edba2014-12-01 14:53:08 -0800641 if (Op.LHS.second && Op.RHS.second) {
642 // If both operands are complex, emit the core math directly, and then
643 // test for NaNs. If we find NaNs in the result, we delegate to a libcall
644 // to carefully re-compute the correct infinity representation if
645 // possible. The expectation is that the presence of NaNs here is
646 // *extremely* rare, and so the cost of the libcall is almost irrelevant.
647 // This is good, because the libcall re-computes the core multiplication
648 // exactly the same as we do here and re-tests for NaNs in order to be
649 // a generic complex*complex libcall.
650
651 // First compute the four products.
652 Value *AC = Builder.CreateFMul(Op.LHS.first, Op.RHS.first, "mul_ac");
653 Value *BD = Builder.CreateFMul(Op.LHS.second, Op.RHS.second, "mul_bd");
654 Value *AD = Builder.CreateFMul(Op.LHS.first, Op.RHS.second, "mul_ad");
655 Value *BC = Builder.CreateFMul(Op.LHS.second, Op.RHS.first, "mul_bc");
656
657 // The real part is the difference of the first two, the imaginary part is
658 // the sum of the second.
659 ResR = Builder.CreateFSub(AC, BD, "mul_r");
660 ResI = Builder.CreateFAdd(AD, BC, "mul_i");
661
662 // Emit the test for the real part becoming NaN and create a branch to
663 // handle it. We test for NaN by comparing the number to itself.
664 Value *IsRNaN = Builder.CreateFCmpUNO(ResR, ResR, "isnan_cmp");
665 llvm::BasicBlock *ContBB = CGF.createBasicBlock("complex_mul_cont");
666 llvm::BasicBlock *INaNBB = CGF.createBasicBlock("complex_mul_imag_nan");
667 llvm::Instruction *Branch = Builder.CreateCondBr(IsRNaN, INaNBB, ContBB);
668 llvm::BasicBlock *OrigBB = Branch->getParent();
669
670 // Give hint that we very much don't expect to see NaNs.
671 // Value chosen to match UR_NONTAKEN_WEIGHT, see BranchProbabilityInfo.cpp
672 llvm::MDNode *BrWeight = MDHelper.createBranchWeights(1, (1U << 20) - 1);
673 Branch->setMetadata(llvm::LLVMContext::MD_prof, BrWeight);
674
675 // Now test the imaginary part and create its branch.
676 CGF.EmitBlock(INaNBB);
677 Value *IsINaN = Builder.CreateFCmpUNO(ResI, ResI, "isnan_cmp");
678 llvm::BasicBlock *LibCallBB = CGF.createBasicBlock("complex_mul_libcall");
679 Branch = Builder.CreateCondBr(IsINaN, LibCallBB, ContBB);
680 Branch->setMetadata(llvm::LLVMContext::MD_prof, BrWeight);
681
682 // Now emit the libcall on this slowest of the slow paths.
683 CGF.EmitBlock(LibCallBB);
684 Value *LibCallR, *LibCallI;
685 std::tie(LibCallR, LibCallI) = EmitComplexBinOpLibCall(
686 getComplexMultiplyLibCallName(Op.LHS.first->getType()), Op);
687 Builder.CreateBr(ContBB);
688
689 // Finally continue execution by phi-ing together the different
690 // computation paths.
691 CGF.EmitBlock(ContBB);
692 llvm::PHINode *RealPHI = Builder.CreatePHI(ResR->getType(), 3, "real_mul_phi");
693 RealPHI->addIncoming(ResR, OrigBB);
694 RealPHI->addIncoming(ResR, INaNBB);
695 RealPHI->addIncoming(LibCallR, LibCallBB);
696 llvm::PHINode *ImagPHI = Builder.CreatePHI(ResI->getType(), 3, "imag_mul_phi");
697 ImagPHI->addIncoming(ResI, OrigBB);
698 ImagPHI->addIncoming(ResI, INaNBB);
699 ImagPHI->addIncoming(LibCallI, LibCallBB);
700 return ComplexPairTy(RealPHI, ImagPHI);
701 }
702 assert((Op.LHS.second || Op.RHS.second) &&
703 "At least one operand must be complex!");
704
705 // If either of the operands is a real rather than a complex, the
706 // imaginary component is ignored when computing the real component of the
707 // result.
708 ResR = Builder.CreateFMul(Op.LHS.first, Op.RHS.first, "mul.rl");
709
710 ResI = Op.LHS.second
711 ? Builder.CreateFMul(Op.LHS.second, Op.RHS.first, "mul.il")
712 : Builder.CreateFMul(Op.LHS.first, Op.RHS.second, "mul.ir");
Chris Lattner87415d22009-06-17 06:36:24 +0000713 } else {
Stephen Hines176edba2014-12-01 14:53:08 -0800714 assert(Op.LHS.second && Op.RHS.second &&
715 "Both operands of integer complex operators must be complex!");
Chris Lattner87415d22009-06-17 06:36:24 +0000716 Value *ResRl = Builder.CreateMul(Op.LHS.first, Op.RHS.first, "mul.rl");
Stephen Hines176edba2014-12-01 14:53:08 -0800717 Value *ResRr = Builder.CreateMul(Op.LHS.second, Op.RHS.second, "mul.rr");
718 ResR = Builder.CreateSub(ResRl, ResRr, "mul.r");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000719
Chris Lattner87415d22009-06-17 06:36:24 +0000720 Value *ResIl = Builder.CreateMul(Op.LHS.second, Op.RHS.first, "mul.il");
721 Value *ResIr = Builder.CreateMul(Op.LHS.first, Op.RHS.second, "mul.ir");
Stephen Hines176edba2014-12-01 14:53:08 -0800722 ResI = Builder.CreateAdd(ResIl, ResIr, "mul.i");
Chris Lattner87415d22009-06-17 06:36:24 +0000723 }
Chris Lattner2823c192007-08-21 16:34:16 +0000724 return ComplexPairTy(ResR, ResI);
725}
726
Stephen Hines176edba2014-12-01 14:53:08 -0800727// See C11 Annex G.5.1 for the semantics of multiplicative operators on complex
728// typed values.
Chris Lattnerab340c22007-08-26 22:09:01 +0000729ComplexPairTy ComplexExprEmitter::EmitBinDiv(const BinOpInfo &Op) {
730 llvm::Value *LHSr = Op.LHS.first, *LHSi = Op.LHS.second;
731 llvm::Value *RHSr = Op.RHS.first, *RHSi = Op.RHS.second;
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000732
Chris Lattner3219c5d2007-08-26 21:24:19 +0000733
734 llvm::Value *DSTr, *DSTi;
Stephen Hines176edba2014-12-01 14:53:08 -0800735 if (LHSr->getType()->isFloatingPointTy()) {
736 // If we have a complex operand on the RHS, we delegate to a libcall to
737 // handle all of the complexities and minimize underflow/overflow cases.
738 //
739 // FIXME: We would be able to avoid the libcall in many places if we
740 // supported imaginary types in addition to complex types.
741 if (RHSi) {
742 BinOpInfo LibCallOp = Op;
743 // If LHS was a real, supply a null imaginary part.
744 if (!LHSi)
745 LibCallOp.LHS.second = llvm::Constant::getNullValue(LHSr->getType());
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000746
Stephen Hines176edba2014-12-01 14:53:08 -0800747 StringRef LibCallName;
748 switch (LHSr->getType()->getTypeID()) {
749 default:
750 llvm_unreachable("Unsupported floating point type!");
751 case llvm::Type::HalfTyID:
752 return EmitComplexBinOpLibCall("__divhc3", LibCallOp);
753 case llvm::Type::FloatTyID:
754 return EmitComplexBinOpLibCall("__divsc3", LibCallOp);
755 case llvm::Type::DoubleTyID:
756 return EmitComplexBinOpLibCall("__divdc3", LibCallOp);
757 case llvm::Type::PPC_FP128TyID:
758 return EmitComplexBinOpLibCall("__divtc3", LibCallOp);
759 case llvm::Type::X86_FP80TyID:
760 return EmitComplexBinOpLibCall("__divxc3", LibCallOp);
761 case llvm::Type::FP128TyID:
762 return EmitComplexBinOpLibCall("__divtc3", LibCallOp);
763 }
764 }
765 assert(LHSi && "Can have at most one non-complex operand!");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000766
Stephen Hines176edba2014-12-01 14:53:08 -0800767 DSTr = Builder.CreateFDiv(LHSr, RHSr);
768 DSTi = Builder.CreateFDiv(LHSi, RHSr);
Chris Lattner3219c5d2007-08-26 21:24:19 +0000769 } else {
Stephen Hines176edba2014-12-01 14:53:08 -0800770 assert(Op.LHS.second && Op.RHS.second &&
771 "Both operands of integer complex operators must be complex!");
Chris Lattner87415d22009-06-17 06:36:24 +0000772 // (a+ib) / (c+id) = ((ac+bd)/(cc+dd)) + i((bc-ad)/(cc+dd))
Benjamin Kramer578faa82011-09-27 21:06:10 +0000773 llvm::Value *Tmp1 = Builder.CreateMul(LHSr, RHSr); // a*c
774 llvm::Value *Tmp2 = Builder.CreateMul(LHSi, RHSi); // b*d
775 llvm::Value *Tmp3 = Builder.CreateAdd(Tmp1, Tmp2); // ac+bd
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000776
Benjamin Kramer578faa82011-09-27 21:06:10 +0000777 llvm::Value *Tmp4 = Builder.CreateMul(RHSr, RHSr); // c*c
778 llvm::Value *Tmp5 = Builder.CreateMul(RHSi, RHSi); // d*d
779 llvm::Value *Tmp6 = Builder.CreateAdd(Tmp4, Tmp5); // cc+dd
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000780
Benjamin Kramer578faa82011-09-27 21:06:10 +0000781 llvm::Value *Tmp7 = Builder.CreateMul(LHSi, RHSr); // b*c
782 llvm::Value *Tmp8 = Builder.CreateMul(LHSr, RHSi); // a*d
783 llvm::Value *Tmp9 = Builder.CreateSub(Tmp7, Tmp8); // bc-ad
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000784
John McCall9d232c82013-03-07 21:37:08 +0000785 if (Op.Ty->castAs<ComplexType>()->getElementType()->isUnsignedIntegerType()) {
Benjamin Kramer578faa82011-09-27 21:06:10 +0000786 DSTr = Builder.CreateUDiv(Tmp3, Tmp6);
787 DSTi = Builder.CreateUDiv(Tmp9, Tmp6);
Chris Lattner3219c5d2007-08-26 21:24:19 +0000788 } else {
Benjamin Kramer578faa82011-09-27 21:06:10 +0000789 DSTr = Builder.CreateSDiv(Tmp3, Tmp6);
790 DSTi = Builder.CreateSDiv(Tmp9, Tmp6);
Chris Lattner3219c5d2007-08-26 21:24:19 +0000791 }
792 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000793
Chris Lattner3219c5d2007-08-26 21:24:19 +0000794 return ComplexPairTy(DSTr, DSTi);
795}
796
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000797ComplexExprEmitter::BinOpInfo
Chris Lattnerab340c22007-08-26 22:09:01 +0000798ComplexExprEmitter::EmitBinOps(const BinaryOperator *E) {
Mike Stump7f79f9b2009-05-29 15:46:01 +0000799 TestAndClearIgnoreReal();
800 TestAndClearIgnoreImag();
Chris Lattnerab340c22007-08-26 22:09:01 +0000801 BinOpInfo Ops;
Stephen Hines176edba2014-12-01 14:53:08 -0800802 if (E->getLHS()->getType()->isRealFloatingType())
803 Ops.LHS = ComplexPairTy(CGF.EmitScalarExpr(E->getLHS()), nullptr);
804 else
805 Ops.LHS = Visit(E->getLHS());
806 if (E->getRHS()->getType()->isRealFloatingType())
807 Ops.RHS = ComplexPairTy(CGF.EmitScalarExpr(E->getRHS()), nullptr);
808 else
809 Ops.RHS = Visit(E->getRHS());
810
Chris Lattnerab340c22007-08-26 22:09:01 +0000811 Ops.Ty = E->getType();
812 return Ops;
813}
814
815
John McCallb418d742010-11-16 10:08:07 +0000816LValue ComplexExprEmitter::
817EmitCompoundAssignLValue(const CompoundAssignOperator *E,
818 ComplexPairTy (ComplexExprEmitter::*Func)(const BinOpInfo&),
Eli Friedman0934e182013-06-12 01:40:06 +0000819 RValue &Val) {
Mike Stump7f79f9b2009-05-29 15:46:01 +0000820 TestAndClearIgnoreReal();
821 TestAndClearIgnoreImag();
Jeffrey Yasskindec09842011-01-18 02:00:16 +0000822 QualType LHSTy = E->getLHS()->getType();
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700823 if (const AtomicType *AT = LHSTy->getAs<AtomicType>())
824 LHSTy = AT->getValueType();
Chris Lattnerab340c22007-08-26 22:09:01 +0000825
826 BinOpInfo OpInfo;
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000827
Mike Stump7f79f9b2009-05-29 15:46:01 +0000828 // Load the RHS and LHS operands.
829 // __block variables need to have the rhs evaluated first, plus this should
John McCall641ca7d2010-11-16 05:45:35 +0000830 // improve codegen a little.
Eli Friedmanab3a8522009-03-28 01:22:36 +0000831 OpInfo.Ty = E->getComputationResultType();
Stephen Hines176edba2014-12-01 14:53:08 -0800832 QualType ComplexElementTy = cast<ComplexType>(OpInfo.Ty)->getElementType();
John McCall641ca7d2010-11-16 05:45:35 +0000833
834 // The RHS should have been converted to the computation type.
Stephen Hines176edba2014-12-01 14:53:08 -0800835 if (E->getRHS()->getType()->isRealFloatingType()) {
836 assert(
837 CGF.getContext()
838 .hasSameUnqualifiedType(ComplexElementTy, E->getRHS()->getType()));
839 OpInfo.RHS = ComplexPairTy(CGF.EmitScalarExpr(E->getRHS()), nullptr);
840 } else {
841 assert(CGF.getContext()
842 .hasSameUnqualifiedType(OpInfo.Ty, E->getRHS()->getType()));
843 OpInfo.RHS = Visit(E->getRHS());
844 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700845
Daniel Dunbar120bc772010-06-29 22:44:21 +0000846 LValue LHS = CGF.EmitLValue(E->getLHS());
John McCall0e800c92010-12-04 08:14:53 +0000847
Eli Friedman0934e182013-06-12 01:40:06 +0000848 // Load from the l-value and convert it.
849 if (LHSTy->isAnyComplexType()) {
Nick Lewycky4ee7dc22013-10-02 02:29:49 +0000850 ComplexPairTy LHSVal = EmitLoadOfLValue(LHS, E->getExprLoc());
Eli Friedman0934e182013-06-12 01:40:06 +0000851 OpInfo.LHS = EmitComplexToComplexCast(LHSVal, LHSTy, OpInfo.Ty);
852 } else {
Nick Lewycky4ee7dc22013-10-02 02:29:49 +0000853 llvm::Value *LHSVal = CGF.EmitLoadOfScalar(LHS, E->getExprLoc());
Stephen Hines176edba2014-12-01 14:53:08 -0800854 // For floating point real operands we can directly pass the scalar form
855 // to the binary operator emission and potentially get more efficient code.
856 if (LHSTy->isRealFloatingType()) {
857 if (!CGF.getContext().hasSameUnqualifiedType(ComplexElementTy, LHSTy))
858 LHSVal = CGF.EmitScalarConversion(LHSVal, LHSTy, ComplexElementTy);
859 OpInfo.LHS = ComplexPairTy(LHSVal, nullptr);
860 } else {
861 OpInfo.LHS = EmitScalarToComplexCast(LHSVal, LHSTy, OpInfo.Ty);
862 }
Eli Friedman0934e182013-06-12 01:40:06 +0000863 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000864
Chris Lattnerab340c22007-08-26 22:09:01 +0000865 // Expand the binary operator.
866 ComplexPairTy Result = (this->*Func)(OpInfo);
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000867
Eli Friedman0934e182013-06-12 01:40:06 +0000868 // Truncate the result and store it into the LHS lvalue.
869 if (LHSTy->isAnyComplexType()) {
870 ComplexPairTy ResVal = EmitComplexToComplexCast(Result, OpInfo.Ty, LHSTy);
871 EmitStoreOfComplex(ResVal, LHS, /*isInit*/ false);
872 Val = RValue::getComplex(ResVal);
873 } else {
874 llvm::Value *ResVal =
875 CGF.EmitComplexToScalarConversion(Result, OpInfo.Ty, LHSTy);
876 CGF.EmitStoreOfScalar(ResVal, LHS, /*isInit*/ false);
877 Val = RValue::get(ResVal);
878 }
Daniel Dunbar120bc772010-06-29 22:44:21 +0000879
John McCallb418d742010-11-16 10:08:07 +0000880 return LHS;
Chris Lattnerab340c22007-08-26 22:09:01 +0000881}
Chris Lattner3219c5d2007-08-26 21:24:19 +0000882
John McCallb418d742010-11-16 10:08:07 +0000883// Compound assignments.
884ComplexPairTy ComplexExprEmitter::
885EmitCompoundAssign(const CompoundAssignOperator *E,
886 ComplexPairTy (ComplexExprEmitter::*Func)(const BinOpInfo&)){
Eli Friedman0934e182013-06-12 01:40:06 +0000887 RValue Val;
John McCallb418d742010-11-16 10:08:07 +0000888 LValue LV = EmitCompoundAssignLValue(E, Func, Val);
889
890 // The result of an assignment in C is the assigned r-value.
Richard Smith7edf9e32012-11-01 22:30:59 +0000891 if (!CGF.getLangOpts().CPlusPlus)
Eli Friedman0934e182013-06-12 01:40:06 +0000892 return Val.getComplexVal();
John McCallb418d742010-11-16 10:08:07 +0000893
John McCallb418d742010-11-16 10:08:07 +0000894 // If the lvalue is non-volatile, return the computed value of the assignment.
895 if (!LV.isVolatileQualified())
Eli Friedman0934e182013-06-12 01:40:06 +0000896 return Val.getComplexVal();
John McCallb418d742010-11-16 10:08:07 +0000897
Nick Lewycky4ee7dc22013-10-02 02:29:49 +0000898 return EmitLoadOfLValue(LV, E->getExprLoc());
John McCallb418d742010-11-16 10:08:07 +0000899}
900
901LValue ComplexExprEmitter::EmitBinAssignLValue(const BinaryOperator *E,
902 ComplexPairTy &Val) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700903 assert(CGF.getContext().hasSameUnqualifiedType(E->getLHS()->getType(),
Douglas Gregor63982352010-07-13 18:40:04 +0000904 E->getRHS()->getType()) &&
Chris Lattner96196622008-07-26 22:37:01 +0000905 "Invalid assignment");
John McCallb418d742010-11-16 10:08:07 +0000906 TestAndClearIgnoreReal();
907 TestAndClearIgnoreImag();
908
John McCallcd940a12010-12-06 06:10:02 +0000909 // Emit the RHS. __block variables need the RHS evaluated first.
John McCallb418d742010-11-16 10:08:07 +0000910 Val = Visit(E->getRHS());
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000911
912 // Compute the address to store into.
913 LValue LHS = CGF.EmitLValue(E->getLHS());
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000914
Daniel Dunbar120bc772010-06-29 22:44:21 +0000915 // Store the result value into the LHS lvalue.
John McCall9d232c82013-03-07 21:37:08 +0000916 EmitStoreOfComplex(Val, LHS, /*isInit*/ false);
Daniel Dunbar42f963d2009-06-10 04:38:50 +0000917
John McCallb418d742010-11-16 10:08:07 +0000918 return LHS;
919}
Daniel Dunbar120bc772010-06-29 22:44:21 +0000920
John McCallb418d742010-11-16 10:08:07 +0000921ComplexPairTy ComplexExprEmitter::VisitBinAssign(const BinaryOperator *E) {
922 ComplexPairTy Val;
923 LValue LV = EmitBinAssignLValue(E, Val);
924
925 // The result of an assignment in C is the assigned r-value.
Richard Smith7edf9e32012-11-01 22:30:59 +0000926 if (!CGF.getLangOpts().CPlusPlus)
John McCallb418d742010-11-16 10:08:07 +0000927 return Val;
928
John McCallb418d742010-11-16 10:08:07 +0000929 // If the lvalue is non-volatile, return the computed value of the assignment.
930 if (!LV.isVolatileQualified())
931 return Val;
932
Nick Lewycky4ee7dc22013-10-02 02:29:49 +0000933 return EmitLoadOfLValue(LV, E->getExprLoc());
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000934}
935
Chris Lattner756a4d82007-08-21 17:15:50 +0000936ComplexPairTy ComplexExprEmitter::VisitBinComma(const BinaryOperator *E) {
John McCall2a416372010-12-05 02:00:02 +0000937 CGF.EmitIgnoredExpr(E->getLHS());
Chris Lattner756a4d82007-08-21 17:15:50 +0000938 return Visit(E->getRHS());
939}
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000940
941ComplexPairTy ComplexExprEmitter::
John McCall56ca35d2011-02-17 10:25:35 +0000942VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
Mike Stump7f79f9b2009-05-29 15:46:01 +0000943 TestAndClearIgnoreReal();
944 TestAndClearIgnoreImag();
Daniel Dunbar9615ecb2008-11-13 01:38:36 +0000945 llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
946 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
947 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000948
John McCall56ca35d2011-02-17 10:25:35 +0000949 // Bind the common expression if necessary.
Eli Friedmand97927d2012-01-06 20:42:20 +0000950 CodeGenFunction::OpaqueValueMapping binding(CGF, E);
John McCall150b4622011-01-26 04:00:11 +0000951
Stephen Hines651f13c2014-04-23 16:59:28 -0700952 RegionCounter Cnt = CGF.getPGORegionCounter(E);
John McCall56ca35d2011-02-17 10:25:35 +0000953 CodeGenFunction::ConditionalEvaluation eval(CGF);
Stephen Hines651f13c2014-04-23 16:59:28 -0700954 CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock, Cnt.getCount());
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000955
John McCall150b4622011-01-26 04:00:11 +0000956 eval.begin(CGF);
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000957 CGF.EmitBlock(LHSBlock);
Stephen Hines651f13c2014-04-23 16:59:28 -0700958 Cnt.beginRegion(Builder);
Fariborz Jahanianaa3b57e2010-09-20 23:50:22 +0000959 ComplexPairTy LHS = Visit(E->getTrueExpr());
Chris Lattner5083a532007-08-21 17:39:38 +0000960 LHSBlock = Builder.GetInsertBlock();
Daniel Dunbard57a8712008-11-11 09:41:28 +0000961 CGF.EmitBranch(ContBlock);
John McCall150b4622011-01-26 04:00:11 +0000962 eval.end(CGF);
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000963
John McCall150b4622011-01-26 04:00:11 +0000964 eval.begin(CGF);
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000965 CGF.EmitBlock(RHSBlock);
John McCall56ca35d2011-02-17 10:25:35 +0000966 ComplexPairTy RHS = Visit(E->getFalseExpr());
Chris Lattner5083a532007-08-21 17:39:38 +0000967 RHSBlock = Builder.GetInsertBlock();
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000968 CGF.EmitBlock(ContBlock);
John McCall150b4622011-01-26 04:00:11 +0000969 eval.end(CGF);
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000970
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000971 // Create a PHI node for the real part.
Jay Foadbbf3bac2011-03-30 11:28:58 +0000972 llvm::PHINode *RealPN = Builder.CreatePHI(LHS.first->getType(), 2, "cond.r");
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000973 RealPN->addIncoming(LHS.first, LHSBlock);
974 RealPN->addIncoming(RHS.first, RHSBlock);
975
976 // Create a PHI node for the imaginary part.
Jay Foadbbf3bac2011-03-30 11:28:58 +0000977 llvm::PHINode *ImagPN = Builder.CreatePHI(LHS.first->getType(), 2, "cond.i");
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000978 ImagPN->addIncoming(LHS.second, LHSBlock);
979 ImagPN->addIncoming(RHS.second, RHSBlock);
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000980
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000981 return ComplexPairTy(RealPN, ImagPN);
982}
983
Chris Lattnerd0b12032007-08-24 02:18:47 +0000984ComplexPairTy ComplexExprEmitter::VisitChooseExpr(ChooseExpr *E) {
Eli Friedmana5e66012013-07-20 00:40:58 +0000985 return Visit(E->getChosenSubExpr());
Chris Lattnerd0b12032007-08-24 02:18:47 +0000986}
987
Eli Friedmanc4777f12008-05-13 23:11:35 +0000988ComplexPairTy ComplexExprEmitter::VisitInitListExpr(InitListExpr *E) {
Mike Stump7f79f9b2009-05-29 15:46:01 +0000989 bool Ignore = TestAndClearIgnoreReal();
990 (void)Ignore;
991 assert (Ignore == false && "init list ignored");
992 Ignore = TestAndClearIgnoreImag();
993 (void)Ignore;
994 assert (Ignore == false && "init list ignored");
Eli Friedman0c706c22011-09-19 23:17:44 +0000995
996 if (E->getNumInits() == 2) {
997 llvm::Value *Real = CGF.EmitScalarExpr(E->getInit(0));
998 llvm::Value *Imag = CGF.EmitScalarExpr(E->getInit(1));
999 return ComplexPairTy(Real, Imag);
1000 } else if (E->getNumInits() == 1) {
Eli Friedmanc4777f12008-05-13 23:11:35 +00001001 return Visit(E->getInit(0));
Eli Friedman0c706c22011-09-19 23:17:44 +00001002 }
Eli Friedmanc4777f12008-05-13 23:11:35 +00001003
1004 // Empty init list intializes to null
Eli Friedman0c706c22011-09-19 23:17:44 +00001005 assert(E->getNumInits() == 0 && "Unexpected number of inits");
John McCall9d232c82013-03-07 21:37:08 +00001006 QualType Ty = E->getType()->castAs<ComplexType>()->getElementType();
Chris Lattner2acc6e32011-07-18 04:24:23 +00001007 llvm::Type* LTy = CGF.ConvertType(Ty);
Owen Andersonc9c88b42009-07-31 20:28:54 +00001008 llvm::Value* zeroConstant = llvm::Constant::getNullValue(LTy);
Eli Friedmanc4777f12008-05-13 23:11:35 +00001009 return ComplexPairTy(zeroConstant, zeroConstant);
1010}
1011
Daniel Dunbar4e484b82009-02-10 03:03:30 +00001012ComplexPairTy ComplexExprEmitter::VisitVAArgExpr(VAArgExpr *E) {
Daniel Dunbar07855702009-02-11 22:25:55 +00001013 llvm::Value *ArgValue = CGF.EmitVAListRef(E->getSubExpr());
Daniel Dunbar4e484b82009-02-10 03:03:30 +00001014 llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, E->getType());
1015
1016 if (!ArgPtr) {
1017 CGF.ErrorUnsupported(E, "complex va_arg expression");
Chris Lattner2acc6e32011-07-18 04:24:23 +00001018 llvm::Type *EltTy =
John McCall9d232c82013-03-07 21:37:08 +00001019 CGF.ConvertType(E->getType()->castAs<ComplexType>()->getElementType());
Daniel Dunbar4e484b82009-02-10 03:03:30 +00001020 llvm::Value *U = llvm::UndefValue::get(EltTy);
1021 return ComplexPairTy(U, U);
1022 }
1023
Nick Lewycky4ee7dc22013-10-02 02:29:49 +00001024 return EmitLoadOfLValue(CGF.MakeNaturalAlignAddrLValue(ArgPtr, E->getType()),
1025 E->getExprLoc());
Daniel Dunbar4e484b82009-02-10 03:03:30 +00001026}
1027
Chris Lattnerb6ef18a2007-08-21 05:54:00 +00001028//===----------------------------------------------------------------------===//
1029// Entry Point into this File
1030//===----------------------------------------------------------------------===//
1031
1032/// EmitComplexExpr - Emit the computation of the specified expression of
1033/// complex type, ignoring the result.
Mike Stump7f79f9b2009-05-29 15:46:01 +00001034ComplexPairTy CodeGenFunction::EmitComplexExpr(const Expr *E, bool IgnoreReal,
John McCallb418d742010-11-16 10:08:07 +00001035 bool IgnoreImag) {
John McCall9d232c82013-03-07 21:37:08 +00001036 assert(E && getComplexType(E->getType()) &&
Chris Lattnerb6ef18a2007-08-21 05:54:00 +00001037 "Invalid complex expression to emit");
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001038
John McCallb418d742010-11-16 10:08:07 +00001039 return ComplexExprEmitter(*this, IgnoreReal, IgnoreImag)
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001040 .Visit(const_cast<Expr *>(E));
Chris Lattnerb6ef18a2007-08-21 05:54:00 +00001041}
1042
John McCall9d232c82013-03-07 21:37:08 +00001043void CodeGenFunction::EmitComplexExprIntoLValue(const Expr *E, LValue dest,
1044 bool isInit) {
1045 assert(E && getComplexType(E->getType()) &&
Chris Lattner23b1cdb2007-08-23 23:43:33 +00001046 "Invalid complex expression to emit");
1047 ComplexExprEmitter Emitter(*this);
1048 ComplexPairTy Val = Emitter.Visit(const_cast<Expr*>(E));
John McCall9d232c82013-03-07 21:37:08 +00001049 Emitter.EmitStoreOfComplex(Val, dest, isInit);
Chris Lattner23b1cdb2007-08-23 23:43:33 +00001050}
Chris Lattner9b655512007-08-31 22:49:20 +00001051
John McCall9d232c82013-03-07 21:37:08 +00001052/// EmitStoreOfComplex - Store a complex number into the specified l-value.
1053void CodeGenFunction::EmitStoreOfComplex(ComplexPairTy V, LValue dest,
1054 bool isInit) {
1055 ComplexExprEmitter(*this).EmitStoreOfComplex(V, dest, isInit);
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001056}
1057
John McCall9d232c82013-03-07 21:37:08 +00001058/// EmitLoadOfComplex - Load a complex number from the specified address.
Nick Lewycky4ee7dc22013-10-02 02:29:49 +00001059ComplexPairTy CodeGenFunction::EmitLoadOfComplex(LValue src,
1060 SourceLocation loc) {
1061 return ComplexExprEmitter(*this).EmitLoadOfLValue(src, loc);
Chris Lattner9b655512007-08-31 22:49:20 +00001062}
John McCall83ce9d42010-11-16 23:07:28 +00001063
1064LValue CodeGenFunction::EmitComplexAssignmentLValue(const BinaryOperator *E) {
John McCall2a416372010-12-05 02:00:02 +00001065 assert(E->getOpcode() == BO_Assign);
John McCall83ce9d42010-11-16 23:07:28 +00001066 ComplexPairTy Val; // ignored
John McCall2a416372010-12-05 02:00:02 +00001067 return ComplexExprEmitter(*this).EmitBinAssignLValue(E, Val);
1068}
John McCall83ce9d42010-11-16 23:07:28 +00001069
Eli Friedman0934e182013-06-12 01:40:06 +00001070typedef ComplexPairTy (ComplexExprEmitter::*CompoundFunc)(
1071 const ComplexExprEmitter::BinOpInfo &);
John McCall83ce9d42010-11-16 23:07:28 +00001072
Eli Friedman0934e182013-06-12 01:40:06 +00001073static CompoundFunc getComplexOp(BinaryOperatorKind Op) {
1074 switch (Op) {
1075 case BO_MulAssign: return &ComplexExprEmitter::EmitBinMul;
1076 case BO_DivAssign: return &ComplexExprEmitter::EmitBinDiv;
1077 case BO_SubAssign: return &ComplexExprEmitter::EmitBinSub;
1078 case BO_AddAssign: return &ComplexExprEmitter::EmitBinAdd;
John McCall83ce9d42010-11-16 23:07:28 +00001079 default:
1080 llvm_unreachable("unexpected complex compound assignment");
John McCall83ce9d42010-11-16 23:07:28 +00001081 }
Eli Friedman0934e182013-06-12 01:40:06 +00001082}
John McCall83ce9d42010-11-16 23:07:28 +00001083
Eli Friedman0934e182013-06-12 01:40:06 +00001084LValue CodeGenFunction::
1085EmitComplexCompoundAssignmentLValue(const CompoundAssignOperator *E) {
1086 CompoundFunc Op = getComplexOp(E->getOpcode());
1087 RValue Val;
John McCall2a416372010-12-05 02:00:02 +00001088 return ComplexExprEmitter(*this).EmitCompoundAssignLValue(E, Op, Val);
John McCall83ce9d42010-11-16 23:07:28 +00001089}
Eli Friedman0934e182013-06-12 01:40:06 +00001090
1091LValue CodeGenFunction::
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001092EmitScalarCompoundAssignWithComplex(const CompoundAssignOperator *E,
1093 llvm::Value *&Result) {
Eli Friedman0934e182013-06-12 01:40:06 +00001094 CompoundFunc Op = getComplexOp(E->getOpcode());
1095 RValue Val;
1096 LValue Ret = ComplexExprEmitter(*this).EmitCompoundAssignLValue(E, Op, Val);
1097 Result = Val.getScalarVal();
1098 return Ret;
1099}